Handle non-String property values and keys. These should never happen in real life (unless somebody abuses System.getProperties()) so

I opted out to ignore them (there is no way to even tell if such properties will implement hashCode/equals/toString/whatever).

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1296971 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dawid Weiss 2012-03-05 09:09:04 +00:00
parent 0eb091b2ca
commit 1ded74e10d
3 changed files with 48 additions and 9 deletions

View File

@ -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:

View File

@ -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());
}
}

View File

@ -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<String,String> cloneAsMap(Properties properties) {
TreeMap<String,String> result = new TreeMap<String,String>();
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;
}