|
35 | 35 | import static org.junit.Assert.assertEquals;
|
36 | 36 | import static org.junit.Assert.assertFalse;
|
37 | 37 | import static org.junit.Assert.assertTrue;
|
| 38 | +import static org.junit.Assert.fail; |
38 | 39 |
|
39 | 40 | import java.io.File;
|
40 | 41 | import java.io.FileOutputStream;
|
41 | 42 | import java.io.IOException;
|
| 43 | +import java.net.URI; |
42 | 44 |
|
43 | 45 | import org.junit.Test;
|
44 | 46 | import org.scijava.Context;
|
@@ -108,4 +110,28 @@ public void testNotCreatedByClose() throws IOException {
|
108 | 110 | handle.close();
|
109 | 111 | assertFalse(nonExistentFile.exists());
|
110 | 112 | }
|
| 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 | + } |
111 | 137 | }
|
0 commit comments