You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by dw...@apache.org on 2012/03/05 10:09:04 UTC

svn commit: r1296971 - in /lucene/dev/trunk/lucene: CHANGES.txt core/src/test/org/apache/lucene/util/junitcompat/TestSystemPropertiesInvariantRule.java test-framework/src/java/org/apache/lucene/util/SystemPropertiesRestoreRule.java

Author: dweiss
Date: Mon Mar  5 09:09:04 2012
New Revision: 1296971

URL: http://svn.apache.org/viewvc?rev=1296971&view=rev
Log:
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).

Modified:
    lucene/dev/trunk/lucene/CHANGES.txt
    lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestSystemPropertiesInvariantRule.java
    lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/SystemPropertiesRestoreRule.java

Modified: lucene/dev/trunk/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/CHANGES.txt?rev=1296971&r1=1296970&r2=1296971&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/CHANGES.txt (original)
+++ lucene/dev/trunk/lucene/CHANGES.txt Mon Mar  5 09:09:04 2012
@@ -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:
 

Modified: lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestSystemPropertiesInvariantRule.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestSystemPropertiesInvariantRule.java?rev=1296971&r1=1296970&r2=1296971&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestSystemPropertiesInvariantRule.java (original)
+++ lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestSystemPropertiesInvariantRule.java Mon Mar  5 09:09:04 2012
@@ -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 TestSystemPropertiesInvaria
       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 TestSystemPropertiesInvaria
     }
     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());
+  }
 }

Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/SystemPropertiesRestoreRule.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/SystemPropertiesRestoreRule.java?rev=1296971&r1=1296970&r2=1296971&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/SystemPropertiesRestoreRule.java (original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/SystemPropertiesRestoreRule.java Mon Mar  5 09:09:04 2012
@@ -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
   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;
   }