From a31d5a8a9848d7dca19caa6d0157d3577340922c Mon Sep 17 00:00:00 2001 From: Matthias Arzt Date: Thu, 11 Jan 2024 15:14:52 +0100 Subject: [PATCH] Unit tests: add class TestResources for querying paths of resource files --- .../java/demo/LabelingComponentHdf5Demo.java | 3 ++- .../ui/inputimage/SpimDataToImgPlusTest.java | 8 +++--- ...ProbabilityMapWithLabkitIJ1PluginTest.java | 15 +++++------ ...ateProbabilityMapWithLabkitPluginTest.java | 12 +++------ ...lityMapForImagesInDirectoryPluginTest.java | 14 ++++------ ...kitSegmentImagesInDirectoryPluginTest.java | 12 +++------ .../SegmentImageWithLabkitIJ1PluginTest.java | 15 +++++------ .../SegmentImageWithLabkitPluginTest.java | 12 +++------ .../labkit/ui/plugin/SegmentRGBImageTest.java | 27 +++++++------------ .../ui/segmentation/SegmentationToolTest.java | 13 ++++----- .../fiji/labkit/ui/utils/TestResources.java | 10 ++++++- 11 files changed, 59 insertions(+), 82 deletions(-) diff --git a/src/test/java/demo/LabelingComponentHdf5Demo.java b/src/test/java/demo/LabelingComponentHdf5Demo.java index e48dc21a..efffa87d 100644 --- a/src/test/java/demo/LabelingComponentHdf5Demo.java +++ b/src/test/java/demo/LabelingComponentHdf5Demo.java @@ -34,6 +34,7 @@ import sc.fiji.labkit.ui.LabelingComponent; import sc.fiji.labkit.ui.inputimage.SpimDataInputImage; import sc.fiji.labkit.ui.models.ImageLabelingModel; +import sc.fiji.labkit.ui.utils.TestResources; import javax.swing.*; import java.awt.event.WindowAdapter; @@ -46,7 +47,7 @@ public class LabelingComponentHdf5Demo { public static void main(String... args) throws SpimDataException { JFrame frame = initFrame(); - final String fn = LabelingComponentHdf5Demo.class.getResource("/export.xml").getPath(); + final String fn = TestResources.fullPath("/export.xml"); frame.add(initLabelingComponent(frame, fn)); frame.setVisible(true); } diff --git a/src/test/java/sc/fiji/labkit/ui/inputimage/SpimDataToImgPlusTest.java b/src/test/java/sc/fiji/labkit/ui/inputimage/SpimDataToImgPlusTest.java index 1253b65d..421899d8 100644 --- a/src/test/java/sc/fiji/labkit/ui/inputimage/SpimDataToImgPlusTest.java +++ b/src/test/java/sc/fiji/labkit/ui/inputimage/SpimDataToImgPlusTest.java @@ -40,6 +40,7 @@ import net.imglib2.type.numeric.integer.UnsignedShortType; import net.imglib2.util.Cast; import org.junit.Test; +import sc.fiji.labkit.ui.utils.TestResources; import java.util.Arrays; import java.util.stream.IntStream; @@ -50,7 +51,7 @@ public class SpimDataToImgPlusTest { @Test public void testOpen() { - String filename = SpimDataToImgPlusTest.class.getResource("/export.xml").getPath(); + String filename = TestResources.fullPath("/export.xml"); ImgPlus result = Cast.unchecked(SpimDataToImgPlus.open(filename, 0)); assertEquals(Arrays.asList(Axes.X, Axes.Y, Axes.Z, Axes.CHANNEL, Axes.TIME), ImgPlusViewsOld .getAxes(result)); @@ -60,14 +61,13 @@ public void testOpen() { @Test(expected = SpimDataInputException.class) public void testExceptionForMultipleAngles() { - String filename = SpimDataToImgPlusTest.class.getResource("/multi-angle-dataset.xml").getPath(); + String filename = TestResources.fullPath("/multi-angle-dataset.xml"); SpimDataToImgPlus.open(filename, 0); } @Test(expected = SpimDataInputException.class) public void testExceptionForSizeMismatch() { - String filename = SpimDataToImgPlusTest.class.getResource("/size-mismatch-dataset.xml") - .getPath(); + String filename = TestResources.fullPath("/size-mismatch-dataset.xml"); SpimDataToImgPlus.open(filename, 0); } diff --git a/src/test/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1PluginTest.java b/src/test/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1PluginTest.java index 60ee404e..6f9aac98 100644 --- a/src/test/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1PluginTest.java +++ b/src/test/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitIJ1PluginTest.java @@ -12,15 +12,16 @@ import net.imglib2.test.ImgLib2Assert; import org.junit.Test; import sc.fiji.labkit.pixel_classification.utils.SingletonContext; +import sc.fiji.labkit.ui.utils.TestResources; public class CalculateProbabilityMapWithLabkitIJ1PluginTest { @Test public void test() throws IOException { SingletonContext.getInstance(); - String inputImage = fullPath("/blobs.tif"); - String blobsModel = fullPath("/blobs.classifier"); - String source = fullPath("/blobs_probability_map.tif"); + String inputImage = TestResources.fullPath("/blobs.tif"); + String blobsModel = TestResources.fullPath("/blobs.classifier"); + String source = TestResources.fullPath("/blobs_probability_map.tif"); File outputImage = File.createTempFile("labkit-segmentation-test", ".tif"); String macroTemplate = "close('*');\n" + "open('INPUT_TIF');\n" + @@ -37,11 +38,7 @@ public void test() throws IOException { assertTrue(outputImage.exists()); ImagePlus expected = IJ.openImage(source); ImagePlus result = IJ.openImage(outputImage.getAbsolutePath()); - ImgLib2Assert.assertImageEquals(VirtualStackAdapter.wrap(expected), VirtualStackAdapter.wrap(result), Object::equals); - } - - private String fullPath(String name) { - return SegmentImageWithLabkitPluginTest.class.getResource( - name).getFile(); + ImgLib2Assert.assertImageEquals(VirtualStackAdapter.wrap(expected), + VirtualStackAdapter.wrap(result), Object::equals); } } diff --git a/src/test/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitPluginTest.java b/src/test/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitPluginTest.java index 63efa8b0..f55b4c2e 100644 --- a/src/test/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitPluginTest.java +++ b/src/test/java/sc/fiji/labkit/ui/plugin/CalculateProbabilityMapWithLabkitPluginTest.java @@ -34,6 +34,7 @@ import net.imglib2.test.ImgLib2Assert; import org.junit.Test; import sc.fiji.labkit.pixel_classification.utils.SingletonContext; +import sc.fiji.labkit.ui.utils.TestResources; public class CalculateProbabilityMapWithLabkitPluginTest { @@ -41,9 +42,9 @@ public class CalculateProbabilityMapWithLabkitPluginTest { public void test() throws Exception { // setup ImageJ imageJ = new ImageJ(SingletonContext.getInstance()); - Dataset image = imageJ.scifio().datasetIO().open(fullPath("/blobs.tif")); - String blobsModel = fullPath("/blobs.classifier"); - Dataset expectedImage = imageJ.scifio().datasetIO().open(fullPath( + Dataset image = imageJ.scifio().datasetIO().open(TestResources.fullPath("/blobs.tif")); + String blobsModel = TestResources.fullPath("/blobs.classifier"); + Dataset expectedImage = imageJ.scifio().datasetIO().open(TestResources.fullPath( "/blobs_probability_map.tif")); // process Dataset output = (Dataset) imageJ.command().run(CalculateProbabilityMapWithLabkitPlugin.class, @@ -55,9 +56,4 @@ public void test() throws Exception { // test ImgLib2Assert.assertImageEqualsRealType(expectedImage, output, 0.0); } - - private String fullPath(String name) { - return CalculateProbabilityMapWithLabkitPluginTest.class.getResource( - name).getFile(); - } } diff --git a/src/test/java/sc/fiji/labkit/ui/plugin/LabkitProbabilityMapForImagesInDirectoryPluginTest.java b/src/test/java/sc/fiji/labkit/ui/plugin/LabkitProbabilityMapForImagesInDirectoryPluginTest.java index 39fea341..0a3c3098 100644 --- a/src/test/java/sc/fiji/labkit/ui/plugin/LabkitProbabilityMapForImagesInDirectoryPluginTest.java +++ b/src/test/java/sc/fiji/labkit/ui/plugin/LabkitProbabilityMapForImagesInDirectoryPluginTest.java @@ -43,6 +43,7 @@ import org.scijava.io.location.FileLocation; import org.scijava.plugin.Parameter; import sc.fiji.labkit.pixel_classification.utils.SingletonContext; +import sc.fiji.labkit.ui.utils.TestResources; import java.io.File; import java.io.IOException; @@ -81,7 +82,7 @@ public void test() "file_filter", "*.tif", "output_directory", outputDirectory, "output_file_suffix", "_probability_map.tif", - "segmenter_file", fullPath("/leaf.classifier"), + "segmenter_file", TestResources.fullPath("/leaf.classifier"), "use_gpu", false).get(); testOutputDirectory(outputDirectory); } @@ -89,7 +90,7 @@ public void test() private File createTestInputDirectory() throws IOException { File folder = Files.createTempDirectory("labkit-test-input").toFile(); RandomAccessibleInterval image = - Cast.unchecked(io.open(fullPath("/leaf.tif"))); + Cast.unchecked(io.open(TestResources.fullPath("/leaf.tif"))); for (int i = 0; i < 4; i++) { File file = new File(folder, "image" + i + ".tif"); saveImage(image, file); @@ -99,8 +100,8 @@ private File createTestInputDirectory() throws IOException { } private void testOutputDirectory(File folder) throws IOException { - RandomAccessibleInterval> expected = io.open(fullPath( - "/leaf_probability_map.tif")); + RandomAccessibleInterval> expected = io.open( + TestResources.fullPath("/leaf_probability_map.tif")); for (int i = 0; i < 4; i++) { File file = new File(folder, "image" + i + "_probability_map.tif"); assertTrue("Expected output file is missing: " + file, file.exists()); @@ -121,9 +122,4 @@ private void saveImage(RandomAccessibleInterval image, File fi dataset.axis(2).setType(Axes.CHANNEL); io.save(dataset, new FileLocation(file)); } - - private String fullPath(String name) { - return this.getClass().getResource(name).getFile(); - } - } diff --git a/src/test/java/sc/fiji/labkit/ui/plugin/LabkitSegmentImagesInDirectoryPluginTest.java b/src/test/java/sc/fiji/labkit/ui/plugin/LabkitSegmentImagesInDirectoryPluginTest.java index 28dabf13..ffac6990 100644 --- a/src/test/java/sc/fiji/labkit/ui/plugin/LabkitSegmentImagesInDirectoryPluginTest.java +++ b/src/test/java/sc/fiji/labkit/ui/plugin/LabkitSegmentImagesInDirectoryPluginTest.java @@ -46,6 +46,7 @@ import org.scijava.io.location.FileLocation; import org.scijava.plugin.Parameter; import sc.fiji.labkit.pixel_classification.utils.SingletonContext; +import sc.fiji.labkit.ui.utils.TestResources; import java.io.File; import java.io.IOException; @@ -83,7 +84,7 @@ public void test() "file_filter", "*.tif", "output_directory", outputDirectory, "output_file_suffix", "_segmentation.tif", - "segmenter_file", fullPath("/leaf.classifier"), + "segmenter_file", TestResources.fullPath("/leaf.classifier"), "use_gpu", false).get(); testOutputDirectory(outputDirectory); } @@ -91,7 +92,7 @@ public void test() private File createTestInputDirectory() throws IOException { File folder = Files.createTempDirectory("labkit-test-input").toFile(); RandomAccessibleInterval image = - Cast.unchecked(io.open(fullPath("/leaf.tif"))); + Cast.unchecked(io.open(TestResources.fullPath("/leaf.tif"))); for (int i = 0; i < 4; i++) { File file = new File(folder, "image" + i + ".tif"); saveImage(image, file); @@ -101,7 +102,7 @@ private File createTestInputDirectory() throws IOException { } private void testOutputDirectory(File folder) throws IOException { - RandomAccessibleInterval> expected = io.open(fullPath( + RandomAccessibleInterval> expected = io.open(TestResources.fullPath( "/leaf_segmentation.tif")); for (int i = 0; i < 4; i++) { File file = new File(folder, "image" + i + "_segmentation.tif"); @@ -134,9 +135,4 @@ private void assertAlmostIdentical(RandomAccessibleInterval segmentation = tool.segment(image); @@ -70,7 +70,7 @@ public void testSegment() { public void testProbabilityMap() { SegmentationTool tool = new SegmentationTool(); tool.setUseGpu(false); - tool.openModel(fullPath("/leaf.classifier")); + tool.openModel(TestResources.fullPath("/leaf.classifier")); ImgPlus probabilityMap = tool.probabilityMap(image); ImgPlus expectedProbabilityMap = Cast.unchecked(openImage("/leaf_probability_map.tif")); assertImageEqualsRealType(Cast.unchecked(expectedProbabilityMap), probabilityMap, 0.0001); @@ -78,15 +78,12 @@ public void testProbabilityMap() { private ImgPlus openImage(String file) { try { - return SingletonContext.getInstance().service(DatasetIOService.class).open(fullPath(file)) + return SingletonContext.getInstance().service(DatasetIOService.class).open( + TestResources.fullPath(file)) .getImgPlus(); } catch (IOException e) { throw new RuntimeException(e); } } - - private String fullPath(String name) { - return this.getClass().getResource(name).getFile(); - } } diff --git a/src/test/java/sc/fiji/labkit/ui/utils/TestResources.java b/src/test/java/sc/fiji/labkit/ui/utils/TestResources.java index ed1a0cc7..709b205d 100644 --- a/src/test/java/sc/fiji/labkit/ui/utils/TestResources.java +++ b/src/test/java/sc/fiji/labkit/ui/utils/TestResources.java @@ -1,2 +1,10 @@ -package sc.fiji.labkit.ui.utils;public class TestResources { +package sc.fiji.labkit.ui.utils; + +import sc.fiji.labkit.ui.plugin.SegmentImageWithLabkitPluginTest; + +public class TestResources { + + public static String fullPath(String name) { + return TestResources.class.getResource(name).getFile(); + } }