LUCENE-2659: LuceneTestCase ease of use improvements

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1000424 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2010-09-23 12:19:07 +00:00
parent 56ff0c11c2
commit f68b4b4adb
12 changed files with 64 additions and 119 deletions

View File

@ -36,12 +36,10 @@ public class TestHighFreqTerms extends LuceneTestCase {
private static IndexWriter writer =null;
private static Directory dir = null;
private static IndexReader reader =null;
private static Random random = null;
@BeforeClass
public static void setUpClass() throws Exception {
random = newStaticRandom(TestHighFreqTerms.class);
dir = newDirectory(random);
dir = newDirectory();
writer = new IndexWriter(dir, newIndexWriterConfig(random,
TEST_VERSION_CURRENT, new MockAnalyzer(MockTokenizer.WHITESPACE, false))
.setMaxBufferedDocs(2));

View File

@ -17,8 +17,6 @@ package org.apache.lucene.search;
* limitations under the License.
*/
import java.util.Random;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
@ -41,20 +39,19 @@ public class TestRemoteCachingWrapperFilter extends RemoteTestCase {
@BeforeClass
public static void beforeClass() throws Exception {
// construct an index
Random random = newStaticRandom(TestRemoteCachingWrapperFilter.class);
indexStore = newDirectory(random);
IndexWriter writer = new IndexWriter(indexStore, newIndexWriterConfig(random,
indexStore = newDirectory();
IndexWriter writer = new IndexWriter(indexStore, newIndexWriterConfig(
TEST_VERSION_CURRENT, new MockAnalyzer()));
Document doc = new Document();
doc.add(newField(random, "test", "test text", Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField(random, "type", "A", Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField(random, "other", "other test text", Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField("test", "test text", Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField("type", "A", Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField("other", "other test text", Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
//Need a second document to search for
doc = new Document();
doc.add(newField(random, "test", "test text", Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField(random, "type", "B", Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField(random, "other", "other test text", Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField("test", "test text", Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField("type", "B", Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField("other", "other test text", Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
writer.optimize();
writer.close();

View File

@ -29,7 +29,6 @@ import org.junit.Test;
import static org.junit.Assert.*;
import java.util.Collections;
import java.util.Random;
import java.util.Set;
import java.util.HashSet;
@ -40,13 +39,12 @@ public class TestRemoteSearchable extends RemoteTestCase {
@BeforeClass
public static void beforeClass() throws Exception {
// construct an index
Random random = newStaticRandom(TestRemoteSearchable.class);
indexStore = newDirectory(random);
IndexWriter writer = new IndexWriter(indexStore, newIndexWriterConfig(random,
indexStore = newDirectory();
IndexWriter writer = new IndexWriter(indexStore, newIndexWriterConfig(
TEST_VERSION_CURRENT, new MockAnalyzer()));
Document doc = new Document();
doc.add(newField(random, "test", "test text", Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField(random, "other", "other test text", Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField("test", "test text", Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField("other", "other test text", Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
writer.optimize();
writer.close();

View File

@ -83,9 +83,8 @@ public class TestRemoteSort extends RemoteTestCase {
// create an index of all the documents, or just the x, or just the y documents
@BeforeClass
public static void beforeClass() throws Exception {
Random random = newStaticRandom(TestRemoteSort.class);
indexStore = newDirectory(random);
IndexWriter writer = new IndexWriter(indexStore, newIndexWriterConfig(random,
indexStore = newDirectory();
IndexWriter writer = new IndexWriter(indexStore, newIndexWriterConfig(
TEST_VERSION_CURRENT, new MockAnalyzer())
.setMaxBufferedDocs(2));
((LogMergePolicy) writer.getConfig().getMergePolicy()).setMergeFactor(1000);

View File

@ -94,7 +94,6 @@ public class BaseTestRangeFilter extends LuceneTestCase {
@BeforeClass
public static void beforeClassBaseTestRangeFilter() throws Exception {
Random random = newStaticRandom(BaseTestRangeFilter.class);
signedIndexDir = new TestIndex(random, Integer.MAX_VALUE, Integer.MIN_VALUE, true);
unsignedIndexDir = new TestIndex(random, Integer.MAX_VALUE, 0, false);
signedIndexReader = build(random, signedIndexDir);

View File

@ -55,12 +55,11 @@ public class TestBoolean2 extends LuceneTestCase {
@BeforeClass
public static void beforeClass() throws Exception {
Random random = newStaticRandom(TestBoolean2.class);
directory = newDirectory(random);
directory = newDirectory();
RandomIndexWriter writer= new RandomIndexWriter(random, directory);
for (int i = 0; i < docFields.length; i++) {
Document doc = new Document();
doc.add(newField(random, field, docFields[i], Field.Store.NO, Field.Index.ANALYZED));
doc.add(newField(field, docFields[i], Field.Store.NO, Field.Index.ANALYZED));
writer.addDocument(doc);
}
writer.close();
@ -82,15 +81,15 @@ public class TestBoolean2 extends LuceneTestCase {
} while(docCount < 3000);
RandomIndexWriter w = new RandomIndexWriter(random, dir2,
newIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer())
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer())
.setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000)));
Document doc = new Document();
doc.add(newField(random, "field2", "xxx", Field.Store.NO, Field.Index.ANALYZED));
doc.add(newField("field2", "xxx", Field.Store.NO, Field.Index.ANALYZED));
for(int i=0;i<NUM_EXTRA_DOCS/2;i++) {
w.addDocument(doc);
}
doc = new Document();
doc.add(newField(random, "field2", "big bad bug", Field.Store.NO, Field.Index.ANALYZED));
doc.add(newField("field2", "big bad bug", Field.Store.NO, Field.Index.ANALYZED));
for(int i=0;i<NUM_EXTRA_DOCS/2;i++) {
w.addDocument(doc);
}

View File

@ -34,7 +34,6 @@ import static org.junit.Assert.*;
import java.io.IOException;
import java.text.Collator;
import java.util.Locale;
import java.util.Random;
import junit.framework.Assert;
@ -60,20 +59,18 @@ public class TestMultiTermConstantScore extends BaseTestRangeFilter {
"B 2 4 5 6", "Y 3 5 6", null, "C 3 6",
"X 4 5 6" };
Random random = newStaticRandom(TestMultiTermConstantScore.class);
small = newDirectory(random);
small = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random, small, new MockAnalyzer(MockTokenizer.WHITESPACE, false));
for (int i = 0; i < data.length; i++) {
Document doc = new Document();
doc.add(newField(random, "id", String.valueOf(i), Field.Store.YES,
doc.add(newField("id", String.valueOf(i), Field.Store.YES,
Field.Index.NOT_ANALYZED));// Field.Keyword("id",String.valueOf(i)));
doc
.add(newField(random, "all", "all", Field.Store.YES,
.add(newField("all", "all", Field.Store.YES,
Field.Index.NOT_ANALYZED));// Field.Keyword("all","all"));
if (null != data[i]) {
doc.add(newField(random, "data", data[i], Field.Store.YES,
doc.add(newField("data", data[i], Field.Store.YES,
Field.Index.ANALYZED));// Field.Text("data",data[i]));
}
writer.addDocument(doc);

View File

@ -17,8 +17,6 @@ package org.apache.lucene.search;
* limitations under the License.
*/
import java.util.Random;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
@ -49,14 +47,12 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
private static Directory directory = null;
private static IndexReader reader = null;
private static IndexSearcher searcher = null;
private static Random random = null;
@BeforeClass
public static void beforeClass() throws Exception {
random = newStaticRandom(TestNumericRangeQuery32.class);
directory = newDirectory(random);
directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random, directory,
newIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer())
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer())
.setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000)));
NumericField

View File

@ -17,8 +17,6 @@ package org.apache.lucene.search;
* limitations under the License.
*/
import java.util.Random;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
@ -48,14 +46,12 @@ public class TestNumericRangeQuery64 extends LuceneTestCase {
private static Directory directory = null;
private static IndexReader reader = null;
private static IndexSearcher searcher = null;
private static Random random = null;
@BeforeClass
public static void beforeClass() throws Exception {
random = newStaticRandom(TestNumericRangeQuery64.class);
directory = newDirectory(random);
directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random, directory,
newIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer())
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer())
.setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000)));
NumericField

View File

@ -41,18 +41,17 @@ public class TestSubScorerFreqs extends LuceneTestCase {
@BeforeClass
public static void makeIndex() throws Exception {
dir = new RAMDirectory();
Random random = newStaticRandom(TestSubScorerFreqs.class);
RandomIndexWriter w = new RandomIndexWriter(
random, dir);
// make sure we have more than one segment occationally
for (int i = 0; i < 31 * RANDOM_MULTIPLIER; i++) {
Document doc = new Document();
doc.add(newField(random, "f", "a b c d b c d c d d", Field.Store.NO,
doc.add(newField("f", "a b c d b c d c d d", Field.Store.NO,
Field.Index.ANALYZED));
w.addDocument(doc);
doc = new Document();
doc.add(newField(random, "f", "a b c d", Field.Store.NO, Field.Index.ANALYZED));
doc.add(newField("f", "a b c d", Field.Store.NO, Field.Index.ANALYZED));
w.addDocument(doc);
}

View File

@ -18,7 +18,6 @@ package org.apache.lucene.search.spans;
*/
import java.io.IOException;
import java.util.Random;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.analysis.MockTokenizer;
@ -65,15 +64,14 @@ public class TestBasics extends LuceneTestCase {
@BeforeClass
public static void beforeClass() throws Exception {
Random random = newStaticRandom(TestBasics.class);
directory = newDirectory(random);
directory = newDirectory();
RandomIndexWriter writer = new RandomIndexWriter(random, directory,
newIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer(MockTokenizer.SIMPLE, true))
newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(MockTokenizer.SIMPLE, true))
.setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000)));
//writer.infoStream = System.out;
for (int i = 0; i < 1000; i++) {
Document doc = new Document();
doc.add(newField(random, "field", English.intToEnglish(i), Field.Store.YES, Field.Index.ANALYZED));
doc.add(newField("field", English.intToEnglish(i), Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
}
reader = writer.getReader();

View File

@ -77,6 +77,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.TimeZone;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -217,7 +218,7 @@ public abstract class LuceneTestCase extends Assert {
final boolean codecHasParam;
int codecParam = 0;
if (codec.equals("random")) {
codec = pickRandomCodec(seedRnd);
codec = pickRandomCodec(random);
codecHasParam = false;
} else {
Matcher m = codecWithParam.matcher(codec);
@ -241,10 +242,10 @@ public abstract class LuceneTestCase extends Assert {
}
swapCodec(new MockSepCodec());
swapCodec(new PulsingCodec(codecHasParam && "Pulsing".equals(codec) ? codecParam : _TestUtil.nextInt(seedRnd, 1, 20)));
swapCodec(new MockFixedIntBlockCodec(codecHasParam && "MockFixedIntBlock".equals(codec) ? codecParam : _TestUtil.nextInt(seedRnd, 1, 2000)));
swapCodec(new PulsingCodec(codecHasParam && "Pulsing".equals(codec) ? codecParam : _TestUtil.nextInt(random, 1, 20)));
swapCodec(new MockFixedIntBlockCodec(codecHasParam && "MockFixedIntBlock".equals(codec) ? codecParam : _TestUtil.nextInt(random, 1, 2000)));
// baseBlockSize cannot be over 127:
swapCodec(new MockVariableIntBlockCodec(codecHasParam && "MockVariableIntBlock".equals(codec) ? codecParam : _TestUtil.nextInt(seedRnd, 1, 127)));
swapCodec(new MockVariableIntBlockCodec(codecHasParam && "MockVariableIntBlock".equals(codec) ? codecParam : _TestUtil.nextInt(random, 1, 127)));
return cp.lookup(codec);
}
@ -279,13 +280,15 @@ public abstract class LuceneTestCase extends Assert {
@BeforeClass
public static void beforeClassLuceneTestCaseJ4() {
staticSeed = "random".equals(TEST_SEED) ? seedRand.nextLong() : UUID.fromString(TEST_SEED).getMostSignificantBits();
random.setSeed(staticSeed);
stores = Collections.synchronizedMap(new IdentityHashMap<MockDirectoryWrapper,StackTraceElement[]>());
codec = installTestCodecs();
savedLocale = Locale.getDefault();
locale = TEST_LOCALE.equals("random") ? randomLocale(seedRnd) : localeForName(TEST_LOCALE);
locale = TEST_LOCALE.equals("random") ? randomLocale(random) : localeForName(TEST_LOCALE);
Locale.setDefault(locale);
savedTimeZone = TimeZone.getDefault();
timeZone = TEST_TIMEZONE.equals("random") ? randomTimeZone(seedRnd) : TimeZone.getTimeZone(TEST_TIMEZONE);
timeZone = TEST_TIMEZONE.equals("random") ? randomTimeZone(random) : TimeZone.getTimeZone(TEST_TIMEZONE);
TimeZone.setDefault(timeZone);
testsFailed = false;
}
@ -307,6 +310,11 @@ public abstract class LuceneTestCase extends Assert {
}
}
stores = null;
// if tests failed, report some information back
if (testsFailed)
System.out.println("NOTE: test params are: codec=" + codec +
", locale=" + locale +
", timezone=" + (timeZone == null ? "(null)" : timeZone.getID()));
}
private static boolean testsFailed; /* true if any tests failed */
@ -335,10 +343,10 @@ public abstract class LuceneTestCase extends Assert {
@Before
public void setUp() throws Exception {
seed = "random".equals(TEST_SEED) ? seedRand.nextLong() : UUID.fromString(TEST_SEED).getLeastSignificantBits();
random.setSeed(seed);
Assert.assertFalse("ensure your tearDown() calls super.tearDown()!!!", setup);
setup = true;
seed = Long.valueOf(TEST_SEED.equals("random") ? seedRnd.nextLong() : Long.parseLong(TEST_SEED));
random = new Random(seed);
savedUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
@ -350,7 +358,6 @@ public abstract class LuceneTestCase extends Assert {
ConcurrentMergeScheduler.setTestMode();
savedBoolMaxClauseCount = BooleanQuery.getMaxClauseCount();
seed = null;
}
@ -512,36 +519,8 @@ public abstract class LuceneTestCase extends Assert {
dumpIterator(label, iter, stream);
}
private static final Map<Class<? extends LuceneTestCase>,Long> staticSeeds =
Collections.synchronizedMap(new WeakHashMap<Class<? extends LuceneTestCase>,Long>());
/**
* Returns a {@link Random} instance for generating random numbers from a beforeclass
* annotated method.
* The random seed is logged during test execution and printed to System.out on any failure
* for reproducing the test using {@link #newStaticRandom(Class, long)} with the recorded seed
* .
*/
public static Random newStaticRandom(Class<? extends LuceneTestCase> clazz) {
Long seed = seedRnd.nextLong();
staticSeeds.put(clazz, seed);
return new Random(seed);
}
/**
* Returns a {@link Random} instance for generating random numbers from a beforeclass
* annotated method.
* 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 static Random newStaticRandom(Class<? extends LuceneTestCase> clazz, long seed) {
staticSeeds.put(clazz, Long.valueOf(seed));
System.out.println("WARNING: random static seed of testclass '" + clazz + "' is fixed to: " + seed);
return new Random(seed);
}
/** create a new index writer config with random defaults */
public IndexWriterConfig newIndexWriterConfig(Version v, Analyzer a) {
public static IndexWriterConfig newIndexWriterConfig(Version v, Analyzer a) {
return newIndexWriterConfig(random, v, a);
}
@ -586,7 +565,7 @@ public abstract class LuceneTestCase extends Assert {
* some features of Windows, such as not allowing open files to be
* overwritten.
*/
public MockDirectoryWrapper newDirectory() throws IOException {
public static MockDirectoryWrapper newDirectory() throws IOException {
return newDirectory(random);
}
@ -603,11 +582,11 @@ public abstract class LuceneTestCase extends Assert {
* provided directory. See {@link #newDirectory()} for more
* information.
*/
public MockDirectoryWrapper newDirectory(Directory d) throws IOException {
public static MockDirectoryWrapper newDirectory(Directory d) throws IOException {
return newDirectory(random, d);
}
private static MockDirectoryWrapper newDirectory(Random r, Directory d) throws IOException {
public static MockDirectoryWrapper newDirectory(Random r, Directory d) throws IOException {
StackTraceElement[] stack = new Exception().getStackTrace();
Directory impl = newDirectoryImpl(r, TEST_DIRECTORY);
for (String file : d.listAll()) {
@ -618,15 +597,15 @@ public abstract class LuceneTestCase extends Assert {
return dir;
}
public Field newField(String name, String value, Index index) {
public static Field newField(String name, String value, Index index) {
return newField(random, name, value, index);
}
public Field newField(String name, String value, Store store, Index index) {
public static Field newField(String name, String value, Store store, Index index) {
return newField(random, name, value, store, index);
}
public Field newField(String name, String value, Store store, Index index, TermVector tv) {
public static Field newField(String name, String value, Store store, Index index, TermVector tv) {
return newField(random, name, value, store, index, tv);
}
@ -751,27 +730,17 @@ public abstract class LuceneTestCase extends Assert {
// We get here from InterceptTestCaseEvents on the 'failed' event....
public void reportAdditionalFailureInfo() {
Long staticSeed = staticSeeds.get(getClass());
if (staticSeed != null) {
System.out.println("NOTE: random static seed of testclass '" + getName() + "' was: " + staticSeed);
}
System.out.println("NOTE: random codec of testcase '" + getName() + "' was: " + codec);
if (TEST_LOCALE.equals("random"))
System.out.println("NOTE: random locale of testcase '" + getName() + "' was: " + locale);
if (TEST_TIMEZONE.equals("random")) // careful to not deliver NPE here in case they forgot super.setUp
System.out.println("NOTE: random timezone of testcase '" + getName() + "' was: " + (timeZone == null ? "(null)" : timeZone.getID()));
if (seed != null) {
System.out.println("NOTE: random seed of testcase '" + getName() + "' was: " + seed);
}
System.out.println("NOTE: reproduce with: ant test -Dtestcase=" + getClass().getSimpleName()
+ " -Dtestmethod=" + getName() + " -Dtests.seed=" + new UUID(staticSeed, seed));
}
// recorded seed
protected Long seed = null;
protected Random random = null;
// recorded seed: for beforeClass
private static long staticSeed;
// seed for individual test methods, changed in @before
private long seed;
// static members
private static final Random seedRnd = new Random();
private static final Random seedRand = new Random();
protected static final Random random = new Random();
private String name = "<unknown>";