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.Set;
21 import java.util.prefs.Preferences;
22
23 import org.apache.commons.cli2.CommandLine;
24 import org.apache.commons.cli2.CommandLineTestCase;
25
26
27
28
29 public class PreferencesCommandLineTest extends CommandLineTestCase {
30
31
32
33
34 protected CommandLine createCommandLine() {
35
36 final Preferences props = Preferences.userNodeForPackage(PreferencesCommandLineTest.class);
37 props.put("--present","present value");
38 props.put("--alsopresent","");
39 props.put("--multiple","value 1|value 2|value 3");
40 props.put("--bool","true");
41
42 props.put("present","present property");
43
44 return new PreferencesCommandLine(root,props,'|');
45 }
46
47 protected CommandLine createCommandLineNoSep() {
48
49 final Preferences props = Preferences.userNodeForPackage(PreferencesCommandLineTest.class);
50 props.put("--present","present value");
51 props.put("--alsopresent","");
52 props.put("--multiple","value 1|value 2|value 3");
53 props.put("--bool","false");
54
55 props.put("present","present property");
56
57 return new PreferencesCommandLine(root,props);
58 }
59
60 public void testPropertyValues() {
61
62 CommandLine cmdline = createCommandLine();
63
64 assertEquals("wrong value", "present value", cmdline.getValue("--present"));
65 assertEquals("wrong value", "present value", cmdline.getValue("--alsopresent"));
66 assertEquals("wrong # of values", 3, cmdline.getValues("--multiple").size());
67 assertEquals("wrong value 1", "value 1", cmdline.getValues("--multiple").get(0));
68 assertEquals("wrong value 2", "value 2", cmdline.getValues("--multiple").get(1));
69 assertEquals("wrong value 3", "value 3", cmdline.getValues("--multiple").get(2));
70 }
71
72 public void testNoSeparator() {
73
74 CommandLine cmdline = createCommandLineNoSep();
75
76 assertEquals("wrong value", "present value", cmdline.getValue("--present"));
77 assertEquals("wrong value", "present value", cmdline.getValue("--alsopresent"));
78 assertEquals("wrong # of values", 1, cmdline.getValues("--multiple").size());
79 assertEquals("wrong value", "value 1|value 2|value 3", cmdline.getValue("--multiple"));
80 assertFalse("expected a false", cmdline.getSwitch("--bool").booleanValue());
81 }
82
83 public void testNullOption() {
84
85 CommandLine cmdline = createCommandLine();
86
87 assertFalse("should not find null option", cmdline.hasOption((String) null));
88 assertTrue("expected a true", cmdline.getSwitch("--bool").booleanValue());
89 }
90
91 public void testPreferenceTriggers() {
92
93 CommandLine cmdline = createCommandLine();
94
95 Set triggers = cmdline.getOptionTriggers();
96 Iterator iter = triggers.iterator();
97 assertEquals("wrong # of triggers", 4, triggers.size());
98 assertTrue("cannot find trigger", triggers.contains("--bool"));
99 assertTrue("cannot find trigger", triggers.contains("--present"));
100 assertTrue("cannot find trigger", triggers.contains("--multiple"));
101 assertTrue("cannot find trigger", triggers.contains("--alsopresent"));
102
103 assertFalse("should not find null option", cmdline.hasOption((String) null));
104 assertTrue("expected a true", cmdline.getSwitch("--bool").booleanValue());
105 }
106 }