Skip to content

Commit 297aae7

Browse files
committed
Ensure createTemporaryDirectory() can be called in a loop
When calling said method from the same code location multiple times, we should still get an empty temporary directory every time, even if there are still-open files in the directory of the default name. For Windows, emptying directories poses an insurmountable problem when they contain files that are still in use, therefore we have to fall back to creating new temporary directories. Identified by Jennyver Jenkins as a problem when building and testing ij1-patcher on Windows. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 9c9ac28 commit 297aae7

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/test/java/org/scijava/util/TestUtilsTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import static org.junit.Assert.assertTrue;
3535

3636
import java.io.File;
37+
import java.io.FileOutputStream;
3738
import java.io.IOException;
39+
import java.util.Arrays;
3840

3941
import org.junit.Test;
4042
import org.scijava.test.TestUtils;
@@ -63,4 +65,19 @@ public void testCreateTemporaryDirectory() throws IOException {
6365
assertTrue(!tmp3.getAbsolutePath().equals(tmp4.getAbsolutePath()));
6466

6567
}
68+
69+
@Test
70+
public void sameDirectoryTwice() throws IOException {
71+
final FileOutputStream[] out = new FileOutputStream[2];
72+
for (int i = 0; i < 2; i++) {
73+
final File tmp = TestUtils.createTemporaryDirectory("same-");
74+
assertTrue(tmp != null);
75+
final String[] list = tmp.list();
76+
assertTrue("Not null: " + Arrays.toString(list), list == null || list.length == 0);
77+
out[i] = new FileOutputStream(new File(tmp, "hello" + i + ".txt"));
78+
}
79+
for (final FileOutputStream stream : out) {
80+
if (stream != null) stream.close();
81+
}
82+
}
6683
}

0 commit comments

Comments
 (0)