|
17 | 17 |
|
18 | 18 | package org.apache.commons.cli; |
19 | 19 |
|
20 | | -import java.util.Arrays; |
21 | | -import java.util.List; |
22 | | - |
23 | | -import junit.framework.TestCase; |
24 | | - |
25 | | -public class GnuParserTest extends TestCase |
| 20 | +public class GnuParserTest extends ParserTestCase |
26 | 21 | { |
27 | | - private Options options; |
28 | | - private Parser parser; |
29 | | - |
30 | 22 | public void setUp() |
31 | 23 | { |
32 | | - options = new Options() |
33 | | - .addOption("a", "enable-a", false, "turn [a] on or off") |
34 | | - .addOption("b", "bfile", true, "set the value of [b]") |
35 | | - .addOption("c", "copt", false, "turn [c] on or off"); |
36 | | - |
37 | | - parser = new GnuParser( ); |
38 | | - } |
39 | | - |
40 | | - public void testSimpleShort() throws Exception |
41 | | - { |
42 | | - String[] args = new String[] { "-a", |
43 | | - "-b", "toast", |
44 | | - "foo", "bar" }; |
45 | | - |
46 | | - CommandLine cl = parser.parse(options, args); |
47 | | - |
48 | | - assertTrue("Confirm -a is set", cl.hasOption("a")); |
49 | | - assertTrue("Confirm -b is set", cl.hasOption("b")); |
50 | | - assertTrue("Confirm arg of -b", cl.getOptionValue("b").equals("toast")); |
51 | | - assertTrue("Confirm size of extra args", cl.getArgList().size() == 2); |
52 | | - } |
53 | | - |
54 | | - public void testSimpleLong() throws Exception |
55 | | - { |
56 | | - String[] args = new String[] { "--enable-a", |
57 | | - "--bfile", "toast", |
58 | | - "foo", "bar" }; |
59 | | - |
60 | | - CommandLine cl = parser.parse(options, args); |
61 | | - |
62 | | - assertTrue("Confirm -a is set", cl.hasOption("a")); |
63 | | - assertTrue("Confirm -b is set", cl.hasOption("b")); |
64 | | - assertTrue("Confirm arg of -b", cl.getOptionValue("b").equals("toast")); |
65 | | - assertTrue("Confirm size of extra args", cl.getArgList().size() == 2); |
66 | | - } |
67 | | - |
68 | | - public void testUnrecognizedOption() throws Exception |
69 | | - { |
70 | | - String[] args = new String[] { "-a", "-d", "-b", "toast", "foo", "bar" }; |
71 | | - |
72 | | - try |
73 | | - { |
74 | | - parser.parse(options, args); |
75 | | - fail("UnrecognizedOptionException wasn't thrown"); |
76 | | - } |
77 | | - catch (UnrecognizedOptionException e) |
78 | | - { |
79 | | - assertEquals("-d", e.getOption()); |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - public void testMissingArg() throws Exception |
84 | | - { |
85 | | - String[] args = new String[] { "-b" }; |
86 | | - |
87 | | - boolean caught = false; |
88 | | - |
89 | | - try |
90 | | - { |
91 | | - parser.parse(options, args); |
92 | | - } |
93 | | - catch (MissingArgumentException e) |
94 | | - { |
95 | | - caught = true; |
96 | | - assertEquals("option missing an argument", "b", e.getOption().getOpt()); |
97 | | - } |
98 | | - |
99 | | - assertTrue( "Confirm MissingArgumentException caught", caught ); |
100 | | - } |
101 | | - |
102 | | - public void testStop() throws Exception |
103 | | - { |
104 | | - String[] args = new String[] { "-c", |
105 | | - "foober", |
106 | | - "-b", |
107 | | - "toast" }; |
108 | | - |
109 | | - CommandLine cl = parser.parse(options, args, true); |
110 | | - assertTrue("Confirm -c is set", cl.hasOption("c")); |
111 | | - assertTrue("Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3); |
112 | | - } |
113 | | - |
114 | | - public void testMultiple() throws Exception |
115 | | - { |
116 | | - String[] args = new String[] { "-c", |
117 | | - "foobar", |
118 | | - "-b", |
119 | | - "toast" }; |
120 | | - |
121 | | - CommandLine cl = parser.parse(options, args, true); |
122 | | - assertTrue("Confirm -c is set", cl.hasOption("c")); |
123 | | - assertTrue("Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3); |
124 | | - |
125 | | - cl = parser.parse(options, cl.getArgs()); |
126 | | - |
127 | | - assertTrue("Confirm -c is not set", !cl.hasOption("c")); |
128 | | - assertTrue("Confirm -b is set", cl.hasOption("b")); |
129 | | - assertTrue("Confirm arg of -b", cl.getOptionValue("b").equals("toast")); |
130 | | - assertTrue("Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); |
131 | | - assertTrue("Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar")); |
132 | | - } |
133 | | - |
134 | | - public void testMultipleWithLong() throws Exception |
135 | | - { |
136 | | - String[] args = new String[] { "--copt", |
137 | | - "foobar", |
138 | | - "--bfile", "toast" }; |
139 | | - |
140 | | - CommandLine cl = parser.parse(options,args, true); |
141 | | - assertTrue("Confirm -c is set", cl.hasOption("c")); |
142 | | - assertTrue("Confirm 3 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 3); |
143 | | - |
144 | | - cl = parser.parse(options, cl.getArgs()); |
145 | | - |
146 | | - assertTrue("Confirm -c is not set", !cl.hasOption("c")); |
147 | | - assertTrue("Confirm -b is set", cl.hasOption("b")); |
148 | | - assertTrue("Confirm arg of -b", cl.getOptionValue("b").equals("toast")); |
149 | | - assertTrue("Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); |
150 | | - assertTrue("Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("foobar")); |
151 | | - } |
152 | | - |
153 | | - public void testDoubleDash() throws Exception |
154 | | - { |
155 | | - String[] args = new String[] { "--copt", |
156 | | - "--", |
157 | | - "-b", "toast" }; |
158 | | - |
159 | | - CommandLine cl = parser.parse(options, args); |
160 | | - |
161 | | - assertTrue("Confirm -c is set", cl.hasOption("c")); |
162 | | - assertTrue("Confirm -b is not set", !cl.hasOption("b")); |
163 | | - assertTrue("Confirm 2 extra args: " + cl.getArgList().size(), cl.getArgList().size() == 2); |
164 | | - } |
165 | | - |
166 | | - public void testSingleDash() throws Exception |
167 | | - { |
168 | | - String[] args = new String[] { "--copt", |
169 | | - "-b", "-", |
170 | | - "-a", |
171 | | - "-" }; |
172 | | - |
173 | | - CommandLine cl = parser.parse(options, args); |
174 | | - |
175 | | - assertTrue("Confirm -a is set", cl.hasOption("a")); |
176 | | - assertTrue("Confirm -b is set", cl.hasOption("b")); |
177 | | - assertTrue("Confirm arg of -b", cl.getOptionValue("b").equals("-")); |
178 | | - assertTrue("Confirm 1 extra arg: " + cl.getArgList().size(), cl.getArgList().size() == 1); |
179 | | - assertTrue("Confirm value of extra arg: " + cl.getArgList().get(0), cl.getArgList().get(0).equals("-")); |
180 | | - } |
181 | | - |
182 | | - public void testNegativeArgument() throws Exception |
183 | | - { |
184 | | - String[] args = new String[] { "-a", "-1"} ; |
185 | | - |
186 | | - Options options = new Options(); |
187 | | - options.addOption(OptionBuilder.hasArg().create("a")); |
188 | | - |
189 | | - Parser parser = new GnuParser(); |
190 | | - CommandLine cl = parser.parse(options, args); |
191 | | - assertEquals("-1", cl.getOptionValue("a")); |
192 | | - } |
193 | | - |
194 | | - public void testShortWithEqual() throws Exception |
195 | | - { |
196 | | - String[] args = new String[] { "-f=bar" }; |
197 | | - |
198 | | - Options options = new Options(); |
199 | | - options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f')); |
200 | | - |
201 | | - Parser parser = new GnuParser(); |
202 | | - CommandLine cl = parser.parse(options, args); |
203 | | - |
204 | | - assertEquals("bar", cl.getOptionValue("foo")); |
205 | | - } |
206 | | - |
207 | | - public void testShortWithoutEqual() throws Exception |
208 | | - { |
209 | | - String[] args = new String[] { "-fbar" }; |
210 | | - |
211 | | - Options options = new Options(); |
212 | | - options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f')); |
213 | | - |
214 | | - Parser parser = new GnuParser(); |
215 | | - CommandLine cl = parser.parse(options, args); |
216 | | - |
217 | | - assertEquals("bar", cl.getOptionValue("foo")); |
218 | | - } |
219 | | - |
220 | | - public void testLongWithEqual() throws Exception |
221 | | - { |
222 | | - String[] args = new String[] { "--foo=bar" }; |
223 | | - |
224 | | - Options options = new Options(); |
225 | | - options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f')); |
226 | | - |
227 | | - Parser parser = new GnuParser(); |
228 | | - CommandLine cl = parser.parse(options, args); |
229 | | - |
230 | | - assertEquals("bar", cl.getOptionValue("foo")); |
231 | | - } |
232 | | - |
233 | | - public void testLongWithEqualSingleDash() throws Exception |
234 | | - { |
235 | | - String[] args = new String[] { "-foo=bar" }; |
236 | | - |
237 | | - Options options = new Options(); |
238 | | - options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f')); |
239 | | - |
240 | | - Parser parser = new GnuParser(); |
241 | | - CommandLine cl = parser.parse(options, args); |
242 | | - |
243 | | - assertEquals("bar", cl.getOptionValue("foo")); |
244 | | - } |
245 | | - |
246 | | - public void testPropertiesOption() throws Exception |
247 | | - { |
248 | | - String[] args = new String[] { "-Jsource=1.5", "-Jtarget=1.5", "foo" }; |
249 | | - |
250 | | - Options options = new Options(); |
251 | | - options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).create('J')); |
252 | | - |
253 | | - Parser parser = new GnuParser(); |
254 | | - CommandLine cl = parser.parse(options, args); |
255 | | - |
256 | | - List values = Arrays.asList(cl.getOptionValues("J")); |
257 | | - assertNotNull("null values", values); |
258 | | - assertEquals("number of values", 4, values.size()); |
259 | | - assertEquals("value 1", "source", values.get(0)); |
260 | | - assertEquals("value 2", "1.5", values.get(1)); |
261 | | - assertEquals("value 3", "target", values.get(2)); |
262 | | - assertEquals("value 4", "1.5", values.get(3)); |
263 | | - List argsleft = cl.getArgList(); |
264 | | - assertEquals("Should be 1 arg left",1,argsleft.size()); |
265 | | - assertEquals("Expecting foo","foo",argsleft.get(0)); |
| 24 | + super.setUp(); |
| 25 | + parser = new GnuParser(); |
266 | 26 | } |
267 | 27 | } |
0 commit comments