Skip to content

Commit afbcf64

Browse files
committed
Add unit test for validity processing
When a command is invalid, it is supposed to be flagged as such, with problems recorded. There was a manual test for this in scijava-plugins-commands called InvalidCommandDemo, which was a silly place for such a thing. Much, much better to have a unit test here that verifies this feature is working as expected. This code was migrated and adapted from: https://github.com/scijava/scijava-plugins-commands/blob/scijava-plugins-commands-0.1.8/src/main/java/org/scijava/plugins/commands/debug/InvalidCommandDemo.java
1 parent 829afa4 commit afbcf64

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2015 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.command;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertFalse;
36+
import static org.junit.Assert.assertNotNull;
37+
import static org.junit.Assert.assertTrue;
38+
39+
import java.util.List;
40+
41+
import org.junit.Before;
42+
import org.junit.Test;
43+
import org.scijava.Context;
44+
import org.scijava.ItemIO;
45+
import org.scijava.ValidityProblem;
46+
import org.scijava.plugin.Parameter;
47+
import org.scijava.plugin.Plugin;
48+
49+
/**
50+
* Test commands for verifying that invalid module parameters are dealt with
51+
* using proper error handling.
52+
*
53+
* @author Curtis Rueden
54+
*/
55+
public class InvalidCommandTest {
56+
57+
private CommandService commandService;
58+
59+
@Before
60+
public void setUp() {
61+
Context ctx = new Context(CommandService.class);
62+
commandService = ctx.getService(CommandService.class);
63+
}
64+
65+
@Test
66+
public void testValid() {
67+
final CommandInfo info = commandService.getCommand(ValidCommand.class);
68+
assertNotNull(info);
69+
assertTrue(info.isValid());
70+
71+
final List<ValidityProblem> problems = info.getProblems();
72+
assertNotNull(problems);
73+
assertEquals(0, problems.size());
74+
}
75+
76+
@Test
77+
public void testInvalid() {
78+
final CommandInfo info = commandService.getCommand(InvalidCommand.class);
79+
assertNotNull(info);
80+
assertFalse(info.isValid());
81+
82+
final List<ValidityProblem> problems = info.getProblems();
83+
assertNotNull(problems);
84+
assertEquals(2, problems.size());
85+
86+
final String p0 = problems.get(0).getMessage();
87+
assertEquals("Invalid duplicate parameter: private int "
88+
+ "org.scijava.command.InvalidCommandTest$InvalidCommand.x", p0);
89+
90+
final String p1 = problems.get(1).getMessage();
91+
assertEquals("Invalid final parameter: private final float "
92+
+ "org.scijava.command.InvalidCommandTest$InvalidCommand.y", p1);
93+
}
94+
95+
// -- Helper classes --
96+
97+
/** A perfectly valid command! */
98+
@Plugin(type = Command.class)
99+
public static class ValidCommand implements Command {
100+
101+
@Parameter
102+
private double x;
103+
104+
@Parameter(type = ItemIO.OUTPUT)
105+
private String validOutput;
106+
107+
@Override
108+
public void run() {
109+
validOutput = "ValidCommand: success!";
110+
}
111+
112+
}
113+
114+
/** A very much invalid command, for multiple reasons, explained below. */
115+
@Plugin(type = Command.class)
116+
public static abstract class InvalidCommand extends ValidCommand {
117+
118+
/**
119+
* This parameter is invalid because it shadows a private parameter of a
120+
* superclass. Such parameters violate the principle of parameter names as
121+
* unique keys.
122+
*/
123+
@Parameter
124+
private int x;
125+
126+
/**
127+
* This parameter is invalid because it is declared {@code final} without
128+
* being {@link org.scijava.ItemVisibility#MESSAGE} visibility. Java does
129+
* not allow such parameter values to be set via reflection.
130+
*/
131+
@Parameter
132+
private final float y = 0;
133+
134+
@Parameter(type = ItemIO.OUTPUT)
135+
private String invalidOutput;
136+
137+
@Override
138+
public void run() {
139+
invalidOutput = "InvalidCommand: FAILURE";
140+
}
141+
142+
}
143+
144+
}

0 commit comments

Comments
 (0)