Apache Lucene open-source search software
LUCENE-2598: fixes for tests with NIOFS/MMap, add random option (but RAMDirectory is still the default)
Robert Muirfcfa3ef727c8
diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/src/test/org/apache/lucene/index/TestIndexWriter.javaindex 0949b5b316c..5fc6a39f079 100644--- a/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java+++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java@@ -4377,10 +4377,8 @@ public class TestIndexWriter extends LuceneTestCase {@Overridepublic void run() {- Directory dir;- try {- dir = newDirectory(random);- } catch (IOException e) { throw new RuntimeException(e); }+ // LUCENE-2239: won't work with NIOFS/MMAP+ Directory dir = new MockDirectoryWrapper(new RAMDirectory());IndexWriter w = null;while(!finish) {try {diff --git a/lucene/src/test/org/apache/lucene/index/TestTermVectorsReader.java b/lucene/src/test/org/apache/lucene/index/TestTermVectorsReader.javaindex 518c57b79f4..6963360aaa3 100644--- a/lucene/src/test/org/apache/lucene/index/TestTermVectorsReader.java+++ b/lucene/src/test/org/apache/lucene/index/TestTermVectorsReader.java@@ -394,6 +394,8 @@ public class TestTermVectorsReader extends LuceneTestCase {fail();} catch (IOException e) {// expected exception+ } catch (IllegalArgumentException e) {+ // mmapdir will give us this from java.nio.Buffer.position()} finally {reader.close();}@@ -404,6 +406,8 @@ public class TestTermVectorsReader extends LuceneTestCase {fail();} catch (IOException e) {// expected exception+ } catch (IllegalArgumentException e) {+ // mmapdir will give us this from java.nio.Buffer.position()} finally {reader.close();}diff --git a/lucene/src/test/org/apache/lucene/index/TestTransactionRollback.java b/lucene/src/test/org/apache/lucene/index/TestTransactionRollback.javaindex fb3ec9031d5..410be42a2c2 100644--- a/lucene/src/test/org/apache/lucene/index/TestTransactionRollback.java+++ b/lucene/src/test/org/apache/lucene/index/TestTransactionRollback.java@@ -126,8 +126,8 @@ public class TestTransactionRollback extends LuceneTestCase {@Overrideprotected void setUp() throws Exception {super.setUp();+ random = newRandom();dir = newDirectory(random);- random = newRandom();//Build index, of records 1 to 100, committing after each batch of 10IndexDeletionPolicy sdp=new KeepAllDeletionPolicy();IndexWriter w=new IndexWriter(dir, newIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer()).setIndexDeletionPolicy(sdp));diff --git a/lucene/src/test/org/apache/lucene/util/LuceneTestCase.java b/lucene/src/test/org/apache/lucene/util/LuceneTestCase.javaindex 6a23ced275a..fb837f918a0 100644--- a/lucene/src/test/org/apache/lucene/util/LuceneTestCase.java+++ b/lucene/src/test/org/apache/lucene/util/LuceneTestCase.java@@ -335,7 +335,7 @@ public abstract class LuceneTestCase extends TestCase {public MockDirectoryWrapper newDirectory(Random r) throws IOException {StackTraceElement[] stack = new Exception().getStackTrace();- Directory impl = LuceneTestCaseJ4.newDirectoryImpl(TEST_DIRECTORY);+ Directory impl = LuceneTestCaseJ4.newDirectoryImpl(r, TEST_DIRECTORY);MockDirectoryWrapper dir = new MockDirectoryWrapper(impl);stores.put(dir, stack);return dir;@@ -343,7 +343,7 @@ public abstract class LuceneTestCase extends TestCase {public MockDirectoryWrapper newDirectory(Random r, Directory d) throws IOException {StackTraceElement[] stack = new Exception().getStackTrace();- Directory impl = LuceneTestCaseJ4.newDirectoryImpl(TEST_DIRECTORY);+ Directory impl = LuceneTestCaseJ4.newDirectoryImpl(r, TEST_DIRECTORY);for (String file : d.listAll()) {d.copy(impl, file, file);}diff --git a/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java b/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.javaindex e49530002a3..bb65ddb0913 100644--- a/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java+++ b/lucene/src/test/org/apache/lucene/util/LuceneTestCaseJ4.java@@ -27,6 +27,7 @@ import org.apache.lucene.search.BooleanQuery;import org.apache.lucene.search.FieldCache;import org.apache.lucene.search.FieldCache.CacheEntry;import org.apache.lucene.store.Directory;+import org.apache.lucene.store.MMapDirectory;import org.apache.lucene.store.MockDirectoryWrapper;import org.apache.lucene.store.RAMDirectory;import org.apache.lucene.util.FieldCacheSanityChecker.Insanity;@@ -72,6 +73,7 @@ import java.util.Collections;import java.util.regex.Pattern;import java.util.regex.Matcher;import java.lang.reflect.Constructor;+import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import static org.junit.Assert.assertEquals;@@ -588,7 +590,7 @@ public class LuceneTestCaseJ4 {public static MockDirectoryWrapper newDirectory(Random r) throws IOException {StackTraceElement[] stack = new Exception().getStackTrace();- Directory impl = newDirectoryImpl(TEST_DIRECTORY);+ Directory impl = newDirectoryImpl(r, TEST_DIRECTORY);MockDirectoryWrapper dir = new MockDirectoryWrapper(impl);stores.put(dir, stack);return dir;@@ -596,7 +598,7 @@ public class LuceneTestCaseJ4 {public static MockDirectoryWrapper newDirectory(Random r, Directory d) throws IOException {StackTraceElement[] stack = new Exception().getStackTrace();- Directory impl = newDirectoryImpl(TEST_DIRECTORY);+ Directory impl = newDirectoryImpl(r, TEST_DIRECTORY);for (String file : d.listAll()) {d.copy(impl, file, file);}@@ -628,7 +630,24 @@ public class LuceneTestCaseJ4 {}}- static Directory newDirectoryImpl(String clazzName) {+ private static String CORE_DIRECTORIES[] = {+ "RAMDirectory",+ "SimpleFSDirectory",+ "NIOFSDirectory",+ "MMapDirectory"+ };++ public static String randomDirectory(Random random) {+ if (random.nextInt(10) == 0) {+ return CORE_DIRECTORIES[random.nextInt(CORE_DIRECTORIES.length)];+ } else {+ return "RAMDirectory";+ }+ }++ static Directory newDirectoryImpl(Random random, String clazzName) {+ if (clazzName.equals("random"))+ clazzName = randomDirectory(random);if (clazzName.indexOf(".") == -1) // if not fully qualified, assume .storeclazzName = "org.apache.lucene.store." + clazzName;try {@@ -642,7 +661,11 @@ public class LuceneTestCaseJ4 {tmpFile.mkdir();try {Constructor<? extends Directory> ctor = clazz.getConstructor(File.class);- return ctor.newInstance(tmpFile);+ Directory d = ctor.newInstance(tmpFile);+ // try not to enable this hack unless we must.+ if (d instanceof MMapDirectory && Constants.WINDOWS && MMapDirectory.UNMAP_SUPPORTED)+ ((MMapDirectory)d).setUseUnmap(true);+ return d;} catch (Exception e2) {// try .open(File)Method method = clazz.getMethod("open", new Class[] { File.class });