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

svn commit: r1360354 - in /lucene/dev/trunk: lucene/ lucene/test-framework/src/java/org/apache/lucene/codecs/mockrandom/ lucene/test-framework/src/java/org/apache/lucene/util/ lucene/tools/forbiddenApis/ solr/ solr/core/src/test/org/apache/solr/schema/...

Author: rmuir
Date: Wed Jul 11 19:10:08 2012
New Revision: 1360354

URL: http://svn.apache.org/viewvc?rev=1360354&view=rev
Log:
LUCENE-4212: Tests should not use new Random() without args

Added:
    lucene/dev/trunk/lucene/tools/forbiddenApis/tests.txt   (with props)
Modified:
    lucene/dev/trunk/lucene/build.xml
    lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/codecs/mockrandom/MockRandomPostingsFormat.java
    lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java
    lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java
    lucene/dev/trunk/solr/build.xml
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/PreAnalyzedFieldTest.java
    lucene/dev/trunk/solr/core/src/test/org/apache/solr/util/CircularListTest.java

Modified: lucene/dev/trunk/lucene/build.xml
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/build.xml?rev=1360354&r1=1360353&r2=1360354&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/build.xml (original)
+++ lucene/dev/trunk/lucene/build.xml Wed Jul 11 19:10:08 2012
@@ -176,7 +176,7 @@
     <license-check-macro dir="${basedir}" />
   </target>
 
-  <target name="check-forbidden-apis" depends="compile-tools,compile-test,load-custom-tasks,-check-forbidden-jdk-apis,-check-system-out" description="Check forbidden API calls in compiled class files"/>
+  <target name="check-forbidden-apis" depends="compile-tools,compile-test,load-custom-tasks,-check-forbidden-jdk-apis,-check-forbidden-test-apis,-check-system-out" description="Check forbidden API calls in compiled class files"/>
 
   <target name="-check-forbidden-jdk-apis">
     <forbidden-apis> 
@@ -188,6 +188,16 @@
     </forbidden-apis>
   </target>
 
+  <target name="-check-forbidden-test-apis">
+    <forbidden-apis> 
+      <classpath refid="junit-path"/>
+      <apiFileSet dir="${custom-tasks.dir}/forbiddenApis">
+        <include name="tests.txt" />
+      </apiFileSet>
+      <fileset dir="${basedir}/build" includes="**/classes/test/**/*.class,test-framework/**/*.class" />
+    </forbidden-apis>
+  </target>
+
   <target name="-check-system-out">
     <forbidden-apis apiFile="${custom-tasks.dir}/forbiddenApis/system-out.txt">
       <fileset dir="${basedir}/build">

Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/codecs/mockrandom/MockRandomPostingsFormat.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/codecs/mockrandom/MockRandomPostingsFormat.java?rev=1360354&r1=1360353&r2=1360354&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/codecs/mockrandom/MockRandomPostingsFormat.java (original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/codecs/mockrandom/MockRandomPostingsFormat.java Wed Jul 11 19:10:08 2012
@@ -71,13 +71,22 @@ public class MockRandomPostingsFormat ex
   private final String SEED_EXT = "sd";
   
   public MockRandomPostingsFormat() {
-    // just for reading, we are gonna setSeed from the .seed file... right?
-    this(new Random());
+    // This ctor should *only* be used at read-time: get NPE if you use it!
+    this(null);
   }
   
   public MockRandomPostingsFormat(Random random) {
     super("MockRandom");
-    this.seedRandom = new Random(random.nextLong());
+    if (random == null) {
+      this.seedRandom = new Random(0L) {
+        @Override
+        protected int next(int arg0) {
+          throw new IllegalStateException("Please use MockRandomPostingsFormat(Random)");
+        }
+      };
+    } else {
+      this.seedRandom = new Random(random.nextLong());
+    }
   }
 
   // Chooses random IntStreamFactory depending on file's extension

Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java?rev=1360354&r1=1360353&r2=1360354&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java (original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java Wed Jul 11 19:10:08 2012
@@ -31,6 +31,7 @@ import org.apache.lucene.codecs.Codec;
 import org.apache.lucene.codecs.PostingsFormat;
 import org.apache.lucene.codecs.appending.AppendingCodec;
 import org.apache.lucene.codecs.lucene40.Lucene40Codec;
+import org.apache.lucene.codecs.mockrandom.MockRandomPostingsFormat;
 import org.apache.lucene.codecs.simpletext.SimpleTextCodec;
 import org.apache.lucene.index.RandomCodec;
 import org.apache.lucene.search.RandomSimilarityProvider;
@@ -167,9 +168,13 @@ final class TestRuleSetupAndRestoreClass
       assert (codec instanceof PreFlexRWCodec) : "fix your classpath to have tests-framework.jar before lucene-core.jar";
       PREFLEX_IMPERSONATION_IS_ACTIVE = true;
     } else */ if (!"random".equals(TEST_POSTINGSFORMAT)) {
-      codec = new Lucene40Codec() {
-        private final PostingsFormat format = PostingsFormat.forName(TEST_POSTINGSFORMAT);
-        
+      final PostingsFormat format;
+      if ("MockRandom".equals(TEST_POSTINGSFORMAT)) {
+        format = new MockRandomPostingsFormat(random);
+      } else {
+        format = PostingsFormat.forName(TEST_POSTINGSFORMAT);
+      }
+      codec = new Lucene40Codec() {       
         @Override
         public PostingsFormat getPostingsFormatForField(String field) {
           return format;

Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java?rev=1360354&r1=1360353&r2=1360354&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java (original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java Wed Jul 11 19:10:08 2012
@@ -79,6 +79,7 @@ import org.apache.lucene.store.Directory
 import org.apache.lucene.store.IOContext;
 import org.junit.Assert;
 
+import com.carrotsearch.randomizedtesting.RandomizedContext;
 import com.carrotsearch.randomizedtesting.generators.RandomInts;
 import com.carrotsearch.randomizedtesting.generators.RandomPicks;
 
@@ -731,8 +732,12 @@ public class _TestUtil {
     }
     String newSuffix = suffix == null ? ".tmp" : suffix;
     File result;
+    // just pull one long always: we don't want to rely upon what may or may not
+    // already exist. otherwise tests might not reproduce, depending on when you last
+    // ran 'ant clean'
+    final Random random = new Random(RandomizedContext.current().getRandom().nextLong());
     do {
-      result = genTempFile(prefix, newSuffix, directory);
+      result = genTempFile(random, prefix, newSuffix, directory);
     } while (!result.createNewFile());
     return result;
   }
@@ -746,12 +751,12 @@ public class _TestUtil {
   private static class TempFileLocker {};
   private static TempFileLocker tempFileLocker = new TempFileLocker();
 
-  private static File genTempFile(String prefix, String suffix, File directory) {
+  private static File genTempFile(Random random, String prefix, String suffix, File directory) {
     int identify = 0;
 
     synchronized (tempFileLocker) {
       if (counter == 0) {
-        int newInt = new Random().nextInt();
+        int newInt = random.nextInt();
         counter = ((newInt / 65535) & 0xFFFF) + 0x2710;
         counterBase = counter;
       }

Added: lucene/dev/trunk/lucene/tools/forbiddenApis/tests.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/tools/forbiddenApis/tests.txt?rev=1360354&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/tools/forbiddenApis/tests.txt (added)
+++ lucene/dev/trunk/lucene/tools/forbiddenApis/tests.txt Wed Jul 11 19:10:08 2012
@@ -0,0 +1,10 @@
+# All classes should derive from LuceneTestCase
+junit.framework.TestCase
+
+# Use RandomizedRunner's random instead
+java.util.Random#<init>()
+
+# Don't depend on wall clock times
+# TODO: fix tests that do this!
+#java.lang.System#currentTimeMillis()
+#java.lang.System#nanoTime()

Modified: lucene/dev/trunk/solr/build.xml
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/build.xml?rev=1360354&r1=1360353&r2=1360354&view=diff
==============================================================================
--- lucene/dev/trunk/solr/build.xml (original)
+++ lucene/dev/trunk/solr/build.xml Wed Jul 11 19:10:08 2012
@@ -190,7 +190,10 @@
     </license-check-macro>
   </target>
   
-  <target name="check-forbidden-apis" depends="compile-tools,compile-test,load-custom-tasks" description="Check forbidden API calls in compiled class files.">
+  <target name="check-forbidden-apis" depends="compile-tools,compile-test,load-custom-tasks,-check-forbidden-java-apis,-check-forbidden-test-apis" 
+          description="Check forbidden API calls in compiled class files."/>
+
+  <target name="-check-forbidden-java-apis">
     <forbidden-apis>
       <classpath refid="additional.dependencies"/>
       <apiFileSet dir="${custom-tasks.dir}/forbiddenApis">
@@ -206,6 +209,23 @@
     </forbidden-apis>
   </target>
 
+  <target name="-check-forbidden-test-apis">
+    <forbidden-apis>
+      <classpath refid="junit-path"/>
+      <apiFileSet dir="${custom-tasks.dir}/forbiddenApis">
+        <include name="tests.txt" />
+      </apiFileSet>
+      <fileset dir="${basedir}/build">
+        <include name="**/classes/test/**/*.class"/>
+        <include name="solr-test-framework/**/*.class"/>
+        <!-- not actually a test -->
+        <exclude name="solr-core/classes/test/org/apache/solr/search/DocSetPerf.class"/>
+        <!-- imported code -->
+        <exclude name="solr-core/classes/test/org/apache/solr/internal/**/*.class"/>
+      </fileset>
+    </forbidden-apis>
+  </target>
+
   <!-- rat sources -->
   <target name="rat-sources">
     <sequential>

Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java?rev=1360354&r1=1360353&r2=1360354&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java Wed Jul 11 19:10:08 2012
@@ -158,7 +158,7 @@ public class CurrencyFieldTest extends S
 
   @Ignore
   public void testPerformance() throws Exception {
-    Random r = new Random();
+    Random r = random();
     int initDocs = 200000;
 
     for (int i = 1; i <= initDocs; i++) {

Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/PreAnalyzedFieldTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/PreAnalyzedFieldTest.java?rev=1360354&r1=1360353&r2=1360354&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/PreAnalyzedFieldTest.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/PreAnalyzedFieldTest.java Wed Jul 11 19:10:08 2012
@@ -21,13 +21,12 @@ import java.util.Collections;
 import java.util.HashMap;
 
 import org.apache.lucene.document.Field;
+import org.apache.lucene.util.LuceneTestCase;
 import org.apache.solr.common.util.Base64;
 import org.apache.solr.schema.PreAnalyzedField.PreAnalyzedParser;
 import org.junit.Test;
 
-import junit.framework.TestCase;
-
-public class PreAnalyzedFieldTest extends TestCase {
+public class PreAnalyzedFieldTest extends LuceneTestCase {
   
   private static final String[] valid = {
     "1 one two three",                       // simple parsing

Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/util/CircularListTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/util/CircularListTest.java?rev=1360354&r1=1360353&r2=1360354&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/util/CircularListTest.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/util/CircularListTest.java Wed Jul 11 19:10:08 2012
@@ -19,15 +19,14 @@ package org.apache.solr.util;
 
 import java.io.IOException;
 
-import junit.framework.TestCase;
-
+import org.apache.lucene.util.LuceneTestCase;
 import org.apache.solr.logging.CircularList;
 import org.junit.Test;
 
 /** 
  * Test circular list
  */
-public class CircularListTest  extends TestCase {  
+public class CircularListTest  extends LuceneTestCase {  
 
   @Test
   public void testCircularList() throws IOException {