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.bug;
18  
19  import junit.framework.TestCase;
20  
21  import org.apache.commons.cli2.CommandLine;
22  import org.apache.commons.cli2.Group;
23  import org.apache.commons.cli2.Option;
24  import org.apache.commons.cli2.OptionException;
25  import org.apache.commons.cli2.builder.DefaultOptionBuilder;
26  import org.apache.commons.cli2.builder.GroupBuilder;
27  import org.apache.commons.cli2.commandline.Parser;
28  
29  /**
30   * Inconsistent handling of minimum and maximum constraints for groups and their
31   * child groups.
32   *
33   * @author Oliver Heger
34   * @version $Id: BugCLI159Test.java 680178 2008-07-27 20:32:14Z oheger $
35   */
36  public class BugCLI159Test extends TestCase
37  {
38      /** The parent group. */
39      private Group parent;
40  
41      /** The child group. */
42      private Group child;
43  
44      /** The parser. */
45      private Parser parser;
46  
47      /**
48       * Creates some test options, including a group with a child group.
49       *
50       * @param childGroupRequired a flag whether the child group is required
51       */
52      private void setUpOptions(boolean childGroupRequired)
53      {
54          final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
55          final GroupBuilder gbuilder = new GroupBuilder();
56          Option parentOpt = obuilder.withLongName("parent").withShortName("p")
57                  .create();
58          Option childOpt1 = obuilder.withLongName("child").withShortName("c")
59                  .create();
60          Option childOpt2 = obuilder.withLongName("sub").withShortName("s")
61                  .create();
62          Option childOpt3 = obuilder.withLongName("test").withShortName("t")
63                  .create();
64          child = gbuilder.withName("childOptions").withOption(childOpt1)
65                  .withOption(childOpt2).withOption(childOpt3).withMinimum(2)
66                  .withRequired(childGroupRequired).create();
67          parent = gbuilder.withName("options").withOption(parentOpt).withOption(
68                  child).withMinimum(0).create();
69          parser = new Parser();
70          parser.setGroup(parent);
71      }
72  
73      /**
74       * Tests whether the child group can be omitted.
75       */
76      public void testNoChildGroup() throws OptionException
77      {
78          setUpOptions(false);
79          CommandLine cl = parser.parse(new String[] {
80              "--parent"
81          });
82          assertNotNull("No command line parsed", cl);
83          assertFalse("Child group found", cl.hasOption(child));
84      }
85  
86      /**
87       * Tests whether a required child groupd can be omitted.
88       */
89      public void testNoChildGroupRequired()
90      {
91          setUpOptions(true);
92          try
93          {
94              parser.parse(new String[] {
95                  "--parent"
96              });
97              fail("Missing child group not detected!");
98          }
99          catch (OptionException oex)
100         {
101             // ok
102         }
103     }
104 
105     /**
106      * Tests parsing an empty command line. Because the parent group is optional
107      * this should be possible.
108      */
109     public void testNoOptions() throws OptionException
110     {
111         setUpOptions(false);
112         CommandLine cl = parser.parse(new String[0]);
113         assertFalse("Found parent option", cl.hasOption("--parent"));
114         assertFalse("Found child option", cl.hasOption("--child"));
115     }
116 
117     /**
118      * Tests parsing a command line with options of the child group.
119      */
120     public void testWithChildOptions() throws OptionException
121     {
122         setUpOptions(false);
123         CommandLine cl = parser.parse(new String[] {
124             "-ct"
125         });
126         assertTrue("child option not found", cl.hasOption("--child"));
127         assertTrue("test option not found", cl.hasOption("--test"));
128     }
129 
130     /**
131      * Tests a command line containing options of the child group, but the
132      * minimum constraint is violated.
133      */
134     public void testWithChildOptionsMissing()
135     {
136         setUpOptions(false);
137         try
138         {
139             parser.parse(new String[] {
140                     "--parent", "--sub"
141             });
142             fail("Missing options of child group not detected!");
143         }
144         catch (OptionException oex)
145         {
146             // ok
147         }
148     }
149 
150     /**
151      * Tests whether the root group is always validated.
152      */
153     public void testRequiredRootGroup()
154     {
155         setUpOptions(false);
156         parser.setGroup(child);
157         try
158         {
159             parser.parse(new String[] {
160                 "--test"
161             });
162             fail("Missing options not detected!");
163         }
164         catch (OptionException oex)
165         {
166             // ok
167         }
168     }
169 }