|
43 | 43 | import org.scijava.module.Module; |
44 | 44 | import org.scijava.module.process.AbstractPreprocessorPlugin; |
45 | 45 | import org.scijava.module.process.PreprocessorPlugin; |
| 46 | +import org.scijava.plugin.Parameter; |
46 | 47 | import org.scijava.plugin.Plugin; |
47 | 48 |
|
48 | 49 | /** Regression tests for {@link CommandModule}. */ |
@@ -76,6 +77,25 @@ public void testNotCancelable() throws InterruptedException, |
76 | 77 | assertEquals("NO SINGING!", fire.getCancelReason()); |
77 | 78 | } |
78 | 79 |
|
| 80 | + @Test |
| 81 | + public void testDefaultValues() { |
| 82 | + final Context context = new Context(CommandService.class); |
| 83 | + final CommandService commandService = context.service(CommandService.class); |
| 84 | + final CommandInfo info = // |
| 85 | + commandService.getCommand(CommandWithDefaultValues.class); |
| 86 | + |
| 87 | + assertEquals(5, info.getInput("weekdays").getDefaultValue()); |
| 88 | + |
| 89 | + final long defaultTime = (Long) info.getInput("time").getDefaultValue(); |
| 90 | + final long timeDiff = System.currentTimeMillis() - defaultTime; |
| 91 | + assertTrue(timeDiff >= 0 && timeDiff < 50); // 50 ms should be enough ;-) |
| 92 | + |
| 93 | + final String defaultName = (String) info.getInput("name").getDefaultValue(); |
| 94 | + assertEquals("John Jacob Jingleheimer Schmidt", defaultName); |
| 95 | + |
| 96 | + assertEquals(null, info.getInput("thing").getDefaultValue()); |
| 97 | + } |
| 98 | + |
79 | 99 | // -- Helper classes -- |
80 | 100 |
|
81 | 101 | /** A command which implements {@link Cancelable}. */ |
@@ -125,4 +145,27 @@ public void process(final Module module) { |
125 | 145 | } |
126 | 146 | } |
127 | 147 | } |
| 148 | + |
| 149 | + /** A command which assigns default values to its parameters. */ |
| 150 | + @Plugin(type = Command.class) |
| 151 | + public static class CommandWithDefaultValues extends ContextCommand { |
| 152 | + |
| 153 | + @Parameter |
| 154 | + private int weekdays = 5; |
| 155 | + |
| 156 | + @Parameter |
| 157 | + private long time = System.currentTimeMillis(); |
| 158 | + |
| 159 | + @Parameter |
| 160 | + private String name = "John Jacob Jingleheimer Schmidt"; |
| 161 | + |
| 162 | + @Parameter |
| 163 | + private Object thing; |
| 164 | + |
| 165 | + @Override |
| 166 | + public void run() { |
| 167 | + weekdays = 0; |
| 168 | + time = 0; |
| 169 | + } |
| 170 | + } |
128 | 171 | } |
0 commit comments