Skip to content

Commit dbcdc4f

Browse files
committed
CommandModuleTest: test Initializable Command
This verifies that the initialize() method of a Command implementing the Initializable interface is called during module preprocessing.
1 parent 60ef945 commit dbcdc4f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/test/java/org/scijava/command/CommandModuleTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.junit.Test;
4343
import org.scijava.Cancelable;
4444
import org.scijava.Context;
45+
import org.scijava.Initializable;
4546
import org.scijava.ItemIO;
4647
import org.scijava.Priority;
4748
import org.scijava.log.LogService;
@@ -127,6 +128,22 @@ public void testCommandInjection() throws InterruptedException,
127128
assertTrue((boolean) module.getOutput("success"));
128129
}
129130

131+
@Test
132+
public void testInitializable() throws InterruptedException,
133+
ExecutionException
134+
{
135+
final Context context = new Context(CommandService.class);
136+
final CommandService commandService = context.service(CommandService.class);
137+
138+
final CommandModule preprocessedModule = //
139+
commandService.run(InitializableCommand.class, true).get();
140+
assertEquals(42, preprocessedModule.getOutput("output"));
141+
142+
final CommandModule plainModule = //
143+
commandService.run(InitializableCommand.class, false).get();
144+
assertEquals(7, plainModule.getOutput("output"));
145+
}
146+
130147
// -- Helper classes --
131148

132149
/** A command which implements {@link Cancelable}. */
@@ -267,4 +284,20 @@ public void run() {
267284
}
268285
}
269286

287+
@Plugin(type = Command.class)
288+
public static class InitializableCommand implements Command, Initializable {
289+
private int magicNumber = 7;
290+
291+
@Parameter(type = ItemIO.OUTPUT)
292+
private int output;
293+
294+
@Override
295+
public void initialize() {
296+
magicNumber = 42;
297+
}
298+
@Override
299+
public void run() {
300+
output = magicNumber;
301+
}
302+
}
270303
}

0 commit comments

Comments
 (0)