001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.cli2;
018
019import java.util.Collections;
020import java.util.Iterator;
021import java.util.List;
022
023import org.apache.commons.cli2.builder.ArgumentBuilder;
024import org.apache.commons.cli2.builder.DefaultOptionBuilder;
025import org.apache.commons.cli2.builder.GroupBuilder;
026import org.apache.commons.cli2.commandline.Parser;
027import org.apache.commons.cli2.option.ArgumentTest;
028import org.apache.commons.cli2.option.CommandTest;
029import org.apache.commons.cli2.option.DefaultOptionTest;
030import org.apache.commons.cli2.option.OptionTestCase;
031import org.apache.commons.cli2.option.PropertyOption;
032import org.apache.commons.cli2.option.SwitchTest;
033import org.apache.commons.cli2.resource.ResourceConstants;
034import org.apache.commons.cli2.resource.ResourceHelper;
035
036public abstract class CommandLineTestCase
037    extends CLITestCase {
038    private static final ResourceHelper resources = ResourceHelper.getResourceHelper();
039    public final Option present =
040        new DefaultOptionBuilder().withLongName("present").withLongName("alsopresent").create();
041    public final Option missing = new DefaultOptionBuilder().withLongName("missing").create();
042    public final Option multiple = new DefaultOptionBuilder().withLongName("multiple").create();
043    public final Option bool = new DefaultOptionBuilder().withLongName("bool").create();
044    public final Option root =
045        new GroupBuilder().withOption(present).withOption(missing).withOption(multiple)
046                          .withOption(bool).create();
047    private CommandLine commandLine;
048
049    protected abstract CommandLine createCommandLine();
050
051    /*
052     * @see TestCase#setUp()
053     */
054    public void setUp()
055        throws Exception {
056        super.setUp();
057        commandLine = createCommandLine();
058    }
059
060    /*
061     * Class to test for boolean hasOption(String)
062     */
063    public final void testHasOptionString() {
064        assertTrue(commandLine.hasOption("--present"));
065        assertTrue(commandLine.hasOption("--alsopresent"));
066        assertFalse(commandLine.hasOption("--missing"));
067    }
068
069    /*
070     * Class to test for boolean hasOption(Option)
071     */
072    public final void testHasOptionOption() {
073        assertTrue(commandLine.hasOption(present));
074        assertFalse(commandLine.hasOption(missing));
075    }
076
077    public final void testGetOption() {
078        assertSame(present, commandLine.getOption("--present"));
079        assertSame(present, commandLine.getOption("--alsopresent"));
080
081        //TODO decide whether the following assertion is valid
082        //assertSame(missing,commandLine.getOption("--missing"));
083    }
084
085    /*
086     * Class to test for List getValues(String)
087     */
088    public final void testGetValuesString() {
089        assertListContentsEqual(list("present value"), commandLine.getValues("--present"));
090        assertListContentsEqual(list("value 1", "value 2", "value 3"),
091                                commandLine.getValues("--multiple"));
092        assertTrue(commandLine.getValues("--missing").isEmpty());
093    }
094
095    /*
096     * Class to test for List getValues(String, List)
097     */
098    public final void testGetValuesStringList() {
099        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}