Skip to content

Commit 6728974

Browse files
committed
Merged the common parser tests into ParserTestCase
Fixed the integration test for Groovy, highlighting a regression in CLI 1.2 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@695410 13f79535-47bb-0310-9956-ffa450edef68
1 parent 363f8ab commit 6728974

5 files changed

Lines changed: 355 additions & 433 deletions

File tree

src/test/org/apache/commons/cli/ApplicationTest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
import junit.framework.TestCase;
2121

2222
/**
23+
* This is a collection of tests that test real world applications command lines.
24+
*
2325
* <p>
24-
* This is a collection of tests that test real world
25-
* applications command lines.
26-
* </p>
27-
*
28-
* <p>
29-
* The following are the applications that are tested:
26+
* The following applications are tested:
3027
* <ul>
31-
* <li>Ant</li>
28+
* <li>ls</li>
29+
* <li>Ant</li>
30+
* <li>Groovy</li>
3231
* </ul>
3332
* </p>
3433
*
@@ -164,7 +163,7 @@ public void testGroovy() throws Exception {
164163
.create('a'));
165164

166165
Parser parser = new PosixParser();
167-
CommandLine line = parser.parse(options, new String[] { "-e", "println 'hello'" });
166+
CommandLine line = parser.parse(options, new String[] { "-e", "println 'hello'" }, true);
168167

169168
assertTrue(line.hasOption('e'));
170169
assertEquals("println 'hello'", line.getOptionValue('e'));

src/test/org/apache/commons/cli/BasicParserTest.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,40 @@
1717

1818
package org.apache.commons.cli;
1919

20-
import junit.framework.TestCase;
21-
2220
/**
2321
* @author Emmanuel Bourg
2422
* @version $Revision$, $Date$
2523
*/
26-
public class BasicParserTest extends TestCase
24+
public class BasicParserTest extends ParserTestCase
2725
{
28-
public void testParser() throws Exception {
29-
String[] args = new String[] { "-f", "1" };
26+
public void setUp()
27+
{
28+
super.setUp();
29+
parser = new BasicParser();
30+
}
3031

31-
Options options = new Options();
32-
options.addOption("f", "foo", true, null);
32+
public void testPropertiesOption() throws Exception
33+
{
34+
// not supported by the BasicParser
35+
}
3336

34-
CommandLine cl = new BasicParser().parse(options, args);
37+
public void testShortWithEqual() throws Exception
38+
{
39+
// not supported by the BasicParser
40+
}
3541

36-
assertNotNull("null CommandLine", cl);
37-
assertEquals("1", cl.getOptionValue("foo"));
42+
public void testShortWithoutEqual() throws Exception
43+
{
44+
// not supported by the BasicParser
45+
}
46+
47+
public void testLongWithEqual() throws Exception
48+
{
49+
// not supported by the BasicParser
50+
}
3851

52+
public void testLongWithEqualSingleDash() throws Exception
53+
{
54+
// not supported by the BasicParser
3955
}
4056
}

src/test/org/apache/commons/cli/GnuParserTest.java

Lines changed: 3 additions & 243 deletions
Original file line numberDiff line numberDiff line change
@@ -17,251 +17,11 @@
1717

1818
package org.apache.commons.cli;
1919

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
2621
{
27-
private Options options;
28-
private Parser parser;
29-
3022
public void setUp()
3123
{
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();
26626
}
26727
}

0 commit comments

Comments
 (0)