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.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.ArgumentBuilder;
26 import org.apache.commons.cli2.builder.DefaultOptionBuilder;
27 import org.apache.commons.cli2.builder.GroupBuilder;
28 import org.apache.commons.cli2.commandline.Parser;
29
30
31
32
33
34
35
36
37 public class BugCLI123Test extends TestCase {
38
39 private Option parentOption;
40
41
42 private Option childOption1;
43
44
45 private Option childOption2;
46
47
48 private Group parentGroup;
49
50
51 private Group childGroup;
52
53
54 private Parser parser;
55
56 protected void setUp() throws Exception {
57 super.setUp();
58 final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
59 final ArgumentBuilder abuilder = new ArgumentBuilder();
60 final GroupBuilder gbuilder = new GroupBuilder();
61 parentOption = obuilder.withLongName("parent").withShortName("p")
62 .withArgument(abuilder.withName("name").create()).create();
63 childOption1 = obuilder.withLongName("child").withShortName("c")
64 .withArgument(abuilder.withName("c").create()).create();
65 childOption2 = obuilder.withLongName("sub").withShortName("s")
66 .withArgument(abuilder.withName("s").create()).create();
67 childGroup = gbuilder.withName("childOptions").withMinimum(0)
68 .withMaximum(2).withOption(childOption1).withOption(
69 childOption2).create();
70 parentGroup = gbuilder.withName("parentOptions").withMinimum(1)
71 .withMaximum(1).withOption(parentOption).withOption(childGroup)
72 .create();
73 parser = new Parser();
74 parser.setGroup(parentGroup);
75 }
76
77
78
79
80 public void testSingleChildOption() throws OptionException {
81 CommandLine cl = parser.parse(new String[] { "--child", "test" });
82 assertTrue("Child option not found", cl.hasOption(childOption1));
83 assertEquals("Wrong value for option", "test", cl
84 .getValue(childOption1));
85 assertTrue("Child group not found", cl.hasOption(childGroup));
86 }
87
88
89
90
91 public void testMultipleChildOptions() throws OptionException {
92 CommandLine cl = parser.parse(new String[] { "--child", "test",
93 "--sub", "anotherTest" });
94 assertTrue("Child option not found", cl.hasOption(childOption1));
95 assertEquals("Wrong value for option", "test", cl
96 .getValue(childOption1));
97 assertTrue("Sub option not found", cl.hasOption(childOption2));
98 assertEquals("Wrong value for sub option", "anotherTest", cl
99 .getValue(childOption2));
100 assertTrue("Child group not found", cl.hasOption(childGroup));
101 }
102
103
104
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
115
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
124 }
125 }
126 }