Skip to content

Commit

Permalink
Clean up test code
Browse files Browse the repository at this point in the history
* 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. :-(
  • Loading branch information
ctrueden committed Nov 7, 2024
1 parent a3078a2 commit ae74731
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 50 deletions.
27 changes: 19 additions & 8 deletions src/test/java/io/scif/labeling/LabelingIOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,34 @@
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);
Path tempFile = Files.createTempFile(null, null);
Path tempFile = mktemp();
labelingIOService.save(imgLabeling, tempFile.toString());
final ImgLabeling<Integer, IntType> imgLabeling2 = labelingIOService.load(
tempFile.toString(), Integer.class, IntType.class);
Expand All @@ -89,7 +95,7 @@ public void testEquality2() throws IOException {
LabelingIOService.class);
final ImgLabeling<Integer, IntType> 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<Integer, IntType> imgLabeling2 = labelingIOService.load(
tempFile.toString(), Integer.class, IntType.class);
Expand All @@ -102,7 +108,7 @@ public void saveLabelingWithMetadataPrimitiveTest() throws IOException {
final ImgLabeling<Integer, UnsignedByteType> 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
Expand All @@ -123,7 +129,7 @@ public void saveLabelingWithMetadataComplexTest() throws IOException {
final ImgLabeling<Example, IntType> 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));
}

Expand Down Expand Up @@ -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;
}
}
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
39 changes: 21 additions & 18 deletions src/test/java/io/scif/labeling/tutorials/E02_SaveLabeling.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Integer, UnsignedByteType> 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<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, Files.createTempFile(null, null).toString());
Path outFile = Files.createTempFile(null, null);
final LabelingIOService labelingIOService = context.service(LabelingIOService.class);
labelingIOService.save(labeling, outFile.toString());

}

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

}

Expand Down

0 comments on commit ae74731

Please sign in to comment.