Skip to content

Apache Lucene open-source search software

LUCENE-4212: Tests should not use new Random() without args

Robert Muirdc90a019a247
diff --git a/lucene/build.xml b/lucene/build.xml
index 45eb767a681..8d2b8824f77 100644
--- a/lucene/build.xml
+++ b/lucene/build.xml
@@ -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">
diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/mockrandom/MockRandomPostingsFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/mockrandom/MockRandomPostingsFormat.java
index 6ed10f07719..afb836515d3 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/codecs/mockrandom/MockRandomPostingsFormat.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/mockrandom/MockRandomPostingsFormat.java
@@ -71,13 +71,22 @@ public class MockRandomPostingsFormat extends PostingsFormat {
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
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java b/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java
index 22dacfa56b3..19b1047e535 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleSetupAndRestoreClassEnv.java
@@ -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 TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
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;
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java
index bded14137f3..2a719630fa8 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java
@@ -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;
}
diff --git a/lucene/tools/forbiddenApis/tests.txt b/lucene/tools/forbiddenApis/tests.txt
new file mode 100644
index 00000000000..2fca956d6a2
--- /dev/null
+++ b/lucene/tools/forbiddenApis/tests.txt
@@ -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()
diff --git a/solr/build.xml b/solr/build.xml
index 6109e35428b..e3204d10f4b 100644
--- a/solr/build.xml
+++ b/solr/build.xml
@@ -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>
diff --git a/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java b/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java
index 199b79340cf..d698e70ec14 100644
--- a/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java
+++ b/solr/core/src/test/org/apache/solr/schema/CurrencyFieldTest.java
@@ -158,7 +158,7 @@ public class CurrencyFieldTest extends SolrTestCaseJ4 {
@Ignore
public void testPerformance() throws Exception {
- Random r = new Random();
+ Random r = random();
int initDocs = 200000;
for (int i = 1; i <= initDocs; i++) {
diff --git a/solr/core/src/test/org/apache/solr/schema/PreAnalyzedFieldTest.java b/solr/core/src/test/org/apache/solr/schema/PreAnalyzedFieldTest.java
index 365952890ac..dcc59779fb0 100644
--- a/solr/core/src/test/org/apache/solr/schema/PreAnalyzedFieldTest.java
+++ b/solr/core/src/test/org/apache/solr/schema/PreAnalyzedFieldTest.java
@@ -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
diff --git a/solr/core/src/test/org/apache/solr/util/CircularListTest.java b/solr/core/src/test/org/apache/solr/util/CircularListTest.java
index b0c6c06d552..f4c4b9de47d 100644
--- a/solr/core/src/test/org/apache/solr/util/CircularListTest.java
+++ b/solr/core/src/test/org/apache/solr/util/CircularListTest.java
@@ -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 {