diff --git a/lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java index 2091f85645a..4a7b01e41d3 100644 --- a/lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java +++ b/lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java @@ -1179,8 +1179,13 @@ public abstract class LuceneTestCase extends Assert { return d; } - /** Registers a temp file that will be deleted when tests are done. */ - public static void registerTempFile(File tmpFile) { + /** + * Registers a temp directory that will be deleted when tests are done. This + * is used by {@link _TestUtil#getTempDir(String)} and + * {@link _TestUtil#unzip(File, File)}, so you should call these methods when + * possible. + */ + static void registerTempDir(File tmpFile) { tempDirs.put(tmpFile.getAbsoluteFile(), Thread.currentThread().getStackTrace()); } @@ -1193,11 +1198,9 @@ public abstract class LuceneTestCase extends Assert { final Class clazz = Class.forName(clazzName).asSubclass(Directory.class); // If it is a FSDirectory type, try its ctor(File) if (FSDirectory.class.isAssignableFrom(clazz)) { - final File tmpFile = _TestUtil.createTempFile("test", "tmp", TEMP_DIR); - tmpFile.delete(); - tmpFile.mkdir(); - registerTempFile(tmpFile); - return newFSDirectoryImpl(clazz.asSubclass(FSDirectory.class), tmpFile); + final File dir = _TestUtil.getTempDir("index"); + dir.mkdirs(); // ensure it's created so we 'have' it. + return newFSDirectoryImpl(clazz.asSubclass(FSDirectory.class), dir); } // try empty ctor diff --git a/lucene/src/test-framework/java/org/apache/lucene/util/_TestUtil.java b/lucene/src/test-framework/java/org/apache/lucene/util/_TestUtil.java index d0fd0264186..d37c4ccd5dc 100644 --- a/lucene/src/test-framework/java/org/apache/lucene/util/_TestUtil.java +++ b/lucene/src/test-framework/java/org/apache/lucene/util/_TestUtil.java @@ -62,7 +62,7 @@ public class _TestUtil { try { File f = createTempFile(desc, "tmp", LuceneTestCase.TEMP_DIR); f.delete(); - LuceneTestCase.registerTempFile(f); + LuceneTestCase.registerTempDir(f); return f; } catch (IOException e) { throw new RuntimeException(e); @@ -74,6 +74,9 @@ public class _TestUtil { */ public static void rmDir(File dir) throws IOException { if (dir.exists()) { + if (dir.isFile() && !dir.delete()) { + throw new IOException("could not delete " + dir); + } for (File f : dir.listFiles()) { if (f.isDirectory()) { rmDir(f); @@ -101,7 +104,7 @@ public class _TestUtil { rmDir(destDir); destDir.mkdir(); - LuceneTestCase.registerTempFile(destDir); + LuceneTestCase.registerTempDir(destDir); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement();