1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli2.option;
18
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.ListIterator;
23 import java.util.Set;
24
25 import org.apache.commons.cli2.DisplaySetting;
26 import org.apache.commons.cli2.HelpLine;
27 import org.apache.commons.cli2.Option;
28 import org.apache.commons.cli2.OptionException;
29 import org.apache.commons.cli2.WriteableCommandLine;
30 import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;
31
32
33
34
35 public class PropertyOptionTest extends OptionTestCase {
36
37
38
39
40
41
42 public void testCanProcess() {
43 final Option option = new PropertyOption();
44 assertTrue(option.canProcess(new WriteableCommandLineImpl(option,null), "-Dmyprop=myval"));
45 }
46
47 public void testCanProcess_Null() {
48 final Option option = new PropertyOption();
49 assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null), (String) null));
50 }
51
52 public void testCanProcess_TooShort() {
53 final Option option = new PropertyOption();
54 assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null), "-D"));
55 }
56
57 public void testCanProcess_BadMatch() {
58 final Option option = new PropertyOption();
59 assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null),"-dump"));
60 }
61
62
63
64
65
66
67 public void testPrefixes() {
68 final Option option = new PropertyOption();
69 assertContentsEqual(list("-D"), option.getPrefixes());
70 }
71
72
73
74
75
76
77 public void testProcess() throws OptionException {
78 final PropertyOption option = new PropertyOption();
79 final List args = list("-Dmyprop=myvalue");
80 final WriteableCommandLine commandLine = commandLine(option, args);
81 final ListIterator iterator = args.listIterator();
82
83 option.process(commandLine, iterator);
84 assertEquals("myvalue", commandLine.getProperty(option, "myprop"));
85 assertFalse(iterator.hasNext());
86 assertEquals(1, commandLine.getProperties(option).size());
87 }
88
89 public void testProcess_UnexpectedOptionException() {
90 final Option option = new PropertyOption();
91 final List args = list("--help");
92 final WriteableCommandLine commandLine = commandLine(option, args);
93 final ListIterator iterator = args.listIterator();
94
95 try {
96 option.process(commandLine, iterator);
97 fail("UnexpectedOption");
98 }
99 catch (final OptionException uoe) {
100 assertEquals(option, uoe.getOption());
101 assertEquals(
102 "Unexpected --help while processing -Dproperty=value",
103 uoe.getMessage());
104 }
105 }
106
107 public void testProcess_BadPropertyException() throws OptionException {
108 final PropertyOption option = new PropertyOption();
109 final List args = list("-Dmyprop");
110 final WriteableCommandLine commandLine = commandLine(option, args);
111 final ListIterator iterator = args.listIterator();
112
113 option.process(commandLine, iterator);
114
115 assertEquals("true", commandLine.getProperty(option, "myprop"));
116 }
117
118 public void testProcess_SetToEmpty() throws OptionException {
119 final PropertyOption option = new PropertyOption();
120 final List args = list("-Dmyprop=");
121 final WriteableCommandLine commandLine = commandLine(option, args);
122 final ListIterator iterator = args.listIterator();
123
124 option.process(commandLine, iterator);
125 assertEquals("", commandLine.getProperty(option, "myprop"));
126 assertFalse(iterator.hasNext());
127 assertEquals(1, commandLine.getProperties(option).size());
128 }
129
130
131
132
133
134
135 public void testTriggers() {
136 final Option option = new PropertyOption();
137
138 assertContentsEqual(list("-D"), option.getTriggers());
139 }
140
141
142
143
144
145
146 public void testValidate() throws OptionException {
147 final Option option = new PropertyOption();
148 final List args = list("-Dproperty=value");
149 final WriteableCommandLine commandLine = commandLine(option, args);
150 final ListIterator iterator = args.listIterator();
151
152 option.process(commandLine, iterator);
153
154 option.validate(commandLine);
155 }
156
157
158
159
160
161
162 public void testAppendUsage() {
163 final Option option = new PropertyOption();
164 final StringBuffer buffer = new StringBuffer();
165 option.appendUsage(buffer, DisplaySetting.ALL, null);
166
167 assertEquals("-D<property>=<value>", buffer.toString());
168 }
169
170 public void testAppendUsage_Hidden() {
171 final Option option = new PropertyOption();
172 final StringBuffer buffer = new StringBuffer();
173 final Set settings = new HashSet(DisplaySetting.ALL);
174 settings.remove(DisplaySetting.DISPLAY_PROPERTY_OPTION);
175 option.appendUsage(buffer, settings, null);
176
177 assertEquals("", buffer.toString());
178 }
179
180
181
182
183
184
185 public void testGetPreferredName() {
186 final Option option = new PropertyOption();
187 assertEquals("-D", option.getPreferredName());
188 }
189
190
191
192
193
194
195 public void testGetDescription() {
196 final Option option = new PropertyOption();
197 assertEquals(
198 "Passes properties and values to the application",
199 option.getDescription());
200 }
201
202
203
204
205
206
207 public void testHelpLines() {
208 final Option option = new PropertyOption();
209 final List lines = option.helpLines(0, DisplaySetting.ALL, null);
210 final Iterator i = lines.iterator();
211
212 final HelpLine line1 = (HelpLine)i.next();
213 assertEquals(0, line1.getIndent());
214 assertEquals(option, line1.getOption());
215
216 assertFalse(i.hasNext());
217 }
218
219
220
221
222
223
224 public void testHelpLines_NoDisplay() {
225 final Option option = new PropertyOption();
226 final Set settings = new HashSet(DisplaySetting.ALL);
227 settings.remove(DisplaySetting.DISPLAY_PROPERTY_OPTION);
228 final List lines = option.helpLines(0, settings, null);
229 final Iterator i = lines.iterator();
230
231 assertFalse(i.hasNext());
232 }
233 }