From 3300823568bd34c14d16451cfe222e877edeb742 Mon Sep 17 00:00:00 2001 From: Tom Burke Date: Wed, 27 Apr 2022 15:23:51 +0200 Subject: [PATCH 1/3] Fix change on running tests --- .../resources/labeling/labelSaveTestComplex.tif | Bin 889 -> 901 bytes .../labeling/labelSaveTestSimple.lbl.json | 2 +- .../resources/labeling/labelSaveTestSimple.tif | Bin 480 -> 483 bytes .../labeling/labelSaveTestSimpleMeta.lbl.json | 2 +- .../labeling/labelSaveTestSimpleMeta.tif | Bin 453 -> 456 bytes 5 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/resources/labeling/labelSaveTestComplex.tif b/src/test/resources/labeling/labelSaveTestComplex.tif index 0dd20ac92391dea7b89bf5b21739b6ef470cfef9..6e817520581ef1ad3ae66e030fdcd0dc138caa6b 100644 GIT binary patch delta 16 Xcmey#*2+F1ma%eUoHX;~wTwIfGtmWY delta 12 TcmZo=|H(EXmN9o@oHR249nJ(4 diff --git a/src/test/resources/labeling/labelSaveTestSimple.lbl.json b/src/test/resources/labeling/labelSaveTestSimple.lbl.json index 59fb5c0..78776db 100644 --- a/src/test/resources/labeling/labelSaveTestSimple.lbl.json +++ b/src/test/resources/labeling/labelSaveTestSimple.lbl.json @@ -1 +1 @@ -{"version":3,"numSets":4,"numSources":1,"indexImg":"labelSaveTestSimple.tif","labelMapping":{},"labelSets":{"0":[],"1":[13,42],"2":[1],"3":[13,42,1]},"metadata":{"a":"a","b":2.0,"c":1}} \ No newline at end of file +{"version":3,"numSets":4,"numSources":1,"indexImg":"labelSaveTestSimple.tif","labelMapping":{},"labelSets":{"0":[],"1":[13,42],"2":[1],"3":[13,42,1]}} \ No newline at end of file diff --git a/src/test/resources/labeling/labelSaveTestSimple.tif b/src/test/resources/labeling/labelSaveTestSimple.tif index 2061dd07f301c18b3c472bd2cc8626b1f5f50911..331eaf8b1da519475d65de5bbc51da65bbf6e0c4 100644 GIT binary patch delta 21 ccmaFB{Fr${EaQWTamtL$8=K-7nHd?F09ZN(MgRZ+ delta 17 YcmaFN{D65vEaUx&amtL08=K-70Y4iCJpcdz diff --git a/src/test/resources/labeling/labelSaveTestSimpleMeta.lbl.json b/src/test/resources/labeling/labelSaveTestSimpleMeta.lbl.json index 9414e64..76bc5bc 100644 --- a/src/test/resources/labeling/labelSaveTestSimpleMeta.lbl.json +++ b/src/test/resources/labeling/labelSaveTestSimpleMeta.lbl.json @@ -1 +1 @@ -{"version":3,"numSets":4,"numSources":1,"indexImg":"labelSaveTestSimpleMeta.tif","labelSets":{"0":[],"1":[13,42],"2":[1],"3":[13,42,1]},"metadata":{"one":1,"two":2,"three":3}} \ No newline at end of file +{"version":3,"numSets":4,"numSources":1,"indexImg":"labelSaveTestSimpleMeta.tif","labelMapping":{},"labelSets":{"0":[],"1":[13,42],"2":[1],"3":[13,42,1]},"metadata":{"one":1,"two":2,"three":3}} \ No newline at end of file diff --git a/src/test/resources/labeling/labelSaveTestSimpleMeta.tif b/src/test/resources/labeling/labelSaveTestSimpleMeta.tif index 33688ee9872ce0a8e063be7a2a51aac4ddc9c17b..ffc348dc1d9d9248a9aa653bc88425f4b57826ac 100644 GIT binary patch delta 16 XcmX@ge1dsGEaTCQap{cAj0{WwGxP++ delta 12 TcmX@Xe3W@YEaRb#ap{ZzAmRkO From a3078a2f62020cf41662034219946afd886a48ab Mon Sep 17 00:00:00 2001 From: Tom Burke Date: Wed, 27 Apr 2022 15:54:48 +0200 Subject: [PATCH 2/3] Change output of save to temp-files --- .../java/io/scif/labeling/LabelingIOTest.java | 32 ++++++------------- .../labeling/tutorials/E02_SaveLabeling.java | 12 +++---- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/src/test/java/io/scif/labeling/LabelingIOTest.java b/src/test/java/io/scif/labeling/LabelingIOTest.java index ceae747..ce5e57b 100644 --- a/src/test/java/io/scif/labeling/LabelingIOTest.java +++ b/src/test/java/io/scif/labeling/LabelingIOTest.java @@ -39,6 +39,7 @@ import java.io.Reader; import java.lang.reflect.Type; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.List; @@ -74,10 +75,10 @@ public void testEquality() throws IOException { final ImgLabeling imgLabeling = labelingIOService.load( "src/test/resources/labeling/labelSaveTestSimple", Integer.class, IntType.class); - labelingIOService.save(imgLabeling, - "src/test/resources/labeling/example1_sav"); + Path tempFile = Files.createTempFile(null, null); + labelingIOService.save(imgLabeling, tempFile.toString()); final ImgLabeling imgLabeling2 = labelingIOService.load( - "src/test/resources/labeling/example1_sav", Integer.class, IntType.class); + tempFile.toString(), Integer.class, IntType.class); Assert.assertEquals(imgLabeling.getMapping().getLabels(), imgLabeling2 .getMapping().getLabels()); } @@ -88,9 +89,10 @@ public void testEquality2() throws IOException { LabelingIOService.class); final ImgLabeling imgLabeling = labelingIOService.load( "src/test/resources/labeling/test", Integer.class, IntType.class); - labelingIOService.save(imgLabeling, "src/test/resources/labeling/test2"); + Path tempFile = Files.createTempFile(null, null); + labelingIOService.save(imgLabeling, tempFile.toString()); final ImgLabeling imgLabeling2 = labelingIOService.load( - "src/test/resources/labeling/test2", Integer.class, IntType.class); + tempFile.toString(), Integer.class, IntType.class); Assert.assertEquals(imgLabeling.getMapping().getLabels(), imgLabeling2 .getMapping().getLabels()); } @@ -100,8 +102,7 @@ public void saveLabelingWithMetadataPrimitiveTest() throws IOException { final ImgLabeling labeling = getSimpleImgLabeling(); context.getService(LabelingIOService.class).saveWithMetaData(labeling, - new File("src/test/resources/labeling/labelSaveTestSimple.tif") - .getAbsolutePath(), new Example("a", 2.0, 1)); + Files.createTempFile(null, null).toString(), new Example("a", 2.0, 1)); } @Test @@ -122,9 +123,8 @@ public void saveLabelingWithMetadataComplexTest() throws IOException { final ImgLabeling labeling = getComplexImgLabeling(); final LabelingIOService labelingIOService = context.getService( LabelingIOService.class); - labelingIOService.saveWithMetaData(labeling, new File( - "src/test/resources/labeling/labelSaveTestComplexMeta.tif") - .getAbsolutePath(), new Example("a", 2.0, 1)); + labelingIOService.saveWithMetaData(labeling, Files.createTempFile(null, null).toString(), + new Example("a", 2.0, 1)); } @Test @@ -143,18 +143,6 @@ public void loadLabelingWithMetadataComplexWithCodecTest() mapping.getMapping().getLabels()); } - @Test - public void t() throws IOException { - final GsonBuilder builder = new GsonBuilder(); - final Reader reader = Files.newBufferedReader(Paths.get( - "src/test/resources/labeling/labelSaveTestComplexMeta.lbl.json")); - final Type labelingDataType = - new TypeToken>() - {}.getType(); - final LabelingData labelingData = builder.create() - .fromJson(reader, labelingDataType); - } - private ImgLabeling getSimpleImgLabeling() { final Integer[] values1 = new Integer[] { 42, 13 }; final Integer[] values2 = new Integer[] { 1 }; diff --git a/src/test/java/io/scif/labeling/tutorials/E02_SaveLabeling.java b/src/test/java/io/scif/labeling/tutorials/E02_SaveLabeling.java index 118722a..b6535c7 100644 --- a/src/test/java/io/scif/labeling/tutorials/E02_SaveLabeling.java +++ b/src/test/java/io/scif/labeling/tutorials/E02_SaveLabeling.java @@ -34,6 +34,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -67,8 +68,7 @@ public void saveLabelingTest() throws IOException { // get the LabelingIO service from the context final LabelingIOService labelingIOService = context.getService( LabelingIOService.class); - labelingIOService.save(labeling, new File( - "src/test/resources/labeling/labelSaveTestSimple.tif").getAbsolutePath()); + labelingIOService.save(labeling, Files.createTempFile(null, null).toString()); } @@ -78,9 +78,7 @@ public void saveLabelingTest2() throws IOException { // get the LabelingIO service from the context final LabelingIOService labelingIOService = context.getService( LabelingIOService.class); - labelingIOService.save(labeling, new File( - "src/test/resources/labeling/labelSaveTestComplex.tif") - .getAbsolutePath()); + labelingIOService.save(labeling, Files.createTempFile(null, null).toString()); } @@ -105,9 +103,7 @@ public void saveLabelingWithMetaDataTest() throws IOException { // get the LabelingIO service from the context final LabelingIOService labelingIOService = context.getService( LabelingIOService.class); - labelingIOService.saveWithMetaData(labeling, new File( - "src/test/resources/labeling/labelSaveTestSimpleMeta.tif") - .getAbsolutePath(), sources); + labelingIOService.saveWithMetaData(labeling, Files.createTempFile(null, null).toString(), sources); } From ae74731e8b563d1d02bdb9d2093cf259ed9c936a Mon Sep 17 00:00:00 2001 From: Curtis Rueden Date: Thu, 7 Nov 2024 09:06:23 -0600 Subject: [PATCH 3/3] Clean up test code * Create SciJava context once per test class * Use a helper method for the temp file creation. * Delete temp files upon JVM shutdown. Sadly, the saveLabeling tests still do not assert anything. :-( --- .../java/io/scif/labeling/LabelingIOTest.java | 27 +++++++---- .../labeling/tutorials/E01_LoadLabeling.java | 46 +++++++++---------- .../labeling/tutorials/E02_SaveLabeling.java | 39 ++++++++-------- 3 files changed, 62 insertions(+), 50 deletions(-) diff --git a/src/test/java/io/scif/labeling/LabelingIOTest.java b/src/test/java/io/scif/labeling/LabelingIOTest.java index ce5e57b..381526d 100644 --- a/src/test/java/io/scif/labeling/LabelingIOTest.java +++ b/src/test/java/io/scif/labeling/LabelingIOTest.java @@ -54,20 +54,26 @@ import net.imglib2.type.numeric.integer.IntType; import net.imglib2.type.numeric.integer.UnsignedByteType; +import org.junit.AfterClass; import org.junit.Assert; -import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.scijava.Context; public class LabelingIOTest { - Context context; + private static Context context; - @Before - public void beforeTests() { + @BeforeClass + public static void setUp() { context = new Context(); } + @AfterClass + public static void tearDown() { + context.dispose(); + } + @Test public void testEquality() throws IOException { final LabelingIOService labelingIOService = context.getService( @@ -75,7 +81,7 @@ public void testEquality() throws IOException { final ImgLabeling imgLabeling = labelingIOService.load( "src/test/resources/labeling/labelSaveTestSimple", Integer.class, IntType.class); - Path tempFile = Files.createTempFile(null, null); + Path tempFile = mktemp(); labelingIOService.save(imgLabeling, tempFile.toString()); final ImgLabeling imgLabeling2 = labelingIOService.load( tempFile.toString(), Integer.class, IntType.class); @@ -89,7 +95,7 @@ public void testEquality2() throws IOException { LabelingIOService.class); final ImgLabeling imgLabeling = labelingIOService.load( "src/test/resources/labeling/test", Integer.class, IntType.class); - Path tempFile = Files.createTempFile(null, null); + Path tempFile = mktemp(); labelingIOService.save(imgLabeling, tempFile.toString()); final ImgLabeling imgLabeling2 = labelingIOService.load( tempFile.toString(), Integer.class, IntType.class); @@ -102,7 +108,7 @@ public void saveLabelingWithMetadataPrimitiveTest() throws IOException { final ImgLabeling labeling = getSimpleImgLabeling(); context.getService(LabelingIOService.class).saveWithMetaData(labeling, - Files.createTempFile(null, null).toString(), new Example("a", 2.0, 1)); + mktemp().toString(), new Example("a", 2.0, 1)); } @Test @@ -123,7 +129,7 @@ public void saveLabelingWithMetadataComplexTest() throws IOException { final ImgLabeling labeling = getComplexImgLabeling(); final LabelingIOService labelingIOService = context.getService( LabelingIOService.class); - labelingIOService.saveWithMetaData(labeling, Files.createTempFile(null, null).toString(), + labelingIOService.saveWithMetaData(labeling, mktemp().toString(), new Example("a", 2.0, 1)); } @@ -207,4 +213,9 @@ public int compareTo(final Example o) { } } + public static Path mktemp() throws IOException { + Path tempFile = Files.createTempFile(null, null); + tempFile.toFile().deleteOnExit(); + return tempFile; + } } diff --git a/src/test/java/io/scif/labeling/tutorials/E01_LoadLabeling.java b/src/test/java/io/scif/labeling/tutorials/E01_LoadLabeling.java index 18fdb9a..d2245ea 100644 --- a/src/test/java/io/scif/labeling/tutorials/E01_LoadLabeling.java +++ b/src/test/java/io/scif/labeling/tutorials/E01_LoadLabeling.java @@ -37,49 +37,47 @@ import net.imglib2.roi.labeling.ImgLabeling; import net.imglib2.type.numeric.integer.IntType; +import org.junit.AfterClass; import org.junit.Assert; -import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.scijava.Context; public class E01_LoadLabeling { - Context context; + private static Context context; - @Before - public void beforeTests() { + @BeforeClass + public static void setUp() { context = new Context(); } + @AfterClass + public static void tearDown() { + context.dispose(); + } + @Test public void loadBasicLabeling() throws IOException { - // get the LabelingIO service from the context - final LabelingIOService labelingIOService = context.getService( - LabelingIOService.class); - // load a JSON file with IntType labels - // the container contains an ImgLabeling of that type as well as an optional - // sourcemap - // the sourcemap is a mapping of a source img to a list of labels that where - // contained in it and added to - // the ImgLabeling - - final ImgLabeling imgLabeling = labelingIOService.load( - "src/test/resources/labeling/labelSaveTestSimple.lbl.json", Integer.class, - IntType.class); + final LabelingIOService labelingIOService = context.service(LabelingIOService.class); + // Load a JSON file with IntType labels. + // The container contains an ImgLabeling of that type as well as an + // optional sourcemap. The sourcemap is a mapping of a source img to a + // list of labels that were contained in it and added to the ImgLabeling. + String inPath = "src/test/resources/labeling/labelSaveTestSimple.lbl.json"; + final ImgLabeling imgLabeling = + labelingIOService.load(inPath, Integer.class, IntType.class); Assert.assertNotNull(imgLabeling); Assert.assertNotNull(imgLabeling.getIndexImg()); Assert.assertFalse(imgLabeling.getMapping().getLabels().isEmpty()); - } @Test public void loadClassBasedLabeling() throws IOException { - // get the LabelingIO service from the context - final LabelingIOService labelingIOService = context.getService( - LabelingIOService.class); - final Container container = labelingIOService - .loadWithMetadata("src/test/resources/labeling/labelSaveTestComplex", - Example.class, Example.class, IntType.class); + final LabelingIOService labelingIOService = context.service(LabelingIOService.class); + final String inPath = "src/test/resources/labeling/labelSaveTestComplex"; + final Container container = + labelingIOService.loadWithMetadata(inPath, Example.class, Example.class, IntType.class); final ImgLabeling mapping = container.getImgLabeling(); Assert.assertNotNull(mapping); } diff --git a/src/test/java/io/scif/labeling/tutorials/E02_SaveLabeling.java b/src/test/java/io/scif/labeling/tutorials/E02_SaveLabeling.java index b6535c7..7637416 100644 --- a/src/test/java/io/scif/labeling/tutorials/E02_SaveLabeling.java +++ b/src/test/java/io/scif/labeling/tutorials/E02_SaveLabeling.java @@ -35,6 +35,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -48,37 +49,40 @@ import net.imglib2.type.numeric.integer.IntType; import net.imglib2.type.numeric.integer.UnsignedByteType; -import org.junit.Before; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import org.scijava.Context; public class E02_SaveLabeling { - Context context; + private static Context context; - @Before - public void beforeTests() { + @BeforeClass + public static void setUp() { context = new Context(); } + @AfterClass + public static void tearDown() { + context.dispose(); + } + @Test public void saveLabelingTest() throws IOException { - final ImgLabeling labeling = - getSimpleImgLabeling(); - // get the LabelingIO service from the context - final LabelingIOService labelingIOService = context.getService( - LabelingIOService.class); - labelingIOService.save(labeling, Files.createTempFile(null, null).toString()); + final ImgLabeling labeling = getSimpleImgLabeling(); + Path outFile = Files.createTempFile(null, null); + final LabelingIOService labelingIOService = context.service(LabelingIOService.class); + labelingIOService.save(labeling, outFile.toString()); } @Test public void saveLabelingTest2() throws IOException { final ImgLabeling labeling = getComplexImgLabeling(); - // get the LabelingIO service from the context - final LabelingIOService labelingIOService = context.getService( - LabelingIOService.class); - labelingIOService.save(labeling, Files.createTempFile(null, null).toString()); + Path outFile = Files.createTempFile(null, null); + final LabelingIOService labelingIOService = context.service(LabelingIOService.class); + labelingIOService.save(labeling, outFile.toString()); } @@ -100,10 +104,9 @@ public void saveLabelingWithMetaDataTest() throws IOException { container.setMetadata(sources); - // get the LabelingIO service from the context - final LabelingIOService labelingIOService = context.getService( - LabelingIOService.class); - labelingIOService.saveWithMetaData(labeling, Files.createTempFile(null, null).toString(), sources); + Path outFile = Files.createTempFile(null, null); + final LabelingIOService labelingIOService = context.service(LabelingIOService.class); + labelingIOService.saveWithMetaData(labeling, outFile.toString(), sources); }