Skip to content

Commit cde0f42

Browse files
committed
CommandModuleTest: test getDefaultValue()
1 parent 2a2d8e2 commit cde0f42

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.scijava.module.Module;
4444
import org.scijava.module.process.AbstractPreprocessorPlugin;
4545
import org.scijava.module.process.PreprocessorPlugin;
46+
import org.scijava.plugin.Parameter;
4647
import org.scijava.plugin.Plugin;
4748

4849
/** Regression tests for {@link CommandModule}. */
@@ -76,6 +77,25 @@ public void testNotCancelable() throws InterruptedException,
7677
assertEquals("NO SINGING!", fire.getCancelReason());
7778
}
7879

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+
7999
// -- Helper classes --
80100

81101
/** A command which implements {@link Cancelable}. */
@@ -125,4 +145,27 @@ public void process(final Module module) {
125145
}
126146
}
127147
}
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+
}
128171
}

0 commit comments

Comments
 (0)