1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli2.option;
18
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.ListIterator;
22 import java.util.Set;
23
24 import org.apache.commons.cli2.DisplaySetting;
25 import org.apache.commons.cli2.Option;
26 import org.apache.commons.cli2.OptionException;
27 import org.apache.commons.cli2.Parent;
28 import org.apache.commons.cli2.WriteableCommandLine;
29 import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;
30 import org.apache.commons.cli2.resource.ResourceConstants;
31 import org.apache.commons.cli2.resource.ResourceHelper;
32
33
34
35
36
37
38
39 public class SwitchTest
40 extends ParentTestCase {
41 public static Switch buildDisplaySwitch() {
42 final Set aliases = new HashSet();
43 aliases.add("d");
44 aliases.add("disp");
45
46 return new Switch("+", "-", "display", aliases, "Sets whether to display to screen", true,
47 null, null, 'd', null);
48 }
49
50
51
52
53
54
55 public void testProcessParent()
56 throws OptionException {
57 final Switch option = buildDisplaySwitch();
58 final List args = list("+d");
59 final WriteableCommandLine commandLine = commandLine(option, args);
60 final ListIterator iterator = args.listIterator();
61 option.processParent(commandLine, iterator);
62
63 assertFalse(iterator.hasNext());
64 assertTrue(commandLine.hasOption(option));
65 assertTrue(commandLine.hasOption("+d"));
66 assertTrue(commandLine.hasOption("-display"));
67 assertEquals(Boolean.TRUE, commandLine.getSwitch("-d"));
68 assertTrue(commandLine.getValues(option).isEmpty());
69 }
70
71 public void testProcessParent_Disabled()
72 throws OptionException {
73 final Switch option = buildDisplaySwitch();
74 final List args = list("-disp");
75 final WriteableCommandLine commandLine = commandLine(option, args);
76 final ListIterator iterator = args.listIterator();
77 option.process(commandLine, iterator);
78
79 assertFalse(iterator.hasNext());
80 assertTrue(commandLine.hasOption(option));
81 assertTrue(commandLine.hasOption("+d"));
82 assertTrue(commandLine.hasOption("-display"));
83 assertEquals(Boolean.FALSE, commandLine.getSwitch("-d"));
84 assertTrue(commandLine.getValues(option).isEmpty());
85 }
86
87
88
89
90
91
92 public void testCanProcess() {
93 final Switch option = buildDisplaySwitch();
94 assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "+d"));
95 }
96
97 public void testCanProcess_BadMatch() {
98 final Switch option = buildDisplaySwitch();
99 assertFalse(option.canProcess(new WriteableCommandLineImpl(option, null), "-dont"));
100 }
101
102
103
104
105
106
107 public void testPrefixes() {
108 final Switch option = buildDisplaySwitch();
109 assertContentsEqual(list("-", "+"), option.getPrefixes());
110 }
111
112
113
114
115
116
117 public void testProcess() {
118
119 }
120
121
122
123
124
125
126 public void testTriggers() {
127 final Switch option = buildDisplaySwitch();
128 assertContentsEqual(list("-d", "+d", "-disp", "+disp", "+display", "-display"),
129 option.getTriggers());
130 }
131
132
133
134
135
136
137 public void testValidate() {
138 final Parent option = buildDisplaySwitch();
139 final WriteableCommandLine commandLine = commandLine(option, list());
140
141 try {
142 option.validate(commandLine);
143 fail("Missing an option");
144 } catch (OptionException moe) {
145 assertSame(option, moe.getOption());
146 }
147 }
148
149
150
151
152
153
154 public void testAppendUsage() {
155 final Option option = buildDisplaySwitch();
156 final StringBuffer buffer = new StringBuffer();
157 option.appendUsage(buffer, DisplaySetting.ALL, null);
158
159 assertEquals("+display|-display (+d|-d,+disp|-disp)", buffer.toString());
160 }
161
162 public void testAppendUsage_NoAlias() {
163 final Option option = buildDisplaySwitch();
164 final StringBuffer buffer = new StringBuffer();
165 final Set settings = new HashSet(DisplaySetting.ALL);
166 settings.remove(DisplaySetting.DISPLAY_ALIASES);
167 option.appendUsage(buffer, settings, null);
168
169 assertEquals("+display|-display", buffer.toString());
170 }
171
172 public void testAppendUsage_NoDisabled() {
173 final Option option = buildDisplaySwitch();
174 final StringBuffer buffer = new StringBuffer();
175 final Set settings = new HashSet(DisplaySetting.ALL);
176 settings.remove(DisplaySetting.DISPLAY_SWITCH_DISABLED);
177 option.appendUsage(buffer, settings, null);
178
179 assertEquals("+display (+d,+disp)", buffer.toString());
180 }
181
182 public void testAppendUsage_NoEnabled() {
183 final Option option = buildDisplaySwitch();
184 final StringBuffer buffer = new StringBuffer();
185 final Set settings = new HashSet(DisplaySetting.ALL);
186 settings.remove(DisplaySetting.DISPLAY_SWITCH_ENABLED);
187 option.appendUsage(buffer, settings, null);
188
189 assertEquals("-display (-d,-disp)", buffer.toString());
190 }
191
192 public void testAppendUsage_NoDisabledOrEnabled() {
193 final Option option = buildDisplaySwitch();
194 final StringBuffer buffer = new StringBuffer();
195 final Set settings = new HashSet(DisplaySetting.ALL);
196 settings.remove(DisplaySetting.DISPLAY_SWITCH_DISABLED);
197 settings.remove(DisplaySetting.DISPLAY_SWITCH_ENABLED);
198 option.appendUsage(buffer, settings, null);
199
200 assertEquals("+display (+d,+disp)", buffer.toString());
201 }
202
203
204
205
206
207
208 public void testGetPreferredName() {
209 final Option option = buildDisplaySwitch();
210 assertEquals("+display", option.getPreferredName());
211 }
212
213
214
215
216
217
218 public void testGetDescription() {
219 final Option option = buildDisplaySwitch();
220 assertEquals("Sets whether to display to screen", option.getDescription());
221 }
222
223 public void testNullPreferredName() {
224 try {
225 new Switch("+", "-", null, null, "Sets whether to display to screen", true, null, null,
226 'd', null);
227 } catch (IllegalArgumentException exp) {
228 assertEquals("wrong exception message",
229 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_PREFERRED_NAME_TOO_SHORT),
230 exp.getMessage());
231 }
232 }
233
234 public void testEmptyPreferredName() {
235 try {
236 new Switch("+", "-", "", null, "Sets whether to display to screen", true, null, null,
237 'd', null);
238 } catch (IllegalArgumentException exp) {
239 assertEquals("wrong exception message",
240 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_PREFERRED_NAME_TOO_SHORT),
241 exp.getMessage());
242 }
243 }
244
245 public void testNullAliases() {
246 try {
247 new Switch("+", "-", "display", null, "Sets whether to display to screen", true, null,
248 null, 'd', null);
249 } catch (IllegalArgumentException exp) {
250 assertEquals("wrong exception message",
251 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_PREFERRED_NAME_TOO_SHORT),
252 exp.getMessage());
253 }
254 }
255
256 public void testNullEnablePrefix() {
257 try {
258 new Switch(null, "-", "display", null, "Sets whether to display to screen", true, null,
259 null, 'd', null);
260 } catch (IllegalArgumentException exp) {
261 assertEquals("wrong exception message",
262 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_NO_ENABLED_PREFIX),
263 exp.getMessage());
264 }
265 }
266
267 public void testNullDisablePrefix() {
268 try {
269 new Switch("+", null, "display", null, "Sets whether to display to screen", true, null,
270 null, 'd', null);
271 } catch (IllegalArgumentException exp) {
272 assertEquals("wrong exception message",
273 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_NO_DISABLED_PREFIX),
274 exp.getMessage());
275 }
276 }
277
278 public void testEnabledPrefixStartsWithDisabledPrefix() {
279 try {
280 new Switch("-", "-", "display", null, "Sets whether to display to screen", true, null,
281 null, 'd', null);
282 } catch (IllegalArgumentException exp) {
283 assertEquals("wrong exception message",
284 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_ENABLED_STARTS_WITH_DISABLED),
285 exp.getMessage());
286 }
287 }
288
289 public void testDisabledPrefixStartsWithEnabledPrefix() {
290 try {
291 new Switch("o", "on", "display", null, "Sets whether to display to screen", true, null,
292 null, 'd', null);
293 } catch (IllegalArgumentException exp) {
294 assertEquals("wrong exception message",
295 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.SWITCH_DISABLED_STARTWS_WITH_ENABLED),
296 exp.getMessage());
297 }
298 }
299
300
301
302
303
304
305 public void testHelpLines() {
306
307 }
308 }