org.apache.lucene.util
Class LuceneTestCase

java.lang.Object
  extended by org.junit.Assert
      extended by org.apache.lucene.util.LuceneTestCase
Direct Known Subclasses:
BaseTokenStreamTestCase, CollationTestBase, QueryParserTestBase, SearchEquivalenceTestBase, ThreadedIndexingAndSearchingTestCase

public abstract class LuceneTestCase
extends org.junit.Assert

Base class for all Lucene unit tests, Junit3 or Junit4 variant.

If you override either setUp() or tearDown() in your unit test, make sure you call super.setUp() and super.tearDown()

@After - replaces setup @Before - replaces teardown @Test - any public method with this annotation is a test case, regardless of its name

See Junit4 documentation for a complete list of features.

Import from org.junit rather than junit.framework.

You should be able to use this class anywhere you used LuceneTestCase if you annotate your derived class correctly with the annotations above

See Also:
assertSaneFieldCaches(String)

Nested Class Summary
static interface LuceneTestCase.Nightly
          Annotation for tests that should only be run during nightly builds.
 
Field Summary
static org.junit.rules.TestRule classRules
          This controls how suite-level rules are nested.
protected static SmartRandom random
           
static int RANDOM_MULTIPLIER
          A random multiplier which you should use when writing random tests: multiply it by the number of iterations
 org.junit.rules.TestRule ruleChain
          This controls how individual test rules are nested.
protected static Map<MockDirectoryWrapper,StackTraceElement[]> stores
           
static File TEMP_DIR
          Create indexes in this directory, optimally use a subdir, named after the test
static String TEST_CLEAN_THREADS
          whether or not to clean threads between test invocations: "false", "perMethod", "perClass"
static String TEST_DIRECTORY
          Gets the directory to run tests with
static int TEST_ITER
          Get the number of times to run tests
static int TEST_ITER_MIN
          Get the minimum number of times to run tests until a failure happens
static String TEST_LINE_DOCS_FILE
          the line file used by LineFileDocs
static String TEST_LOCALE
          Gets the locale to run tests with
static boolean TEST_NIGHTLY
          whether or not nightly tests should run
static String TEST_SEED
          Get the random seed for tests
static String TEST_TIMEZONE
          Gets the timezone to run tests with
static Version TEST_VERSION_CURRENT
          Use this constant when creating Analyzers and any other version-dependent stuff.
protected static boolean testsFailed
           
static boolean VERBOSE
          true iff tests are run in verbose mode.
 
Constructor Summary
LuceneTestCase()
           
 
Method Summary
static void afterClassLuceneTestCaseJ4()
           
 void alwaysIgnoredTestMethod()
           
static void assertEquals(double expected, double actual)
          Deprecated. 
static void assertEquals(float expected, float actual)
          Deprecated. 
static void assertEquals(String message, double expected, double actual)
          Deprecated. 
static void assertEquals(String message, float expected, float actual)
          Deprecated. 
protected  void assertSaneFieldCaches(String msg)
          Asserts that FieldCacheSanityChecker does not detect any problems with FieldCache.DEFAULT.
static
<T> Set<T>
asSet(T... args)
           
static void assumeFalse(String msg, boolean b)
           
static void assumeNoException(String msg, Exception e)
           
static void assumeTrue(String msg, boolean b)
           
static int atLeast(int i)
           
static int atLeast(Random random, int i)
          Returns a number of at least i
static void beforeClassLuceneTestCaseJ4()
           
static void dumpArray(String label, Object[] objs, PrintStream stream)
          Convenience method for logging an array.
static void dumpIterator(String label, Iterator<?> iter, PrintStream stream)
          Convenience method for logging an iterator.
protected  File getDataFile(String name)
          Gets a resource from the classpath as File.
 String getName()
           
static Class<?> getTestClass()
          Return the current class being tested.
protected  String getTestLabel()
           
protected  boolean isTestThread()
          Returns true if and only if the calling thread is the primary thread executing the test case.
static Locale localeForName(String localeName)
          return a Locale object equivalent to its programmatic name
static MockDirectoryWrapper newDirectory()
          Returns a new Directory instance.
static MockDirectoryWrapper newDirectory(Directory d)
          Returns a new Directory instance, with contents copied from the provided directory.
static MockDirectoryWrapper newDirectory(Random r)
          Returns a new Directory instance, using the specified random.
static MockDirectoryWrapper newDirectory(Random r, Directory d)
          Returns a new Directory instance, using the specified random with contents copied from the provided directory.
static Field newField(Random random, String name, String value, Field.Index index)
          Returns a new field instance, using the specified random.
static Field newField(Random random, String name, String value, Field.Store store, Field.Index index)
          Returns a new field instance, using the specified random.
static Field newField(Random random, String name, String value, Field.Store store, Field.Index index, Field.TermVector tv)
          Returns a new field instance, using the specified random.
static Field newField(String name, String value, Field.Index index)
          Returns a new field instance.
static Field newField(String name, String value, Field.Store store, Field.Index index)
          Returns a new field instance.
static Field newField(String name, String value, Field.Store store, Field.Index index, Field.TermVector tv)
          Returns a new Field instance.
static MockDirectoryWrapper newFSDirectory(File f)
          Returns a new FSDirectory instance over the given file, which must be a folder.
static MockDirectoryWrapper newFSDirectory(File f, LockFactory lf)
          Returns a new FSDirectory instance over the given file, which must be a folder.
static IndexWriterConfig newIndexWriterConfig(Random r, Version v, Analyzer a)
          create a new index writer config with random defaults using the specified random
static IndexWriterConfig newIndexWriterConfig(Version v, Analyzer a)
          create a new index writer config with random defaults
static LogMergePolicy newLogMergePolicy()
           
static LogMergePolicy newLogMergePolicy(boolean useCFS)
           
static LogMergePolicy newLogMergePolicy(boolean useCFS, int mergeFactor)
           
static LogMergePolicy newLogMergePolicy(int mergeFactor)
           
static LogMergePolicy newLogMergePolicy(Random r)
           
static IndexSearcher newSearcher(IndexReader r)
          create a new searcher over the reader.
static IndexSearcher newSearcher(IndexReader r, boolean maybeWrap)
          create a new searcher over the reader.
static TieredMergePolicy newTieredMergePolicy()
           
static TieredMergePolicy newTieredMergePolicy(Random r)
           
protected  void purgeFieldCache(FieldCache fc)
          Forcible purges all cache entries from the FieldCache.
static String randomDirectory(Random random)
           
static Locale randomLocale(Random random)
          return a random Locale from the available locales on the system
static TimeZone randomTimeZone(Random random)
          return a random TimeZone from the available timezones on the system
static boolean rarely()
           
static boolean rarely(Random random)
          Returns true if something should happen rarely,
 void reportAdditionalFailureInfo()
           
static void reportPartialFailureInfo()
           
static void setMergeFactor(MergePolicy mp, int mergeFactor)
           
 void setUp()
          For subclassing only.
static void setUseCompoundFile(MergePolicy mp, boolean useCompound)
           
 void tearDown()
          For subclassing only.
static boolean usually()
           
static boolean usually(Random random)
           
 
Methods inherited from class org.junit.Assert
assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertThat, assertThat, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERBOSE

public static final boolean VERBOSE
true iff tests are run in verbose mode. Note: if it is false, tests are not expected to print any messages.


TEST_VERSION_CURRENT

public static final Version TEST_VERSION_CURRENT
Use this constant when creating Analyzers and any other version-dependent stuff.

NOTE: Change this when development starts for new Lucene version:


TEMP_DIR

public static final File TEMP_DIR
Create indexes in this directory, optimally use a subdir, named after the test


TEST_LOCALE

public static final String TEST_LOCALE
Gets the locale to run tests with


TEST_TIMEZONE

public static final String TEST_TIMEZONE
Gets the timezone to run tests with


TEST_DIRECTORY

public static final String TEST_DIRECTORY
Gets the directory to run tests with


TEST_ITER

public static final int TEST_ITER
Get the number of times to run tests


TEST_ITER_MIN

public static final int TEST_ITER_MIN
Get the minimum number of times to run tests until a failure happens


TEST_SEED

public static final String TEST_SEED
Get the random seed for tests


TEST_NIGHTLY

public static final boolean TEST_NIGHTLY
whether or not nightly tests should run


TEST_LINE_DOCS_FILE

public static final String TEST_LINE_DOCS_FILE
the line file used by LineFileDocs


TEST_CLEAN_THREADS

public static final String TEST_CLEAN_THREADS
whether or not to clean threads between test invocations: "false", "perMethod", "perClass"


RANDOM_MULTIPLIER

public static final int RANDOM_MULTIPLIER
A random multiplier which you should use when writing random tests: multiply it by the number of iterations


stores

protected static Map<MockDirectoryWrapper,StackTraceElement[]> stores

classRules

public static org.junit.rules.TestRule classRules
This controls how suite-level rules are nested. It is important that _all_ rules declared in LuceneTestCase are executed in proper order if they depend on each other.


ruleChain

public final org.junit.rules.TestRule ruleChain
This controls how individual test rules are nested. It is important that _all_ rules declared in LuceneTestCase are executed in proper order if they depend on each other.


testsFailed

protected static boolean testsFailed

random

protected static final SmartRandom random
Constructor Detail

LuceneTestCase

public LuceneTestCase()
Method Detail

beforeClassLuceneTestCaseJ4

public static void beforeClassLuceneTestCaseJ4()

afterClassLuceneTestCaseJ4

public static void afterClassLuceneTestCaseJ4()

purgeFieldCache

protected void purgeFieldCache(FieldCache fc)
Forcible purges all cache entries from the FieldCache.

This method will be called by tearDown to clean up FieldCache.DEFAULT. If a (poorly written) test has some expectation that the FieldCache will persist across test methods (ie: a static IndexReader) this method can be overridden to do nothing.

See Also:
FieldCache.purgeAllCaches()

getTestLabel

protected String getTestLabel()

setUseCompoundFile

public static void setUseCompoundFile(MergePolicy mp,
                                      boolean useCompound)

setMergeFactor

public static void setMergeFactor(MergePolicy mp,
                                  int mergeFactor)

isTestThread

protected boolean isTestThread()
Returns true if and only if the calling thread is the primary thread executing the test case.


setUp

public void setUp()
           throws Exception
For subclassing only. Overrides must call super.setUp().

Throws:
Exception

tearDown

public void tearDown()
              throws Exception
For subclassing only. Overrides must call super.tearDown().

Throws:
Exception

assertSaneFieldCaches

protected void assertSaneFieldCaches(String msg)
Asserts that FieldCacheSanityChecker does not detect any problems with FieldCache.DEFAULT.

If any problems are found, they are logged to System.err (allong with the msg) when the Assertion is thrown.

This method is called by tearDown after every test method, however IndexReaders scoped inside test methods may be garbage collected prior to this method being called, causing errors to be overlooked. Tests are encouraged to keep their IndexReaders scoped at the class level, or to explicitly call this method directly in the same scope as the IndexReader.

See Also:
FieldCacheSanityChecker

atLeast

public static int atLeast(Random random,
                          int i)
Returns a number of at least i

The actual number returned will be influenced by whether TEST_NIGHTLY is active and RANDOM_MULTIPLIER, but also with some random fudge.


atLeast

public static int atLeast(int i)

rarely

public static boolean rarely(Random random)
Returns true if something should happen rarely,

The actual number returned will be influenced by whether TEST_NIGHTLY is active and RANDOM_MULTIPLIER.


rarely

public static boolean rarely()

usually

public static boolean usually(Random random)

usually

public static boolean usually()

assertEquals

@Deprecated
public static void assertEquals(double expected,
                                           double actual)
Deprecated. 


assertEquals

@Deprecated
public static void assertEquals(String message,
                                           double expected,
                                           double actual)
Deprecated. 


assertEquals

@Deprecated
public static void assertEquals(float expected,
                                           float actual)
Deprecated. 


assertEquals

@Deprecated
public static void assertEquals(String message,
                                           float expected,
                                           float actual)
Deprecated. 


assumeTrue

public static void assumeTrue(String msg,
                              boolean b)

assumeFalse

public static void assumeFalse(String msg,
                               boolean b)

assumeNoException

public static void assumeNoException(String msg,
                                     Exception e)

asSet

public static <T> Set<T> asSet(T... args)

dumpIterator

public static void dumpIterator(String label,
                                Iterator<?> iter,
                                PrintStream stream)
Convenience method for logging an iterator.

Parameters:
label - String logged before/after the items in the iterator
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.
stream - Stream to log messages to.

dumpArray

public static void dumpArray(String label,
                             Object[] objs,
                             PrintStream stream)
Convenience method for logging an array. Wraps the array in an iterator and delegates

See Also:
dumpIterator(String,Iterator,PrintStream)

newIndexWriterConfig

public static IndexWriterConfig newIndexWriterConfig(Version v,
                                                     Analyzer a)
create a new index writer config with random defaults


newIndexWriterConfig

public static IndexWriterConfig newIndexWriterConfig(Random r,
                                                     Version v,
                                                     Analyzer a)
create a new index writer config with random defaults using the specified random


newLogMergePolicy

public static LogMergePolicy newLogMergePolicy()

newTieredMergePolicy

public static TieredMergePolicy newTieredMergePolicy()

newLogMergePolicy

public static LogMergePolicy newLogMergePolicy(Random r)

newTieredMergePolicy

public static TieredMergePolicy newTieredMergePolicy(Random r)

newLogMergePolicy

public static LogMergePolicy newLogMergePolicy(boolean useCFS)

newLogMergePolicy

public static LogMergePolicy newLogMergePolicy(boolean useCFS,
                                               int mergeFactor)

newLogMergePolicy

public static LogMergePolicy newLogMergePolicy(int mergeFactor)

newDirectory

public static MockDirectoryWrapper newDirectory()
                                         throws IOException
Returns a new Directory instance. Use this when the test does not care about the specific Directory implementation (most tests).

The Directory is wrapped with MockDirectoryWrapper. By default this means it will be picky, such as ensuring that you properly close it and all open files in your test. It will emulate some features of Windows, such as not allowing open files to be overwritten.

Throws:
IOException

newDirectory

public static MockDirectoryWrapper newDirectory(Random r)
                                         throws IOException
Returns a new Directory instance, using the specified random. See newDirectory() for more information.

Throws:
IOException

newDirectory

public static MockDirectoryWrapper newDirectory(Directory d)
                                         throws IOException
Returns a new Directory instance, with contents copied from the provided directory. See newDirectory() for more information.

Throws:
IOException

newFSDirectory

public static MockDirectoryWrapper newFSDirectory(File f)
                                           throws IOException
Returns a new FSDirectory instance over the given file, which must be a folder.

Throws:
IOException

newFSDirectory

public static MockDirectoryWrapper newFSDirectory(File f,
                                                  LockFactory lf)
                                           throws IOException
Returns a new FSDirectory instance over the given file, which must be a folder.

Throws:
IOException

newDirectory

public static MockDirectoryWrapper newDirectory(Random r,
                                                Directory d)
                                         throws IOException
Returns a new Directory instance, using the specified random with contents copied from the provided directory. See newDirectory() for more information.

Throws:
IOException

newField

public static Field newField(String name,
                             String value,
                             Field.Index index)
Returns a new field instance. See newField(String, String, Field.Store, Field.Index, Field.TermVector) for more information


newField

public static Field newField(String name,
                             String value,
                             Field.Store store,
                             Field.Index index)
Returns a new field instance. See newField(String, String, Field.Store, Field.Index, Field.TermVector) for more information


newField

public static Field newField(String name,
                             String value,
                             Field.Store store,
                             Field.Index index,
                             Field.TermVector tv)
Returns a new Field instance. Use this when the test does not care about some specific field settings (most tests)


newField

public static Field newField(Random random,
                             String name,
                             String value,
                             Field.Index index)
Returns a new field instance, using the specified random. See newField(String, String, Field.Store, Field.Index, Field.TermVector) for more information


newField

public static Field newField(Random random,
                             String name,
                             String value,
                             Field.Store store,
                             Field.Index index)
Returns a new field instance, using the specified random. See newField(String, String, Field.Store, Field.Index, Field.TermVector) for more information


newField

public static Field newField(Random random,
                             String name,
                             String value,
                             Field.Store store,
                             Field.Index index,
                             Field.TermVector tv)
Returns a new field instance, using the specified random. See newField(String, String, Field.Store, Field.Index, Field.TermVector) for more information


randomLocale

public static Locale randomLocale(Random random)
return a random Locale from the available locales on the system


randomTimeZone

public static TimeZone randomTimeZone(Random random)
return a random TimeZone from the available timezones on the system


localeForName

public static Locale localeForName(String localeName)
return a Locale object equivalent to its programmatic name


randomDirectory

public static String randomDirectory(Random random)

newSearcher

public static IndexSearcher newSearcher(IndexReader r)
                                 throws IOException
create a new searcher over the reader. This searcher might randomly use threads.

Throws:
IOException

newSearcher

public static IndexSearcher newSearcher(IndexReader r,
                                        boolean maybeWrap)
                                 throws IOException
create a new searcher over the reader. This searcher might randomly use threads. if maybeWrap is true, this searcher might wrap the reader with one that returns null for getSequentialSubReaders.

Throws:
IOException

getName

public String getName()

getDataFile

protected File getDataFile(String name)
                    throws IOException
Gets a resource from the classpath as File. This method should only be used, if a real file is needed. To get a stream, code should prefer Class.getResourceAsStream(java.lang.String) using this.getClass().

Throws:
IOException

reportPartialFailureInfo

public static void reportPartialFailureInfo()

reportAdditionalFailureInfo

public void reportAdditionalFailureInfo()

getTestClass

public static Class<?> getTestClass()
Return the current class being tested.


alwaysIgnoredTestMethod

public final void alwaysIgnoredTestMethod()