Skip to content

Commit

Permalink
Merge pull request #8 from tomburke-rse/fix_test_filechange
Browse files Browse the repository at this point in the history
Fix test filechange
  • Loading branch information
ctrueden authored Nov 7, 2024
2 parents 6770b38 + ae74731 commit ec726ff
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 75 deletions.
51 changes: 25 additions & 26 deletions src/test/java/io/scif/labeling/LabelingIOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -53,31 +54,37 @@
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(
LabelingIOService.class);
final ImgLabeling<Integer, IntType> 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<Integer, IntType> 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());
}
Expand All @@ -88,9 +95,10 @@ public void testEquality2() throws IOException {
LabelingIOService.class);
final ImgLabeling<Integer, IntType> 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<Integer, IntType> 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());
}
Expand All @@ -100,8 +108,7 @@ public void saveLabelingWithMetadataPrimitiveTest() throws IOException {
final ImgLabeling<Integer, UnsignedByteType> 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
Expand All @@ -122,9 +129,8 @@ public void saveLabelingWithMetadataComplexTest() throws IOException {
final ImgLabeling<Example, IntType> 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
Expand All @@ -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<LabelingData<Example, Example>>()
{}.getType();
final LabelingData<Example, Example> labelingData = builder.create()
.fromJson(reader, labelingDataType);
}

private ImgLabeling<Integer, UnsignedByteType> getSimpleImgLabeling() {
final Integer[] values1 = new Integer[] { 42, 13 };
final Integer[] values2 = new Integer[] { 1 };
Expand Down Expand Up @@ -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;
}
}
46 changes: 22 additions & 24 deletions src/test/java/io/scif/labeling/tutorials/E01_LoadLabeling.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, IntType> 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<Integer, IntType> 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<Example, Example, IntType> 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<Example, Example, IntType> container =
labelingIOService.loadWithMetadata(inPath, Example.class, Example.class, IntType.class);
final ImgLabeling<Example, IntType> mapping = container.getImgLabeling();
Assert.assertNotNull(mapping);
}
Expand Down
45 changes: 22 additions & 23 deletions src/test/java/io/scif/labeling/tutorials/E02_SaveLabeling.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Integer, UnsignedByteType> 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<Integer, UnsignedByteType> 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<Example, IntType> 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());

}

Expand All @@ -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);

}

Expand Down
Binary file modified src/test/resources/labeling/labelSaveTestComplex.tif
Binary file not shown.
2 changes: 1 addition & 1 deletion src/test/resources/labeling/labelSaveTestSimple.lbl.json
Original file line number Diff line number Diff line change
@@ -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}}
{"version":3,"numSets":4,"numSources":1,"indexImg":"labelSaveTestSimple.tif","labelMapping":{},"labelSets":{"0":[],"1":[13,42],"2":[1],"3":[13,42,1]}}
Binary file modified src/test/resources/labeling/labelSaveTestSimple.tif
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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}}
{"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}}
Binary file modified src/test/resources/labeling/labelSaveTestSimpleMeta.tif
Binary file not shown.

0 comments on commit ec726ff

Please sign in to comment.