Skip to content

Commit 6052ea3

Browse files
committed
Add test for ModuleBatchProcessor
1 parent a55d6a4 commit 6052ea3

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,17 @@
113113
<groupId>commons-io</groupId>
114114
<artifactId>commons-io</artifactId>
115115
</dependency>
116+
117+
<!-- Test dependencies -->
116118
<dependency>
117119
<groupId>junit</groupId>
118120
<artifactId>junit</artifactId>
119121
<scope>test</scope>
120122
</dependency>
123+
<dependency>
124+
<groupId>org.scijava</groupId>
125+
<artifactId>scripting-groovy</artifactId>
126+
<scope>test</scope>
127+
</dependency>
121128
</dependencies>
122129
</project>
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
package org.scijava.batch;
22

3+
import static org.junit.Assert.assertEquals;
34
import static org.junit.Assert.assertNotNull;
45

6+
import java.io.File;
7+
import java.io.StringReader;
8+
import java.util.HashMap;
9+
import java.util.concurrent.ExecutionException;
10+
11+
import net.imagej.table.Table;
12+
513
import org.junit.After;
14+
import org.junit.Before;
615
import org.junit.Test;
716
import org.scijava.Context;
17+
import org.scijava.command.CommandInfo;
18+
import org.scijava.command.CommandService;
19+
import org.scijava.module.Module;
20+
import org.scijava.module.ModuleService;
21+
import org.scijava.script.ScriptInfo;
22+
import org.scijava.service.SciJavaService;
823

924
public class BatchServiceTest {
10-
25+
1126
private Context context;
1227

28+
@Before
29+
public void initialize() {
30+
context = new Context(SciJavaService.class);
31+
}
32+
1333
@After
1434
public void disposeContext() {
1535
if (context != null) {
@@ -20,9 +40,54 @@ public void disposeContext() {
2040

2141
@Test
2242
public void testContext() {
23-
context = new Context(BatchService.class);
24-
final BatchService batchService =
25-
context.getService(BatchService.class);
43+
final BatchService batchService = context
44+
.getService(BatchService.class);
2645
assertNotNull(batchService);
2746
}
47+
48+
@Test
49+
public void testModuleBatchProcessor() {
50+
String script = "" //
51+
+ "#@ File input\n" //
52+
+ "#@output result\n" //
53+
+ "" //
54+
+ "result = input";
55+
StringReader scriptReader = new StringReader(script);
56+
ScriptInfo scriptInfo = new ScriptInfo(context, "Foo.groovy",
57+
scriptReader);
58+
59+
assertEquals("Wrong script language", "Groovy", scriptInfo
60+
.getLanguage().getLanguageName());
61+
62+
File[] files = new File[3];
63+
files[0] = new File("foo.txt");
64+
files[1] = new File("bar.txt");
65+
files[2] = new File("quo.txt");
66+
67+
HashMap<String, Object> inputMap = new HashMap<>();
68+
inputMap.put("moduleInfo", scriptInfo);
69+
inputMap.put("inputChoice", "input");
70+
inputMap.put("inputFileList", files);
71+
inputMap.put("outputFolder", null);
72+
ModuleService moduleService = context.getService(ModuleService.class);
73+
CommandService commandService = context
74+
.getService(CommandService.class);
75+
CommandInfo commandInfo = commandService
76+
.getCommand(ModuleBatchProcessor.class);
77+
Module module = moduleService.createModule(commandInfo);
78+
try {
79+
module = moduleService.run(module, true, inputMap).get();
80+
} catch (InterruptedException | ExecutionException exc) {
81+
// TODO Auto-generated catch block
82+
exc.printStackTrace();
83+
}
84+
Table<?, ?> outputs = (Table<?, ?>) module.getOutput("outputTable");
85+
86+
assertEquals("Wrong number of output columns", 1,
87+
outputs.getColumnCount());
88+
assertEquals("Wrong number of output rows", 3, outputs.getRowCount());
89+
assertEquals("Wrong column header", "result",
90+
outputs.getColumnHeader(0));
91+
assertEquals("Wrong file name", "quo.txt", outputs.getRowHeader(2));
92+
}
2893
}

0 commit comments

Comments
 (0)