Skip to content

Commit c2c87f3

Browse files
committed
Add tests for the MainService
1 parent b372a8c commit c2c87f3

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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.main;
33+
34+
import static org.junit.Assert.assertArrayEquals;
35+
import static org.junit.Assert.assertEquals;
36+
37+
import org.junit.After;
38+
import org.junit.Before;
39+
import org.junit.Test;
40+
import org.scijava.Context;
41+
import org.scijava.console.ConsoleService;
42+
import org.scijava.main.console.MainArgument;
43+
44+
/**
45+
* Tests {@link MainService}.
46+
*
47+
* @author Curtis Rueden
48+
*/
49+
public class MainServiceTest {
50+
51+
private MainService mainService;
52+
53+
@Before
54+
public void setUp() {
55+
MathMain.resultCount = 0;
56+
final Context context = new Context();
57+
mainService = context.service(MainService.class);
58+
}
59+
60+
@After
61+
public void tearDown() {
62+
mainService.context().dispose();
63+
}
64+
65+
/**
66+
* Tests {@link MainService#execMains()},
67+
* {@link MainService#addMain(String, String...)} and
68+
* {@link MainService#getMains()}.
69+
*/
70+
@Test
71+
public void testMainService() {
72+
final int mainCount0 = mainService.execMains();
73+
assertEquals(0, mainCount0);
74+
75+
mainService.addMain(MathMain.class.getName(), "12.3", "/", "4.56");
76+
77+
final int mainCount1 = mainService.execMains();
78+
assertEquals(1, mainCount1);
79+
assertEquals(System.getProperty(key(0)), "2.697368421052632");
80+
}
81+
82+
/** Tests usage of {@link MainService} via {@code --main} CLI arguments. */
83+
@Test
84+
public void testConsoleArgs() {
85+
assertEquals(0, mainService.getMains().length);
86+
87+
final ConsoleService consoleService = mainService.context().service(
88+
ConsoleService.class);
89+
consoleService.processArgs("-Dfoo=bar", //
90+
"--main", "org.scijava.main.MainServiceTest$MathMain", "5", "+", "6", //
91+
"--", "-Dwhiz=bang", //
92+
"--main", "org.scijava.main.MainServiceTest$MathMain", "7", "-", "4");
93+
94+
final MainService.Main[] m = mainService.getMains();
95+
assertEquals(2, m.length);
96+
assertEquals("org.scijava.main.MainServiceTest$MathMain", m[0].className());
97+
assertArrayEquals(new String[] {"5", "+", "6"}, m[0].args());
98+
assertEquals("org.scijava.main.MainServiceTest$MathMain", m[1].className());
99+
assertArrayEquals(new String[] {"7", "-", "4"}, m[1].args());
100+
101+
final int mainCount = mainService.execMains();
102+
assertEquals(2, mainCount);
103+
assertEquals(System.getProperty(key(0)), "11.0");
104+
assertEquals(System.getProperty(key(1)), "3.0");
105+
106+
assertEquals(System.getProperty("foo"), "bar");
107+
assertEquals(System.getProperty("whiz"), "bang");
108+
}
109+
110+
// -- Helper methods --
111+
112+
private static String key(final int index) {
113+
return MathMain.class.getName() + ":" + index;
114+
}
115+
116+
// -- Helper classes --
117+
118+
private static class MathMain {
119+
private static int resultCount = 0;
120+
@SuppressWarnings("unused")
121+
public static void main(final String[] args) {
122+
if (args.length != 3) {
123+
throw new IllegalArgumentException("Invalid args: " + args);
124+
}
125+
126+
// compute a result from the arguments
127+
final double operand1 = Double.parseDouble(args[0]);
128+
final String operator = args[1];
129+
final double operand2 = Double.parseDouble(args[2]);
130+
final double result;
131+
if (operator.equals("+")) result = operand1 + operand2;
132+
else if (operator.equals("-")) result = operand1 - operand2;
133+
else if (operator.equals("*")) result = operand1 * operand2;
134+
else if (operator.equals("/")) result = operand1 / operand2;
135+
else throw new IllegalArgumentException("Unknown operator: " + operator);
136+
137+
// save the result to a system property, for later checking
138+
final String key = MathMain.class.getName() + ":" + resultCount++;
139+
final String value = "" + result;
140+
System.setProperty(key, value);
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)