LUCENE-2796: Lucene tests need to clean up after themselves

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1095937 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2011-04-22 14:46:14 +00:00
parent 04d1646611
commit b48ad6841f
16 changed files with 42 additions and 23 deletions

View File

@ -128,6 +128,9 @@ public abstract class LuceneTestCase extends Assert {
TEMP_DIR = new File(s); TEMP_DIR = new File(s);
TEMP_DIR.mkdirs(); TEMP_DIR.mkdirs();
} }
/** set of directories we created, in afterclass we try to clean these up */
static final Set<String> tempDirs = Collections.synchronizedSet(new HashSet<String>());
// by default we randomly pick a different codec for // by default we randomly pick a different codec for
// each test case (non-J4 tests) and each test class (J4 // each test case (non-J4 tests) and each test class (J4
@ -323,6 +326,7 @@ public abstract class LuceneTestCase extends Assert {
public static void beforeClassLuceneTestCaseJ4() { public static void beforeClassLuceneTestCaseJ4() {
staticSeed = "random".equals(TEST_SEED) ? seedRand.nextLong() : TwoLongs.fromString(TEST_SEED).l1; staticSeed = "random".equals(TEST_SEED) ? seedRand.nextLong() : TwoLongs.fromString(TEST_SEED).l1;
random.setSeed(staticSeed); random.setSeed(staticSeed);
tempDirs.clear();
stores = Collections.synchronizedMap(new IdentityHashMap<MockDirectoryWrapper,StackTraceElement[]>()); stores = Collections.synchronizedMap(new IdentityHashMap<MockDirectoryWrapper,StackTraceElement[]>());
savedCodecProvider = CodecProvider.getDefault(); savedCodecProvider = CodecProvider.getDefault();
if ("randomPerField".equals(TEST_CODEC)) { if ("randomPerField".equals(TEST_CODEC)) {
@ -411,6 +415,16 @@ public abstract class LuceneTestCase extends Assert {
+ "free=" + Runtime.getRuntime().freeMemory() + "," + "free=" + Runtime.getRuntime().freeMemory() + ","
+ "total=" + Runtime.getRuntime().totalMemory()); + "total=" + Runtime.getRuntime().totalMemory());
} }
// clear out any temp directories if we can
if (!testsFailed) {
for (String path : tempDirs) {
try {
_TestUtil.rmDir(new File(path));
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }
private static boolean testsFailed; /* true if any tests failed */ private static boolean testsFailed; /* true if any tests failed */
@ -1063,6 +1077,7 @@ public abstract class LuceneTestCase extends Assert {
final File tmpFile = File.createTempFile("test", "tmp", TEMP_DIR); final File tmpFile = File.createTempFile("test", "tmp", TEMP_DIR);
tmpFile.delete(); tmpFile.delete();
tmpFile.mkdir(); tmpFile.mkdir();
tempDirs.add(tmpFile.getAbsolutePath());
return newFSDirectoryImpl(clazz.asSubclass(FSDirectory.class), tmpFile, null); return newFSDirectoryImpl(clazz.asSubclass(FSDirectory.class), tmpFile, null);
} }

View File

@ -54,7 +54,9 @@ public class _TestUtil {
/** Returns temp dir, containing String arg in its name; /** Returns temp dir, containing String arg in its name;
* does not create the directory. */ * does not create the directory. */
public static File getTempDir(String desc) { public static File getTempDir(String desc) {
return new File(LuceneTestCase.TEMP_DIR, desc + "." + new Random().nextLong()); File f = new File(LuceneTestCase.TEMP_DIR, desc + "." + new Random().nextLong());
LuceneTestCase.tempDirs.add(f.getAbsolutePath());
return f;
} }
/** /**
@ -89,6 +91,7 @@ public class _TestUtil {
rmDir(destDir); rmDir(destDir);
destDir.mkdir(); destDir.mkdir();
LuceneTestCase.tempDirs.add(destDir.getAbsolutePath());
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement(); ZipEntry entry = entries.nextElement();

View File

@ -57,8 +57,7 @@ public class TestCompoundFile extends LuceneTestCase
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
File file = new File(TEMP_DIR, "testIndex"); File file = _TestUtil.getTempDir("testIndex");
_TestUtil.rmDir(file);
// use a simple FSDir here, to be sure to have SimpleFSInputs // use a simple FSDir here, to be sure to have SimpleFSInputs
dir = new SimpleFSDirectory(file,null); dir = new SimpleFSDirectory(file,null);
} }
@ -66,7 +65,6 @@ public class TestCompoundFile extends LuceneTestCase
@Override @Override
public void tearDown() throws Exception { public void tearDown() throws Exception {
dir.close(); dir.close();
_TestUtil.rmDir(new File(TEMP_DIR, "testIndex"));
super.tearDown(); super.tearDown();
} }

View File

@ -36,6 +36,7 @@ import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.DocIdSetIterator; import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
import org.apache.lucene.index.codecs.CodecProvider; import org.apache.lucene.index.codecs.CodecProvider;
@ -60,10 +61,10 @@ public class TestDoc extends LuceneTestCase {
if (VERBOSE) { if (VERBOSE) {
System.out.println("TEST: setUp"); System.out.println("TEST: setUp");
} }
workDir = new File(TEMP_DIR,"TestDoc"); workDir = _TestUtil.getTempDir("TestDoc");
workDir.mkdirs(); workDir.mkdirs();
indexDir = new File(workDir, "testIndex"); indexDir = _TestUtil.getTempDir("testIndex");
indexDir.mkdirs(); indexDir.mkdirs();
Directory directory = newFSDirectory(indexDir); Directory directory = newFSDirectory(indexDir);

View File

@ -286,8 +286,7 @@ public class TestFieldsReader extends LuceneTestCase {
*/ */
public void testLazyPerformance() throws Exception { public void testLazyPerformance() throws Exception {
String userName = System.getProperty("user.name"); String userName = System.getProperty("user.name");
File file = new File(TEMP_DIR, "lazyDir" + userName); File file = _TestUtil.getTempDir("lazyDir" + userName);
_TestUtil.rmDir(file);
Directory tmpDir = newFSDirectory(file); Directory tmpDir = newFSDirectory(file);
assertTrue(tmpDir != null); assertTrue(tmpDir != null);
@ -473,7 +472,7 @@ public class TestFieldsReader extends LuceneTestCase {
// LUCENE-1262 // LUCENE-1262
public void testExceptions() throws Throwable { public void testExceptions() throws Throwable {
File indexDir = new File(TEMP_DIR, "testfieldswriterexceptions"); File indexDir = _TestUtil.getTempDir("testfieldswriterexceptions");
try { try {
Directory dir = new FaultyFSDirectory(indexDir); Directory dir = new FaultyFSDirectory(indexDir);

View File

@ -1139,7 +1139,7 @@ public class TestIndexReader extends LuceneTestCase
} }
public void testOpenReaderAfterDelete() throws IOException { public void testOpenReaderAfterDelete() throws IOException {
File dirFile = new File(TEMP_DIR, "deletetest"); File dirFile = _TestUtil.getTempDir("deletetest");
Directory dir = newFSDirectory(dirFile); Directory dir = newFSDirectory(dirFile);
try { try {
IndexReader.open(dir, false); IndexReader.open(dir, false);

View File

@ -1090,7 +1090,7 @@ public class TestIndexReaderReopen extends LuceneTestCase {
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
indexDir = new File(TEMP_DIR, "IndexReaderReopen"); indexDir = _TestUtil.getTempDir("IndexReaderReopen");
} }
public void testCloseOrig() throws Throwable { public void testCloseOrig() throws Throwable {

View File

@ -2755,7 +2755,7 @@ public class TestIndexWriter extends LuceneTestCase {
// Tests that if FSDir is opened w/ a NoLockFactory (or SingleInstanceLF), // Tests that if FSDir is opened w/ a NoLockFactory (or SingleInstanceLF),
// then IndexWriter ctor succeeds. Previously (LUCENE-2386) it failed // then IndexWriter ctor succeeds. Previously (LUCENE-2386) it failed
// when listAll() was called in IndexFileDeleter. // when listAll() was called in IndexFileDeleter.
Directory dir = newFSDirectory(new File(TEMP_DIR, "emptyFSDirNoLock"), NoLockFactory.getNoLockFactory()); Directory dir = newFSDirectory(_TestUtil.getTempDir("emptyFSDirNoLock"), NoLockFactory.getNoLockFactory());
new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random))).close(); new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random))).close();
dir.close(); dir.close();
} }

View File

@ -21,6 +21,7 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig.OpenMode; import org.apache.lucene.index.IndexWriterConfig.OpenMode;
@ -39,7 +40,7 @@ public class TestIndexWriterLockRelease extends LuceneTestCase {
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
if (this.__test_dir == null) { if (this.__test_dir == null) {
this.__test_dir = new File(TEMP_DIR, "testIndexWriter"); this.__test_dir = _TestUtil.getTempDir("testIndexWriter");
if (this.__test_dir.exists()) { if (this.__test_dir.exists()) {
throw new IOException("test directory \"" + this.__test_dir.getPath() + "\" already exists (please remove by hand)"); throw new IOException("test directory \"" + this.__test_dir.getPath() + "\" already exists (please remove by hand)");

View File

@ -41,7 +41,7 @@ public class TestIndexWriterOnJRECrash extends TestNRTThreads {
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
tempDir = File.createTempFile("jrecrash", "tmp", TEMP_DIR); tempDir = _TestUtil.getTempDir("jrecrash");
tempDir.delete(); tempDir.delete();
tempDir.mkdir(); tempDir.mkdir();
} }

View File

@ -67,7 +67,7 @@ public class TestLongPostings extends LuceneTestCase {
// Don't use _TestUtil.getTempDir so that we own the // Don't use _TestUtil.getTempDir so that we own the
// randomness (ie same seed will point to same dir): // randomness (ie same seed will point to same dir):
Directory dir = newFSDirectory(new File(LuceneTestCase.TEMP_DIR, "longpostings" + "." + random.nextLong())); Directory dir = newFSDirectory(_TestUtil.getTempDir("longpostings" + "." + random.nextLong()));
final int NUM_DOCS = (int) ((TEST_NIGHTLY ? 4e6 : (RANDOM_MULTIPLIER*2e4)) * (1+random.nextDouble())); final int NUM_DOCS = (int) ((TEST_NIGHTLY ? 4e6 : (RANDOM_MULTIPLIER*2e4)) * (1+random.nextDouble()));

View File

@ -240,7 +240,7 @@ public class TestBufferedIndexInput extends LuceneTestCase {
} }
public void testSetBufferSize() throws IOException { public void testSetBufferSize() throws IOException {
File indexDir = new File(TEMP_DIR, "testSetBufferSize"); File indexDir = _TestUtil.getTempDir("testSetBufferSize");
MockFSDirectory dir = new MockFSDirectory(indexDir, random); MockFSDirectory dir = new MockFSDirectory(indexDir, random);
try { try {
IndexWriter writer = new IndexWriter( IndexWriter writer = new IndexWriter(

View File

@ -42,7 +42,7 @@ public class TestDirectory extends LuceneTestCase {
// Test that different instances of FSDirectory can coexist on the same // Test that different instances of FSDirectory can coexist on the same
// path, can read, write, and lock files. // path, can read, write, and lock files.
public void testDirectInstantiation() throws Exception { public void testDirectInstantiation() throws Exception {
File path = new File(TEMP_DIR, "testDirectInstantiation"); File path = _TestUtil.getTempDir("testDirectInstantiation");
int sz = 3; int sz = 3;
Directory[] dirs = new Directory[sz]; Directory[] dirs = new Directory[sz];
@ -134,7 +134,7 @@ public class TestDirectory extends LuceneTestCase {
// LUCENE-1468 // LUCENE-1468
public void testFSDirectoryFilter() throws IOException { public void testFSDirectoryFilter() throws IOException {
checkDirectoryFilter(newFSDirectory(new File(TEMP_DIR,"test"))); checkDirectoryFilter(newFSDirectory(_TestUtil.getTempDir("test")));
} }
// LUCENE-1468 // LUCENE-1468
@ -151,7 +151,7 @@ public class TestDirectory extends LuceneTestCase {
// LUCENE-1468 // LUCENE-1468
public void testCopySubdir() throws Throwable { public void testCopySubdir() throws Throwable {
File path = new File(TEMP_DIR, "testsubdir"); File path = _TestUtil.getTempDir("testsubdir");
try { try {
path.mkdirs(); path.mkdirs();
new File(path, "subdir").mkdirs(); new File(path, "subdir").mkdirs();
@ -164,7 +164,7 @@ public class TestDirectory extends LuceneTestCase {
// LUCENE-1468 // LUCENE-1468
public void testNotDirectory() throws Throwable { public void testNotDirectory() throws Throwable {
File path = new File(TEMP_DIR, "testnotdir"); File path = _TestUtil.getTempDir("testnotdir");
Directory fsDir = new SimpleFSDirectory(path, null); Directory fsDir = new SimpleFSDirectory(path, null);
try { try {
IndexOutput out = fsDir.createOutput("afile"); IndexOutput out = fsDir.createOutput("afile");

View File

@ -41,7 +41,7 @@ public class TestMultiMMap extends LuceneTestCase {
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
workDir = new File(TEMP_DIR, "TestMultiMMap"); workDir = _TestUtil.getTempDir("TestMultiMMap");
workDir.mkdirs(); workDir.mkdirs();
} }

View File

@ -24,6 +24,7 @@ import java.io.ObjectOutputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
@ -49,7 +50,7 @@ public class TestRAMDirectory extends LuceneTestCase {
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
indexDir = new File(TEMP_DIR, "RAMDirIndex"); indexDir = _TestUtil.getTempDir("RAMDirIndex");
Directory dir = newFSDirectory(indexDir); Directory dir = newFSDirectory(indexDir);
IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig( IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(

View File

@ -20,6 +20,7 @@ package org.apache.lucene.store;
import java.io.File; import java.io.File;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
@ -59,7 +60,7 @@ public class TestWindowsMMap extends LuceneTestCase {
} }
private final static String storePathname = private final static String storePathname =
new File(TEMP_DIR,"testLuceneMmap").getAbsolutePath(); _TestUtil.getTempDir("testLuceneMmap").getAbsolutePath();
public void testMmapIndex() throws Exception { public void testMmapIndex() throws Exception {
// sometimes the directory is not cleaned by rmDir, because on Windows it // sometimes the directory is not cleaned by rmDir, because on Windows it