Skip to content

Commit b5ad01a

Browse files
committed
Add test that reproduces #329
1 parent 0a6709e commit b5ad01a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
import static org.junit.Assert.assertEquals;
3636
import static org.junit.Assert.assertFalse;
3737
import static org.junit.Assert.assertTrue;
38+
import static org.junit.Assert.fail;
3839

3940
import java.io.File;
4041
import java.io.FileOutputStream;
4142
import java.io.IOException;
43+
import java.net.URI;
4244

4345
import org.junit.Test;
4446
import org.scijava.Context;
@@ -108,4 +110,28 @@ public void testNotCreatedByClose() throws IOException {
108110
handle.close();
109111
assertFalse(nonExistentFile.exists());
110112
}
113+
114+
@Test
115+
public void testNotCreatedByRead() throws IOException {
116+
final Context ctx = new Context();
117+
final DataHandleService dhs = ctx.service(DataHandleService.class);
118+
119+
final File nonExistentFile = //
120+
File.createTempFile("FileHandleTest", "none xistent file");
121+
final URI uri = nonExistentFile.toURI();
122+
assertTrue(nonExistentFile.delete());
123+
assertFalse(nonExistentFile.exists());
124+
125+
final FileLocation loc = new FileLocation(uri);
126+
try (final DataHandle<?> handle = dhs.create(loc)) {
127+
assertFalse(handle.exists());
128+
handle.read(); // this will fail as there is no underlying file!
129+
fail("Read successfully from non-existing file!");
130+
}
131+
catch (final IOException exc) {
132+
// should be thrown
133+
}
134+
assertFalse(nonExistentFile.exists());
135+
// reading from the non-existing file should not create it!
136+
}
111137
}

0 commit comments

Comments
 (0)