rename LTC.registerTempFile to registerTempDir, clarified javadocs. Also fixed _TestUtil.rmDir() to not throw NPE if a 'file' is given

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1200403 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shai Erera 2011-11-10 16:16:50 +00:00
parent edb422e30b
commit 969afcf642
2 changed files with 15 additions and 9 deletions

View File

@ -1179,8 +1179,13 @@ public abstract class LuceneTestCase extends Assert {
return d; 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()); tempDirs.put(tmpFile.getAbsoluteFile(), Thread.currentThread().getStackTrace());
} }
@ -1193,11 +1198,9 @@ public abstract class LuceneTestCase extends Assert {
final Class<? extends Directory> clazz = Class.forName(clazzName).asSubclass(Directory.class); final Class<? extends Directory> clazz = Class.forName(clazzName).asSubclass(Directory.class);
// If it is a FSDirectory type, try its ctor(File) // If it is a FSDirectory type, try its ctor(File)
if (FSDirectory.class.isAssignableFrom(clazz)) { if (FSDirectory.class.isAssignableFrom(clazz)) {
final File tmpFile = _TestUtil.createTempFile("test", "tmp", TEMP_DIR); final File dir = _TestUtil.getTempDir("index");
tmpFile.delete(); dir.mkdirs(); // ensure it's created so we 'have' it.
tmpFile.mkdir(); return newFSDirectoryImpl(clazz.asSubclass(FSDirectory.class), dir);
registerTempFile(tmpFile);
return newFSDirectoryImpl(clazz.asSubclass(FSDirectory.class), tmpFile);
} }
// try empty ctor // try empty ctor

View File

@ -62,7 +62,7 @@ public class _TestUtil {
try { try {
File f = createTempFile(desc, "tmp", LuceneTestCase.TEMP_DIR); File f = createTempFile(desc, "tmp", LuceneTestCase.TEMP_DIR);
f.delete(); f.delete();
LuceneTestCase.registerTempFile(f); LuceneTestCase.registerTempDir(f);
return f; return f;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -74,6 +74,9 @@ public class _TestUtil {
*/ */
public static void rmDir(File dir) throws IOException { public static void rmDir(File dir) throws IOException {
if (dir.exists()) { if (dir.exists()) {
if (dir.isFile() && !dir.delete()) {
throw new IOException("could not delete " + dir);
}
for (File f : dir.listFiles()) { for (File f : dir.listFiles()) {
if (f.isDirectory()) { if (f.isDirectory()) {
rmDir(f); rmDir(f);
@ -101,7 +104,7 @@ public class _TestUtil {
rmDir(destDir); rmDir(destDir);
destDir.mkdir(); destDir.mkdir();
LuceneTestCase.registerTempFile(destDir); LuceneTestCase.registerTempDir(destDir);
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement(); ZipEntry entry = entries.nextElement();