diff --git a/src/test/java/io/scif/labeling/LabelingIOTest.java b/src/test/java/io/scif/labeling/LabelingIOTest.java index ceae747..381526d 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; @@ -53,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( @@ -74,10 +81,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 = mktemp(); + 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 +95,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 = mktemp(); + 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 +108,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)); + mktemp().toString(), new Example("a", 2.0, 1)); } @Test @@ -122,9 +129,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, mktemp().toString(), + new Example("a", 2.0, 1)); } @Test @@ -143,18 +149,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 }; @@ -219,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 118722a..7637416 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,8 @@ 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; @@ -47,40 +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, new File( - "src/test/resources/labeling/labelSaveTestSimple.tif").getAbsolutePath()); + 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, new File( - "src/test/resources/labeling/labelSaveTestComplex.tif") - .getAbsolutePath()); + Path outFile = Files.createTempFile(null, null); + final LabelingIOService labelingIOService = context.service(LabelingIOService.class); + labelingIOService.save(labeling, outFile.toString()); } @@ -102,12 +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, new File( - "src/test/resources/labeling/labelSaveTestSimpleMeta.tif") - .getAbsolutePath(), sources); + Path outFile = Files.createTempFile(null, null); + final LabelingIOService labelingIOService = context.service(LabelingIOService.class); + labelingIOService.saveWithMetaData(labeling, outFile.toString(), sources); } diff --git a/src/test/resources/labeling/labelSaveTestComplex.tif b/src/test/resources/labeling/labelSaveTestComplex.tif index 0dd20ac..6e81752 100644 Binary files a/src/test/resources/labeling/labelSaveTestComplex.tif and b/src/test/resources/labeling/labelSaveTestComplex.tif differ 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 2061dd0..331eaf8 100644 Binary files a/src/test/resources/labeling/labelSaveTestSimple.tif and b/src/test/resources/labeling/labelSaveTestSimple.tif differ 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 33688ee..ffc348d 100644 Binary files a/src/test/resources/labeling/labelSaveTestSimpleMeta.tif and b/src/test/resources/labeling/labelSaveTestSimpleMeta.tif differ