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.Group;
22  import org.apache.commons.cli2.Option;
23  import org.apache.commons.cli2.OptionException;
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  
28  /**
29   * @author John Keyes
30   */
31  public class Bug13886Test extends TestCase {
32  
33      public Bug13886Test(final String name) {
34          super(name);
35      }
36  
37      public void testMandatoryGroup() throws Exception {
38          final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
39          final GroupBuilder gbuilder = new GroupBuilder();
40  
41          final Option a = obuilder.withShortName("a").create();
42  
43          final Option b = obuilder.withShortName("b").create();
44  
45          final Group options =
46              gbuilder
47                  .withOption(a)
48                  .withOption(b)
49                  .withMaximum(1)
50                  .withMinimum(1)
51                  .create();
52  
53          final Parser parser = new Parser();
54          parser.setGroup(options);
55  
56          try {
57              parser.parse(new String[] {
58              });
59              fail("Expected MissingOptionException not caught");
60          }
61          catch (final OptionException exp) {
62              assertEquals("Missing option -a|-b", exp.getMessage());
63          }
64  
65          try {
66              parser.parse(new String[] { "-a" });
67          }
68          catch (final OptionException exp) {
69              fail("Unexpected MissingOptionException caught");
70          }
71  
72          try {
73              parser.parse(new String[] { "-b" });
74          }
75          catch (final OptionException exp) {
76              fail("Unexpected MissingOptionException caught");
77          }
78  
79          try {
80              parser.parse(new String[] { "-a", "-b" });
81              fail("Expected UnexpectedOptionException not caught");
82          }
83          catch (final OptionException exp) {
84              assertEquals(
85                  "Unexpected -b while processing -a|-b",
86                  exp.getMessage());
87          }
88      }
89  }