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.bug;
018
019import junit.framework.TestCase;
020
021import org.apache.commons.cli2.CommandLine;
022import org.apache.commons.cli2.Group;
023import org.apache.commons.cli2.Option;
024import org.apache.commons.cli2.OptionException;
025import org.apache.commons.cli2.builder.ArgumentBuilder;
026import org.apache.commons.cli2.builder.DefaultOptionBuilder;
027import org.apache.commons.cli2.builder.GroupBuilder;
028import org.apache.commons.cli2.commandline.Parser;
029
030/**
031 * Group options are not added to the command line when child elements are
032 * detected. This causes the validation of maximum and minimum to fail.
033 *
034 * @author Oliver Heger
035 * @version $Id: BugCLI123Test.java 679530 2008-07-24 20:28:16Z oheger $
036 */
037public class BugCLI123Test extends TestCase {
038    /** An option of the parent group. */
039    private Option parentOption;
040
041    /** An option of the child group. */
042    private Option childOption1;
043
044    /** Another option of the child group. */
045    private Option childOption2;
046
047    /** The parent group. */
048    private Group parentGroup;
049
050    /** The child group. */
051    private Group childGroup;
052
053    /** The parser. */
054    private Parser parser;
055
056    protected void setUp() throws Exception {
057        super.setUp();
058        final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
059        final ArgumentBuilder abuilder = new ArgumentBuilder();
060        final GroupBuilder gbuilder = new GroupBuilder();
061        parentOption = obuilder.withLongName("parent").withShortName("p")
062                .withArgument(abuilder.withName("name").create()).create();
063        childOption1 = obuilder.withLongName("child").withShortName("c")
064                .withArgument(abuilder.withName("c").create()).create();
065        childOption2 = obuilder.withLongName("sub").withShortName("s")
066                .withArgument(abuilder.withName("s").create()).create();
067        childGroup = gbuilder.withName("childOptions").withMinimum(0)
068                .withMaximum(2).withOption(childOption1).withOption(
069                        childOption2).create();
070        parentGroup = gbuilder.withName("parentOptions").withMinimum(1)
071                .withMaximum(1).withOption(parentOption).withOption(childGroup)
072                .create();
073        parser = new Parser();
074        parser.setGroup(parentGroup);
075    }
076
077    /**
078     * A single option of the child group is specified.
079     */
080    public void testSingleChildOption() throws OptionException {
081        CommandLine cl = parser.parse(new String[] { "--child", "test" });
082        assertTrue("Child option not found", cl.hasOption(childOption1));
083        assertEquals("Wrong value for option", "test", cl
084                .getValue(childOption1));
085        assertTrue("Child group not found", cl.hasOption(childGroup));
086    }
087
088    /**
089     * Two options of the child group are specified.
090     */
091    public void testMultipleChildOptions() throws OptionException {
092        CommandLine cl = parser.parse(new String[] { "--child", "test",
093                "--sub", "anotherTest" });
094        assertTrue("Child option not found", cl.hasOption(childOption1));
095        assertEquals("Wrong value for option", "test", cl
096                .getValue(childOption1));
097        assertTrue("Sub option not found", cl.hasOption(childOption2));
098        assertEquals("Wrong value for sub option", "anotherTest", cl
099                .getValue(childOption2));
100        assertTrue("Child group not found", cl.hasOption(childGroup));
101    }
102
103    /**
104     * The option defined for the parent group is specified.
105     */
106    public void testSingleParentOption() throws OptionException {
107        CommandLine cl = parser.parse(new String[] { "--parent", "yes" });
108        assertTrue("Parent option not found", cl.hasOption(parentOption));
109        assertEquals("Wrong value for option", "yes", cl.getValue(parentOption));
110        assertFalse("Found child group", cl.hasOption(childGroup));
111    }
112
113    /**
114     * The parent option and an option of the child group is specified. This
115     * should cause an exception.
116     */
117    public void testParentOptionAndChildOption() throws OptionException {
118        try {
119            parser.parse(new String[] { "--parent", "error", "--child",
120                    "exception" });
121            fail("Maximum restriction for parent not verified!");
122        } catch (OptionException oex) {
123            // ok
124        }
125    }
126}