1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
package com.jeantessier.commandline; |
34 | |
|
35 | |
import org.jmock.integration.junit3.*; |
36 | |
import org.jmock.*; |
37 | |
|
38 | 12 | public class TestAliasSwitch extends MockObjectTestCase { |
39 | |
private static final String SWITCH_NAME = "switch"; |
40 | |
private static final String SWITCH_VALUE = "value"; |
41 | |
|
42 | |
private CommandLineSwitch switch1; |
43 | |
private CommandLineSwitch switch2; |
44 | |
|
45 | |
private AliasSwitch aliasSwitch; |
46 | |
|
47 | |
protected void setUp() throws Exception { |
48 | 11 | super.setUp(); |
49 | |
|
50 | 11 | switch1 = new SingleValueSwitch("switch1"); |
51 | 11 | switch2 = new OptionalValueSwitch("switch2"); |
52 | |
|
53 | 11 | aliasSwitch = new AliasSwitch(SWITCH_NAME, switch1, switch2); |
54 | 11 | } |
55 | |
|
56 | |
public void testConstructor() { |
57 | 1 | assertEquals("Name", SWITCH_NAME, aliasSwitch.getName()); |
58 | 1 | assertEquals("Nb switches", 2, aliasSwitch.getSwitches().size()); |
59 | 1 | assertTrue("Missing switch1", aliasSwitch.getSwitches().contains(switch1)); |
60 | 1 | assertTrue("Missing switch2", aliasSwitch.getSwitches().contains(switch2)); |
61 | 1 | } |
62 | |
|
63 | |
public void testSetValueToNull() { |
64 | 1 | aliasSwitch.setValue(null); |
65 | 1 | assertEquals("Switch1 not default value", switch1.getDefaultValue(), switch1.getValue()); |
66 | 1 | assertEquals("Switch2 not default value", switch2.getDefaultValue(), switch2.getValue()); |
67 | 1 | } |
68 | |
|
69 | |
public void testSetValueToValue() { |
70 | 1 | aliasSwitch.setValue(SWITCH_VALUE); |
71 | 1 | assertEquals("Switch1 not new value", SWITCH_VALUE, switch1.getValue()); |
72 | 1 | assertEquals("Switch2 not new value", SWITCH_VALUE, switch2.getValue()); |
73 | 1 | } |
74 | |
|
75 | |
public void testIsPresent() { |
76 | 1 | aliasSwitch.setValue(SWITCH_VALUE); |
77 | 1 | assertTrue("Switch1 not present", switch1.isPresent()); |
78 | 1 | assertTrue("Switch2 not present", switch2.isPresent()); |
79 | 1 | assertTrue("Not present", aliasSwitch.isPresent()); |
80 | 1 | } |
81 | |
|
82 | |
public void testIsPresentWithNoSwitches() { |
83 | 1 | aliasSwitch = new AliasSwitch(SWITCH_NAME); |
84 | 1 | aliasSwitch.setValue(SWITCH_VALUE); |
85 | 1 | assertFalse("Present", aliasSwitch.isPresent()); |
86 | 1 | } |
87 | |
|
88 | |
public void testParseNull() throws CommandLineException { |
89 | 1 | aliasSwitch = new AliasSwitch(SWITCH_NAME, switch2); |
90 | 1 | int step = aliasSwitch.parse(null); |
91 | 1 | assertEquals("step", 1, step); |
92 | 1 | assertFalse("Switch1 was set", switch1.isPresent()); |
93 | 1 | assertEquals("Switch2 not default value", switch2.getDefaultValue(), switch2.getValue()); |
94 | 1 | } |
95 | |
|
96 | |
public void testParseNullWithError() throws CommandLineException { |
97 | |
try { |
98 | 1 | aliasSwitch.parse(null); |
99 | 0 | fail("Alias with SingleValueSwitch parsed null"); |
100 | 1 | } catch (CommandLineException e) { |
101 | |
|
102 | 0 | } |
103 | 1 | } |
104 | |
|
105 | |
public void testParseValue() throws CommandLineException { |
106 | 1 | int step = aliasSwitch.parse(SWITCH_VALUE); |
107 | 1 | assertEquals("step", 2, step); |
108 | 1 | assertEquals("Switch1 not new value", SWITCH_VALUE, switch1.getValue()); |
109 | 1 | assertEquals("Switch2 not new value", SWITCH_VALUE, switch2.getValue()); |
110 | 1 | } |
111 | |
|
112 | |
public void testParseNullWithNoSwitches() throws CommandLineException { |
113 | 1 | aliasSwitch = new AliasSwitch(SWITCH_NAME); |
114 | 1 | int step = aliasSwitch.parse(null); |
115 | 1 | assertEquals("step", 1, step); |
116 | 1 | } |
117 | |
|
118 | |
public void testParseValueWithNoSwitches() throws CommandLineException { |
119 | 1 | aliasSwitch = new AliasSwitch(SWITCH_NAME); |
120 | 1 | int step = aliasSwitch.parse(SWITCH_VALUE); |
121 | 1 | assertEquals("step", 1, step); |
122 | 1 | } |
123 | |
|
124 | |
public void testAccept() { |
125 | 1 | final Visitor mockVisitor = mock(Visitor.class); |
126 | |
|
127 | 1 | checking(new Expectations() {{ |
128 | 1 | one (mockVisitor).visitAliasSwitch(aliasSwitch); |
129 | 1 | }}); |
130 | |
|
131 | 1 | aliasSwitch.accept(mockVisitor); |
132 | 1 | } |
133 | |
} |