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

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1360354 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-07-11 19:10:08 +00:00
parent 29d8ba42d4
commit dc90a019a2
9 changed files with 75 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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++) {

View File

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

View File

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