Skip to content

Commit bfc0a60

Browse files
committed
Add unit tests for CodeRunner plugins
(Well, not the ScriptCodeRunner. Testing that one is too annoying, at least for the moment.)
1 parent aec9f92 commit bfc0a60

File tree

3 files changed

+368
-0
lines changed

3 files changed

+368
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 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.run;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertFalse;
36+
import static org.junit.Assert.assertTrue;
37+
38+
import java.lang.reflect.InvocationTargetException;
39+
import java.util.HashMap;
40+
import java.util.Map;
41+
42+
import org.junit.After;
43+
import org.junit.Before;
44+
import org.junit.Test;
45+
import org.scijava.Context;
46+
import org.scijava.ItemIO;
47+
import org.scijava.command.Command;
48+
import org.scijava.command.CommandService;
49+
import org.scijava.plugin.Parameter;
50+
import org.scijava.plugin.Plugin;
51+
52+
/**
53+
* Tests {@link CommandCodeRunner}.
54+
*
55+
* @author Curtis Rueden
56+
*/
57+
public class CommandCodeRunnerTest {
58+
59+
private Context context;
60+
private CommandCodeRunner runner;
61+
62+
@Before
63+
public void setUp() {
64+
context = new Context(CommandService.class);
65+
runner = new CommandCodeRunner();
66+
context.inject(runner);
67+
}
68+
69+
@After
70+
public void tearDown() {
71+
context.dispose();
72+
}
73+
74+
@Test
75+
public void testRunList() throws InvocationTargetException {
76+
final StringBuilder buffer = new StringBuilder();
77+
78+
runner.run(OpenSesame.class, "buffer", buffer);
79+
assertEquals("Alakazam", buffer.toString());
80+
81+
runner.run(OpenSesame.class, "buffer", buffer, "magicWord", "Shazam");
82+
assertEquals("AlakazamShazam", buffer.toString());
83+
84+
runner.run("Open Sesame", "buffer", buffer, "magicWord", "Marzipan");
85+
assertEquals("AlakazamShazamMarzipan", buffer.toString());
86+
}
87+
88+
@Test
89+
public void testRunMap() throws InvocationTargetException {
90+
final StringBuilder buffer = new StringBuilder();
91+
92+
final Map<String, Object> inputMap = new HashMap<String, Object>();
93+
inputMap.put("buffer", buffer);
94+
95+
runner.run(OpenSesame.class, inputMap);
96+
assertEquals("Alakazam", buffer.toString());
97+
98+
inputMap.put("magicWord", "Shazam");
99+
runner.run(OpenSesame.class, inputMap);
100+
assertEquals("AlakazamShazam", buffer.toString());
101+
102+
inputMap.put("magicWord", "Marzipan");
103+
runner.run("Open Sesame", inputMap);
104+
assertEquals("AlakazamShazamMarzipan", buffer.toString());
105+
}
106+
107+
@Test
108+
public void testSupports() {
109+
assertTrue(runner.supports(OpenSesame.class));
110+
assertTrue(runner.supports(OpenSesame.class.getName()));
111+
assertTrue(runner.supports("Open Sesame"));
112+
113+
assertFalse(runner.supports(CommandCodeRunnerTest.class));
114+
assertFalse(runner.supports("Not an actual command"));
115+
assertFalse(runner.supports(0));
116+
}
117+
118+
// -- Helper methods --
119+
120+
@Plugin(type = Command.class, label = "Open Sesame")
121+
public static class OpenSesame implements Command {
122+
123+
@Parameter(type = ItemIO.BOTH)
124+
private StringBuilder buffer;
125+
126+
@Parameter(required = false, persist = false)
127+
private String magicWord;
128+
129+
@Override
130+
public void run() {
131+
buffer.append(magicWord == null ? "Alakazam" : magicWord);
132+
}
133+
134+
}
135+
136+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 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.main.run;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertFalse;
36+
import static org.junit.Assert.assertTrue;
37+
38+
import java.lang.reflect.InvocationTargetException;
39+
import java.util.HashMap;
40+
41+
import org.junit.Before;
42+
import org.junit.Test;
43+
44+
/**
45+
* Tests {@link MainCodeRunner}.
46+
*
47+
* @author Curtis Rueden
48+
*/
49+
public class MainCodeRunnerTest {
50+
51+
private MainCodeRunner runner;
52+
53+
@Before
54+
public void setUp() {
55+
runner = new MainCodeRunner();
56+
}
57+
58+
@Test
59+
public void testRunList() throws InvocationTargetException {
60+
runner.run(Counter.class);
61+
assertEquals(Counter.counter, 0);
62+
runner.run(Counter.class, "a");
63+
assertEquals(Counter.counter, 1);
64+
runner.run(Counter.class, "b", "c");
65+
assertEquals(Counter.counter, 3);
66+
runner.run(Counter.class, "d", "e", "f");
67+
assertEquals(Counter.counter, 6);
68+
}
69+
70+
@Test(expected = UnsupportedOperationException.class)
71+
public void testRunMap() throws InvocationTargetException {
72+
runner.run(Counter.class, new HashMap<String, Object>());
73+
}
74+
75+
@Test
76+
public void testSupports() {
77+
assertTrue(runner.supports(Counter.class));
78+
assertTrue(runner.supports(Counter.class.getName()));
79+
80+
assertFalse(runner.supports(getClass()));
81+
assertFalse(runner.supports("Not an actual class"));
82+
assertFalse(runner.supports(0));
83+
}
84+
85+
// -- Helper classes --
86+
87+
public static class Counter {
88+
89+
public static int counter;
90+
91+
public static void main(final String[] args) {
92+
counter += args.length;
93+
}
94+
}
95+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 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.module.run;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertFalse;
36+
import static org.junit.Assert.assertTrue;
37+
38+
import java.lang.reflect.InvocationTargetException;
39+
import java.util.HashMap;
40+
import java.util.Map;
41+
42+
import org.junit.After;
43+
import org.junit.Before;
44+
import org.junit.Test;
45+
import org.scijava.Context;
46+
import org.scijava.ItemIO;
47+
import org.scijava.module.DefaultMutableModule;
48+
import org.scijava.module.DefaultMutableModuleInfo;
49+
import org.scijava.module.DefaultMutableModuleItem;
50+
import org.scijava.module.ModuleService;
51+
52+
/**
53+
* Tests {@link ModuleCodeRunner}.
54+
*
55+
* @author Curtis Rueden
56+
*/
57+
public class ModuleCodeRunnerTest {
58+
59+
private Context context;
60+
private ModuleCodeRunner runner;
61+
62+
@Before
63+
public void setUp() {
64+
context = new Context(ModuleService.class);
65+
context.service(ModuleService.class).addModule(new AlphabetModuleInfo());
66+
runner = new ModuleCodeRunner();
67+
context.inject(runner);
68+
}
69+
70+
@After
71+
public void tearDown() {
72+
context.dispose();
73+
}
74+
75+
@Test
76+
public void testRunList() throws InvocationTargetException {
77+
final StringBuilder sb = new StringBuilder();
78+
runner.run("module:" + AlphabetModule.class.getName(), //
79+
"buffer", sb, "length", 3);
80+
assertEquals("ABC", sb.toString());
81+
}
82+
83+
@Test
84+
public void testRunMap() throws InvocationTargetException {
85+
final StringBuilder sb = new StringBuilder();
86+
final Map<String, Object> inputMap = new HashMap<String, Object>();
87+
inputMap.put("buffer", sb);
88+
inputMap.put("length", 4);
89+
runner.run("module:" + AlphabetModule.class.getName(), inputMap);
90+
assertEquals("ABCD", sb.toString());
91+
}
92+
93+
@Test
94+
public void testSupports() {
95+
assertTrue(runner.supports("module:" + AlphabetModule.class.getName()));
96+
97+
assertFalse(runner.supports("module:" + getClass().getName()));
98+
}
99+
100+
// -- Helper classes --
101+
102+
/** A module that writes the alphabet into a buffer. */
103+
public static class AlphabetModule extends DefaultMutableModule {
104+
105+
@Override
106+
public AlphabetModuleInfo getInfo() { return new AlphabetModuleInfo(); }
107+
108+
@Override
109+
public void run() {
110+
final StringBuilder sb = (StringBuilder) getInput("buffer");
111+
final int length = (Integer) getInput("length");
112+
sb.setLength(0);
113+
for (int i = 0; i < length; i++) {
114+
final char letter = (char) ('A' + i);
115+
sb.append(letter);
116+
}
117+
}
118+
}
119+
120+
/** Module metadata for {@link AlphabetModule}. */
121+
public static class AlphabetModuleInfo extends DefaultMutableModuleInfo {
122+
123+
public AlphabetModuleInfo() {
124+
// So much fun to construct modules by hand! Who needs commands?
125+
setModuleClass(AlphabetModule.class);
126+
final DefaultMutableModuleItem<StringBuilder> bufferItem =
127+
new DefaultMutableModuleItem<StringBuilder>(this, "buffer",
128+
StringBuilder.class);
129+
bufferItem.setIOType(ItemIO.BOTH);
130+
addInput(bufferItem);
131+
addInput(new DefaultMutableModuleItem<Integer>(this, "length",
132+
int.class));
133+
}
134+
135+
}
136+
137+
}

0 commit comments

Comments
 (0)