Skip to content

Commit 3f97699

Browse files
authored
Merge pull request #335 from scijava/close-handle-in-test
Fix FileHandleTest on Windows
2 parents 9554f8f + 0d3a59c commit 3f97699

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

src/test/java/org/scijava/io/handle/FileHandleTest.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import org.scijava.Context;
4747
import org.scijava.io.location.FileLocation;
4848
import org.scijava.io.location.Location;
49-
import org.scijava.util.PlatformUtils;
5049

5150
/**
5251
* Tests {@link FileHandle}.
@@ -76,22 +75,22 @@ public void testExists() throws IOException {
7675

7776
final File nonExistentFile = //
7877
File.createTempFile("FileHandleTest", "nonexistent-file");
79-
final boolean deleted = delete(nonExistentFile);
78+
assertTrue(nonExistentFile.delete());
79+
assertFalse(nonExistentFile.exists());
8080

8181
final FileLocation loc = new FileLocation(nonExistentFile);
82-
final DataHandle<?> handle = dhs.create(loc);
83-
assertTrue(handle instanceof FileHandle);
84-
if (deleted) {
82+
try (final DataHandle<?> handle = dhs.create(loc)) {
83+
assertTrue(handle instanceof FileHandle);
8584
assertFalse(handle.exists());
8685
assertEquals(-1, handle.length());
87-
}
8886

89-
handle.writeBoolean(true);
90-
assertTrue(handle.exists());
91-
assertEquals(1, handle.length());
87+
handle.writeBoolean(true);
88+
assertTrue(handle.exists());
89+
assertEquals(1, handle.length());
90+
}
9291

9392
// Clean up.
94-
delete(nonExistentFile);
93+
assertTrue(nonExistentFile.delete());
9594
}
9695

9796
@Test
@@ -120,7 +119,8 @@ public void testNotCreatedByRead() throws IOException {
120119
final File nonExistentFile = //
121120
File.createTempFile("FileHandleTest", "none xistent file");
122121
final URI uri = nonExistentFile.toURI();
123-
delete(nonExistentFile);
122+
assertTrue(nonExistentFile.delete());
123+
assertFalse(nonExistentFile.exists());
124124

125125
final FileLocation loc = new FileLocation(uri);
126126
try (final DataHandle<?> handle = dhs.create(loc)) {
@@ -134,15 +134,4 @@ public void testNotCreatedByRead() throws IOException {
134134
assertFalse(nonExistentFile.exists());
135135
// reading from the non-existing file should not create it!
136136
}
137-
138-
/** A workaround for Windows being bad at deleting files. */
139-
private boolean delete(final File file) {
140-
final boolean success = file.delete();
141-
if (!success) file.deleteOnExit();
142-
if (!PlatformUtils.isWindows()) {
143-
assertTrue(success);
144-
assertFalse(file.exists());
145-
}
146-
return success;
147-
}
148137
}

0 commit comments

Comments
 (0)