Skip to content

Commit

Permalink
Fix FileToDatasetConverter picking up non-existing files
Browse files Browse the repository at this point in the history
It turns out that io.canOpen actually returns true for a file like 'blobs.gif' even if it doesn't exist on the given path. FileToDatasetConverter should really only be accepting requests where the file exists (or the name ends with '.fake' for the special case of the fake file format).

The tests are adjusted to be testing for unsupported formats as well as non-existing but theoretically supported files.
  • Loading branch information
imagejan committed Aug 25, 2022
1 parent 1ec558a commit 02a513b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/main/java/io/scif/convert/FileToDatasetConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,17 @@ public boolean canConvert(Object src, Class<?> dest) {
return io != null &&
src != null &&
super.canConvert(src, dest) &&
io.canOpen(((File) src).getAbsolutePath()); // and/or ((File) src).exists()
(((File) src).getName().endsWith(".fake") || ((File) src).exists()) &&
io.canOpen(((File) src).getAbsolutePath());
}

@Override
public boolean canConvert(Object src, Type dest) {
return io != null &&
src != null &&
super.canConvert(src, dest) &&
io.canOpen(((File) src).getAbsolutePath()); // and/or ((File) src).exists()
(((File) src).getName().endsWith(".fake") || ((File) src).exists()) &&
io.canOpen(((File) src).getAbsolutePath());
}

@SuppressWarnings("unchecked")
Expand Down
14 changes: 8 additions & 6 deletions src/test/java/io/scif/convert/FileToDatasetConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,42 @@

public class FileToDatasetConverterTest {
private Context c;
private File nonexistentFile;
private File unsupportedFile;

@Before
public void setUp() throws IOException {
c = new Context();
nonexistentFile = Files.createTempFile("non-existent", ".file").toFile();
unsupportedFile = Files.createTempFile("non-existent", ".file").toFile();
}

@After
public void tearDown() {
c.dispose();
c = null;
nonexistentFile.delete();
unsupportedFile.delete();
}

@Test
public void testFileToDatasetConverter() {
final ConvertService convertService = c.service(ConvertService.class);
File imageFile = new File("image&pixelType=uint8&axes=X,Y,Z&lengths=256,128,32.fake");
File nonExistingGifFile = new File("nonexistent/path/blobs.gif");

Converter<?, ?> handler = convertService.getHandler(imageFile, Dataset.class);
Converter<?, ?> nonExistentFileHandler = convertService.getHandler(nonexistentFile, Dataset.class);
Converter<?, ?> nonExistentFileHandler = convertService.getHandler(unsupportedFile, Dataset.class);
// Make sure we got the right converter back
assertSame(FileToDatasetConverter.class, handler.getClass());
assertNull(nonExistentFileHandler);

// Test handler capabilities
assertTrue(handler.canConvert(imageFile, Dataset.class));
assertFalse(handler.canConvert((Object) null, Dataset.class));
assertFalse(handler.canConvert(nonexistentFile, Dataset.class));
assertFalse(handler.canConvert(unsupportedFile, Dataset.class));

// Make sure we can convert with ConvertService
assertTrue(convertService.supports(imageFile, Dataset.class));
assertFalse(convertService.supports(nonexistentFile, Dataset.class));
assertFalse(convertService.supports(unsupportedFile, Dataset.class));
assertFalse(convertService.supports(nonExistingGifFile, Dataset.class));

// Convert and check dimensions
Dataset dataset = convertService.convert(imageFile, Dataset.class);
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/io/scif/convert/StringToDatasetConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@

public class StringToDatasetConverterTest {
private Context c;
private String nonexistentPath;
private String unsupportedPath;

@Before
public void setUp() throws IOException {
c = new Context();
nonexistentPath = Files.createTempFile("non-existent", ".file").toString();
unsupportedPath = Files.createTempFile("non-existent", ".file").toString();
}

@After
public void tearDown() {
c.dispose();
c = null;
new File(nonexistentPath).delete();
new File(unsupportedPath).delete();
}

@Test
Expand All @@ -70,19 +70,19 @@ public void testFileToDatasetConverter() {
String imagePath = "image&pixelType=uint8&axes=X,Y,Z&lengths=256,128,32.fake";

Converter<?, ?> handler = convertService.getHandler(imagePath, Dataset.class);
Converter<?, ?> nonExistentFileHandler = convertService.getHandler(nonexistentPath, Dataset.class);
Converter<?, ?> nonExistentFileHandler = convertService.getHandler(unsupportedPath, Dataset.class);
// Make sure we got the right converter back
assertSame(StringToDatasetConverter.class, handler.getClass());
assertNull(nonExistentFileHandler);

// Test handler capabilities
assertTrue(handler.canConvert(imagePath, Dataset.class));
assertFalse(handler.canConvert((Object) null, Dataset.class));
assertFalse(handler.canConvert(nonexistentPath, Dataset.class));
assertFalse(handler.canConvert(unsupportedPath, Dataset.class));

// Make sure we can convert with ConvertService
assertTrue(convertService.supports(imagePath, Dataset.class));
assertFalse(convertService.supports(nonexistentPath, Dataset.class));
assertFalse(convertService.supports(unsupportedPath, Dataset.class));

// Convert and check dimensions
Dataset dataset = convertService.convert(imagePath, Dataset.class);
Expand Down

0 comments on commit 02a513b

Please sign in to comment.