Apache Lucene open-source search software
LUCENE-2928: Improve LuceneTestCase javadocs
Robert Muir81f2f88ecdeb
diff --git a/lucene/src/test-framework/org/apache/lucene/analysis/MockAnalyzer.java b/lucene/src/test-framework/org/apache/lucene/analysis/MockAnalyzer.javaindex d23b093031f..a99a3435b0e 100644--- a/lucene/src/test-framework/org/apache/lucene/analysis/MockAnalyzer.java+++ b/lucene/src/test-framework/org/apache/lucene/analysis/MockAnalyzer.java@@ -36,6 +36,10 @@ public final class MockAnalyzer extends Analyzer {private final boolean payload;private int positionIncrementGap;+ /**+ * Calls {@link #MockAnalyzer(CharacterRunAutomaton, boolean, CharacterRunAutomaton, boolean, boolean)+ * MockAnalyzer(runAutomaton, lowerCase, filter, enablePositionIncrements, true}).+ */public MockAnalyzer(CharacterRunAutomaton runAutomaton, boolean lowerCase, CharacterRunAutomaton filter, boolean enablePositionIncrements) {this(runAutomaton, lowerCase, filter, enablePositionIncrements, true);}@@ -47,7 +51,7 @@ public final class MockAnalyzer extends Analyzer {* @param lowerCase true if the tokenizer should lowercase terms* @param filter DFA describing how terms should be filtered (set of stopwords, etc)* @param enablePositionIncrements true if position increments should reflect filtered terms.- * @param payload if payloads should be added+ * @param payload if payloads should be added containing the positions (for testing)*/public MockAnalyzer(CharacterRunAutomaton runAutomaton, boolean lowerCase, CharacterRunAutomaton filter, boolean enablePositionIncrements, boolean payload) {this.runAutomaton = runAutomaton;@@ -58,21 +62,26 @@ public final class MockAnalyzer extends Analyzer {}/**- * Creates a new MockAnalyzer, with no filtering.- *- * @param runAutomaton DFA describing how tokenization should happen (e.g. [a-zA-Z]+)- * @param lowerCase true if the tokenizer should lowercase terms+ * Calls {@link #MockAnalyzer(CharacterRunAutomaton, boolean, CharacterRunAutomaton, boolean, boolean)+ * MockAnalyzer(runAutomaton, lowerCase, MockTokenFilter.EMPTY_STOPSET, false, true}).*/public MockAnalyzer(CharacterRunAutomaton runAutomaton, boolean lowerCase) {this(runAutomaton, lowerCase, MockTokenFilter.EMPTY_STOPSET, false, true);}+ /**+ * Calls {@link #MockAnalyzer(CharacterRunAutomaton, boolean, CharacterRunAutomaton, boolean, boolean)+ * MockAnalyzer(runAutomaton, lowerCase, MockTokenFilter.EMPTY_STOPSET, false, payload}).+ */public MockAnalyzer(CharacterRunAutomaton runAutomaton, boolean lowerCase, boolean payload) {this(runAutomaton, lowerCase, MockTokenFilter.EMPTY_STOPSET, false, payload);}/**- * Create a Whitespace-lowercasing analyzer with no stopwords removal+ * Create a Whitespace-lowercasing analyzer with no stopwords removal.+ * <p>+ * Calls {@link #MockAnalyzer(CharacterRunAutomaton, boolean, CharacterRunAutomaton, boolean, boolean)+ * MockAnalyzer(MockTokenizer.WHITESPACE, true, MockTokenFilter.EMPTY_STOPSET, false, true}).*/public MockAnalyzer() {this(MockTokenizer.WHITESPACE, true);diff --git a/lucene/src/test-framework/org/apache/lucene/analysis/MockTokenFilter.java b/lucene/src/test-framework/org/apache/lucene/analysis/MockTokenFilter.javaindex f16165bab39..97863a40bd3 100644--- a/lucene/src/test-framework/org/apache/lucene/analysis/MockTokenFilter.java+++ b/lucene/src/test-framework/org/apache/lucene/analysis/MockTokenFilter.java@@ -60,6 +60,13 @@ public final class MockTokenFilter extends TokenFilter {private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);private final PositionIncrementAttribute posIncrAtt = addAttribute(PositionIncrementAttribute.class);+ /**+ * Create a new MockTokenFilter.+ *+ * @param input TokenStream to filter+ * @param filter DFA representing the terms that should be removed.+ * @param enablePositionIncrements true if the removal should accumulate position increments.+ */public MockTokenFilter(TokenStream input, CharacterRunAutomaton filter, boolean enablePositionIncrements) {super(input);this.filter = filter;diff --git a/lucene/src/test-framework/org/apache/lucene/analysis/MockTokenizer.java b/lucene/src/test-framework/org/apache/lucene/analysis/MockTokenizer.javaindex 017f828931f..6e4f30b3968 100644--- a/lucene/src/test-framework/org/apache/lucene/analysis/MockTokenizer.java+++ b/lucene/src/test-framework/org/apache/lucene/analysis/MockTokenizer.java@@ -20,7 +20,7 @@ package org.apache.lucene.analysis;import java.io.IOException;import java.io.Reader;-import org.apache.lucene.util.Version;+import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.automaton.CharacterRunAutomaton;import org.apache.lucene.util.automaton.RegExp;@@ -46,14 +46,14 @@ public class MockTokenizer extends CharTokenizer {private int state;public MockTokenizer(AttributeFactory factory, Reader input, CharacterRunAutomaton runAutomaton, boolean lowerCase) {- super(Version.LUCENE_CURRENT, factory, input);+ super(LuceneTestCase.TEST_VERSION_CURRENT, factory, input);this.runAutomaton = runAutomaton;this.lowerCase = lowerCase;this.state = runAutomaton.getInitialState();}public MockTokenizer(Reader input, CharacterRunAutomaton runAutomaton, boolean lowerCase) {- super(Version.LUCENE_CURRENT, input);+ super(LuceneTestCase.TEST_VERSION_CURRENT, input);this.runAutomaton = runAutomaton;this.lowerCase = lowerCase;this.state = runAutomaton.getInitialState();diff --git a/lucene/src/test-framework/org/apache/lucene/index/RandomIndexWriter.java b/lucene/src/test-framework/org/apache/lucene/index/RandomIndexWriter.javaindex d0afe3f262f..e8236993975 100644--- a/lucene/src/test-framework/org/apache/lucene/index/RandomIndexWriter.java+++ b/lucene/src/test-framework/org/apache/lucene/index/RandomIndexWriter.java@@ -91,6 +91,10 @@ public class RandomIndexWriter implements Closeable {}}+ /**+ * Adds a Document.+ * @see IndexWriter#addDocument(Document)+ */public void addDocument(Document doc) throws IOException {w.addDocument(doc);if (docCount++ == flushAt) {@@ -102,6 +106,10 @@ public class RandomIndexWriter implements Closeable {}}+ /**+ * Updates a document.+ * @see IndexWriter#updateDocument(Term, Document)+ */public void updateDocument(Term t, Document doc) throws IOException {w.updateDocument(t, doc);if (docCount++ == flushAt) {@@ -162,6 +170,10 @@ public class RandomIndexWriter implements Closeable {}}+ /**+ * Close this writer.+ * @see IndexWriter#close()+ */public void close() throws IOException {// if someone isn't using getReader() API, we want to be sure to// maybeOptimize since presumably they might open a reader on the dir.@@ -171,6 +183,13 @@ public class RandomIndexWriter implements Closeable {w.close();}+ /**+ * Forces an optimize.+ * <p>+ * NOTE: this should be avoided in tests unless absolutely necessary,+ * as it will result in less test coverage.+ * @see IndexWriter#optimize()+ */public void optimize() throws IOException {w.optimize();}diff --git a/lucene/src/test-framework/org/apache/lucene/store/MockDirectoryWrapper.java b/lucene/src/test-framework/org/apache/lucene/store/MockDirectoryWrapper.javaindex bb9552bf7d9..03c07f45db3 100644--- a/lucene/src/test-framework/org/apache/lucene/store/MockDirectoryWrapper.java+++ b/lucene/src/test-framework/org/apache/lucene/store/MockDirectoryWrapper.java@@ -38,6 +38,18 @@ import org.apache.lucene.util._TestUtil;/*** This is a Directory Wrapper that adds methods* intended to be used only by unit tests.+ * It also adds a number of features useful for testing:+ * <ul>+ * <li> Instances created by {@link LuceneTestCase#newDirectory()} are tracked+ * to ensure they are closed by the test.+ * <li> When a MockDirectoryWrapper is closed, it will throw an exception if+ * it has any open files against it (with a stacktrace indicating where+ * they were opened from).+ * <li> When a MockDirectoryWrapper is closed, it runs CheckIndex to test if+ * the index was corrupted.+ * <li> MockDirectoryWrapper simulates some "features" of Windows, such as+ * refusing to write/delete to open files.+ * </ul>*/public class MockDirectoryWrapper extends Directory {diff --git a/lucene/src/test-framework/org/apache/lucene/util/LineFileDocs.java b/lucene/src/test-framework/org/apache/lucene/util/LineFileDocs.javaindex 56cb3e089d3..76577166623 100644--- a/lucene/src/test-framework/org/apache/lucene/util/LineFileDocs.java+++ b/lucene/src/test-framework/org/apache/lucene/util/LineFileDocs.java@@ -32,9 +32,9 @@ import java.util.Random;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;-// Minimal port of contrib/benchmark's LneDocSource +-// DocMaker, so tests can enum docs from a line file created-// by contrib/benchmark's WriteLineDoc task+/** Minimal port of contrib/benchmark's LneDocSource ++ * DocMaker, so tests can enum docs from a line file created+ * by contrib/benchmark's WriteLineDoc task */public class LineFileDocs implements Closeable {private BufferedReader reader;@@ -42,8 +42,8 @@ public class LineFileDocs implements Closeable {private final AtomicInteger id = new AtomicInteger();private final String path;- // If forever is true, we rewind the file at EOF (repeat- // the docs over and over)+ /** If forever is true, we rewind the file at EOF (repeat+ * the docs over and over) */public LineFileDocs(Random random, String path) throws IOException {this.path = path;open(random);@@ -138,7 +138,7 @@ public class LineFileDocs implements Closeable {private final ThreadLocal<DocState> threadDocs = new ThreadLocal<DocState>();- // Document instance is re-used per-thread+ /** Note: Document instance is re-used per-thread */public Document nextDoc() throws IOException {String line;synchronized(this) {diff --git a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.javaindex 6469ca9302b..65359b34dd8 100644--- a/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java+++ b/lucene/src/test-framework/org/apache/lucene/util/LuceneTestCase.java@@ -708,7 +708,7 @@ public abstract class LuceneTestCase extends Assert {}/**- * Convinience method for logging an iterator.+ * Convenience method for logging an iterator.** @param label String logged before/after the items in the iterator* @param iter Each next() is toString()ed and logged on it's own line. If iter is null this is logged differnetly then an empty iterator.@@ -728,7 +728,7 @@ public abstract class LuceneTestCase extends Assert {}/**- * Convinience method for logging an array. Wraps the array in an iterator and delegates+ * Convenience method for logging an array. Wraps the array in an iterator and delegates** @see #dumpIterator(String,Iterator,PrintStream)*/@@ -743,6 +743,7 @@ public abstract class LuceneTestCase extends Assert {return newIndexWriterConfig(random, v, a);}+ /** create a new index writer config with random defaults using the specified random */public static IndexWriterConfig newIndexWriterConfig(Random r, Version v, Analyzer a) {IndexWriterConfig c = new IndexWriterConfig(v, a);if (r.nextBoolean()) {@@ -835,6 +836,10 @@ public abstract class LuceneTestCase extends Assert {return newDirectory(random);}+ /**+ * Returns a new Directory instance, using the specified random.+ * See {@link #newDirectory()} for more information.+ */public static MockDirectoryWrapper newDirectory(Random r) throws IOException {Directory impl = newDirectoryImpl(r, TEST_DIRECTORY);MockDirectoryWrapper dir = new MockDirectoryWrapper(r, impl);@@ -889,6 +894,11 @@ public abstract class LuceneTestCase extends Assert {}}+ /**+ * Returns a new Directory instance, using the specified random+ * with contents copied from the provided directory. See+ * {@link #newDirectory()} for more information.+ */public static MockDirectoryWrapper newDirectory(Random r, Directory d) throws IOException {Directory impl = newDirectoryImpl(r, TEST_DIRECTORY);for (String file : d.listAll()) {@@ -899,26 +909,45 @@ public abstract class LuceneTestCase extends Assert {return dir;}+ /** Returns a new field instance.+ * See {@link #newField(String, String, Store, Index, TermVector)} for more information */public static Field newField(String name, String value, Index index) {return newField(random, name, value, index);}+ /** Returns a new field instance.+ * See {@link #newField(String, String, Store, Index, TermVector)} for more information */public static Field newField(String name, String value, Store store, Index index) {return newField(random, name, value, store, index);}+ /**+ * Returns a new Field instance. Use this when the test does not+ * care about some specific field settings (most tests)+ * <ul>+ * <li>If the store value is set to Store.NO, sometimes the field will be randomly stored.+ * <li>More term vector data than you ask for might be indexed, for example if you choose YES+ * it might index term vectors with offsets too.+ * </ul>+ */public static Field newField(String name, String value, Store store, Index index, TermVector tv) {return newField(random, name, value, store, index, tv);}+ /** Returns a new field instance, using the specified random.+ * See {@link #newField(String, String, Store, Index, TermVector)} for more information */public static Field newField(Random random, String name, String value, Index index) {return newField(random, name, value, Store.NO, index);}+ /** Returns a new field instance, using the specified random.+ * See {@link #newField(String, String, Store, Index, TermVector)} for more information */public static Field newField(Random random, String name, String value, Store store, Index index) {return newField(random, name, value, store, index, TermVector.NO);}+ /** Returns a new field instance, using the specified random.+ * See {@link #newField(String, String, Store, Index, TermVector)} for more information */public static Field newField(Random random, String name, String value, Store store, Index index, TermVector tv) {if (!index.isIndexed())return new Field(name, value, store, index);@@ -1029,7 +1058,8 @@ public abstract class LuceneTestCase extends Assert {}}- /** create a new searcher over the reader */+ /** create a new searcher over the reader.+ * This searcher might randomly use threads. */public static IndexSearcher newSearcher(IndexReader r) throws IOException {if (random.nextBoolean()) {return new IndexSearcher(r);diff --git a/lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java b/lucene/src/test-framework/org/apache/lucene/util/_TestUtil.javaindex f16a971020c..b31e4bca039 100644--- a/lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java+++ b/lucene/src/test-framework/org/apache/lucene/util/_TestUtil.java@@ -52,6 +52,9 @@ public class _TestUtil {return new File(LuceneTestCase.TEMP_DIR, desc + "." + new Random().nextLong());}+ /**+ * Deletes a directory and everything underneath it.+ */public static void rmDir(File dir) throws IOException {if (dir.exists()) {for (File f : dir.listFiles()) {@@ -155,6 +158,9 @@ public class _TestUtil {return randomUnicodeString(r, 20);}+ /**+ * Returns a random string up to a certain length.+ */public static String randomUnicodeString(Random r, int maxLength) {final int end = r.nextInt(maxLength);if (end == 0) {@@ -280,8 +286,8 @@ public class _TestUtil {}}- // just tries to configure things to keep the open file- // count lowish+ /** just tries to configure things to keep the open file+ * count lowish */public static void reduceOpenFiles(IndexWriter w) {// keep number of open files lowishLogMergePolicy lmp = (LogMergePolicy) w.getConfig().getMergePolicy();diff --git a/lucene/src/test-framework/org/apache/lucene/util/automaton/AutomatonTestUtil.java b/lucene/src/test-framework/org/apache/lucene/util/automaton/AutomatonTestUtil.javaindex 6b9ef6e223e..69704f06c09 100644--- a/lucene/src/test-framework/org/apache/lucene/util/automaton/AutomatonTestUtil.java+++ b/lucene/src/test-framework/org/apache/lucene/util/automaton/AutomatonTestUtil.java@@ -80,9 +80,9 @@ public class AutomatonTestUtil {return new String(buffer, 0, end);}- // picks a random int code point, avoiding surrogates;- // throws IllegalArgumentException if this transition only- // accepts surrogates+ /** picks a random int code point, avoiding surrogates;+ * throws IllegalArgumentException if this transition only+ * accepts surrogates */private static int getRandomCodePoint(final Random r, final Transition t) {final int code;if (t.max < UnicodeUtil.UNI_SUR_HIGH_START ||