|
42 | 42 | import org.junit.Test; |
43 | 43 | import org.scijava.Cancelable; |
44 | 44 | import org.scijava.Context; |
| 45 | +import org.scijava.Initializable; |
45 | 46 | import org.scijava.ItemIO; |
46 | 47 | import org.scijava.Priority; |
47 | 48 | import org.scijava.log.LogService; |
@@ -127,6 +128,22 @@ public void testCommandInjection() throws InterruptedException, |
127 | 128 | assertTrue((boolean) module.getOutput("success")); |
128 | 129 | } |
129 | 130 |
|
| 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 | + |
130 | 147 | // -- Helper classes -- |
131 | 148 |
|
132 | 149 | /** A command which implements {@link Cancelable}. */ |
@@ -267,4 +284,20 @@ public void run() { |
267 | 284 | } |
268 | 285 | } |
269 | 286 |
|
| 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 | + } |
270 | 303 | } |
0 commit comments