Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FileToDatasetConverter picking up non-existing files #479

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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