Ok I made these changes:
1) Renamed registerTempFile to registerTempDir and made it package-private
2) Fixed _TestUtil.rmDir to not fail if someone calls it with a 'file' and
not 'directory'
Here is the patch that I intend to commit:
Index:
lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java
===================================================================
---
lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java
(revision 1200269)
+++
lucene/src/test-framework/java/org/apache/lucene/util/LuceneTestCase.java
(working copy)
@@ -1152,9 +1152,14 @@
}
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());
}
@@ -1167,11 +1172,9 @@
final Class<? extends Directory> 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
Index: lucene/src/test-framework/java/org/apache/lucene/util/_TestUtil.java
===================================================================
--- lucene/src/test-framework/java/org/apache/lucene/util/_TestUtil.java
(revision 1200269)
+++ lucene/src/test-framework/java/org/apache/lucene/util/_TestUtil.java
(working copy)
@@ -58,7 +58,7 @@
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);
@@ -70,6 +70,9 @@
*/
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);
@@ -97,7 +100,7 @@
rmDir(destDir);
destDir.mkdir();
- LuceneTestCase.registerTempFile(destDir);
+ LuceneTestCase.registerTempDir(destDir);
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
Shai
On Thu, Nov 10, 2011 at 4:00 PM, Robert Muir <[email protected]> wrote:
> On Thu, Nov 10, 2011 at 8:53 AM, Shai Erera <[email protected]> wrote:
> > Hi
> >
> > I use Lucene's test-framework and I did this:
> >
> > File f = _TestUtil.createTempFile("test", "tmp", TEMP_DIR);
> > registerTempFile(f);
> >
> > However, the test fails on NPE thrown from
> > _TestUtil.rmDirjava.lang.RuntimeException: java.lang.NullPointerException
> >
>
> I think we could improve the javadocs here:
> registerTempFile should be renamed to registerTempDir (and probably be
> package-private).
>
> The usual idiom is to do:
> File tmpDir = _TestUtil.getTempDir("work"); // this 'registers' it for
> deletion, but does not make it
> // make the directory
> // put stuff in it
>
> if you then really need a unique temp file within that directory you
> would just call _TestUtil.createTempFile(..., tmpDir)
> so that it creates it in your already registered dir.
>
> --
> lucidimagination.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>