Index: solr/src/test/org/apache/solr/core/AlternateDirectoryTest.java =================================================================== --- solr/src/test/org/apache/solr/core/AlternateDirectoryTest.java (revision 1043862) +++ solr/src/test/org/apache/solr/core/AlternateDirectoryTest.java (working copy) @@ -42,14 +42,20 @@ assertQ(req("q","*:*","qt","standard")); assertTrue(TestFSDirectoryFactory.openCalled); assertTrue(TestIndexReaderFactory.newReaderCalled); + TestFSDirectoryFactory.dir.close(); } static public class TestFSDirectoryFactory extends DirectoryFactory { public static volatile boolean openCalled = false; - - public FSDirectory open(String path) throws IOException { + public static volatile Directory dir; + + public Directory open(String path) throws IOException { openCalled = true; - return FSDirectory.open(new File(path)); + // need to close the directory, or otherwise the test fails. + if (dir != null) { + dir.close(); + } + return dir = newFSDirectory(new File(path)); } } Index: solr/src/test/org/apache/solr/core/TestArbitraryIndexDir.java =================================================================== --- solr/src/test/org/apache/solr/core/TestArbitraryIndexDir.java (revision 1043862) +++ solr/src/test/org/apache/solr/core/TestArbitraryIndexDir.java (working copy) @@ -97,7 +97,7 @@ } //add a doc in the new index dir - Directory dir = FSDirectory.open(newDir); + Directory dir = newFSDirectory(newDir); IndexWriter iw = new IndexWriter( dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new StandardAnalyzer(TEST_VERSION_CURRENT)). @@ -109,7 +109,8 @@ iw.addDocument(doc); iw.commit(); iw.close(); - + dir.close(); + //commit will cause searcher to open with the new index dir assertU(commit()); //new index dir contains just 1 doc. Index: solr/src/test/org/apache/solr/spelling/IndexBasedSpellCheckerTest.java =================================================================== --- solr/src/test/org/apache/solr/spelling/IndexBasedSpellCheckerTest.java (revision 1043862) +++ solr/src/test/org/apache/solr/spelling/IndexBasedSpellCheckerTest.java (working copy) @@ -30,6 +30,7 @@ import org.apache.lucene.search.spell.StringDistance; import org.apache.lucene.search.spell.SuggestWord; import org.apache.lucene.search.spell.SuggestWordFrequencyComparator; +import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.util.NamedList; @@ -284,8 +285,9 @@ File indexDir = new File(TEMP_DIR, "spellingIdx" + new Date().getTime()); //create a standalone index File altIndexDir = new File(TEMP_DIR, "alternateIdx" + new Date().getTime()); + Directory dir = newFSDirectory(altIndexDir); IndexWriter iw = new IndexWriter( - FSDirectory.open(altIndexDir), + dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)). setMaxFieldLength(IndexWriterConfig.UNLIMITED_FIELD_LENGTH) ); @@ -296,6 +298,7 @@ } iw.optimize(); iw.close(); + dir.close(); indexDir.mkdirs(); spellchecker.add(AbstractLuceneSpellChecker.INDEX_DIR, indexDir.getAbsolutePath()); spellchecker.add(AbstractLuceneSpellChecker.LOCATION, altIndexDir.getAbsolutePath()); Index: lucene/CHANGES.txt =================================================================== --- lucene/CHANGES.txt (revision 1043862) +++ lucene/CHANGES.txt (working copy) @@ -74,6 +74,9 @@ If you index empty fields and uses positions/offsets information on that fields, reindex is recommended. (David Smiley, Koji Sekiguchi) +* LUCENE-2804: Directory.setLockFactory new declares throwing an IOException. + (Shai Erera, Robert Muir) + Changes in runtime behavior * LUCENE-1923: Made IndexReader.toString() produce something @@ -544,6 +547,9 @@ as Eclipse and IntelliJ. (Paolo Castagna, Steven Rowe via Robert Muir) +* LUCENE-2804: add newFSDirectory to LuceneTestCase to create a FSDirectory at + random. (Shai Erera, Robert Muir) + Documentation * LUCENE-2579: Fix oal.search's package.html description of abstract Index: lucene/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestSnapshotDeletionPolicy.java (working copy) @@ -84,18 +84,9 @@ @Test public void testSnapshotDeletionPolicy() throws Exception { - File dir = _TestUtil.getTempDir(INDEX_PATH); - try { - Directory fsDir = FSDirectory.open(dir); - runTest(random, fsDir); - fsDir.close(); - } finally { - _TestUtil.rmDir(dir); - } - - Directory dir2 = newDirectory(); - runTest(random, dir2); - dir2.close(); + Directory fsDir = newDirectory(); + runTest(random, fsDir); + fsDir.close(); } private void runTest(Random random, Directory dir) throws Exception { Index: lucene/src/test/org/apache/lucene/index/TestDoc.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestDoc.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestDoc.java (working copy) @@ -34,10 +34,8 @@ import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriterConfig.OpenMode; import org.apache.lucene.store.Directory; -import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.LuceneTestCase; - /** JUnit adaptation of an older test case DocTest. */ public class TestDoc extends LuceneTestCase { @@ -46,12 +44,10 @@ TestRunner.run (new TestSuite(TestDoc.class)); } - private File workDir; private File indexDir; private LinkedList files; - /** Set the test case. This test case needs * a few text files created in the current working directory. */ @@ -64,7 +60,7 @@ indexDir = new File(workDir, "testIndex"); indexDir.mkdirs(); - Directory directory = FSDirectory.open(indexDir); + Directory directory = newFSDirectory(indexDir); directory.close(); files = new LinkedList(); @@ -109,7 +105,7 @@ StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw, true); - Directory directory = FSDirectory.open(indexDir); + Directory directory = newFSDirectory(indexDir); IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig( TEST_VERSION_CURRENT, new SimpleAnalyzer(TEST_VERSION_CURRENT)) .setOpenMode(OpenMode.CREATE).setMaxBufferedDocs(-1)); @@ -140,7 +136,7 @@ sw = new StringWriter(); out = new PrintWriter(sw, true); - directory = FSDirectory.open(indexDir); + directory = newFSDirectory(indexDir); writer = new IndexWriter(directory, newIndexWriterConfig( TEST_VERSION_CURRENT, new SimpleAnalyzer(TEST_VERSION_CURRENT)) .setOpenMode(OpenMode.CREATE).setMaxBufferedDocs(-1)); Index: lucene/src/test/org/apache/lucene/index/TestIndexReader.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestIndexReader.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestIndexReader.java (working copy) @@ -46,7 +46,6 @@ import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.AlreadyClosedException; import org.apache.lucene.store.Directory; -import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.store.MockDirectoryWrapper; import org.apache.lucene.store.NoSuchDirectoryException; @@ -55,8 +54,7 @@ import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util._TestUtil; -public class TestIndexReader extends LuceneTestCase -{ +public class TestIndexReader extends LuceneTestCase { public void testCommitUserData() throws Exception { Directory d = newDirectory(); @@ -519,8 +517,7 @@ // Make sure you can set norms & commit even if a reader // is open against the index: public void testWritingNorms() throws IOException { - File indexDir = new File(TEMP_DIR, "lucenetestnormwriter"); - Directory dir = FSDirectory.open(indexDir); + Directory dir = newDirectory(); IndexWriter writer; IndexReader reader; Term searchTerm = new Term("content", "aaa"); @@ -556,8 +553,6 @@ reader2.close(); dir.close(); - - rmDir(indexDir); } @@ -700,7 +695,7 @@ public void testFilesOpenClose() throws IOException { // Create initial data set File dirFile = _TestUtil.getTempDir("TestIndexReader.testFilesOpenClose"); - Directory dir = FSDirectory.open(dirFile); + Directory dir = newFSDirectory(dirFile); IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT))); addDoc(writer, "test"); writer.close(); @@ -708,7 +703,7 @@ // Try to erase the data - this ensures that the writer closed all files _TestUtil.rmDir(dirFile); - dir = FSDirectory.open(dirFile); + dir = newFSDirectory(dirFile); // Now create the data set again, just as before writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)).setOpenMode(OpenMode.CREATE)); @@ -717,7 +712,7 @@ dir.close(); // Now open existing directory and test that reader closes all files - dir = FSDirectory.open(dirFile); + dir = newFSDirectory(dirFile); IndexReader reader1 = IndexReader.open(dir, false); reader1.close(); dir.close(); @@ -1151,7 +1146,7 @@ public void testOpenReaderAfterDelete() throws IOException { File dirFile = new File(TEMP_DIR, "deletetest"); - Directory dir = FSDirectory.open(dirFile); + Directory dir = newFSDirectory(dirFile); try { IndexReader.open(dir, false); fail("expected FileNotFoundException"); @@ -1306,19 +1301,11 @@ writer.addDocument(doc); } - private void addDoc(IndexWriter writer, String value) throws IOException - { + private void addDoc(IndexWriter writer, String value) throws IOException { Document doc = new Document(); doc.add(newField("content", value, Field.Store.NO, Field.Index.ANALYZED)); writer.addDocument(doc); } - private void rmDir(File dir) { - File[] files = dir.listFiles(); - for (int i = 0; i < files.length; i++) { - files[i].delete(); - } - dir.delete(); - } public static void assertIndexEquals(IndexReader index1, IndexReader index2) throws IOException { assertEquals("IndexReaders have different values for numDocs.", index1.numDocs(), index2.numDocs()); @@ -1567,7 +1554,7 @@ // IndexReader on a non-existent directory, you get a // good exception public void testNoDir() throws Throwable { - Directory dir = FSDirectory.open(_TestUtil.getTempDir("doesnotexist")); + Directory dir = newFSDirectory(_TestUtil.getTempDir("doesnotexist")); try { IndexReader.open(dir, true); fail("did not hit expected exception"); Index: lucene/src/test/org/apache/lucene/index/TestStressIndexing2.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestStressIndexing2.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestStressIndexing2.java (working copy) @@ -76,7 +76,6 @@ public void testRandom() throws Throwable { Directory dir1 = newDirectory(); - // dir1 = FSDirectory.open("foofoofoo"); Directory dir2 = newDirectory(); // mergeFactor=2; maxBufferedDocs=2; Map docs = indexRandom(1, 3, 2, dir1); int maxThreadStates = 1+random.nextInt(10); Index: lucene/src/test/org/apache/lucene/index/TestIndexWriter.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestIndexWriter.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestIndexWriter.java (working copy) @@ -58,7 +58,6 @@ import org.apache.lucene.search.spans.SpanTermQuery; import org.apache.lucene.store.AlreadyClosedException; import org.apache.lucene.store.Directory; -import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.IndexOutput; import org.apache.lucene.store.Lock; import org.apache.lucene.store.LockFactory; @@ -377,34 +376,30 @@ // reader holds it open (this fails pre lock-less // commits on windows): public void testCreateWithReader() throws IOException { - File indexDir = _TestUtil.getTempDir("lucenetestindexwriter"); - - try { - Directory dir = FSDirectory.open(indexDir); - - // add one document & close writer - IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT))); - addDoc(writer); - writer.close(); - - // now open reader: - IndexReader reader = IndexReader.open(dir, true); - assertEquals("should be one document", reader.numDocs(), 1); - - // now open index for create: - writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)).setOpenMode(OpenMode.CREATE)); - assertEquals("should be zero documents", writer.maxDoc(), 0); - addDoc(writer); - writer.close(); - - assertEquals("should be one document", reader.numDocs(), 1); - IndexReader reader2 = IndexReader.open(dir, true); - assertEquals("should be one document", reader2.numDocs(), 1); - reader.close(); - reader2.close(); - } finally { - rmDir(indexDir); - } + Directory dir = newDirectory(); + + // add one document & close writer + IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT))); + addDoc(writer); + writer.close(); + + // now open reader: + IndexReader reader = IndexReader.open(dir, true); + assertEquals("should be one document", reader.numDocs(), 1); + + // now open index for create: + writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)).setOpenMode(OpenMode.CREATE)); + assertEquals("should be zero documents", writer.maxDoc(), 0); + addDoc(writer); + writer.close(); + + assertEquals("should be one document", reader.numDocs(), 1); + IndexReader reader2 = IndexReader.open(dir, true); + assertEquals("should be one document", reader2.numDocs(), 1); + reader.close(); + reader2.close(); + + dir.close(); } public void testChangesAfterClose() throws IOException { @@ -1097,16 +1092,6 @@ dir.close(); } - private void rmDir(File dir) { - File[] files = dir.listFiles(); - if (files != null) { - for (int i = 0; i < files.length; i++) { - files[i].delete(); - } - } - dir.delete(); - } - /** * Test that no NullPointerException will be raised, * when adding one document with a single, empty field @@ -2107,11 +2092,9 @@ // create=true does not remove non-index files public void testOtherFiles() throws Throwable { - File indexDir = new File(TEMP_DIR, "otherfiles"); - Directory dir = FSDirectory.open(indexDir); + Directory dir = newDirectory(); try { // Create my own random file: - IndexOutput out = dir.createOutput("myrandomfile"); out.writeByte((byte) 42); out.close(); @@ -2126,7 +2109,6 @@ dir2.close(); } finally { dir.close(); - _TestUtil.rmDir(indexDir); } } @@ -2632,8 +2614,9 @@ // Tests that if FSDir is opened w/ a NoLockFactory (or SingleInstanceLF), // then IndexWriter ctor succeeds. Previously (LUCENE-2386) it failed // when listAll() was called in IndexFileDeleter. - FSDirectory dir = FSDirectory.open(new File(TEMP_DIR, "emptyFSDirNoLock"), NoLockFactory.getNoLockFactory()); + Directory dir = newFSDirectory(new File(TEMP_DIR, "emptyFSDirNoLock"), NoLockFactory.getNoLockFactory()); new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT))).close(); + dir.close(); } public void testEmptyDirRollback() throws Exception { @@ -2681,27 +2664,22 @@ } public void testNoSegmentFile() throws IOException { - File tempDir = _TestUtil.getTempDir("noSegmentFile"); - try { - Directory dir = FSDirectory.open(tempDir); - dir.setLockFactory(NoLockFactory.getNoLockFactory()); - IndexWriter w = new IndexWriter(dir, newIndexWriterConfig( - TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)).setMaxBufferedDocs(2)); - - Document doc = new Document(); - doc.add(newField("c", "val", Store.YES, Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS)); - w.addDocument(doc); - w.addDocument(doc); - IndexWriter w2 = new IndexWriter(dir, newIndexWriterConfig( - TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)) - .setMaxBufferedDocs(2).setOpenMode(OpenMode.CREATE)); - - w2.close(); - w.rollback(); - dir.close(); - } finally { - _TestUtil.rmDir(tempDir); - } + Directory dir = newDirectory(); + dir.setLockFactory(NoLockFactory.getNoLockFactory()); + IndexWriter w = new IndexWriter(dir, newIndexWriterConfig( + TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)).setMaxBufferedDocs(2)); + + Document doc = new Document(); + doc.add(newField("c", "val", Store.YES, Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS)); + w.addDocument(doc); + w.addDocument(doc); + IndexWriter w2 = new IndexWriter(dir, newIndexWriterConfig( + TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)) + .setMaxBufferedDocs(2).setOpenMode(OpenMode.CREATE)); + + w2.close(); + w.rollback(); + dir.close(); } public void testFutureCommit() throws Exception { Index: lucene/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestIndexWriterLockRelease.java (working copy) @@ -24,7 +24,7 @@ import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig.OpenMode; -import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.store.Directory; /** * This tests the patch for issue #LUCENE-715 (IndexWriter does not @@ -73,7 +73,7 @@ } public void testIndexWriterLockRelease() throws IOException { - FSDirectory dir = FSDirectory.open(this.__test_dir); + Directory dir = newFSDirectory(this.__test_dir); try { new IndexWriter(dir, new IndexWriterConfig(TEST_VERSION_CURRENT, new StandardAnalyzer(TEST_VERSION_CURRENT)) Index: lucene/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java (working copy) @@ -17,7 +17,6 @@ * limitations under the License. */ -import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Random; @@ -34,7 +33,6 @@ import org.apache.lucene.search.DefaultSimilarity; import org.apache.lucene.search.Similarity; import org.apache.lucene.store.Directory; -import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.LuceneTestCase; /** @@ -79,8 +77,7 @@ */ public void testNorms() throws IOException { // test with a single index: index1 - File indexDir1 = new File(TEMP_DIR, "lucenetestindex1"); - Directory dir1 = FSDirectory.open(indexDir1); + Directory dir1 = newDirectory(); IndexWriter.unlock(dir1); norms = new ArrayList(); @@ -98,15 +95,13 @@ modifiedNorms = new ArrayList(); numDocNorms = 0; - File indexDir2 = new File(TEMP_DIR, "lucenetestindex2"); - Directory dir2 = FSDirectory.open(indexDir2); + Directory dir2 = newDirectory(); createIndex(random, dir2); doTestNorms(random, dir2); // add index1 and index2 to a third index: index3 - File indexDir3 = new File(TEMP_DIR, "lucenetestindex3"); - Directory dir3 = FSDirectory.open(indexDir3); + Directory dir3 = newDirectory(); createIndex(random, dir3); IndexWriter iw = new IndexWriter(dir3, newIndexWriterConfig( @@ -157,6 +152,9 @@ verifyIndex(irc3); irc3.flush(); irc3.close(); + + irc.close(); + ir.close(); } public void testNormsClose() throws IOException { Index: lucene/src/test/org/apache/lucene/index/TestCrash.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestCrash.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestCrash.java (working copy) @@ -21,6 +21,7 @@ import java.util.Random; import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.store.Directory; import org.apache.lucene.store.MockDirectoryWrapper; import org.apache.lucene.store.NoLockFactory; import org.apache.lucene.analysis.WhitespaceAnalyzer; @@ -66,7 +67,7 @@ // happened, so we must create an initial commit just to allow that, but // before any documents were added. IndexWriter writer = initIndex(random, true); - MockDirectoryWrapper dir = (MockDirectoryWrapper) writer.getDirectory(); + Directory dir = writer.getDirectory(); crash(writer); IndexReader reader = IndexReader.open(dir, false); assertTrue(reader.numDocs() < 157); Index: lucene/src/test/org/apache/lucene/index/TestThreadedOptimize.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestThreadedOptimize.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestThreadedOptimize.java (working copy) @@ -136,12 +136,5 @@ runTest(random, directory, new SerialMergeScheduler()); runTest(random, directory, new ConcurrentMergeScheduler()); directory.close(); - - File dirName = new File(TEMP_DIR, "luceneTestThreadedOptimize"); - directory = FSDirectory.open(dirName); - runTest(random, directory, new SerialMergeScheduler()); - runTest(random, directory, new ConcurrentMergeScheduler()); - directory.close(); - _TestUtil.rmDir(dirName); } } Index: lucene/src/test/org/apache/lucene/index/TestAtomicUpdate.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestAtomicUpdate.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestAtomicUpdate.java (working copy) @@ -195,7 +195,7 @@ // Second in an FSDirectory: File dirPath = _TestUtil.getTempDir("lucene.test.atomic"); - directory = FSDirectory.open(dirPath); + directory = newFSDirectory(dirPath); runTest(directory); directory.close(); _TestUtil.rmDir(dirPath); Index: lucene/src/test/org/apache/lucene/index/TestFieldsReader.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestFieldsReader.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestFieldsReader.java (working copy) @@ -36,7 +36,6 @@ import org.apache.lucene.store.AlreadyClosedException; import org.apache.lucene.store.BufferedIndexInput; import org.apache.lucene.store.Directory; -import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.IndexOutput; import org.apache.lucene.util.LuceneTestCase; @@ -162,7 +161,6 @@ assertTrue(dir != null); assertTrue(fieldInfos != null); FieldsReader reader = new FieldsReader(dir, TEST_SEGMENT_NAME, fieldInfos); - assertTrue(reader != null); assertTrue(reader.size() == 1); Set loadFieldNames = new HashSet(); loadFieldNames.add(DocHelper.TEXT_FIELD_1_KEY); @@ -176,6 +174,7 @@ // Use LATENT instead of LAZY SetBasedFieldSelector fieldSelector = new SetBasedFieldSelector(loadFieldNames, lazyFieldNames) { + @Override public FieldSelectorResult accept(String fieldName) { final FieldSelectorResult result = super.accept(fieldName); if (result == FieldSelectorResult.LAZY_LOAD) { @@ -292,7 +291,7 @@ String userName = System.getProperty("user.name"); File file = new File(TEMP_DIR, "lazyDir" + userName); _TestUtil.rmDir(file); - FSDirectory tmpDir = FSDirectory.open(file); + Directory tmpDir = newFSDirectory(file); assertTrue(tmpDir != null); IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)).setOpenMode(OpenMode.CREATE); @@ -345,8 +344,9 @@ assertTrue("value is null and it shouldn't be", value != null); lazyTime += (finish - start); reader.close(); - } + tmpDir.close(); + if (VERBOSE) { System.out.println("Average Non-lazy time (should be very close to zero): " + regularTime / length + " ms for " + length + " reads"); System.out.println("Average Lazy Time (should be greater than zero): " + lazyTime / length + " ms for " + length + " reads"); @@ -390,9 +390,9 @@ public static class FaultyFSDirectory extends Directory { - FSDirectory fsDir; + Directory fsDir; public FaultyFSDirectory(File dir) throws IOException { - fsDir = FSDirectory.open(dir); + fsDir = newFSDirectory(dir); lockFactory = fsDir.getLockFactory(); } @Override Index: lucene/src/test/org/apache/lucene/index/TestIndexReaderReopen.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestIndexReaderReopen.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestIndexReaderReopen.java (working copy) @@ -42,11 +42,9 @@ import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.Directory; -import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.AlreadyClosedException; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.BitVector; -import org.apache.lucene.util.LuceneTestCase; public class TestIndexReaderReopen extends LuceneTestCase { @@ -151,12 +149,12 @@ // in each iteration verify the work of previous iteration. // try this once with reopen once recreate, on both RAMDir and FSDir. public void testCommitReopenFS () throws IOException { - Directory dir = FSDirectory.open(indexDir); + Directory dir = newFSDirectory(indexDir); doTestReopenWithCommit(random, dir, true); dir.close(); } public void testCommitRecreateFS () throws IOException { - Directory dir = FSDirectory.open(indexDir); + Directory dir = newFSDirectory(indexDir); doTestReopenWithCommit(random, dir, false); dir.close(); } Index: lucene/src/test/org/apache/lucene/index/TestStressIndexing.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestStressIndexing.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestStressIndexing.java (working copy) @@ -163,17 +163,8 @@ FSDirectory. */ public void testStressIndexAndSearching() throws Exception { - // With ConcurrentMergeScheduler, in RAMDir Directory directory = newDirectory(); runStressTest(directory, new ConcurrentMergeScheduler()); directory.close(); - - // With ConcurrentMergeScheduler, in FSDir - File dirPath = _TestUtil.getTempDir("lucene.test.stress"); - directory = FSDirectory.open(dirPath); - runStressTest(directory, new ConcurrentMergeScheduler()); - directory.close(); - - _TestUtil.rmDir(dirPath); } } Index: lucene/src/test/org/apache/lucene/index/TestPayloads.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestPayloads.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestPayloads.java (working copy) @@ -37,7 +37,6 @@ import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriterConfig.OpenMode; import org.apache.lucene.store.Directory; -import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.UnicodeUtil; import org.apache.lucene.util._TestUtil; @@ -157,7 +156,7 @@ dir.close(); // now use a FSDirectory and repeat same test File dirName = _TestUtil.getTempDir("test_payloads"); - dir = FSDirectory.open(dirName); + dir = newFSDirectory(dirName); performTest(dir); _TestUtil.rmDir(dirName); dir.close(); Index: lucene/src/test/org/apache/lucene/index/Test2BTerms.java =================================================================== --- lucene/src/test/org/apache/lucene/index/Test2BTerms.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/Test2BTerms.java (working copy) @@ -43,7 +43,6 @@ private final static int TOKEN_LEN = 5; private final char[] chars; private final byte[] bytes; - private int charUpto; public MyTokenStream(int tokensPerDoc) { super(); @@ -54,6 +53,7 @@ bytes = new byte[2*TOKEN_LEN]; } + @Override public boolean incrementToken() { if (tokenCount >= tokensPerDoc) { return false; @@ -68,6 +68,7 @@ return true; } + @Override public void reset() { tokenCount = 0; } @@ -80,7 +81,7 @@ int TERMS_PER_DOC = 1000000; - Directory dir = FSDirectory.open(_TestUtil.getTempDir("2BTerms")); + Directory dir = newFSDirectory(_TestUtil.getTempDir("2BTerms")); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)) .setMaxBufferedDocs(IndexWriterConfig.DISABLE_AUTO_FLUSH) Index: lucene/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java (revision 1043862) +++ lucene/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java (working copy) @@ -47,7 +47,6 @@ import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.store.Directory; -import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.ReaderUtil; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util._TestUtil; @@ -213,7 +212,7 @@ for(int i=0;i 1) ? elements[1] : null; + // Look for the first class that is not LuceneTestCase that requested + // a Directory. The first two items are of Thread's, so skipping over + // them. + StackTraceElement element = null; + for (int i = 2; i < elements.length; i++) { + StackTraceElement ste = elements[i]; + if (ste.getClassName().indexOf("LuceneTestCase") == -1) { + element = ste; + break; + } + } fail("directory of test was not closed, opened from: " + element); } } @@ -552,7 +562,7 @@ } /** - * Returns a new Dictionary instance. Use this when the test does not + * 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 {@link MockDirectoryWrapper}. @@ -566,15 +576,14 @@ } public static MockDirectoryWrapper newDirectory(Random r) throws IOException { - StackTraceElement[] stack = new Exception().getStackTrace(); Directory impl = newDirectoryImpl(r, TEST_DIRECTORY); MockDirectoryWrapper dir = new MockDirectoryWrapper(r, impl); - stores.put(dir, stack); + stores.put(dir, Thread.currentThread().getStackTrace()); return dir; } /** - * Returns a new Dictionary instance, with contents copied from the + * Returns a new Directory instance, with contents copied from the * provided directory. See {@link #newDirectory()} for more * information. */ @@ -582,14 +591,46 @@ return newDirectory(random, d); } + /** Returns a new FSDirectory instance over the given file, which must be a folder. */ + public static MockDirectoryWrapper newFSDirectory(File f) throws IOException { + return newFSDirectory(f, null); + } + + /** Returns a new FSDirectory instance over the given file, which must be a folder. */ + public static MockDirectoryWrapper newFSDirectory(File f, LockFactory lf) throws IOException { + String fsdirClass = TEST_DIRECTORY; + if (fsdirClass.equals("random")) { + fsdirClass = FS_DIRECTORIES[random.nextInt(FS_DIRECTORIES.length)]; + } + + if (fsdirClass.indexOf(".") == -1) {// if not fully qualified, assume .store + fsdirClass = "org.apache.lucene.store." + fsdirClass; + } + + Class clazz; + try { + try { + clazz = Class.forName(fsdirClass).asSubclass(FSDirectory.class); + } catch (ClassCastException e) { + // TEST_DIRECTORY is not a sub-class of FSDirectory, so draw one at random + fsdirClass = FS_DIRECTORIES[random.nextInt(FS_DIRECTORIES.length)]; + clazz = Class.forName(fsdirClass).asSubclass(FSDirectory.class); + } + MockDirectoryWrapper dir = new MockDirectoryWrapper(random, newFSDirectoryImpl(clazz, f, lf)); + stores.put(dir, Thread.currentThread().getStackTrace()); + return dir; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + 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()) { d.copy(impl, file, file); } MockDirectoryWrapper dir = new MockDirectoryWrapper(r, impl); - stores.put(dir, stack); + stores.put(dir, Thread.currentThread().getStackTrace()); return dir; } @@ -665,12 +706,16 @@ } } - private static String CORE_DIRECTORIES[] = { - "RAMDirectory", + private static final String FS_DIRECTORIES[] = { "SimpleFSDirectory", "NIOFSDirectory", "MMapDirectory" }; + + private static final String CORE_DIRECTORIES[] = { + "RAMDirectory", + FS_DIRECTORIES[0], FS_DIRECTORIES[1], FS_DIRECTORIES[2] + }; public static String randomDirectory(Random random) { if (random.nextInt(10) == 0) { @@ -679,6 +724,27 @@ return "RAMDirectory"; } } + + private static Directory newFSDirectoryImpl( + Class clazz, File file, LockFactory lockFactory) + throws IOException { + try { + // Assuming every FSDirectory has a ctor(File), but not all may take a + // LockFactory too, so setting it afterwards. + Constructor ctor = clazz.getConstructor(File.class); + FSDirectory d = ctor.newInstance(file); + if (lockFactory != null) { + d.setLockFactory(lockFactory); + } + // 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 e) { + return FSDirectory.open(file); + } + } static Directory newDirectoryImpl(Random random, String clazzName) { if (clazzName.equals("random")) @@ -687,27 +753,22 @@ clazzName = "org.apache.lucene.store." + clazzName; try { final Class clazz = Class.forName(clazzName).asSubclass(Directory.class); - try { - // try empty ctor - return clazz.newInstance(); - } catch (Exception e) { + // If it is a FSDirectory type, try its ctor(File) + if (FSDirectory.class.isAssignableFrom(clazz)) { final File tmpFile = File.createTempFile("test", "tmp", TEMP_DIR); tmpFile.delete(); tmpFile.mkdir(); - try { - Constructor ctor = clazz.getConstructor(File.class); - return ctor.newInstance(tmpFile); - } catch (Exception e2) { - // try .open(File) - Method method = clazz.getMethod("open", new Class[] { File.class }); - return (Directory) method.invoke(null, tmpFile); - } + return newFSDirectoryImpl(clazz.asSubclass(FSDirectory.class), tmpFile, null); } + + // try empty ctor + return clazz.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } } + public String getName() { return this.name; } @@ -716,6 +777,7 @@ * if a real file is needed. To get a stream, code should prefer * {@link Class#getResourceAsStream} using {@code this.getClass()}. */ + protected File getDataFile(String name) throws IOException { try { return new File(this.getClass().getResource(name).toURI()); Index: lucene/src/java/org/apache/lucene/store/RAMDirectory.java =================================================================== --- lucene/src/java/org/apache/lucene/store/RAMDirectory.java (revision 1043862) +++ lucene/src/java/org/apache/lucene/store/RAMDirectory.java (working copy) @@ -48,7 +48,11 @@ /** Constructs an empty {@link Directory}. */ public RAMDirectory() { - setLockFactory(new SingleInstanceLockFactory()); + try { + setLockFactory(new SingleInstanceLockFactory()); + } catch (IOException e) { + // Cannot happen + } } /** Index: lucene/src/java/org/apache/lucene/store/Directory.java =================================================================== --- lucene/src/java/org/apache/lucene/store/Directory.java (revision 1043862) +++ lucene/src/java/org/apache/lucene/store/Directory.java (working copy) @@ -170,7 +170,7 @@ * * @param lockFactory instance of {@link LockFactory}. */ - public void setLockFactory(LockFactory lockFactory) { + public void setLockFactory(LockFactory lockFactory) throws IOException { assert lockFactory != null; this.lockFactory = lockFactory; lockFactory.setLockPrefix(this.getLockID()); Index: lucene/src/java/org/apache/lucene/store/FSDirectory.java =================================================================== --- lucene/src/java/org/apache/lucene/store/FSDirectory.java (revision 1043862) +++ lucene/src/java/org/apache/lucene/store/FSDirectory.java (working copy) @@ -156,20 +156,6 @@ throw new NoSuchDirectoryException("file '" + directory + "' exists but is not a directory"); setLockFactory(lockFactory); - - // for filesystem based LockFactory, delete the lockPrefix, if the locks are placed - // in index dir. If no index dir is given, set ourselves - if (lockFactory instanceof FSLockFactory) { - final FSLockFactory lf = (FSLockFactory) lockFactory; - final File dir = lf.getLockDir(); - // if the lock factory has no lockDir set, use the this directory as lockDir - if (dir == null) { - lf.setLockDir(directory); - lf.setLockPrefix(null); - } else if (dir.getCanonicalPath().equals(directory.getCanonicalPath())) { - lf.setLockPrefix(null); - } - } } /** Creates an FSDirectory instance, trying to pick the @@ -209,6 +195,26 @@ } } + @Override + public void setLockFactory(LockFactory lockFactory) throws IOException { + super.setLockFactory(lockFactory); + + // for filesystem based LockFactory, delete the lockPrefix, if the locks are placed + // in index dir. If no index dir is given, set ourselves + if (lockFactory instanceof FSLockFactory) { + final FSLockFactory lf = (FSLockFactory) lockFactory; + final File dir = lf.getLockDir(); + // if the lock factory has no lockDir set, use the this directory as lockDir + if (dir == null) { + lf.setLockDir(directory); + lf.setLockPrefix(null); + } else if (dir.getCanonicalPath().equals(directory.getCanonicalPath())) { + lf.setLockPrefix(null); + } + } + + } + /** Lists all files (not subdirectories) in the * directory. This method never returns null (throws * {@link IOException} instead). Index: lucene/contrib/ant/src/test/org/apache/lucene/ant/IndexTaskTest.java =================================================================== --- lucene/contrib/ant/src/test/org/apache/lucene/ant/IndexTaskTest.java (revision 1043862) +++ lucene/contrib/ant/src/test/org/apache/lucene/ant/IndexTaskTest.java (working copy) @@ -26,7 +26,7 @@ import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.Searcher; -import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.store.Directory; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.FileSet; import org.apache.lucene.util.LuceneTestCase; @@ -41,7 +41,7 @@ private Searcher searcher; private Analyzer analyzer; - private FSDirectory dir; + private Directory dir; /** @@ -68,7 +68,7 @@ task.setProject(project); task.execute(); - dir = FSDirectory.open(indexDir); + dir = newFSDirectory(indexDir); searcher = new IndexSearcher(dir, true); analyzer = new StopAnalyzer(TEST_VERSION_CURRENT); } Index: lucene/contrib/wordnet/src/test/org/apache/lucene/wordnet/TestWordnet.java =================================================================== --- lucene/contrib/wordnet/src/test/org/apache/lucene/wordnet/TestWordnet.java (revision 1043862) +++ lucene/contrib/wordnet/src/test/org/apache/lucene/wordnet/TestWordnet.java (working copy) @@ -28,15 +28,15 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.Searcher; import org.apache.lucene.search.TermQuery; -import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.store.Directory; import org.apache.lucene.util.LuceneTestCase; public class TestWordnet extends LuceneTestCase { private Searcher searcher; - - String storePathName = - new File(TEMP_DIR,"testLuceneWordnet").getAbsolutePath(); + private Directory dir; + String storePathName = new File(TEMP_DIR,"testLuceneWordnet").getAbsolutePath(); + @Override public void setUp() throws Exception { super.setUp(); @@ -48,7 +48,8 @@ Syns2Index.main(commandLineArgs); } catch (Throwable t) { throw new RuntimeException(t); } - searcher = new IndexSearcher(FSDirectory.open(new File(storePathName)), true); + dir = newFSDirectory(new File(storePathName)); + searcher = new IndexSearcher(dir, true); } public void testExpansion() throws IOException { @@ -72,6 +73,7 @@ @Override public void tearDown() throws Exception { searcher.close(); + dir.close(); rmDir(storePathName); // delete our temporary synonym index super.tearDown(); } Index: lucene/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestIndicesEquals.java =================================================================== --- lucene/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestIndicesEquals.java (revision 1043862) +++ lucene/contrib/instantiated/src/test/org/apache/lucene/store/instantiated/TestIndicesEquals.java (working copy) @@ -30,7 +30,6 @@ import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; -import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.Payload; import org.apache.lucene.index.Term; import org.apache.lucene.index.TermDocs; Index: lucene/contrib/benchmark/src/test/org/apache/lucene/benchmark/quality/TestQualityRun.java =================================================================== --- lucene/contrib/benchmark/src/test/org/apache/lucene/benchmark/quality/TestQualityRun.java (revision 1043862) +++ lucene/contrib/benchmark/src/test/org/apache/lucene/benchmark/quality/TestQualityRun.java (working copy) @@ -23,7 +23,7 @@ import org.apache.lucene.benchmark.quality.utils.SimpleQQParser; import org.apache.lucene.benchmark.quality.utils.SubmissionReport; import org.apache.lucene.search.IndexSearcher; -import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.store.Directory; import java.io.BufferedReader; import java.io.File; @@ -67,7 +67,8 @@ // validate topics & judgments match each other judge.validateData(qqs, logger); - IndexSearcher searcher = new IndexSearcher(FSDirectory.open(new File(getWorkDir(),"index")), true); + Directory dir = newFSDirectory(new File(getWorkDir(),"index")); + IndexSearcher searcher = new IndexSearcher(dir, true); QualityQueryParser qqParser = new SimpleQQParser("title","body"); QualityBenchmark qrun = new QualityBenchmark(qqs, qqParser, searcher, docNameField); @@ -131,8 +132,9 @@ for (int j = 1; j <= QualityStats.MAX_POINTS; j++) { assertTrue("avg p_at_"+j+" should be hurt: "+avg.getPrecisionAt(j), 1.0 > avg.getPrecisionAt(j)); } - + searcher.close(); + dir.close(); } public void testTrecTopicsReader() throws Exception { Index: lucene/contrib/misc/src/test/org/apache/lucene/index/TestIndexSplitter.java =================================================================== --- lucene/contrib/misc/src/test/org/apache/lucene/index/TestIndexSplitter.java (revision 1043862) +++ lucene/contrib/misc/src/test/org/apache/lucene/index/TestIndexSplitter.java (working copy) @@ -21,7 +21,7 @@ import org.apache.lucene.analysis.WhitespaceAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexWriterConfig.OpenMode; -import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.store.Directory; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util._TestUtil; @@ -33,7 +33,7 @@ File destDir = new File(TEMP_DIR, "testfilesplitterdest"); _TestUtil.rmDir(destDir); destDir.mkdirs(); - FSDirectory fsDir = FSDirectory.open(dir); + Directory fsDir = newFSDirectory(dir); IndexWriterConfig conf = new IndexWriterConfig(TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)) .setOpenMode(OpenMode.CREATE); @@ -56,14 +56,20 @@ iw.addDocument(doc); } iw.commit(); - assertEquals(3, iw.getReader().getSequentialSubReaders().length); + IndexReader iwReader = iw.getReader(); + assertEquals(3, iwReader.getSequentialSubReaders().length); + iwReader.close(); iw.close(); + // we should have 2 segments now IndexSplitter is = new IndexSplitter(dir); String splitSegName = is.infos.info(1).name; is.split(destDir, new String[] {splitSegName}); - IndexReader r = IndexReader.open(FSDirectory.open(destDir), true); + Directory fsDirDest = newFSDirectory(destDir); + IndexReader r = IndexReader.open(fsDirDest, true); assertEquals(50, r.maxDoc()); + r.close(); + fsDirDest.close(); // now test cmdline File destDir2 = new File(TEMP_DIR, "testfilesplitterdest2"); @@ -71,12 +77,17 @@ destDir2.mkdirs(); IndexSplitter.main(new String[] {dir.getAbsolutePath(), destDir2.getAbsolutePath(), splitSegName}); assertEquals(3, destDir2.listFiles().length); - r = IndexReader.open(FSDirectory.open(destDir2), true); + Directory fsDirDest2 = newFSDirectory(destDir2); + r = IndexReader.open(fsDirDest2, true); assertEquals(50, r.maxDoc()); + r.close(); + fsDirDest2.close(); // now remove the copied segment from src IndexSplitter.main(new String[] {dir.getAbsolutePath(), "-d", splitSegName}); - r = IndexReader.open(FSDirectory.open(dir), true); + r = IndexReader.open(fsDir, true); assertEquals(2, r.getSequentialSubReaders().length); + r.close(); + fsDir.close(); } }