4646import org .scijava .Context ;
4747import org .scijava .io .location .FileLocation ;
4848import org .scijava .io .location .Location ;
49+ import org .scijava .util .PlatformUtils ;
4950
5051/**
5152 * Tests {@link FileHandle}.
@@ -75,21 +76,22 @@ public void testExists() throws IOException {
7576
7677 final File nonExistentFile = //
7778 File .createTempFile ("FileHandleTest" , "nonexistent-file" );
78- assertTrue (nonExistentFile .delete ());
79- assertFalse (nonExistentFile .exists ());
79+ final boolean deleted = delete (nonExistentFile );
8080
8181 final FileLocation loc = new FileLocation (nonExistentFile );
8282 final DataHandle <?> handle = dhs .create (loc );
8383 assertTrue (handle instanceof FileHandle );
84- assertFalse (handle .exists ());
85- assertEquals (-1 , handle .length ());
84+ if (deleted ) {
85+ assertFalse (handle .exists ());
86+ assertEquals (-1 , handle .length ());
87+ }
8688
8789 handle .writeBoolean (true );
8890 assertTrue (handle .exists ());
8991 assertEquals (1 , handle .length ());
9092
9193 // Clean up.
92- assertTrue ( nonExistentFile . delete () );
94+ delete (nonExistentFile );
9395 }
9496
9597 @ Test
@@ -118,8 +120,7 @@ public void testNotCreatedByRead() throws IOException {
118120 final File nonExistentFile = //
119121 File .createTempFile ("FileHandleTest" , "none xistent file" );
120122 final URI uri = nonExistentFile .toURI ();
121- assertTrue (nonExistentFile .delete ());
122- assertFalse (nonExistentFile .exists ());
123+ delete (nonExistentFile );
123124
124125 final FileLocation loc = new FileLocation (uri );
125126 try (final DataHandle <?> handle = dhs .create (loc )) {
@@ -133,4 +134,15 @@ public void testNotCreatedByRead() throws IOException {
133134 assertFalse (nonExistentFile .exists ());
134135 // reading from the non-existing file should not create it!
135136 }
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+ }
136148}
0 commit comments