Apache Solr open-source search software
LUCENE-1535: use random seed for randomness, but print the seed on failure for reproducibility
Michael McCandlessdbe0898d63ef
diff --git a/contrib/db/bdb-je/build.xml b/contrib/db/bdb-je/build.xmlindex 19ec02ec1cb..782a297a9ea 100644--- a/contrib/db/bdb-je/build.xml+++ b/contrib/db/bdb-je/build.xml@@ -23,6 +23,13 @@<import file="../../contrib-build.xml" />+ <path id="test.classpath">+ <path refid="classpath"/>+ <pathelement location="../../../build/classes/test/"/>+ <path refid="junit-path"/>+ <pathelement location="${build.dir}/classes/java"/>+ </path>+<target name="get-je-jar" unless="je.jar.exists"><mkdir dir="lib" /><get src="http://download.oracle.com/berkeley-db/je-${je.version}.zip"diff --git a/contrib/db/bdb-je/src/test/org/apache/lucene/store/je/JEStoreTest.java b/contrib/db/bdb-je/src/test/org/apache/lucene/store/je/JEStoreTest.javaindex 3e39eb419e9..cf2c5de389f 100644--- a/contrib/db/bdb-je/src/test/org/apache/lucene/store/je/JEStoreTest.java+++ b/contrib/db/bdb-je/src/test/org/apache/lucene/store/je/JEStoreTest.java@@ -23,11 +23,10 @@ import java.util.Arrays;import java.util.Date;import java.util.Random;-import junit.framework.TestCase;-import org.apache.lucene.store.Directory;import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.IndexOutput;+import org.apache.lucene.util.LuceneTestCase;import com.sleepycat.je.Cursor;import com.sleepycat.je.Database;@@ -46,7 +45,7 @@ import com.sleepycat.je.Transaction;* Adapted from Andi Vajda's org.apache.lucene.db.DbStoreTest.**/-public class JEStoreTest extends TestCase {+public class JEStoreTest extends LuceneTestCase {protected File dbHome = new File(System.getProperty("java.io.tmpdir"),"index");protected Environment env;@@ -112,7 +111,9 @@ public class JEStoreTest extends TestCase {final int count = 250;final int LENGTH_MASK = 0xffff;- Random gen = new Random(1251971);+ Random r = newRandom();+ final long seed = r.nextLong();+ Random gen = new Random(seed);int totalLength = 0;int duration;Date end;@@ -171,7 +172,7 @@ public class JEStoreTest extends TestCase {txn = env.beginTransaction(null, null);store = new JEDirectory(txn, index, blocks);- gen = new Random(1251971);+ gen = new Random(seed);start = new Date();for (int i = 0; i < count; i++) {@@ -220,7 +221,7 @@ public class JEStoreTest extends TestCase {txn = env.beginTransaction(null, null);store = new JEDirectory(txn, index, blocks);- gen = new Random(1251971);+ gen = new Random(seed);start = new Date();for (int i = 0; i < count; i++) {@@ -257,7 +258,9 @@ public class JEStoreTest extends TestCase {final int count = 250;final int LENGTH_MASK = 0xffff;- Random gen = new Random(1251971);+ Random r = newRandom();+ final long seed = r.nextLong();+ Random gen = new Random(seed);int totalLength = 0;int duration;Date end;@@ -316,7 +319,7 @@ public class JEStoreTest extends TestCase {txn = env.beginTransaction(null, null);store = new JEDirectory(txn, index, blocks);- gen = new Random(1251971);+ gen = new Random(seed);start = new Date();for (int i = 0; i < count; i++) {@@ -357,7 +360,7 @@ public class JEStoreTest extends TestCase {txn = env.beginTransaction(null, null);store = new JEDirectory(txn, index, blocks);- gen = new Random(1251971);+ gen = new Random(seed);start = new Date();for (int i = 0; i < count; i++) {@@ -412,7 +415,7 @@ public class JEStoreTest extends TestCase {txn = env.beginTransaction(null, null);store = new JEDirectory(txn, index, blocks);- gen = new Random(1251971);+ gen = new Random(seed);start = new Date();for (int i = 0; i < count; i++) {@@ -486,7 +489,9 @@ public class JEStoreTest extends TestCase {final int count = 250;final int LENGTH_MASK = 0xffff;- Random gen = new Random(1251971);+ Random r = newRandom();+ final long seed = r.nextLong();+ Random gen = new Random(seed);int totalLength = 0;int duration;Date end;@@ -543,7 +548,7 @@ public class JEStoreTest extends TestCase {txn = env.beginTransaction(null, null);store = new JEDirectory(txn, index, blocks);- gen = new Random(1251971);+ gen = new Random(seed);start = new Date();for (int i = 0; i < count; i++) {@@ -593,7 +598,7 @@ public class JEStoreTest extends TestCase {txn = env.beginTransaction(null, null);store = new JEDirectory(txn, index, blocks);- gen = new Random(1251971);+ gen = new Random(seed);start = new Date();for (int i = 0; i < count; i++) {String name = i + ".dat";diff --git a/contrib/db/bdb/build.xml b/contrib/db/bdb/build.xmlindex d0c6a3575af..77e63622bdd 100644--- a/contrib/db/bdb/build.xml+++ b/contrib/db/bdb/build.xml@@ -23,6 +23,13 @@<import file="../../contrib-build.xml" />+ <path id="test.classpath">+ <path refid="classpath"/>+ <pathelement location="../../../build/classes/test/"/>+ <path refid="junit-path"/>+ <pathelement location="${build.dir}/classes/java"/>+ </path>+<target name="get-db-jar" unless="db.jar.exists"><mkdir dir="lib" /><get src="http://downloads.osafoundation.org/db/db-${db.version}.jar"diff --git a/contrib/db/bdb/src/test/org/apache/lucene/store/db/DbStoreTest.java b/contrib/db/bdb/src/test/org/apache/lucene/store/db/DbStoreTest.javaindex 03bf3102181..5e965316ab5 100644--- a/contrib/db/bdb/src/test/org/apache/lucene/store/db/DbStoreTest.java+++ b/contrib/db/bdb/src/test/org/apache/lucene/store/db/DbStoreTest.java@@ -24,8 +24,6 @@ import java.util.Arrays;import java.io.File;import java.io.IOException;-import junit.framework.TestCase;-import com.sleepycat.db.EnvironmentConfig;import com.sleepycat.db.Environment;import com.sleepycat.db.Transaction;@@ -37,13 +35,14 @@ import com.sleepycat.db.DatabaseException;import org.apache.lucene.store.Directory;import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.IndexOutput;+import org.apache.lucene.util.LuceneTestCase;/*** Tests {@link DbDirectory}.** Adapted from org.apache.lucene.StoreTest with larger files and random bytes.*/-public class DbStoreTest extends TestCase {+public class DbStoreTest extends LuceneTestCase {protected File dbHome = new File(System.getProperty("java.io.tmpdir"),"index");protected Environment env;protected Database index, blocks;@@ -117,7 +116,10 @@ public class DbStoreTest extends TestCase {final int count = 250;final int LENGTH_MASK = 0xffff;- Random gen = new Random(1251971);+ Random r = newRandom();+ final long seed = r.nextLong();++ Random gen = new Random(seed);int totalLength = 0;int duration;Date end;@@ -177,7 +179,7 @@ public class DbStoreTest extends TestCase {txn = env.beginTransaction(null, null);store = new DbDirectory(txn, index, blocks);- gen = new Random(1251971);+ gen = new Random(seed);start = new Date();for (int i = 0; i < count; i++) {@@ -227,7 +229,7 @@ public class DbStoreTest extends TestCase {txn = env.beginTransaction(null, null);store = new DbDirectory(txn, index, blocks);- gen = new Random(1251971);+ gen = new Random(seed);start = new Date();for (int i = 0; i < count; i++) {@@ -267,7 +269,10 @@ public class DbStoreTest extends TestCase {final int count = 250;final int LENGTH_MASK = 0xffff;- Random gen = new Random(1251971);+ Random r = newRandom();+ final long seed = r.nextLong();++ Random gen = new Random(seed);int totalLength = 0;int duration;Date end;@@ -325,7 +330,7 @@ public class DbStoreTest extends TestCase {txn = env.beginTransaction(null, null);store = new DbDirectory(txn, index, blocks);- gen = new Random(1251971);+ gen = new Random(seed);start = new Date();for (int i = 0; i < count; i++) {@@ -376,7 +381,7 @@ public class DbStoreTest extends TestCase {txn = env.beginTransaction(null, null);store = new DbDirectory(txn, index, blocks);- gen = new Random(1251971);+ gen = new Random(seed);start = new Date();for (int i = 0; i < count; i++) {diff --git a/contrib/db/build.xml b/contrib/db/build.xmlindex 7c9ec1301f1..e36b5d0ee0f 100644--- a/contrib/db/build.xml+++ b/contrib/db/build.xml@@ -64,5 +64,6 @@<ant dir="bdb-je" target="javadocs" /></target>+</project>diff --git a/contrib/queries/src/test/org/apache/lucene/search/trie/TestTrieRangeQuery.java b/contrib/queries/src/test/org/apache/lucene/search/trie/TestTrieRangeQuery.javaindex e4da7319888..d132bca733f 100644--- a/contrib/queries/src/test/org/apache/lucene/search/trie/TestTrieRangeQuery.java+++ b/contrib/queries/src/test/org/apache/lucene/search/trie/TestTrieRangeQuery.java@@ -36,11 +36,10 @@ public class TestTrieRangeQuery extends LuceneTestCase{private static final long distance=66666;- private static Random rnd=new Random();- private static RAMDirectory directory;- private static IndexSearcher searcher;+ private static final RAMDirectory directory;+ private static final IndexSearcher searcher;static {- try {+ try {directory = new RAMDirectory();IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(),true, MaxFieldLength.UNLIMITED);@@ -136,6 +135,7 @@ public class TestTrieRangeQuery extends LuceneTestCase}private void testRandomTrieAndClassicRangeQuery(final TrieUtils variant) throws Exception {+ final Random rnd=newRandom();String field="field"+variant.TRIE_BITS;// 50 random tests, the tests may also return 0 results, if min>max, but this is okfor (int i=0; i<50; i++) {@@ -185,6 +185,7 @@ public class TestTrieRangeQuery extends LuceneTestCase}private void testRangeSplit(final TrieUtils variant) throws Exception {+ final Random rnd=newRandom();String field="ascfield"+variant.TRIE_BITS;// 50 random testsfor (int i=0; i<50; i++) {@@ -225,6 +226,7 @@ public class TestTrieRangeQuery extends LuceneTestCase}private void testSorting(final TrieUtils variant) throws Exception {+ final Random rnd=newRandom();String field="field"+variant.TRIE_BITS;// 10 random tests, the index order is ascending,// so using a reverse sort field should retun descending documentsdiff --git a/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java b/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.javaindex 4653cd2f77e..0185f17d3a1 100644--- a/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java+++ b/src/java/org/apache/lucene/index/ConcurrentMergeScheduler.java@@ -329,6 +329,9 @@ public class ConcurrentMergeScheduler extends MergeScheduler {/** Used for testing */public static boolean anyUnhandledExceptions() {+ if (allInstances == null) {+ throw new RuntimeException("setTestMode() was not called; often this is because your test case's setUp method fails to call super.setUp in LuceneTestCase");+ }synchronized(allInstances) {final int count = allInstances.size();// Make sure all outstanding threads are done so we seediff --git a/src/test/org/apache/lucene/index/TestAtomicUpdate.java b/src/test/org/apache/lucene/index/TestAtomicUpdate.javaindex 31617e80cdc..f03a2c21a87 100644--- a/src/test/org/apache/lucene/index/TestAtomicUpdate.java+++ b/src/test/org/apache/lucene/index/TestAtomicUpdate.java@@ -29,7 +29,7 @@ import java.io.IOException;public class TestAtomicUpdate extends LuceneTestCase {private static final Analyzer ANALYZER = new SimpleAnalyzer();- private static final Random RANDOM = new Random();+ private Random RANDOM;public class MockIndexWriter extends IndexWriter {@@ -179,7 +179,7 @@ public class TestAtomicUpdate extends LuceneTestCase {FSDirectory.*/public void testAtomicUpdates() throws Exception {-+ RANDOM = newRandom();Directory directory;// First in a RAM directory:diff --git a/src/test/org/apache/lucene/index/TestByteSlices.java b/src/test/org/apache/lucene/index/TestByteSlices.javaindex 14a84485941..903e87e0eb1 100644--- a/src/test/org/apache/lucene/index/TestByteSlices.java+++ b/src/test/org/apache/lucene/index/TestByteSlices.java@@ -52,7 +52,7 @@ public class TestByteSlices extends LuceneTestCase {int[] uptos = new int[NUM_STREAM];int[] counters = new int[NUM_STREAM];- Random r = new Random(1);+ Random r = newRandom();ByteSliceReader reader = new ByteSliceReader();diff --git a/src/test/org/apache/lucene/index/TestIndexReaderReopen.java b/src/test/org/apache/lucene/index/TestIndexReaderReopen.javaindex 63a854f970b..3781e89ce4d 100644--- a/src/test/org/apache/lucene/index/TestIndexReaderReopen.java+++ b/src/test/org/apache/lucene/index/TestIndexReaderReopen.java@@ -710,7 +710,7 @@ public class TestIndexReaderReopen extends LuceneTestCase {final List readers = Collections.synchronizedList(new ArrayList());IndexReader firstReader = IndexReader.open(dir);IndexReader reader = firstReader;- final Random rnd = new Random();+ final Random rnd = newRandom();ReaderThread[] threads = new ReaderThread[n];final Set readersToClose = Collections.synchronizedSet(new HashSet());@@ -1113,7 +1113,7 @@ public class TestIndexReaderReopen extends LuceneTestCase {IndexReader lastReader = IndexReader.open(indexDir2);- Random r = new Random(42);+ Random r = newRandom();for(int i=0;i<10;i++) {int mod = r.nextInt(5);modifyIndex(mod, lastReader.directory());diff --git a/src/test/org/apache/lucene/index/TestIndexWriter.java b/src/test/org/apache/lucene/index/TestIndexWriter.javaindex 6ac7919a84b..20421e84185 100644--- a/src/test/org/apache/lucene/index/TestIndexWriter.java+++ b/src/test/org/apache/lucene/index/TestIndexWriter.java@@ -1411,7 +1411,7 @@ public class TestIndexWriter extends LuceneTestCaseRAMDirectory dir = new RAMDirectory();IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);writer.setRAMBufferSizeMB(0.5);- Random rand = new Random(31415);+ Random rand = newRandom();for(int i=0;i<3;i++) {// First, docs where every term is unique (heavy on// Posting instances)@@ -3442,7 +3442,7 @@ public class TestIndexWriter extends LuceneTestCase}}- Random r = new Random();+ Random r;private int nextInt(int lim) {return r.nextInt(lim);@@ -3498,6 +3498,7 @@ public class TestIndexWriter extends LuceneTestCase// LUCENE-510public void testRandomUnicodeStrings() throws Throwable {+ r = newRandom();char[] buffer = new char[20];char[] expected = new char[20];@@ -3525,6 +3526,7 @@ public class TestIndexWriter extends LuceneTestCase// LUCENE-510public void testIncrementalUnicodeStrings() throws Throwable {+ r = newRandom();char[] buffer = new char[20];char[] expected = new char[20];diff --git a/src/test/org/apache/lucene/index/TestLazyBug.java b/src/test/org/apache/lucene/index/TestLazyBug.javaindex 3f68e7ec80a..c3cbd0ab263 100755--- a/src/test/org/apache/lucene/index/TestLazyBug.java+++ b/src/test/org/apache/lucene/index/TestLazyBug.java@@ -25,7 +25,6 @@ import org.apache.lucene.store.Directory;import org.apache.lucene.store.RAMDirectory;import java.util.*;-import java.lang.reflect.Array;/**@@ -34,8 +33,6 @@ import java.lang.reflect.Array;*/public class TestLazyBug extends LuceneTestCase {- public static int BASE_SEED = 13;-public static int NUM_DOCS = 500;public static int NUM_FIELDS = 100;@@ -62,10 +59,10 @@ public class TestLazyBug extends LuceneTestCase {}};- private static Directory makeIndex() throws RuntimeException {+ private Directory makeIndex() throws RuntimeException {Directory dir = new RAMDirectory();try {- Random r = new Random(BASE_SEED + 42) ;+ Random r = newRandom();Analyzer analyzer = new SimpleAnalyzer();IndexWriter writer = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);@@ -89,12 +86,12 @@ public class TestLazyBug extends LuceneTestCase {return dir;}- public static void doTest(int[] docs) throws Exception {+ public void doTest(int[] docs) throws Exception {Directory dir = makeIndex();IndexReader reader = IndexReader.open(dir);for (int i = 0; i < docs.length; i++) {Document d = reader.document(docs[i], SELECTOR);- String trash = d.get(MAGIC_FIELD);+ d.get(MAGIC_FIELD);List fields = d.getFields();for (Iterator fi = fields.iterator(); fi.hasNext(); ) {diff --git a/src/test/org/apache/lucene/index/TestPayloads.java b/src/test/org/apache/lucene/index/TestPayloads.javaindex f915e6d8aa2..21e73ba8990 100644--- a/src/test/org/apache/lucene/index/TestPayloads.java+++ b/src/test/org/apache/lucene/index/TestPayloads.java@@ -47,6 +47,7 @@ public class TestPayloads extends LuceneTestCase {// Simple tests to test the Payload classpublic void testPayload() throws Exception {+ rnd = newRandom();byte[] testData = "This is a test!".getBytes();Payload payload = new Payload(testData);assertEquals("Wrong payload length.", testData.length, payload.length());@@ -95,6 +96,7 @@ public class TestPayloads extends LuceneTestCase {// Tests whether the DocumentWriter and SegmentMerger correctly enable the// payload bit in the FieldInfopublic void testPayloadFieldBit() throws Exception {+ rnd = newRandom();Directory ram = new RAMDirectory();PayloadAnalyzer analyzer = new PayloadAnalyzer();IndexWriter writer = new IndexWriter(ram, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);@@ -151,6 +153,7 @@ public class TestPayloads extends LuceneTestCase {// Tests if payloads are correctly stored and loaded using both RamDirectory and FSDirectorypublic void testPayloadsEncoding() throws Exception {+ rnd = newRandom();// first perform the test using a RAMDirectoryDirectory dir = new RAMDirectory();performTest(dir);@@ -333,13 +336,13 @@ public class TestPayloads extends LuceneTestCase {}- private static Random rnd = new Random();+ private Random rnd;- private static void generateRandomData(byte[] data) {+ private void generateRandomData(byte[] data) {rnd.nextBytes(data);}- private static byte[] generateRandomData(int n) {+ private byte[] generateRandomData(int n) {byte[] data = new byte[n];generateRandomData(data);return data;@@ -473,6 +476,7 @@ public class TestPayloads extends LuceneTestCase {}public void testThreadSafety() throws IOException {+ rnd = newRandom();final int numThreads = 5;final int numDocs = 50;final ByteArrayPool pool = new ByteArrayPool(numThreads, 5);@@ -525,7 +529,7 @@ public class TestPayloads extends LuceneTestCase {assertEquals(pool.size(), numThreads);}- private static class PoolingPayloadTokenStream extends TokenStream {+ private class PoolingPayloadTokenStream extends TokenStream {private byte[] payload;private boolean first;private ByteArrayPool pool;diff --git a/src/test/org/apache/lucene/index/TestStressIndexing.java b/src/test/org/apache/lucene/index/TestStressIndexing.javaindex 17b6968cf11..a7f730f72bd 100644--- a/src/test/org/apache/lucene/index/TestStressIndexing.java+++ b/src/test/org/apache/lucene/index/TestStressIndexing.java@@ -28,7 +28,7 @@ import java.io.File;public class TestStressIndexing extends LuceneTestCase {private static final Analyzer ANALYZER = new SimpleAnalyzer();- private static final Random RANDOM = new Random();+ private Random RANDOM;private static abstract class TimedThread extends Thread {boolean failed;@@ -67,7 +67,7 @@ public class TestStressIndexing extends LuceneTestCase {}}- private static class IndexerThread extends TimedThread {+ private class IndexerThread extends TimedThread {IndexWriter writer;public int count;int nextID;@@ -164,6 +164,7 @@ public class TestStressIndexing extends LuceneTestCase {FSDirectory.*/public void testStressIndexAndSearching() throws Exception {+ RANDOM = newRandom();// RAMDirDirectory directory = new MockRAMDirectory();diff --git a/src/test/org/apache/lucene/index/TestStressIndexing2.java b/src/test/org/apache/lucene/index/TestStressIndexing2.javaindex c7932db9a55..0ae023b8c8f 100644--- a/src/test/org/apache/lucene/index/TestStressIndexing2.java+++ b/src/test/org/apache/lucene/index/TestStressIndexing2.java@@ -36,7 +36,7 @@ public class TestStressIndexing2 extends LuceneTestCase {static int maxBufferedDocs=3;static int seed=0;- static Random r = new Random(0);+ Random r;public class MockIndexWriter extends IndexWriter {@@ -53,6 +53,7 @@ public class TestStressIndexing2 extends LuceneTestCase {}public void testRandom() throws Throwable {+ r = newRandom();Directory dir1 = new MockRAMDirectory();// dir1 = FSDirectory.getDirectory("foofoofoo");Directory dir2 = new MockRAMDirectory();@@ -69,6 +70,7 @@ public class TestStressIndexing2 extends LuceneTestCase {public void testMultiConfig() throws Throwable {// test lots of smaller different params together+ r = newRandom();for (int i=0; i<100; i++) { // increase iterations for better testingsameFieldOrder=r.nextBoolean();autoCommit=r.nextBoolean();diff --git a/src/test/org/apache/lucene/index/TestTermdocPerf.java b/src/test/org/apache/lucene/index/TestTermdocPerf.javaindex 04b4d20b17a..806e8f1ffb4 100644--- a/src/test/org/apache/lucene/index/TestTermdocPerf.java+++ b/src/test/org/apache/lucene/index/TestTermdocPerf.java@@ -58,7 +58,7 @@ class RepeatingTokenStream extends TokenStream {public class TestTermdocPerf extends LuceneTestCase {void addDocs(Directory dir, final int ndocs, String field, final String val, final int maxTF, final float percentDocs) throws IOException {- final Random random = new Random(0);+ final Random random = newRandom();final RepeatingTokenStream ts = new RepeatingTokenStream(val);Analyzer analyzer = new Analyzer() {diff --git a/src/test/org/apache/lucene/index/TestTransactions.java b/src/test/org/apache/lucene/index/TestTransactions.javaindex 3af46d63040..f623d76e934 100644--- a/src/test/org/apache/lucene/index/TestTransactions.java+++ b/src/test/org/apache/lucene/index/TestTransactions.java@@ -26,7 +26,7 @@ import org.apache.lucene.document.*;public class TestTransactions extends LuceneTestCase{- private static final Random RANDOM = new Random();+ private Random RANDOM;private static volatile boolean doFail;private class RandomFailure extends MockRAMDirectory.Failure {@@ -68,7 +68,7 @@ public class TestTransactions extends LuceneTestCase}}- private static class IndexerThread extends TimedThread {+ private class IndexerThread extends TimedThread {Directory dir1;Directory dir2;Object lock;@@ -183,6 +183,7 @@ public class TestTransactions extends LuceneTestCase}public void testTransactions() throws Throwable {+ RANDOM = newRandom();MockRAMDirectory dir1 = new MockRAMDirectory();MockRAMDirectory dir2 = new MockRAMDirectory();dir1.setPreventDoubleWrite(false);diff --git a/src/test/org/apache/lucene/search/BaseTestRangeFilter.java b/src/test/org/apache/lucene/search/BaseTestRangeFilter.javaindex 35956e80dcb..f6db5af8ce4 100644--- a/src/test/org/apache/lucene/search/BaseTestRangeFilter.java+++ b/src/test/org/apache/lucene/search/BaseTestRangeFilter.java@@ -32,7 +32,7 @@ public class BaseTestRangeFilter extends LuceneTestCase {public static final boolean F = false;public static final boolean T = true;- Random rand = new Random(101); // use a set seed to test is deterministic+ protected Random rand;/*** Collation interacts badly with hyphens -- collation produces different@@ -83,10 +83,12 @@ public class BaseTestRangeFilter extends LuceneTestCase {public BaseTestRangeFilter(String name) {super(name);+ rand = newRandom();build(signedIndex);build(unsignedIndex);}public BaseTestRangeFilter() {+ rand = newRandom();build(signedIndex);build(unsignedIndex);}diff --git a/src/test/org/apache/lucene/search/TestBoolean2.java b/src/test/org/apache/lucene/search/TestBoolean2.javaindex d7ec5cf7705..bd8282b9407 100644--- a/src/test/org/apache/lucene/search/TestBoolean2.java+++ b/src/test/org/apache/lucene/search/TestBoolean2.java@@ -152,7 +152,7 @@ public class TestBoolean2 extends LuceneTestCase {}public void testRandomQueries() throws Exception {- Random rnd = new Random(0);+ Random rnd = newRandom();String[] vals = {"w1","w2","w3","w4","w5","xx","yy","zzz"};@@ -163,7 +163,7 @@ public class TestBoolean2 extends LuceneTestCase {// increase number of iterations for more complete testingfor (int i=0; i<1000; i++) {int level = rnd.nextInt(3);- BooleanQuery q1 = randBoolQuery(new Random(i), level, field, vals, null);+ BooleanQuery q1 = randBoolQuery(new Random(rnd.nextLong()), level, field, vals, null);// Can't sort by relevance since floating point numbers may not quite// match up.diff --git a/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java b/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.javaindex 8d4b73ce633..87494d64c48 100644--- a/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java+++ b/src/test/org/apache/lucene/search/TestBooleanMinShouldMatch.java@@ -296,7 +296,7 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {}public void testRandomQueries() throws Exception {- final Random rnd = new Random(0);+ final Random rnd = newRandom();String field="data";String[] vals = {"1","2","3","4","5","6","A","Z","B","Y","Z","X","foo"};@@ -319,9 +319,10 @@ public class TestBooleanMinShouldMatch extends LuceneTestCase {// increase number of iterations for more complete testingfor (int i=0; i<1000; i++) {int lev = rnd.nextInt(maxLev);- BooleanQuery q1 = TestBoolean2.randBoolQuery(new Random(i), lev, field, vals, null);- // BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(i), lev, field, vals, minNrCB);- BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(i), lev, field, vals, null);+ final long seed = rnd.nextLong();+ BooleanQuery q1 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, null);+ // BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, minNrCB);+ BooleanQuery q2 = TestBoolean2.randBoolQuery(new Random(seed), lev, field, vals, null);// only set minimumNumberShouldMatch on the top level query since setting// at a lower level can change the score.minNrCB.postCreate(q2);diff --git a/src/test/org/apache/lucene/search/TestCustomSearcherSort.java b/src/test/org/apache/lucene/search/TestCustomSearcherSort.javaindex 142e6ab025d..02349923f64 100644--- a/src/test/org/apache/lucene/search/TestCustomSearcherSort.java+++ b/src/test/org/apache/lucene/search/TestCustomSearcherSort.java@@ -25,7 +25,6 @@ import java.util.Random;import java.util.TreeMap;import junit.framework.Test;-import junit.framework.TestCase;import junit.framework.TestSuite;import junit.textui.TestRunner;@@ -38,6 +37,7 @@ import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.store.Directory;import org.apache.lucene.store.RAMDirectory;+import org.apache.lucene.util.LuceneTestCase;/*** Unit test for sorting code.@@ -45,7 +45,7 @@ import org.apache.lucene.store.RAMDirectory;*/public class TestCustomSearcherSort-extends TestCase+extends LuceneTestCaseimplements Serializable {private Directory index = null;@@ -71,7 +71,7 @@ implements Serializable {throws IOException {RAMDirectory indexStore = new RAMDirectory ();IndexWriter writer = new IndexWriter (indexStore, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);- RandomGen random = new RandomGen();+ RandomGen random = new RandomGen(newRandom());for (int i=0; i<INDEX_SIZE; ++i) { // don't decrease; if to low the problem doesn't show upDocument doc = new Document();if((i%5)!=0) { // some documents must not have an entry in the first sort field@@ -93,8 +93,9 @@ implements Serializable {* Create index and query for test cases.*/public void setUp() throws Exception {- index = getIndex();- query = new TermQuery( new Term("content", "test"));+ super.setUp();+ index = getIndex();+ query = new TermQuery( new Term("content", "test"));}/**@@ -164,7 +165,6 @@ implements Serializable {ScoreDoc[] resultSort = searcher.search (query, null, 1000, sort).scoreDocs;checkHits(resultSort, "Sort by custom criteria: "); // check for duplicates- String lf = System.getProperty("line.separator", "\n");// besides the sorting both sets of hits must be identicalfor(int hitid=0;hitid<resultSort.length; ++hitid) {Integer idHitDate = new Integer(resultSort[hitid].doc); // document ID from sorted search@@ -263,12 +263,15 @@ implements Serializable {}}private class RandomGen {- private Random random = new Random(0); // to generate some arbitrary contents- private Calendar base = new GregorianCalendar(1980, 1, 1);+ RandomGen(Random random) {+ this.random = random;+ }+ private Random random;+ private Calendar base = new GregorianCalendar(1980, 1, 1);- // Just to generate some different Lucene Date strings- private String getLuceneDate() {- return DateTools.timeToString(base.getTimeInMillis() + random.nextInt() - Integer.MIN_VALUE, DateTools.Resolution.DAY);- }+ // Just to generate some different Lucene Date strings+ private String getLuceneDate() {+ return DateTools.timeToString(base.getTimeInMillis() + random.nextInt() - Integer.MIN_VALUE, DateTools.Resolution.DAY);+ }}}diff --git a/src/test/org/apache/lucene/search/TestScorerPerf.java b/src/test/org/apache/lucene/search/TestScorerPerf.javaindex 4794b9743f6..9bf736329b8 100755--- a/src/test/org/apache/lucene/search/TestScorerPerf.java+++ b/src/test/org/apache/lucene/search/TestScorerPerf.java@@ -38,7 +38,7 @@ import org.apache.lucene.document.Field;* @version $Id$*/public class TestScorerPerf extends LuceneTestCase {- Random r = new Random(0);+ Random r;boolean validate = true; // set to false when doing performance testingBitSet[] sets;@@ -306,6 +306,7 @@ public class TestScorerPerf extends LuceneTestCase {public void testConjunctions() throws Exception {// test many small sets... the bugs will be found on boundary conditions+ r = newRandom();createDummySearcher();validate=true;sets=randBitSets(1000,10);@@ -318,6 +319,7 @@ public class TestScorerPerf extends LuceneTestCase {int bigIter=10;public void testConjunctionPerf() throws Exception {+ r = newRandom();createDummySearcher();validate=false;sets=randBitSets(32,1000000);@@ -331,6 +333,7 @@ public class TestScorerPerf extends LuceneTestCase {}public void testNestedConjunctionPerf() throws Exception {+ r = newRandom();createDummySearcher();validate=false;sets=randBitSets(32,1000000);@@ -345,6 +348,7 @@ public class TestScorerPerf extends LuceneTestCase {public void testConjunctionTerms() throws Exception {+ r = newRandom();validate=false;RAMDirectory dir = new RAMDirectory();System.out.println("Creating index");@@ -361,6 +365,7 @@ public class TestScorerPerf extends LuceneTestCase {}public void testNestedConjunctionTerms() throws Exception {+ r = newRandom();validate=false;RAMDirectory dir = new RAMDirectory();System.out.println("Creating index");@@ -378,6 +383,7 @@ public class TestScorerPerf extends LuceneTestCase {public void testSloppyPhrasePerf() throws Exception {+ r = newRandom();validate=false;RAMDirectory dir = new RAMDirectory();System.out.println("Creating index");diff --git a/src/test/org/apache/lucene/search/TestSort.java b/src/test/org/apache/lucene/search/TestSort.javaindex 954da0ca2e2..1c1b2273379 100644--- a/src/test/org/apache/lucene/search/TestSort.java+++ b/src/test/org/apache/lucene/search/TestSort.java@@ -180,7 +180,7 @@ implements Serializable {return new IndexSearcher (indexStore);}- public static String getRandomNumberString(int num, int low, int high) {+ public String getRandomNumberString(int num, int low, int high) {StringBuilder sb = new StringBuilder();for (int i = 0; i < num; i++) {sb.append(getRandomNumber(low, high));@@ -188,11 +188,11 @@ implements Serializable {return sb.toString();}- public static String getRandomCharString(int num) {+ public String getRandomCharString(int num) {return getRandomCharString(num, 48, 122);}- public static String getRandomCharString(int num, int start, int end) {+ public String getRandomCharString(int num, int start, int end) {StringBuilder sb = new StringBuilder();for (int i = 0; i < num; i++) {sb.append(new Character((char) getRandomNumber(start, end)));@@ -200,9 +200,9 @@ implements Serializable {return sb.toString();}- static Random r = new Random();+ Random r;- public static int getRandomNumber(final int low, final int high) {+ public int getRandomNumber(final int low, final int high) {int randInt = (Math.abs(r.nextInt()) % (high - low)) + low;@@ -284,6 +284,7 @@ implements Serializable {* Test String sorting: small queue to many matches, multi field sort, reverse sort*/public void testStringSort() throws IOException, ParseException {+ r = newRandom();ScoreDoc[] result = null;IndexSearcher searcher = getFullStrings();sort.setSort(new SortField[] {diff --git a/src/test/org/apache/lucene/search/TestStressSort.java b/src/test/org/apache/lucene/search/TestStressSort.javaindex 671870ab799..070425fb114 100644--- a/src/test/org/apache/lucene/search/TestStressSort.java+++ b/src/test/org/apache/lucene/search/TestStressSort.java@@ -35,7 +35,7 @@ public class TestStressSort extends LuceneTestCase {// NOTE: put seed in here to make failures// deterministic, but do not commit with a seed (to// better test):- private final Random r = new Random();+ private Random r;private Directory dir, dir2, dir3;private IndexSearcher searcherMultiSegment;private IndexSearcher searcherFewSegment;@@ -67,7 +67,6 @@ public class TestStressSort extends LuceneTestCase {// NOTE: put seed in here to make failures// deterministic, but do not commit with a seed (to// better test):- final Random r = new Random();dir = new MockRAMDirectory();IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);writer.setMaxBufferedDocs(17);@@ -180,6 +179,7 @@ public class TestStressSort extends LuceneTestCase {}public void testSort() throws Throwable {+ r = newRandom();// reverse & not// all typesdiff --git a/src/test/org/apache/lucene/search/TestThreadSafe.java b/src/test/org/apache/lucene/search/TestThreadSafe.javaindex 0c465d34230..77458fb0aca 100755--- a/src/test/org/apache/lucene/search/TestThreadSafe.java+++ b/src/test/org/apache/lucene/search/TestThreadSafe.java@@ -34,7 +34,7 @@ import java.io.IOException;* @version $Id$*/public class TestThreadSafe extends LuceneTestCase {- Random r = new Random();+ Random r;Directory dir1;Directory dir2;@@ -48,7 +48,7 @@ public class TestThreadSafe extends LuceneTestCase {final int iter;final Random rand;// pass in random in case we want to make things reproducable- public Thr(int iter, Random rand, int level) {+ public Thr(int iter, Random rand) {this.iter = iter;this.rand = rand;}@@ -132,7 +132,7 @@ public class TestThreadSafe extends LuceneTestCase {void doTest(int iter, int nThreads) throws Exception {Thr[] tarr = new Thr[nThreads];for (int i=0; i<nThreads; i++) {- tarr[i] = new Thr(iter, new Random(), 1);+ tarr[i] = new Thr(iter, new Random(r.nextLong()));tarr[i].start();}for (int i=0; i<nThreads; i++) {@@ -144,6 +144,7 @@ public class TestThreadSafe extends LuceneTestCase {}public void testLazyLoadThreadSafety() throws Exception{+ r = newRandom();dir1 = new RAMDirectory();// test w/ field sizes bigger than the buffer of an index inputbuildDir(dir1, 15, 5, 2000);diff --git a/src/test/org/apache/lucene/store/TestBufferedIndexInput.java b/src/test/org/apache/lucene/store/TestBufferedIndexInput.javaindex df4a1791ecd..3148e3483da 100755--- a/src/test/org/apache/lucene/store/TestBufferedIndexInput.java+++ b/src/test/org/apache/lucene/store/TestBufferedIndexInput.java@@ -157,7 +157,7 @@ public class TestBufferedIndexInput extends LuceneTestCase {public void testSetBufferSize() throws IOException {File indexDir = new File(System.getProperty("tempDir"), "testSetBufferSize");- MockFSDirectory dir = new MockFSDirectory(indexDir);+ MockFSDirectory dir = new MockFSDirectory(indexDir, newRandom());try {IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);writer.setUseCompoundFile(false);@@ -205,11 +205,12 @@ public class TestBufferedIndexInput extends LuceneTestCase {List allIndexInputs = new ArrayList();- Random rand = new Random(788);+ Random rand;private Directory dir;- public MockFSDirectory(File path) throws IOException {+ public MockFSDirectory(File path, Random rand) throws IOException {+ this.rand = rand;lockFactory = new NoLockFactory();dir = FSDirectory.getDirectory(path);}diff --git a/src/test/org/apache/lucene/store/TestWindowsMMap.java b/src/test/org/apache/lucene/store/TestWindowsMMap.javaindex 0eede677dfd..d6c649b449c 100644--- a/src/test/org/apache/lucene/store/TestWindowsMMap.java+++ b/src/test/org/apache/lucene/store/TestWindowsMMap.java@@ -28,7 +28,6 @@ import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.search.IndexSearcher;-import org.apache.lucene.store.FSDirectory;public class TestWindowsMMap extends LuceneTestCase {@@ -37,7 +36,7 @@ public class TestWindowsMMap extends LuceneTestCase {public void setUp() throws Exception {super.setUp();- random = new Random();+ random = newRandom();System.setProperty("org.apache.lucene.FSDirectory.class", "org.apache.lucene.store.MMapDirectory");}diff --git a/src/test/org/apache/lucene/util/LuceneTestCase.java b/src/test/org/apache/lucene/util/LuceneTestCase.javaindex 2d3c46734d6..d734c11a47f 100644--- a/src/test/org/apache/lucene/util/LuceneTestCase.java+++ b/src/test/org/apache/lucene/util/LuceneTestCase.java@@ -20,6 +20,7 @@ package org.apache.lucene.util;import org.apache.lucene.analysis.TokenStream;import org.apache.lucene.index.ConcurrentMergeScheduler;import junit.framework.TestCase;+import java.util.Random;/** Base class for all Lucene unit tests. Currently the* only added functionality over JUnit's TestCase is@@ -54,4 +55,48 @@ public abstract class LuceneTestCase extends TestCase {fail("ConcurrentMergeScheduler hit unhandled exceptions");}}++ /**+ * Returns a {@link Random} instance for generating random numbers during the test.+ * The random seed is logged during test execution and printed to System.out on any failure+ * for reproducing the test using {@link #newRandom(long)} with the recorded seed+ *.+ */+ public Random newRandom() {+ if (seed != null) {+ throw new IllegalStateException("please call LuceneTestCase.newRandom only once per test");+ }+ return newRandom(seedRnd.nextLong());+ }++ /**+ * Returns a {@link Random} instance for generating random numbers during the test.+ * If an error occurs in the test that is not reproducible, you can use this method to+ * initialize the number generator with the seed that was printed out during the failing test.+ */+ public Random newRandom(long seed) {+ if (this.seed != null) {+ throw new IllegalStateException("please call LuceneTestCase.newRandom only once per test");+ }+ this.seed = new Long(seed);+ return new Random(seed);+ }++ protected void runTest() throws Throwable {+ try {+ seed = null;+ super.runTest();+ } catch (Throwable e) {+ if (seed != null) {+ System.out.println("NOTE: random seed of testcase '" + getName() + "' was: " + seed);+ }+ throw e;+ }+ }++ // recorded seed+ protected Long seed = null;++ // static members+ private static final Random seedRnd = new Random();}diff --git a/src/test/org/apache/lucene/util/TestOpenBitSet.java b/src/test/org/apache/lucene/util/TestOpenBitSet.javaindex ecbfb93fc0b..2ac6dd74609 100644--- a/src/test/org/apache/lucene/util/TestOpenBitSet.java+++ b/src/test/org/apache/lucene/util/TestOpenBitSet.java@@ -17,16 +17,14 @@package org.apache.lucene.util;-import junit.framework.TestCase;-import java.util.Random;import java.util.BitSet;/*** @version $Id$*/-public class TestOpenBitSet extends TestCase {- static Random rand = new Random();+public class TestOpenBitSet extends LuceneTestCase {+ Random rand;void doGet(BitSet a, OpenBitSet b) {int max = a.size();@@ -184,17 +182,20 @@ public class TestOpenBitSet extends TestCase {// large enough to flush obvious bugs, small enough to run in <.5 sec as part of a// larger testsuite.public void testSmall() {+ rand = newRandom();doRandomSets(1200,1000, 1);doRandomSets(1200,1000, 2);}public void testBig() {// uncomment to run a bigger test (~2 minutes).+ // rand = newRandom();// doRandomSets(2000,200000, 1);// doRandomSets(2000,200000, 2);}public void testEquals() {+ rand = newRandom();OpenBitSet b1 = new OpenBitSet(1111);OpenBitSet b2 = new OpenBitSet(2222);assertTrue(b1.equals(b2));@@ -218,6 +219,7 @@ public class TestOpenBitSet extends TestCase {public void testBitUtils(){+ rand = newRandom();long num = 100000;assertEquals( 5, BitUtil.ntz(num) );assertEquals( 5, BitUtil.ntz2(num) );diff --git a/src/test/org/apache/lucene/util/TestPriorityQueue.java b/src/test/org/apache/lucene/util/TestPriorityQueue.javaindex 28c3f20179e..4e9afe9a510 100644--- a/src/test/org/apache/lucene/util/TestPriorityQueue.java+++ b/src/test/org/apache/lucene/util/TestPriorityQueue.java@@ -45,13 +45,12 @@ public class TestPriorityQueuepublic void testPQ()throws Exception{- testPQ(10000);+ testPQ(10000, newRandom());}- public static void testPQ(int count)+ public static void testPQ(int count, Random gen){PriorityQueue pq = new IntegerQueue(count);- Random gen = new Random();int sum = 0, sum2 = 0;for (int i = 0; i < count; i++)diff --git a/src/test/org/apache/lucene/util/TestSmallFloat.java b/src/test/org/apache/lucene/util/TestSmallFloat.javaindex 5a4608e5704..6434f5658ec 100644--- a/src/test/org/apache/lucene/util/TestSmallFloat.java+++ b/src/test/org/apache/lucene/util/TestSmallFloat.java@@ -16,7 +16,6 @@ package org.apache.lucene.util;* limitations under the License.*/-import org.apache.lucene.util.LuceneTestCase;import java.util.Random;/**@@ -74,7 +73,7 @@ public class TestSmallFloat extends LuceneTestCase {}public void testFloatToByte() {- Random rand = new Random(0);+ Random rand = newRandom();// up iterations for more exhaustive test after changing somethingfor (int i=0; i<100000; i++) {float f = Float.intBitsToFloat(rand.nextInt());