1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli2.commandline;
18
19 import java.util.Iterator;
20 import java.util.Properties;
21 import java.util.Set;
22
23 import org.apache.commons.cli2.CommandLine;
24 import org.apache.commons.cli2.CommandLineTestCase;
25
26
27
28
29 public class PropertiesCommandLineTest
30 extends CommandLineTestCase {
31 private Properties props = null;
32
33 protected CommandLine createCommandLine() {
34 props = new Properties();
35 props.setProperty("--present", "present value");
36 props.setProperty("--alsopresent", "");
37 props.setProperty("--multiple", "value 1|value 2|value 3");
38 props.setProperty("--bool", "true");
39
40 props.setProperty("present", "present property");
41 return new PropertiesCommandLine(root, props, '|');
42 }
43
44 protected CommandLine createCommandLineNoSep() {
45 props = new Properties();
46 props.setProperty("--present", "present value");
47 props.setProperty("--alsopresent", "");
48 props.setProperty("--multiple", "value 1|value 2|value 3");
49 props.setProperty("--bool", "false");
50
51 props.setProperty("present", "present property");
52 return new PropertiesCommandLine(root, props);
53 }
54
55 public void testPropertyValues() {
56
57 CommandLine cmdline = createCommandLine();
58
59 assertEquals("wrong value", "present value", cmdline.getValue("--present"));
60 assertEquals("wrong value", "present value", cmdline.getValue("--alsopresent"));
61 assertEquals("wrong # of values", 3, cmdline.getValues("--multiple").size());
62 assertEquals("wrong value 1", "value 1", cmdline.getValues("--multiple").get(0));
63 assertEquals("wrong value 2", "value 2", cmdline.getValues("--multiple").get(1));
64 assertEquals("wrong value 3", "value 3", cmdline.getValues("--multiple").get(2));
65 }
66
67 public void testNoSeparator() {
68
69 CommandLine cmdline = createCommandLineNoSep();
70
71 assertEquals("wrong value", "present value", cmdline.getValue("--present"));
72 assertEquals("wrong value", "present value", cmdline.getValue("--alsopresent"));
73 assertEquals("wrong # of values", 1, cmdline.getValues("--multiple").size());
74 assertEquals("wrong value", "value 1|value 2|value 3", cmdline.getValue("--multiple"));
75 assertFalse("expected a false", cmdline.getSwitch("--bool").booleanValue());
76 }
77
78 public void testNullOption() {
79
80 CommandLine cmdline = createCommandLine();
81
82 assertFalse("should not find null option", cmdline.hasOption((String) null));
83 assertTrue("expected a true", cmdline.getSwitch("--bool").booleanValue());
84 }
85
86 public void testPropertyTriggers() {
87
88 CommandLine cmdline = createCommandLine();
89
90 Set triggers = cmdline.getOptionTriggers();
91 Iterator iter = triggers.iterator();
92 assertEquals("wrong # of triggers", 4, triggers.size());
93 assertTrue("cannot find trigger", triggers.contains("--bool"));
94 assertTrue("cannot find trigger", triggers.contains("--present"));
95 assertTrue("cannot find trigger", triggers.contains("--multiple"));
96 assertTrue("cannot find trigger", triggers.contains("--alsopresent"));
97
98 assertFalse("should not find null option", cmdline.hasOption((String) null));
99 assertTrue("expected a true", cmdline.getSwitch("--bool").booleanValue());
100 }
101 }