3232
3333package org .scijava .io .handle ;
3434
35+ import static org .junit .Assert .assertEquals ;
36+ import static org .junit .Assert .assertFalse ;
37+ import static org .junit .Assert .assertTrue ;
38+
3539import java .io .File ;
3640import java .io .FileOutputStream ;
3741import java .io .IOException ;
3842
39- import org .scijava . io . handle . DataHandle ;
40- import org .scijava .io . handle . FileHandle ;
43+ import org .junit . Test ;
44+ import org .scijava .Context ;
4145import org .scijava .io .location .FileLocation ;
4246import org .scijava .io .location .Location ;
4347
@@ -62,4 +66,27 @@ public Location createLocation() throws IOException {
6266 return new FileLocation (tmpFile );
6367 }
6468
69+ @ Test
70+ public void testExists () throws IOException {
71+ final Context ctx = new Context ();
72+ final DataHandleService dhs = ctx .service (DataHandleService .class );
73+
74+ final File nonExistentFile = //
75+ File .createTempFile ("FileHandleTest" , "nonexistent-file" );
76+ assertTrue (nonExistentFile .delete ());
77+ assertFalse (nonExistentFile .exists ());
78+
79+ final FileLocation loc = new FileLocation (nonExistentFile );
80+ final DataHandle <?> handle = dhs .create (loc );
81+ assertTrue (handle instanceof FileHandle );
82+ assertFalse (handle .exists ());
83+ assertEquals (-1 , handle .length ());
84+
85+ handle .writeBoolean (true );
86+ assertTrue (handle .exists ());
87+ assertEquals (1 , handle .length ());
88+
89+ // Clean up.
90+ assertTrue (nonExistentFile .delete ());
91+ }
6592}
0 commit comments