Skip to content

Commit ba3db96

Browse files
author
John Keyes
committed
- added tests git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@293045 13f79535-47bb-0310-9956-ffa450edef68
1 parent 5ad6ce2 commit ba3db96

5 files changed

Lines changed: 152 additions & 17 deletions

File tree

src/test/org/apache/commons/cli2/PrecedenceTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void testChildren() throws OptionException {
102102
assertEquals(new String[] { "-f", "-i", "-l", "-e" }, cl);
103103
}
104104

105-
public void XtestSimpleVsArgument() throws OptionException {
105+
public void testSimpleVsArgument() throws OptionException {
106106
final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
107107
final GroupBuilder gBuilder = new GroupBuilder();
108108
final ArgumentBuilder aBuilder = new ArgumentBuilder();
@@ -121,7 +121,7 @@ public void XtestSimpleVsArgument() throws OptionException {
121121
assertEquals(new String[] { "-f" }, cl);
122122
}
123123

124-
public void XtestSimpleVsBurst() throws OptionException {
124+
public void testSimpleVsBurst() throws OptionException {
125125
final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
126126
final GroupBuilder gBuilder = new GroupBuilder();
127127
final Group options =
@@ -137,7 +137,7 @@ public void XtestSimpleVsBurst() throws OptionException {
137137
assertEquals(new String[] { "-f", "-i", "-l", "-e" }, cl);
138138
}
139139

140-
public void XtestSimpleVsChildren() throws OptionException {
140+
public void testSimpleVsChildren() throws OptionException {
141141
final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
142142
final GroupBuilder gBuilder = new GroupBuilder();
143143

@@ -248,7 +248,7 @@ public void testBurstVsChildren() throws OptionException {
248248
cl);
249249
}
250250

251-
public void XtestSimpleVsArgumentVsBurst() throws OptionException {
251+
public void testSimpleVsArgumentVsBurst() throws OptionException {
252252
final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
253253
final GroupBuilder gBuilder = new GroupBuilder();
254254
final ArgumentBuilder aBuilder = new ArgumentBuilder();
@@ -270,7 +270,7 @@ public void XtestSimpleVsArgumentVsBurst() throws OptionException {
270270
assertEquals(new String[] { "-f" }, cl);
271271
}
272272

273-
public void XtestSimpleVsArgumentVsChildren() throws OptionException {
273+
public void testSimpleVsArgumentVsChildren() throws OptionException {
274274
final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
275275
final GroupBuilder gBuilder = new GroupBuilder();
276276
final ArgumentBuilder aBuilder = new ArgumentBuilder();
@@ -300,7 +300,7 @@ public void XtestSimpleVsArgumentVsChildren() throws OptionException {
300300
assertEquals(new String[] { "-f" }, cl);
301301
}
302302

303-
public void XtestSimpleVsBurstVsChildren() throws OptionException {
303+
public void testSimpleVsBurstVsChildren() throws OptionException {
304304
final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
305305
final GroupBuilder gBuilder = new GroupBuilder();
306306

@@ -363,7 +363,7 @@ public void testArgumentVsBurstVsChildren() throws OptionException {
363363
assertEquals(new String[] { "-f" }, cl);
364364
}
365365

366-
public void XtestSimpleVsArgumentVsBurstVsChildren()
366+
public void testSimpleVsArgumentVsBurstVsChildren()
367367
throws OptionException {
368368
final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
369369
final GroupBuilder gBuilder = new GroupBuilder();
@@ -408,9 +408,8 @@ public void assertEquals(final String options[], final CommandLine line) {
408408
final List expected = Arrays.asList(options);
409409
final Set actual = line.getOptionTriggers();
410410

411-
//System.out.println(getName() + ": " + actual);
412-
413411
assertTrue(expected.containsAll(actual));
414412
assertTrue(actual.containsAll(expected));
415413
}
414+
416415
}

src/test/org/apache/commons/cli2/commandline/DefaultingCommandLineTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package org.apache.commons.cli2.commandline;
1717

1818
import java.util.ArrayList;
19+
import java.util.Collections;
1920
import java.util.Iterator;
21+
import java.util.Set;
2022

2123
import org.apache.commons.cli2.CommandLine;
2224
import org.apache.commons.cli2.CommandLineTestCase;
@@ -125,4 +127,24 @@ public final void testInsertCommandLine() {
125127
assertSame(first, i.next());
126128
assertFalse(i.hasNext());
127129
}
130+
131+
public void testTriggers() {
132+
final DefaultingCommandLine defaults = new DefaultingCommandLine();
133+
defaults.appendCommandLine(first);
134+
defaults.appendCommandLine(second);
135+
136+
Set set = defaults.getOptionTriggers();
137+
Iterator iter = set.iterator();
138+
assertEquals("wrong # of triggers", 3, set.size());
139+
assertTrue("cannot find trigger", set.contains("--insecond"));
140+
assertTrue("cannot find trigger", set.contains("--inboth"));
141+
assertTrue("cannot find trigger", set.contains("--infirst"));
142+
}
143+
144+
public void testDefaults() {
145+
final DefaultingCommandLine defaults = new DefaultingCommandLine();
146+
147+
assertEquals("wrong # of defaults", 0, defaults.getValues("--insecond").size());
148+
assertEquals("wrong Set of defaults", Collections.EMPTY_LIST, defaults.getValues("--insecond", null));
149+
}
128150
}

src/test/org/apache/commons/cli2/commandline/PreferencesCommandLineTest.java

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.apache.commons.cli2.commandline;
1717

18+
import java.util.Iterator;
19+
import java.util.Set;
1820
import java.util.prefs.Preferences;
1921

2022
import org.apache.commons.cli2.CommandLine;
@@ -40,8 +42,64 @@ protected CommandLine createCommandLine() {
4042

4143
return new PreferencesCommandLine(root,props,'|');
4244
}
43-
44-
public void testToMakeEclipseSpotTheTestCase(){
45-
// nothing to test
45+
46+
protected CommandLine createCommandLineNoSep() {
47+
// TODO Auto-generated method stub
48+
final Preferences props = Preferences.userNodeForPackage(PreferencesCommandLineTest.class);
49+
props.put("--present","present value");
50+
props.put("--alsopresent","");
51+
props.put("--multiple","value 1|value 2|value 3");
52+
props.put("--bool","false");
53+
54+
props.put("present","present property");
55+
56+
return new PreferencesCommandLine(root,props);
4657
}
58+
59+
public void testPropertyValues() {
60+
// nothing to test
61+
CommandLine cmdline = createCommandLine();
62+
63+
assertEquals("wrong value", "present value", cmdline.getValue("--present"));
64+
assertEquals("wrong value", "present value", cmdline.getValue("--alsopresent"));
65+
assertEquals("wrong # of values", 3, cmdline.getValues("--multiple").size());
66+
assertEquals("wrong value 1", "value 1", cmdline.getValues("--multiple").get(0));
67+
assertEquals("wrong value 2", "value 2", cmdline.getValues("--multiple").get(1));
68+
assertEquals("wrong value 3", "value 3", cmdline.getValues("--multiple").get(2));
69+
}
70+
71+
public void testNoSeparator() {
72+
// nothing to test
73+
CommandLine cmdline = createCommandLineNoSep();
74+
75+
assertEquals("wrong value", "present value", cmdline.getValue("--present"));
76+
assertEquals("wrong value", "present value", cmdline.getValue("--alsopresent"));
77+
assertEquals("wrong # of values", 1, cmdline.getValues("--multiple").size());
78+
assertEquals("wrong value", "value 1|value 2|value 3", cmdline.getValue("--multiple"));
79+
assertFalse("expected a false", cmdline.getSwitch("--bool").booleanValue());
80+
}
81+
82+
public void testNullOption() {
83+
// nothing to test
84+
CommandLine cmdline = createCommandLine();
85+
86+
assertFalse("should not find null option", cmdline.hasOption((String) null));
87+
assertTrue("expected a true", cmdline.getSwitch("--bool").booleanValue());
88+
}
89+
90+
public void testPreferenceTriggers() {
91+
// nothing to test
92+
CommandLine cmdline = createCommandLine();
93+
94+
Set triggers = cmdline.getOptionTriggers();
95+
Iterator iter = triggers.iterator();
96+
assertEquals("wrong # of triggers", 4, triggers.size());
97+
assertTrue("cannot find trigger", triggers.contains("--bool"));
98+
assertTrue("cannot find trigger", triggers.contains("--present"));
99+
assertTrue("cannot find trigger", triggers.contains("--multiple"));
100+
assertTrue("cannot find trigger", triggers.contains("--alsopresent"));
101+
102+
assertFalse("should not find null option", cmdline.hasOption((String) null));
103+
assertTrue("expected a true", cmdline.getSwitch("--bool").booleanValue());
104+
}
47105
}

src/test/org/apache/commons/cli2/commandline/PropertiesCommandLineTest.java

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.apache.commons.cli2.commandline;
1717

18+
import java.util.Iterator;
1819
import java.util.Properties;
20+
import java.util.Set;
1921

2022
import org.apache.commons.cli2.CommandLine;
2123
import org.apache.commons.cli2.CommandLineTestCase;
@@ -27,9 +29,6 @@ public class PropertiesCommandLineTest
2729
extends CommandLineTestCase {
2830
private Properties props = null;
2931

30-
/* (non-Javadoc)
31-
* @see org.apache.commons.cli2.CommandLineTest#createCommandLine()
32-
*/
3332
protected CommandLine createCommandLine() {
3433
props = new Properties();
3534
props.setProperty("--present", "present value");
@@ -38,11 +37,64 @@ protected CommandLine createCommandLine() {
3837
props.setProperty("--bool", "true");
3938

4039
props.setProperty("present", "present property");
40+
return new PropertiesCommandLine(root, props, '|');
41+
}
42+
43+
protected CommandLine createCommandLineNoSep() {
44+
props = new Properties();
45+
props.setProperty("--present", "present value");
46+
props.setProperty("--alsopresent", "");
47+
props.setProperty("--multiple", "value 1|value 2|value 3");
48+
props.setProperty("--bool", "false");
4149

42-
return new PropertiesCommandLine(root, props, '|');
50+
props.setProperty("present", "present property");
51+
return new PropertiesCommandLine(root, props);
4352
}
53+
54+
public void testPropertyValues() {
55+
// nothing to test
56+
CommandLine cmdline = createCommandLine();
57+
58+
assertEquals("wrong value", "present value", cmdline.getValue("--present"));
59+
assertEquals("wrong value", "present value", cmdline.getValue("--alsopresent"));
60+
assertEquals("wrong # of values", 3, cmdline.getValues("--multiple").size());
61+
assertEquals("wrong value 1", "value 1", cmdline.getValues("--multiple").get(0));
62+
assertEquals("wrong value 2", "value 2", cmdline.getValues("--multiple").get(1));
63+
assertEquals("wrong value 3", "value 3", cmdline.getValues("--multiple").get(2));
64+
}
65+
66+
public void testNoSeparator() {
67+
// nothing to test
68+
CommandLine cmdline = createCommandLineNoSep();
69+
70+
assertEquals("wrong value", "present value", cmdline.getValue("--present"));
71+
assertEquals("wrong value", "present value", cmdline.getValue("--alsopresent"));
72+
assertEquals("wrong # of values", 1, cmdline.getValues("--multiple").size());
73+
assertEquals("wrong value", "value 1|value 2|value 3", cmdline.getValue("--multiple"));
74+
assertFalse("expected a false", cmdline.getSwitch("--bool").booleanValue());
75+
}
76+
77+
public void testNullOption() {
78+
// nothing to test
79+
CommandLine cmdline = createCommandLine();
4480

45-
public void testToMakeEclipseSpotTheTestCase() {
81+
assertFalse("should not find null option", cmdline.hasOption((String) null));
82+
assertTrue("expected a true", cmdline.getSwitch("--bool").booleanValue());
83+
}
84+
85+
public void testPropertyTriggers() {
4686
// nothing to test
87+
CommandLine cmdline = createCommandLine();
88+
89+
Set triggers = cmdline.getOptionTriggers();
90+
Iterator iter = triggers.iterator();
91+
assertEquals("wrong # of triggers", 4, triggers.size());
92+
assertTrue("cannot find trigger", triggers.contains("--bool"));
93+
assertTrue("cannot find trigger", triggers.contains("--present"));
94+
assertTrue("cannot find trigger", triggers.contains("--multiple"));
95+
assertTrue("cannot find trigger", triggers.contains("--alsopresent"));
96+
97+
assertFalse("should not find null option", cmdline.hasOption((String) null));
98+
assertTrue("expected a true", cmdline.getSwitch("--bool").booleanValue());
4799
}
48100
}

src/test/org/apache/commons/cli2/option/ArgumentTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,14 @@ public void testProcess_InterrogatedDefaultValues()
591591

592592
bounds.process(commandLine, iterator);
593593

594+
// test with values
594595
List values = new ArrayList();
595596
values.add("50");
596597
values.add("100");
597598
assertEquals(values, commandLine.getValues(bounds, values));
599+
600+
// test without values
601+
assertEquals(Collections.EMPTY_LIST, commandLine.getValues(bounds, null));
598602
}
599603

600604
public void testProcess_StripBoundaryQuotes()

0 commit comments

Comments
 (0)