From 6a18630b5bb7ca8729ccca6f34124a4313b833ae Mon Sep 17 00:00:00 2001 From: John Bogovic Date: Tue, 24 Sep 2024 20:10:00 -0400 Subject: [PATCH] tests: fix TestExportImports and make more granular --- pom.xml | 15 ++++ .../saalfeldlab/n5/MetadataIoTests.java | 63 +++++++++----- .../saalfeldlab/n5/TestExportImports.java | 83 +++++++++++-------- .../n5/metadata/MetadataTests.java | 11 +-- 4 files changed, 108 insertions(+), 64 deletions(-) diff --git a/pom.xml b/pom.xml index 58ddc06b..c4ce941a 100644 --- a/pom.xml +++ b/pom.xml @@ -214,4 +214,19 @@ https://maven.scijava.org/content/groups/public + + + + + org.apache.maven.plugins + maven-surefire-plugin + + 1 + true + + + + + + diff --git a/src/test/java/org/janelia/saalfeldlab/n5/MetadataIoTests.java b/src/test/java/org/janelia/saalfeldlab/n5/MetadataIoTests.java index 587a1eeb..269c4842 100644 --- a/src/test/java/org/janelia/saalfeldlab/n5/MetadataIoTests.java +++ b/src/test/java/org/janelia/saalfeldlab/n5/MetadataIoTests.java @@ -34,6 +34,7 @@ import java.util.Optional; import java.util.stream.IntStream; +import org.codehaus.plexus.util.FileUtils; import org.janelia.saalfeldlab.n5.hdf5.N5HDF5Writer; import org.janelia.saalfeldlab.n5.ij.N5IJUtils; import org.janelia.saalfeldlab.n5.metadata.imagej.CosemToImagePlus; @@ -53,8 +54,9 @@ import org.janelia.saalfeldlab.n5.universe.metadata.N5SingleScaleMetadataParser; import org.janelia.saalfeldlab.n5.universe.metadata.N5ViewerMultiscaleMetadataParser; import org.janelia.saalfeldlab.n5.zarr.N5ZarrWriter; +import org.junit.AfterClass; import org.junit.Assert; -import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import ij.ImagePlus; @@ -65,7 +67,7 @@ public class MetadataIoTests { - static private String testDirPath = createTestDirPath("n5-test"); + private static String testDirPath = createTestDirPath("n5-ij-metatest-"); private static String createTestDirPath(String dirName) { try { return Files.createTempDirectory(dirName).toString(); @@ -75,13 +77,13 @@ private static String createTestDirPath(String dirName) { } static private String testBaseDatasetName = "/test/data"; - private ImagePlus imp2d; - private ImagePlus imp3d; - private ImagePlus imp4d; - private ImagePlus imp5d; + private static ImagePlus imp2d; + private static ImagePlus imp3d; + private static ImagePlus imp4d; + private static ImagePlus imp5d; - @Before - public void setUp() throws IOException + @BeforeClass + public static void setUp() throws IOException { final ArrayImgFactory< UnsignedByteType > factory = new ArrayImgFactory<>( new UnsignedByteType() ); @@ -108,6 +110,18 @@ public void setUp() throws IOException imp5d.getCalibration().pixelDepth = 1.1; } + @AfterClass + public static void tearDown() { + + try { + final File d = new File(testDirPath); + FileUtils.deleteDirectory(d); + d.delete(); + } catch (IOException e) { + e.printStackTrace(); + } + } + @Test public void testCustomMetadata() { @@ -117,7 +131,7 @@ public void testCustomMetadata() N5FSWriter n5 = null; try { - n5 = new N5FSWriter( testDirPath+ ".n5" ); + n5 = new N5FSWriter(new File(testDirPath, "testCustom.n5").getAbsolutePath()); n5.createDataset( "/testpath", new long[]{2,3,4}, new int[] {2,3,4}, DataType.UINT8 , new RawCompression() ); @@ -245,8 +259,9 @@ public void testCosem() @Test public void testH5() { - final N5HDF5Writer n5 = new N5HDF5Writer( testDirPath + ".h5", 32, 32, 32, 32, 32 ); - testAllMetadataTypes( n5 ); + final N5HDF5Writer h5 = new N5HDF5Writer( new File( testDirPath, "test.h5").getAbsolutePath(), 32, 32, 32, 32, 32 ); + testAllMetadataTypes( h5 ); + h5.close(); } @Test @@ -254,19 +269,21 @@ public void testZarr() { try { - final N5ZarrWriter n5 = new N5ZarrWriter( testDirPath+ ".zarr" ); - testAllMetadataTypes( n5 ); + final N5ZarrWriter zarr = new N5ZarrWriter(new File(testDirPath, "test.zarr").getAbsolutePath()); + testAllMetadataTypes( zarr ); - n5.remove( testBaseDatasetName + "/imp2d" ); - n5.remove( testBaseDatasetName + "/imp2_cosemd" ); - n5.remove( testBaseDatasetName + "/imp2_n5v" ); + zarr.remove( testBaseDatasetName + "/imp2d" ); + zarr.remove( testBaseDatasetName + "/imp2_cosemd" ); + zarr.remove( testBaseDatasetName + "/imp2_n5v" ); - n5.remove( testBaseDatasetName + "/imp3d" ); - n5.remove( testBaseDatasetName + "/imp3d_cosem" ); - n5.remove( testBaseDatasetName + "/imp3d_n5v" ); + zarr.remove( testBaseDatasetName + "/imp3d" ); + zarr.remove( testBaseDatasetName + "/imp3d_cosem" ); + zarr.remove( testBaseDatasetName + "/imp3d_n5v" ); - n5.remove( testBaseDatasetName + "/imp4d" ); - n5.remove( testBaseDatasetName + "/imp5d" ); + zarr.remove( testBaseDatasetName + "/imp4d" ); + zarr.remove( testBaseDatasetName + "/imp5d" ); + + zarr.close(); } catch ( final N5Exception e ) { @@ -281,7 +298,7 @@ public void testN5FileSystem() N5FSWriter n5; try { - n5 = new N5FSWriter( testDirPath+ ".n5" ); + n5 = new N5FSWriter(new File(testDirPath, "test.n5").getAbsolutePath()); testAllMetadataTypes( n5 ); n5.remove( testBaseDatasetName + "/imp2d" ); @@ -294,6 +311,8 @@ public void testN5FileSystem() n5.remove( testBaseDatasetName + "/imp4d" ); n5.remove( testBaseDatasetName + "/imp5d" ); + + n5.close(); } catch ( final N5Exception e ) { diff --git a/src/test/java/org/janelia/saalfeldlab/n5/TestExportImports.java b/src/test/java/org/janelia/saalfeldlab/n5/TestExportImports.java index 7b4d5182..502d5064 100644 --- a/src/test/java/org/janelia/saalfeldlab/n5/TestExportImports.java +++ b/src/test/java/org/janelia/saalfeldlab/n5/TestExportImports.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -28,9 +29,9 @@ import org.janelia.saalfeldlab.n5.universe.metadata.ome.ngff.v04.NgffSingleScaleMetadataParser; import org.janelia.saalfeldlab.n5.zarr.N5ZarrReader; import org.janelia.saalfeldlab.n5.zarr.N5ZarrWriter; -import org.junit.After; +import org.junit.AfterClass; import org.junit.Assert; -import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import ij.ImagePlus; @@ -49,10 +50,10 @@ public class TestExportImports { - private File baseDir; + private static File baseDir; - @Before - public void before() { + @BeforeClass + public static void setup() { final URL configUrl = RunImportExportTest.class.getResource("/plugins.config"); baseDir = new File(configUrl.getFile()).getParentFile(); @@ -65,20 +66,25 @@ public void before() { } } - @After - public void after() { + @AfterClass + public static void tearDown() { baseDir.delete(); } - private boolean deleteContainer(final String rootPath) { + private static boolean deleteContainer(final String rootPath) { - N5Writer n5w = new N5Factory().openWriter(rootPath); - return n5w.remove(); + try ( final N5Writer n5w = new N5Factory().openWriter(rootPath) ) { + return n5w.remove(); + } catch( N5Exception e ) { + e.printStackTrace(); + } + + return false; } @Test - public void testEmptyMeta() + public void testEmptyMeta() throws InterruptedException { final ImagePlus imp = NewImage.createImage("test", 8, 6, 2, 16, NewImage.FILL_NOISE); final String metaType = N5Importer.MetadataDefaultKey; @@ -127,7 +133,7 @@ public void test4dN5v() } @Test - public void testReadWriteParse() + public void testReadWriteParse() throws InterruptedException { final HashMap typeToExtension = new HashMap<>(); typeToExtension.put( "FILESYSTEM", "n5" ); @@ -152,11 +158,12 @@ public void testReadWriteParse() { for( final String metatype : metadataTypes ) { - final String n5RootPath = baseDir + "/test." + typeToExtension.get( containerType ); + final String n5RootPath = baseDir + "/test-" + metatype + "-" + bitDepth + "." + typeToExtension.get( containerType ); final String datasetBase = "/test_"+metatype+"_"+bitDepth; final String dataset = datasetBase; singleReadWriteParseTest( imp, n5RootPath, dataset, blockSizeString, metatype, compressionString, true ); + Thread.sleep(25); } } } @@ -249,12 +256,12 @@ public void singleReadWriteParseTest( final String blockSizeString, final String metadataType, final String compressionType, - final boolean testMeta ) + final boolean testMeta ) throws InterruptedException { singleReadWriteParseTest( imp, outputPath, dataset, blockSizeString, metadataType, compressionType, testMeta, true); } - public void singleReadWriteParseTest( + public static void singleReadWriteParseTest( final ImagePlus imp, final String outputPath, final String dataset, @@ -262,7 +269,7 @@ public void singleReadWriteParseTest( final String metadataType, final String compressionType, final boolean testMeta, - final boolean testData ) + final boolean testData ) throws InterruptedException { final N5ScalePyramidExporter writer = new N5ScalePyramidExporter(); writer.setOptions( imp, outputPath, dataset, N5ScalePyramidExporter.AUTO_FORMAT, blockSizeString, false, @@ -279,10 +286,19 @@ else if (metadataType.equals(N5Importer.MetadataOmeZarrKey) || metadataType.equa final String n5PathAndDataset = outputPath + readerDataset; + final File n5RootWritten = new File(outputPath); + assertTrue("root does not exist: " + outputPath, n5RootWritten.exists()); + if (outputPath.endsWith(".h5")) + assertTrue("hdf5 file exists", n5RootWritten.exists()); + else + assertTrue("n5 or zarr root is not a directory:" + outputPath, n5RootWritten.isDirectory()); + + Thread.sleep(25); final N5Importer reader = new N5Importer(); reader.setShow( false ); final List< ImagePlus > impList = reader.process( n5PathAndDataset, false ); + assertNotNull(String.format( "Failed to open image: %s %s ", outputPath, dataset ), impList); assertEquals( String.format( "%s %s one image opened ", outputPath, dataset ), 1, impList.size() ); final double EPS = 1e-9; @@ -293,8 +309,7 @@ else if (metadataType.equals(N5Importer.MetadataOmeZarrKey) || metadataType.equa assertEquals( String.format( "%s resolutions y", dataset ), imp.getCalibration().pixelHeight, impRead.getCalibration().pixelHeight, EPS ); assertEquals( String.format( "%s resolutions z", dataset ), imp.getCalibration().pixelDepth, impRead.getCalibration().pixelDepth, EPS ); - final boolean unitsEqual = impRead.getCalibration().getUnit().equals( imp.getCalibration().getUnit() ); - assertTrue( String.format( "%s units ", dataset ), unitsEqual ); + final boolean unitsEqual = impRead.getCalibration().getUnit().equals( imp.getCalibration().getUnit() ); assertTrue( String.format( "%s units ", dataset ), unitsEqual ); } if( testData ) @@ -311,19 +326,12 @@ else if (metadataType.equals(N5Importer.MetadataOmeZarrKey) || metadataType.equa assertTrue( String.format( "%s data ", dataset ), imagesEqual ); } - try { - final N5Writer n5w = new N5Factory().openWriter(outputPath); - n5w.remove(); - } catch (final N5Exception e) { - e.printStackTrace(); - } - impRead.close(); - + deleteContainer(outputPath); } @Test - public void testRgb() + public void testRgb() throws InterruptedException { final ImagePlus imp = NewImage.createRGBImage("test", 8, 6, 4, NewImage.FILL_NOISE); final String metaType = N5Importer.MetadataImageJKey; @@ -346,10 +354,14 @@ public void testMultiChannel() { for( final String suffix : new String[] { ".h5", ".n5", ".zarr" }) { - testMultiChannelHelper(N5Importer.MetadataN5ViewerKey, suffix); - testMultiChannelHelper(N5Importer.MetadataN5CosemKey, suffix); - testMultiChannelHelper(N5Importer.MetadataOmeZarrKey, suffix); - testMultiChannelHelper(N5Importer.MetadataImageJKey, suffix); + try { + testMultiChannelHelper(N5Importer.MetadataN5ViewerKey, suffix); + testMultiChannelHelper(N5Importer.MetadataN5CosemKey, suffix); + testMultiChannelHelper(N5Importer.MetadataOmeZarrKey, suffix); + testMultiChannelHelper(N5Importer.MetadataImageJKey, suffix); + } catch (InterruptedException e) { + e.printStackTrace(); + } } } @@ -563,11 +575,9 @@ public void reset() { } - public void testMultiChannelHelper( final String metatype, final String suffix ) + public void testMultiChannelHelper( final String metatype, final String suffix ) throws InterruptedException { final int bitDepth = 8; - - final String n5RootPath = baseDir + "/test_"+ metatype+"_dimCombos" + suffix; final String blockSizeString = "16"; final String compressionString = "raw"; @@ -591,8 +601,11 @@ public void testMultiChannelHelper( final String metatype, final String suffix ) if( nz > 1 ) imp.getCalibration().pixelDepth = 0.7; - final String dataset = String.format("/c%dz%dt%d", nc, nz, nt); + final String dimCode = String.format("c%dz%dt%d", nc, nz, nt); + final String n5RootPath = baseDir + "/test_" + metatype + "_" + dimCode + suffix; + final String dataset = String.format("/%s", dimCode); singleReadWriteParseTest( imp, n5RootPath, dataset, blockSizeString, metatype, compressionString, true, nc == 1 ); + Thread.sleep(25); } } } diff --git a/src/test/java/org/janelia/saalfeldlab/n5/metadata/MetadataTests.java b/src/test/java/org/janelia/saalfeldlab/n5/metadata/MetadataTests.java index 91d63165..bda8e672 100644 --- a/src/test/java/org/janelia/saalfeldlab/n5/metadata/MetadataTests.java +++ b/src/test/java/org/janelia/saalfeldlab/n5/metadata/MetadataTests.java @@ -12,10 +12,9 @@ import org.janelia.saalfeldlab.n5.universe.metadata.N5SingleScaleMetadataParser; import org.janelia.saalfeldlab.n5.universe.metadata.SpatialMetadata; import org.junit.Assert; -import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; -import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -30,20 +29,18 @@ public class MetadataTests { - N5FSReader n5; + private static N5FSReader n5; - @Before - public void setUp() throws IOException { + @BeforeClass + public static void setUp() throws IOException { final String n5Root = "src/test/resources/test.n5"; - File n5rootF = new File(n5Root); n5 = new N5FSReader(n5Root); } @Test public void testCosemMetadataMultiscale() { - final double eps = 1e-6; final N5MetadataParser[] parsers = new N5MetadataParser[]{new N5CosemMetadataParser()}; final N5MetadataParser[] grpparsers = new N5MetadataParser[]{new N5CosemMultiScaleMetadata.CosemMultiScaleParser()};