1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }