Skip to content

Commit efd4216

Browse files
committed
ModuleServiceTest: test the run methods explicitly
Previously, they were tested only indirectly by e.g. CommandServiceTest.
1 parent 41d00e6 commit efd4216

File tree

1 file changed

+106
-1
lines changed

1 file changed

+106
-1
lines changed

src/test/java/org/scijava/module/ModuleServiceTest.java

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@
3131

3232
package org.scijava.module;
3333

34+
import static org.junit.Assert.assertEquals;
3435
import static org.junit.Assert.assertNull;
3536
import static org.junit.Assert.assertSame;
3637

38+
import java.util.HashMap;
39+
import java.util.Map;
40+
import java.util.Map.Entry;
41+
import java.util.concurrent.ExecutionException;
42+
3743
import org.junit.After;
3844
import org.junit.Before;
3945
import org.junit.Test;
@@ -59,6 +65,50 @@ public void tearDown() {
5965
moduleService.context().dispose();
6066
}
6167

68+
/** Tests {@link ModuleService#run(ModuleInfo, boolean, Object...)}. */
69+
@Test
70+
public void testRunModuleInfoArray() throws InterruptedException,
71+
ExecutionException
72+
{
73+
final ModuleInfo info = new FooModuleInfo();
74+
final Module m = moduleService.run(info, false, createInputArray()).get();
75+
assertEquals(expectedResult(), m.getOutput("result"));
76+
}
77+
78+
/** Tests {@link ModuleService#run(ModuleInfo, boolean, Map)}. */
79+
@Test
80+
public void testRunModuleInfoMap() throws InterruptedException,
81+
ExecutionException
82+
{
83+
final ModuleInfo info = new FooModuleInfo();
84+
final Module m = moduleService.run(info, false, createInputMap()).get();
85+
assertEquals(expectedResult(), m.getOutput("result"));
86+
}
87+
88+
/** Tests {@link ModuleService#run(Module, boolean, Object...)}. */
89+
@Test
90+
public void testRunModuleArray() throws ModuleException, InterruptedException,
91+
ExecutionException
92+
{
93+
final ModuleInfo info = new FooModuleInfo();
94+
final Module module = info.createModule();
95+
final Module m = moduleService.run(module, false, createInputArray()).get();
96+
assertSame(module, m);
97+
assertEquals(expectedResult(), m.getOutput("result"));
98+
}
99+
100+
/** Tests {@link ModuleService#run(Module, boolean, Map)}. */
101+
@Test
102+
public void testRunModuleMap() throws ModuleException, InterruptedException,
103+
ExecutionException
104+
{
105+
final ModuleInfo info = new FooModuleInfo();
106+
final Module module = info.createModule();
107+
final Module m = moduleService.run(module, false, createInputMap()).get();
108+
assertSame(module, m);
109+
assertEquals(expectedResult(), m.getOutput("result"));
110+
}
111+
62112
@Test
63113
public void testGetSingleInput() throws ModuleException {
64114
final ModuleInfo info = new FooModuleInfo();
@@ -86,6 +136,44 @@ public void testGetSingleInput() throws ModuleException {
86136
assertSame(info.getInput("double2"), singleDouble);
87137
}
88138

139+
// -- Helper methods --
140+
141+
private Object[] createInputArray() {
142+
return new Object[] { //
143+
"string", "hello", //
144+
"float", 1.234f, //
145+
"integer1", -2, //
146+
"integer2", 7, //
147+
"double1", Math.E, //
148+
"double2", Math.PI //
149+
};
150+
}
151+
152+
private Map<String, Object> createInputMap() {
153+
final Map<String, Object> inputMap = new HashMap<>();
154+
inputMap.put("string", "hello");
155+
inputMap.put("float", 1.234f);
156+
inputMap.put("integer1", -2);
157+
inputMap.put("integer2", 7);
158+
inputMap.put("double1", Math.E);
159+
inputMap.put("double2", Math.PI);
160+
return inputMap;
161+
}
162+
163+
private String expectedResult() {
164+
return mapToString(createInputMap());
165+
}
166+
167+
private static String mapToString(final Map<String, Object> map) {
168+
final StringBuilder sb = new StringBuilder();
169+
for (final Entry<String, Object> entry : map.entrySet()) {
170+
sb.append(entry.getKey() + " = " + entry.getValue() + "\n");
171+
}
172+
return sb.toString();
173+
}
174+
175+
// -- Helper classes --
176+
89177
/** A sample module for testing the module service. */
90178
public static class FooModule extends AbstractModule {
91179

@@ -102,7 +190,7 @@ public ModuleInfo getInfo() {
102190

103191
@Override
104192
public void run() {
105-
// TODO Auto-generated method stub
193+
setOutput("result", mapToString(getInputs()));
106194
}
107195

108196
}
@@ -133,6 +221,7 @@ protected void parseParameters() {
133221
addInput("integer2", Integer.class, true);
134222
addInput("double1", Double.class, false);
135223
addInput("double2", Double.class, true);
224+
addOutput("result", String.class);
136225
}
137226

138227
private <T> void addInput(final String name, final Class<T> type,
@@ -158,6 +247,22 @@ public boolean isAutoFill() {
158247
});
159248
}
160249

250+
private <T> void addOutput(final String name, final Class<T> type) {
251+
registerOutput(new AbstractModuleItem<T>(this) {
252+
253+
@Override
254+
public String getName() {
255+
return name;
256+
}
257+
258+
@Override
259+
public Class<T> getType() {
260+
return type;
261+
}
262+
263+
});
264+
}
265+
161266
}
162267

163268
}

0 commit comments

Comments
 (0)