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.DefaultOptionBuilder;
26 import org.apache.commons.cli2.builder.GroupBuilder;
27 import org.apache.commons.cli2.commandline.Parser;
28
29
30
31
32
33
34
35
36 public class BugCLI159Test extends TestCase
37 {
38
39 private Group parent;
40
41
42 private Group child;
43
44
45 private Parser parser;
46
47
48
49
50
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
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
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
102 }
103 }
104
105
106
107
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
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
132
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
147 }
148 }
149
150
151
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
167 }
168 }
169 }