Skip to content

Commit 135281e

Browse files
committed
Fixed the definition of the h option in ValuesTest (1 argument instead of 2)
Code simplification git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@780217 13f79535-47bb-0310-9956-ffa450edef68
1 parent 560487a commit 135281e

2 files changed

Lines changed: 53 additions & 60 deletions

File tree

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,22 @@
132132
<version>3.8.2</version>
133133
<scope>test</scope>
134134
</dependency>
135+
<dependency>
136+
<groupId>junit-addons</groupId>
137+
<artifactId>junit-addons</artifactId>
138+
<version>1.4</version>
139+
<scope>test</scope>
140+
<exclusions>
141+
<exclusion>
142+
<groupId>xerces</groupId>
143+
<artifactId>xercesImpl</artifactId>
144+
</exclusion>
145+
<exclusion>
146+
<groupId>xerces</groupId>
147+
<artifactId>xmlParserAPIs</artifactId>
148+
</exclusion>
149+
</exclusions>
150+
</dependency>
135151
</dependencies>
136152

137153
<properties>

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

Lines changed: 37 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717

1818
package org.apache.commons.cli;
1919

20-
import java.util.Arrays;
21-
2220
import junit.framework.TestCase;
21+
import junitx.framework.ArrayAssert;
2322

2423
public class ValuesTest extends TestCase
2524
{
26-
/** CommandLine instance */
27-
private CommandLine _cmdline = null;
25+
private CommandLine cmd;
2826

2927
public void setUp() throws Exception
3028
{
@@ -38,7 +36,7 @@ public void setUp() throws Exception
3836
options.addOption(OptionBuilder.withLongOpt("e").hasArgs().withDescription("set -e ").create('e'));
3937
options.addOption("f", "f", false, "jk");
4038
options.addOption(OptionBuilder.withLongOpt("g").hasArgs(2).withDescription("set -g").create('g'));
41-
options.addOption(OptionBuilder.withLongOpt("h").hasArgs(2).withDescription("set -h").create('h'));
39+
options.addOption(OptionBuilder.withLongOpt("h").hasArg().withDescription("set -h").create('h'));
4240
options.addOption(OptionBuilder.withLongOpt("i").withDescription("set -i").create('i'));
4341
options.addOption(OptionBuilder.withLongOpt("j").hasArgs().withDescription("set -j").withValueSeparator('=').create('j'));
4442
options.addOption(OptionBuilder.withLongOpt("k").hasArgs().withDescription("set -k").withValueSeparator('=').create('k'));
@@ -62,91 +60,70 @@ public void setUp() throws Exception
6260

6361
CommandLineParser parser = new PosixParser();
6462

65-
_cmdline = parser.parse(options,args);
63+
cmd = parser.parse(options,args);
6664
}
6765

6866
public void testShortArgs()
6967
{
70-
assertTrue( _cmdline.hasOption("a") );
71-
assertTrue( _cmdline.hasOption("c") );
68+
assertTrue("Option a is not set", cmd.hasOption("a"));
69+
assertTrue("Option c is not set", cmd.hasOption("c"));
7270

73-
assertNull( _cmdline.getOptionValues("a") );
74-
assertNull( _cmdline.getOptionValues("c") );
71+
assertNull(cmd.getOptionValues("a"));
72+
assertNull(cmd.getOptionValues("c"));
7573
}
7674

7775
public void testShortArgsWithValue()
7876
{
79-
assertTrue( _cmdline.hasOption("b") );
80-
assertTrue( _cmdline.getOptionValue("b").equals("foo"));
81-
assertEquals(1, _cmdline.getOptionValues("b").length);
77+
assertTrue("Option b is not set", cmd.hasOption("b"));
78+
assertTrue(cmd.getOptionValue("b").equals("foo"));
79+
assertEquals(1, cmd.getOptionValues("b").length);
8280

83-
assertTrue( _cmdline.hasOption("d") );
84-
assertTrue( _cmdline.getOptionValue("d").equals("bar"));
85-
assertEquals(1, _cmdline.getOptionValues("d").length);
81+
assertTrue("Option d is not set", cmd.hasOption("d"));
82+
assertTrue(cmd.getOptionValue("d").equals("bar"));
83+
assertEquals(1, cmd.getOptionValues("d").length);
8684
}
8785

8886
public void testMultipleArgValues()
8987
{
90-
String[] result = _cmdline.getOptionValues("e");
91-
String[] values = new String[] { "one", "two" };
92-
assertTrue( _cmdline.hasOption("e") );
93-
assertEquals(2, _cmdline.getOptionValues("e").length);
94-
assertTrue( Arrays.equals( values, _cmdline.getOptionValues("e") ) );
88+
assertTrue("Option e is not set", cmd.hasOption("e"));
89+
ArrayAssert.assertEquals(new String[] { "one", "two" }, cmd.getOptionValues("e"));
9590
}
9691

9792
public void testTwoArgValues()
9893
{
99-
String[] result = _cmdline.getOptionValues("g");
100-
String[] values = new String[] { "val1", "val2" };
101-
assertTrue( _cmdline.hasOption("g") );
102-
assertEquals(2, _cmdline.getOptionValues("g").length);
103-
assertTrue( Arrays.equals( values, _cmdline.getOptionValues("g") ) );
94+
assertTrue("Option g is not set", cmd.hasOption("g"));
95+
ArrayAssert.assertEquals(new String[] { "val1", "val2" }, cmd.getOptionValues("g"));
10496
}
10597

10698
public void testComplexValues()
10799
{
108-
String[] result = _cmdline.getOptionValues("h");
109-
String[] values = new String[] { "val1", "val2" };
110-
assertTrue( _cmdline.hasOption("i") );
111-
assertTrue( _cmdline.hasOption("h") );
112-
assertEquals(2, _cmdline.getOptionValues("h").length);
113-
assertTrue( Arrays.equals( values, _cmdline.getOptionValues("h") ) );
100+
assertTrue("Option i is not set", cmd.hasOption("i"));
101+
assertTrue("Option h is not set", cmd.hasOption("h"));
102+
ArrayAssert.assertEquals(new String[] { "val1", "val2" }, cmd.getOptionValues("h"));
114103
}
115104

116105
public void testExtraArgs()
117106
{
118-
String[] args = new String[] { "arg1", "arg2", "arg3" };
119-
assertEquals(3, _cmdline.getArgs().length);
120-
assertTrue( Arrays.equals( args, _cmdline.getArgs() ) );
107+
ArrayAssert.assertEquals("Extra args", new String[] { "arg1", "arg2", "arg3" }, cmd.getArgs());
121108
}
122109

123110
public void testCharSeparator()
124111
{
125-
// tests the char methods of CommandLine that delegate to
126-
// the String methods
127-
String[] values = new String[] { "key", "value", "key", "value" };
128-
assertTrue( _cmdline.hasOption( "j" ) );
129-
assertTrue( _cmdline.hasOption( 'j' ) );
130-
assertEquals( 4, _cmdline.getOptionValues( "j" ).length );
131-
assertEquals( 4, _cmdline.getOptionValues( 'j' ).length );
132-
assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "j" ) ) );
133-
assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'j' ) ) );
134-
135-
values = new String[] { "key1", "value1", "key2", "value2" };
136-
assertTrue( _cmdline.hasOption( "k" ) );
137-
assertTrue( _cmdline.hasOption( 'k' ) );
138-
assertEquals(4, _cmdline.getOptionValues( "k" ).length);
139-
assertEquals(4, _cmdline.getOptionValues( 'k' ).length);
140-
assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "k" ) ) );
141-
assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'k' ) ) );
142-
143-
values = new String[] { "key", "value" };
144-
assertTrue( _cmdline.hasOption( "m" ) );
145-
assertTrue( _cmdline.hasOption( 'm' ) );
146-
assertEquals(2, _cmdline.getOptionValues( "m" ).length);
147-
assertEquals(2, _cmdline.getOptionValues( 'm' ).length);
148-
assertTrue( Arrays.equals( values, _cmdline.getOptionValues( "m" ) ) );
149-
assertTrue( Arrays.equals( values, _cmdline.getOptionValues( 'm' ) ) );
112+
// tests the char methods of CommandLine that delegate to the String methods
113+
assertTrue("Option j is not set", cmd.hasOption("j"));
114+
assertTrue("Option j is not set", cmd.hasOption('j'));
115+
ArrayAssert.assertEquals(new String[] { "key", "value", "key", "value" }, cmd.getOptionValues("j"));
116+
ArrayAssert.assertEquals(new String[] { "key", "value", "key", "value" }, cmd.getOptionValues('j'));
117+
118+
assertTrue("Option k is not set", cmd.hasOption("k"));
119+
assertTrue("Option k is not set", cmd.hasOption('k'));
120+
ArrayAssert.assertEquals(new String[] { "key1", "value1", "key2", "value2" }, cmd.getOptionValues("k"));
121+
ArrayAssert.assertEquals(new String[] { "key1", "value1", "key2", "value2" }, cmd.getOptionValues('k'));
122+
123+
assertTrue("Option m is not set", cmd.hasOption("m"));
124+
assertTrue("Option m is not set", cmd.hasOption('m'));
125+
ArrayAssert.assertEquals(new String[] { "key", "value" }, cmd.getOptionValues("m"));
126+
ArrayAssert.assertEquals(new String[] { "key", "value" }, cmd.getOptionValues('m'));
150127
}
151128

152129
/**

0 commit comments

Comments
 (0)