View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.cli2;
18  
19  import java.util.Collections;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.apache.commons.cli2.builder.ArgumentBuilder;
24  import org.apache.commons.cli2.builder.DefaultOptionBuilder;
25  import org.apache.commons.cli2.builder.GroupBuilder;
26  import org.apache.commons.cli2.commandline.Parser;
27  import org.apache.commons.cli2.option.ArgumentTest;
28  import org.apache.commons.cli2.option.CommandTest;
29  import org.apache.commons.cli2.option.DefaultOptionTest;
30  import org.apache.commons.cli2.option.OptionTestCase;
31  import org.apache.commons.cli2.option.PropertyOption;
32  import org.apache.commons.cli2.option.SwitchTest;
33  import org.apache.commons.cli2.resource.ResourceConstants;
34  import org.apache.commons.cli2.resource.ResourceHelper;
35  
36  public abstract class CommandLineTestCase
37      extends CLITestCase {
38      private static final ResourceHelper resources = ResourceHelper.getResourceHelper();
39      public final Option present =
40          new DefaultOptionBuilder().withLongName("present").withLongName("alsopresent").create();
41      public final Option missing = new DefaultOptionBuilder().withLongName("missing").create();
42      public final Option multiple = new DefaultOptionBuilder().withLongName("multiple").create();
43      public final Option bool = new DefaultOptionBuilder().withLongName("bool").create();
44      public final Option root =
45          new GroupBuilder().withOption(present).withOption(missing).withOption(multiple)
46                            .withOption(bool).create();
47      private CommandLine commandLine;
48  
49      protected abstract CommandLine createCommandLine();
50  
51      /*
52       * @see TestCase#setUp()
53       */
54      public void setUp()
55          throws Exception {
56          super.setUp();
57          commandLine = createCommandLine();
58      }
59  
60      /*
61       * Class to test for boolean hasOption(String)
62       */
63      public final void testHasOptionString() {
64          assertTrue(commandLine.hasOption("--present"));
65          assertTrue(commandLine.hasOption("--alsopresent"));
66          assertFalse(commandLine.hasOption("--missing"));
67      }
68  
69      /*
70       * Class to test for boolean hasOption(Option)
71       */
72      public final void testHasOptionOption() {
73          assertTrue(commandLine.hasOption(present));
74          assertFalse(commandLine.hasOption(missing));
75      }
76  
77      public final void testGetOption() {
78          assertSame(present, commandLine.getOption("--present"));
79          assertSame(present, commandLine.getOption("--alsopresent"));
80  
81          //TODO decide whether the following assertion is valid
82          //assertSame(missing,commandLine.getOption("--missing"));
83      }
84  
85      /*
86       * Class to test for List getValues(String)
87       */
88      public final void testGetValuesString() {
89          assertListContentsEqual(list("present value"), commandLine.getValues("--present"));
90          assertListContentsEqual(list("value 1", "value 2", "value 3"),
91                                  commandLine.getValues("--multiple"));
92          assertTrue(commandLine.getValues("--missing").isEmpty());
93      }
94  
95      /*
96       * Class to test for List getValues(String, List)
97       */
98      public final void testGetValuesStringList() {
99          assertListContentsEqual(list("present value"), commandLine.getValues("--present", null));
100         assertListContentsEqual(list("present value"), commandLine.getValues("--alsopresent", null));
101         assertSame(commandLine.getValues("--missing", Collections.EMPTY_LIST),
102                    Collections.EMPTY_LIST);
103 
104         final List def = Collections.singletonList("default value");
105         assertSame(def, commandLine.getValues("--missing", def));
106     }
107 
108     /*
109      * Class to test for List getValues(Option)
110      */
111     public final void testGetValuesOption() {
112         assertListContentsEqual(list("present value"), commandLine.getValues(present));
113         assertTrue(commandLine.getValues(missing).isEmpty());
114     }
115 
116     /*
117      * Class to test for List getValues(Option, List)
118      */
119     public final void testGetValuesOptionList() {
120         assertListContentsEqual(list("present value"), commandLine.getValues(present));
121         assertSame(commandLine.getValues(missing, Collections.EMPTY_LIST), Collections.EMPTY_LIST);
122 
123         final List defs = Collections.singletonList("custom default");
124         assertSame(defs, commandLine.getValues(missing, defs));
125     }
126 
127     /*
128      * Class to test for Object getValue(String)
129      */
130     public final void testGetValueString() {
131         assertEquals("present value", commandLine.getValue("--present"));
132         assertEquals("present value", commandLine.getValue("--alsopresent"));
133         assertNull(commandLine.getValue("--missing"));
134 
135         try {
136             commandLine.getValue("--multiple");
137             fail("expected IllegalStateException");
138         } catch (IllegalStateException e) {
139             assertEquals(resources.getMessage(ResourceConstants.ARGUMENT_TOO_MANY_VALUES),
140                          e.getMessage());
141         }
142     }
143 
144     /*
145      * Class to test for Object getValue(String, Object)
146      */
147     public final void testGetValueStringObject() {
148         assertEquals("present value", commandLine.getValue("--present", "default value"));
149         assertEquals("present value", commandLine.getValue("--alsopresent", "default value"));
150         assertEquals("default value", commandLine.getValue("--missing", "default value"));
151 
152         try {
153             commandLine.getValue("--multiple");
154             fail("expected IllegalStateException");
155         } catch (IllegalStateException e) {
156             assertEquals(resources.getMessage(ResourceConstants.ARGUMENT_TOO_MANY_VALUES),
157                          e.getMessage());
158         }
159     }
160 
161     /*
162      * Class to test for Object getValue(Option)
163      */
164     public final void testGetValueOption() {
165         assertEquals("present value", commandLine.getValue(present));
166         assertNull(commandLine.getValue(missing));
167 
168         try {
169             commandLine.getValue(multiple);
170             fail("expected IllegalStateException");
171         } catch (IllegalStateException e) {
172             assertEquals(resources.getMessage(ResourceConstants.ARGUMENT_TOO_MANY_VALUES),
173                          e.getMessage());
174         }
175     }
176 
177     /*
178      * Class to test for Object getValue(Option, Object)
179      */
180     public final void testGetValueOptionObject() {
181         assertEquals("present value", commandLine.getValue(present, "default value"));
182         assertEquals("default value", commandLine.getValue(missing, "default value"));
183 
184         try {
185             commandLine.getValue(multiple);
186             fail("expected IllegalStateException");
187         } catch (IllegalStateException e) {
188             assertEquals(resources.getMessage(ResourceConstants.ARGUMENT_TOO_MANY_VALUES),
189                          e.getMessage());
190         }
191     }
192 
193     /*
194      * Class to test for Boolean getSwitch(String)
195      */
196     public final void testGetSwitchString() {
197         assertEquals(Boolean.TRUE, commandLine.getSwitch("--bool"));
198         assertNull(commandLine.getSwitch("--missing"));
199     }
200 
201     /*
202      * Class to test for Boolean getSwitch(String, Boolean)
203      */
204     public final void testGetSwitchStringBoolean() {
205         assertEquals(Boolean.TRUE, commandLine.getSwitch("--bool", Boolean.FALSE));
206         assertEquals(Boolean.FALSE, commandLine.getSwitch("--missing", Boolean.FALSE));
207     }
208 
209     /*
210      * Class to test for Boolean getSwitch(Option)
211      */
212     public final void testGetSwitchOption() {
213         assertEquals(Boolean.TRUE, commandLine.getSwitch(bool));
214         assertNull(commandLine.getSwitch(missing));
215     }
216 
217     /*
218      * Class to test for Boolean getSwitch(Option, Boolean)
219      */
220     public final void testGetSwitchOptionBoolean() {
221         assertEquals(Boolean.TRUE, commandLine.getSwitch(bool, Boolean.FALSE));
222         assertEquals(Boolean.FALSE, commandLine.getSwitch(missing, Boolean.FALSE));
223     }
224 
225     /*
226      * Class to test for String getProperty(String)
227      */
228     public final void testGetPropertyString() {
229         assertEquals("present property", commandLine.getProperty("present"));
230         assertNull(commandLine.getProperty("missing"));
231     }
232 
233     /*
234      * Class to test for String getProperty(String, String)
235      */
236     public final void testGetPropertyStringString() {
237         assertEquals("present property", commandLine.getProperty(new PropertyOption(), "present", "default property"));
238         assertEquals("default property", commandLine.getProperty(new PropertyOption(), "missing", "default property"));
239     }
240 
241     public final void testGetProperties() {
242         assertTrue(commandLine.getProperties().containsAll(list("present")));
243     }
244 
245     /*
246      * Class to test for int getOptionCount(String)
247      */
248     public final void testGetOptionCountString() {
249         // one option, one switch
250         assertTrue(1 <= commandLine.getOptionCount("--present"));
251         assertTrue(1 <= commandLine.getOptionCount("--bool"));
252         assertEquals(0, commandLine.getOptionCount("--missing"));
253     }
254 
255     /*
256      * Class to test for int getOptionCount(Option)
257      */
258     public final void testGetOptionCountOption() {
259         // one option, one switch
260         assertTrue(1 <= commandLine.getOptionCount(present));
261         assertTrue(1 <= commandLine.getOptionCount(bool));
262         assertEquals(0, commandLine.getOptionCount(missing));
263     }
264 
265     public final void testGetOptions() {
266         //TODO Implement getOptions().
267     }
268 
269     public final void testGetOptionTriggers() {
270         //TODO Implement getOptionTriggers().
271     }
272 
273     // OLD TESTS FOLLOW
274     public final void testProperties() {
275         final PropertyOption option = new PropertyOption();
276         final List args = CLITestCase.list();
277         final WriteableCommandLine writeable = OptionTestCase.commandLine(option, args);
278 
279         assertTrue(writeable.getProperties(option).isEmpty());
280 
281         writeable.addProperty(option, "myprop", "myval");
282         assertEquals(1, writeable.getProperties(option).size());
283         assertEquals("myval", writeable.getProperty(option, "myprop"));
284 
285         writeable.addProperty(option, "myprop", "myval2");
286         assertEquals(1, writeable.getProperties(option).size());
287         assertEquals("myval2", writeable.getProperty(option, "myprop"));
288 
289         writeable.addProperty(option, "myprop2", "myval3");
290         assertEquals(2, writeable.getProperties(option).size());
291         assertEquals("myval3", writeable.getProperty(option, "myprop2"));
292     }
293 
294     public final void testOptions() {
295         final Option option = new PropertyOption();
296         final List args = CLITestCase.list();
297         final WriteableCommandLine writeable = OptionTestCase.commandLine(option, args);
298 
299         final Option start = CommandTest.buildStartCommand();
300 
301         assertFalse(writeable.hasOption(start));
302         assertFalse(writeable.hasOption("start"));
303         assertFalse(writeable.hasOption("go"));
304 
305         writeable.addOption(start);
306 
307         assertTrue(writeable.hasOption(start));
308         assertTrue(writeable.hasOption("start"));
309         assertTrue(writeable.hasOption("go"));
310     }
311 
312     public final void testValues() {
313         final Option option = new PropertyOption();
314         final List args = CLITestCase.list();
315         final WriteableCommandLine writeable = OptionTestCase.commandLine(option, args);
316 
317         final Option start = CommandTest.buildStartCommand();
318 
319         assertNull(writeable.getValue(start));
320         assertTrue(writeable.getValues(start).isEmpty());
321 
322         writeable.addOption(start);
323 
324         assertTrue(writeable.getValues(start).isEmpty());
325 
326         writeable.addValue(start, "file1");
327 
328         assertEquals("file1", writeable.getValue(start));
329         assertEquals("file1", writeable.getValue("start"));
330         assertEquals("file1", writeable.getValue("go"));
331         assertEquals(1, writeable.getValues(start).size());
332         assertEquals(1, writeable.getValues("start").size());
333         assertEquals(1, writeable.getValues("go").size());
334         assertTrue(writeable.getValues(start).contains("file1"));
335         assertTrue(writeable.getValues("start").contains("file1"));
336         assertTrue(writeable.getValues("go").contains("file1"));
337 
338         writeable.addValue(start, "file2");
339 
340         try {
341             writeable.getValue(start);
342             fail("Cannot get single value if multiple are present");
343         } catch (IllegalStateException ise) {
344             assertEquals(resources.getMessage(ResourceConstants.ARGUMENT_TOO_MANY_VALUES),
345                          ise.getMessage());
346         }
347 
348         try {
349             writeable.getValue("start");
350             fail("Cannot get single value if multiple are present");
351         } catch (IllegalStateException ise) {
352             assertEquals(resources.getMessage(ResourceConstants.ARGUMENT_TOO_MANY_VALUES),
353                          ise.getMessage());
354         }
355 
356         writeable.getValues(start).add("file3");
357     }
358 
359     public final void testSwitches() {
360         final Option option = new PropertyOption();
361         final List args = CLITestCase.list();
362         final WriteableCommandLine writeable = OptionTestCase.commandLine(option, args);
363 
364         final Option start = CommandTest.buildStartCommand();
365 
366         assertNull(writeable.getSwitch(start));
367         assertNull(writeable.getSwitch("start"));
368         assertNull(writeable.getSwitch("go"));
369 
370         writeable.addSwitch(start, true);
371 
372         try {
373             writeable.addSwitch(start, false);
374             fail("Switch cannot be changed");
375         } catch (IllegalStateException ise) {
376             assertEquals(resources.getMessage(ResourceConstants.SWITCH_ALREADY_SET),
377                          ise.getMessage());
378         }
379     }
380 
381     public final void testSwitches_True() {
382         final Option option = new PropertyOption();
383         final List args = CLITestCase.list();
384         final WriteableCommandLine writeable = OptionTestCase.commandLine(option, args);
385 
386         final Option start = CommandTest.buildStartCommand();
387 
388         writeable.addSwitch(start, true);
389         assertSame(Boolean.TRUE, writeable.getSwitch(start));
390     }
391 
392     public final void testSwitches_False() {
393         final Option option = new PropertyOption();
394         final List args = CLITestCase.list();
395         final WriteableCommandLine writeable = OptionTestCase.commandLine(option, args);
396 
397         final Option start = CommandTest.buildStartCommand();
398 
399         writeable.addSwitch(start, false);
400         assertSame(Boolean.FALSE, writeable.getSwitch(start));
401     }
402 
403     //    public final void testLooksLikeOption() {
404     //        final Option option = new PropertyOption();
405     //        final List args = OptionTestCase.list();
406     //        final WriteableCommandLine commandLine =
407     //            OptionTestCase.commandLine(option, args);
408     //
409     //        assertTrue(commandLine.looksLikeOption("-D"));
410     //        assertFalse(commandLine.looksLikeOption("--help"));
411     //        assertFalse(commandLine.looksLikeOption("+display"));
412     //        assertFalse(commandLine.looksLikeOption("myprefix"));
413     //        assertFalse(commandLine.looksLikeOption("myprefix2"));
414     //        assertFalse(commandLine.looksLikeOption("myprefference"));
415     //        assertFalse(commandLine.looksLikeOption("/SCANDISK"));
416     //        assertFalse(commandLine.looksLikeOption("update"));
417     //    }
418     public final void testGetOptions_Order()
419         throws OptionException {
420         final Option help = DefaultOptionTest.buildHelpOption();
421         final Option login = CommandTest.buildLoginCommand();
422         final Option targets = ArgumentTest.buildTargetsArgument();
423 
424         final Group group =
425             new GroupBuilder().withOption(help).withOption(login).withOption(targets).create();
426 
427         final Parser parser = new Parser();
428         parser.setGroup(group);
429 
430         final CommandLine cl =
431             parser.parse(new String[] { "login", "rob", "--help", "target1", "target2" });
432 
433         final Iterator i = cl.getOptions().iterator();
434 
435         assertSame(login, i.next());
436         assertSame(group, i.next());
437         assertSame(help, i.next());
438         assertSame(targets, i.next());
439         assertSame(targets, i.next());
440         assertFalse(i.hasNext());
441     }
442 
443     public final void testGetOptionCount()
444         throws OptionException {
445         final Option help = DefaultOptionTest.buildHelpOption();
446         final Option login = CommandTest.buildLoginCommand();
447         final Option targets = ArgumentTest.buildTargetsArgument();
448         final Option display = SwitchTest.buildDisplaySwitch();
449 
450         final Group group =
451             new GroupBuilder().withOption(help).withOption(login).withOption(targets)
452                               .withOption(display).create();
453 
454         final Parser parser = new Parser();
455         parser.setGroup(group);
456 
457         final CommandLine cl =
458             parser.parse(new String[] {
459                              "--help", "login", "rob", "+display", "--help", "--help", "target1",
460                              "target2"
461                          });
462 
463         assertEquals(1, cl.getOptionCount(login));
464         assertEquals(3, cl.getOptionCount(help));
465         assertEquals(2, cl.getOptionCount(targets));
466         assertEquals(1, cl.getOptionCount(display));
467     }
468 
469     public final void testGetOptionCount_Strings()
470         throws OptionException {
471         final Option help = DefaultOptionTest.buildHelpOption();
472         final Option login = CommandTest.buildLoginCommand();
473         final Option targets = ArgumentTest.buildTargetsArgument();
474         final Option display = SwitchTest.buildDisplaySwitch();
475 
476         final Group group =
477             new GroupBuilder().withOption(help).withOption(login).withOption(targets)
478                               .withOption(display).create();
479 
480         final Parser parser = new Parser();
481         parser.setGroup(group);
482 
483         final CommandLine cl =
484             parser.parse(new String[] {
485                              "--help", "login", "rob", "+display", "--help", "--help", "target1",
486                              "target2"
487                          });
488 
489         assertEquals(1, cl.getOptionCount("login"));
490         assertEquals(3, cl.getOptionCount("-?"));
491         assertEquals(1, cl.getOptionCount("+display"));
492     }
493 
494     public final void testOptionAsArgument()
495         throws OptionException {
496         final Option p = new DefaultOptionBuilder().withShortName("p").create();
497         final Argument argument = new ArgumentBuilder().create();
498         final Option withArgument =
499             new DefaultOptionBuilder().withShortName("attr").withArgument(argument).create();
500 
501         final Group group = new GroupBuilder().withOption(p).withOption(withArgument).create();
502 
503         final Parser parser = new Parser();
504         parser.setGroup(group);
505 
506         final CommandLine cl = parser.parse(new String[] { "-p", "-attr", "p" });
507 
508         assertEquals(1, cl.getOptionCount("-p"));
509         assertTrue(cl.hasOption("-p"));
510         assertTrue(cl.hasOption("-attr"));
511         assertTrue(cl.getValue("-attr").equals("p"));
512     }
513 }