diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 1e04d79068d..1eadfbafad0 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -932,7 +932,7 @@ Build properties before and after each test (and suite). If changes are detected, the test will fail. A rule can be used to reset system properties to before-scope state (and this has been used to make Solr tests pass). - (Dawid Weiss). + (Dawid Weiss, Uwe Schindler). * LUCENE-3228: Stop downloading external javadoc package-list files: diff --git a/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestSystemPropertiesInvariantRule.java b/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestSystemPropertiesInvariantRule.java index a0aa89f3044..0ea78204919 100644 --- a/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestSystemPropertiesInvariantRule.java +++ b/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestSystemPropertiesInvariantRule.java @@ -1,10 +1,9 @@ package org.apache.lucene.util.junitcompat; +import java.util.Properties; + import org.apache.lucene.util.LuceneTestCase; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.*; import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; @@ -43,7 +42,28 @@ public class TestSystemPropertiesInvariantRule { testMethod1(); } } - + + public static class NonStringProperties extends Base { + public void testMethod1() { + if (System.getProperties().get(PROP_KEY1) != null) { + throw new RuntimeException("Will pass."); + } + + Properties properties = System.getProperties(); + properties.put(PROP_KEY1, new Object()); + Assert.assertTrue(System.getProperties().get(PROP_KEY1) != null); + } + + public void testMethod2() { + testMethod1(); + } + + @AfterClass + public static void cleanup() { + System.getProperties().remove(PROP_KEY1); + } + } + @Test public void testRuleInvariantBeforeClass() { Result runClasses = JUnitCore.runClasses(InBeforeClass.class); @@ -71,4 +91,12 @@ public class TestSystemPropertiesInvariantRule { } Assert.assertNull(System.getProperty(PROP_KEY1)); } + + @Test + public void testNonStringProperties() { + Result runClasses = JUnitCore.runClasses(NonStringProperties.class); + Assert.assertEquals(1, runClasses.getFailureCount()); + Assert.assertTrue(runClasses.getFailures().get(0).getMessage().contains("Will pass")); + Assert.assertEquals(3, runClasses.getRunCount()); + } } diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/SystemPropertiesRestoreRule.java b/lucene/test-framework/src/java/org/apache/lucene/util/SystemPropertiesRestoreRule.java index 6a36a47c8e9..77778fc2c92 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/util/SystemPropertiesRestoreRule.java +++ b/lucene/test-framework/src/java/org/apache/lucene/util/SystemPropertiesRestoreRule.java @@ -1,6 +1,5 @@ package org.apache.lucene.util; -import java.util.Map; import java.util.*; import org.junit.rules.TestRule; @@ -33,8 +32,20 @@ public class SystemPropertiesRestoreRule implements TestRule { static TreeMap cloneAsMap(Properties properties) { TreeMap result = new TreeMap(); for (Enumeration e = properties.propertyNames(); e.hasMoreElements();) { - String key = (String) e.nextElement(); - result.put(key, (String) properties.get(key)); + final Object key = e.nextElement(); + // Skip non-string properties or values, they're abuse of Properties object. + if (key instanceof String) { + String value = properties.getProperty((String) key); + if (value == null) { + Object ovalue = properties.get(key); + if (ovalue != null) { + // ovalue has to be a non-string object. Skip the property because + // System.clearProperty won't be able to cast back the existing value. + continue; + } + } + result.put((String) key, value); + } } return result; }