1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli2;
18
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.List;
22
23 import junit.framework.TestCase;
24
25 import org.apache.commons.cli2.builder.ArgumentBuilder;
26 import org.apache.commons.cli2.builder.SwitchBuilder;
27 import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;
28
29
30
31
32
33
34
35
36
37
38
39
40 public class CommandLineDefaultsTest extends TestCase {
41
42
43
44
45
46 private Object methodSwitch(WriteableCommandLine cl, Option o, Boolean bool) {
47 return cl.getSwitch(o, bool);
48 }
49
50 private Object methodSwitchNull(WriteableCommandLine cl, Option o) {
51 return methodSwitch(cl, o, null);
52 }
53
54 private Object methodSwitchOff(WriteableCommandLine cl, Option o) {
55 return methodSwitch(cl, o, Boolean.FALSE);
56 }
57
58 private Object methodSwitchOn(WriteableCommandLine cl, Option o) {
59 return methodSwitch(cl, o, Boolean.TRUE);
60 }
61
62 private Object methodValueMissing(WriteableCommandLine cl, Option o) {
63 return cl.getValue(o);
64 }
65
66 private Object methodValuePresent(WriteableCommandLine cl, Option o) {
67 return cl.getValue(o, "method");
68 }
69
70
71
72
73
74 private Option optionSwitch(Boolean bool) {
75 return new SwitchBuilder().withName("switch").withSwitchDefault(bool)
76 .create();
77 }
78
79 private Option optionSwitchNull() {
80 return optionSwitch(null);
81 }
82
83 private Option optionSwitchOff() {
84 return optionSwitch(Boolean.FALSE);
85 }
86
87 private Option optionSwitchOn() {
88 return optionSwitch(Boolean.TRUE);
89 }
90
91 private Option optionValueMissing() {
92 return new ArgumentBuilder().create();
93 }
94
95 private Option optionValuePresent() {
96 return new ArgumentBuilder().withDefaults(
97 Arrays.asList(new String[] { "option" })).create();
98 }
99
100
101
102
103
104 private WriteableCommandLine parsedSwitch(Option o, Boolean bool) {
105 final List args;
106 if (bool == null) {
107 args = Collections.EMPTY_LIST;
108 } else {
109 args = Collections
110 .singletonList(String.valueOf(bool).toLowerCase());
111 }
112 WriteableCommandLine cl = new WriteableCommandLineImpl(o, args);
113 o.defaults(cl);
114 if (bool != null) {
115 cl.addSwitch(o, bool.booleanValue());
116 }
117 return cl;
118 }
119
120 private WriteableCommandLine parsedSwitchNull(Option o) {
121 return parsedSwitch(o, null);
122 }
123
124 private WriteableCommandLine parsedSwitchOn(Option o) {
125 return parsedSwitch(o, Boolean.TRUE);
126 }
127
128 private WriteableCommandLine parsedValueMissing(Option o) {
129 WriteableCommandLine cl = new WriteableCommandLineImpl(o,
130 Collections.EMPTY_LIST);
131 o.defaults(cl);
132 return cl;
133 }
134
135 private WriteableCommandLine parsedValuePresent(Option o) {
136 WriteableCommandLine cl = new WriteableCommandLineImpl(o, Arrays
137 .asList(new String[] { "parsed" }));
138 o.defaults(cl);
139 cl.addValue(o, "parsed");
140 return cl;
141 }
142
143
144
145
146
147 public void testSwitch_Method() {
148 final Option o = optionSwitchNull();
149 final WriteableCommandLine cl = parsedSwitchNull(o);
150 final Object v = methodSwitchOn(cl, o);
151 assertEquals(Boolean.TRUE, v);
152 }
153
154 public void testSwitch_Method_Option() {
155 final Option o = optionSwitchOff();
156 final WriteableCommandLine cl = parsedSwitchNull(o);
157 final Object v = methodSwitchOn(cl, o);
158 assertEquals(Boolean.TRUE, v);
159 }
160
161 public void testSwitch_Option() {
162 final Option o = optionSwitchOn();
163 final WriteableCommandLine cl = parsedSwitchNull(o);
164 final Object v = methodSwitchNull(cl, o);
165 assertEquals(Boolean.TRUE, v);
166 }
167
168 public void testSwitch_Parsed() {
169 final Option o = optionSwitchNull();
170 final WriteableCommandLine cl = parsedSwitchOn(o);
171 final Object v = methodSwitchNull(cl, o);
172 assertEquals(Boolean.TRUE, v);
173 }
174
175 public void testSwitch_Parsed_Method() {
176 final Option o = optionSwitchOff();
177 final WriteableCommandLine cl = parsedSwitchOn(o);
178 final Object v = methodSwitchNull(cl, o);
179 assertEquals(Boolean.TRUE, v);
180 }
181
182 public void testSwitch_Parsed_Method_Option() {
183 final Option o = optionSwitchOff();
184 final WriteableCommandLine cl = parsedSwitchOn(o);
185 final Object v = methodSwitchOff(cl, o);
186 assertEquals(Boolean.TRUE, v);
187 }
188
189 public void testSwitch_Parsed_Option() {
190 final Option o = optionSwitchOff();
191 final WriteableCommandLine cl = parsedSwitchOn(o);
192 final Object v = methodSwitchNull(cl, o);
193 assertEquals(Boolean.TRUE, v);
194 }
195
196 public void testValues() {
197 final Option o = optionValueMissing();
198 final WriteableCommandLine cl = parsedValueMissing(o);
199 final Object v = methodValueMissing(cl, o);
200 assertNull(v);
201 }
202
203 public void testValues_Method() {
204 final Option o = optionValueMissing();
205 final WriteableCommandLine cl = parsedValueMissing(o);
206 final Object v = methodValuePresent(cl, o);
207 assertEquals("method", v);
208 }
209
210 public void testValues_Method_Option() {
211 final Option o = optionValuePresent();
212 final WriteableCommandLine cl = parsedValueMissing(o);
213 final Object v = methodValuePresent(cl, o);
214 assertEquals("method", v);
215 }
216
217 public void testValues_Option() {
218 final Option o = optionValuePresent();
219 final WriteableCommandLine cl = parsedValueMissing(o);
220 final Object v = methodValueMissing(cl, o);
221 assertEquals("option", v);
222 }
223
224 public void testValues_Parsed() {
225 final Option o = optionValueMissing();
226 final WriteableCommandLine cl = parsedValuePresent(o);
227 final Object v = methodValueMissing(cl, o);
228 assertEquals("parsed", v);
229 }
230
231 public void testValues_Parsed_Method() {
232 final Option o = optionValueMissing();
233 final WriteableCommandLine cl = parsedValuePresent(o);
234 final Object v = methodValuePresent(cl, o);
235 assertEquals("parsed", v);
236 }
237
238 public void testValues_Parsed_Method_Option() {
239 final Option o = optionValuePresent();
240 final WriteableCommandLine cl = parsedValuePresent(o);
241 final Object v = methodValuePresent(cl, o);
242 assertEquals("parsed", v);
243 }
244
245 public void testValues_Parsed_Option() {
246 final Option o = optionValuePresent();
247 final WriteableCommandLine cl = parsedValuePresent(o);
248 final Object v = methodValueMissing(cl, o);
249 assertEquals("parsed", v);
250 }
251 }