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.Collections;
20 import java.util.HashSet;
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.Option;
27 import org.apache.commons.cli2.OptionException;
28 import org.apache.commons.cli2.Parent;
29 import org.apache.commons.cli2.WriteableCommandLine;
30 import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;
31 import org.apache.commons.cli2.resource.ResourceConstants;
32 import org.apache.commons.cli2.resource.ResourceHelper;
33
34
35
36
37
38
39
40 public class CommandTest
41 extends ParentTestCase {
42 public static Command buildStartCommand() {
43 return new Command("start", "Begins the process", Collections.singleton("go"), false, null,
44 null, 0);
45 }
46
47 public static Command buildCommitCommand() {
48 return new Command("commit", "Commit the changes to the database", null, true, null, null, 0);
49 }
50
51 public static Command buildLoginCommand() {
52 return new Command("login", "Initiates a session for the user", null, false,
53 ArgumentTest.buildUsernameArgument(), null, 0);
54 }
55
56
57
58
59
60
61 public void testProcessParent()
62 throws OptionException {
63 final Command option = buildStartCommand();
64 final List args = list("go");
65 final WriteableCommandLine commandLine = commandLine(option, args);
66 final ListIterator iterator = args.listIterator();
67 option.processParent(commandLine, iterator);
68
69 assertFalse(iterator.hasNext());
70 assertTrue(commandLine.hasOption(option));
71 assertTrue(commandLine.hasOption("start"));
72 assertTrue(commandLine.hasOption("go"));
73 assertTrue(commandLine.getValues(option).isEmpty());
74 }
75
76 public void testProcessParent_Spare()
77 throws OptionException {
78 final Command option = buildLoginCommand();
79 final List args = list("login", "rob");
80 final WriteableCommandLine commandLine = commandLine(option, args);
81 final ListIterator iterator = args.listIterator();
82 option.processParent(commandLine, iterator);
83
84 assertEquals("rob", iterator.next());
85 assertFalse(iterator.hasNext());
86 assertTrue(commandLine.hasOption(option));
87 assertTrue(commandLine.hasOption("login"));
88 assertTrue(commandLine.getValues(option).isEmpty());
89 }
90
91
92
93
94
95
96 public void testCanProcess() {
97 final Command option = buildStartCommand();
98 assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "start"));
99 }
100
101 public void testCanProcess_BadMatch() {
102 final Command option = buildStartCommand();
103 assertFalse(option.canProcess(new WriteableCommandLineImpl(option, null), "stop"));
104 }
105
106 public void testCanProcess_Alias() {
107 final Command option = buildStartCommand();
108 assertTrue(option.canProcess(new WriteableCommandLineImpl(option, null), "go"));
109 }
110
111
112
113
114
115
116 public void testPrefixes() {
117 final Command option = buildStartCommand();
118 assertTrue(option.getPrefixes().isEmpty());
119 }
120
121
122
123
124
125
126 public void testProcess()
127 throws OptionException {
128 final Command option = buildLoginCommand();
129 final List args = list("login", "rob");
130 final WriteableCommandLine commandLine = commandLine(option, args);
131 final ListIterator iterator = args.listIterator();
132 option.process(commandLine, iterator);
133
134 assertFalse(iterator.hasNext());
135 assertTrue(commandLine.hasOption(option));
136 assertTrue(commandLine.hasOption("login"));
137 assertEquals("rob", commandLine.getValue(option));
138 }
139
140
141
142
143
144
145 public void testTriggers() {
146 final Command option = buildStartCommand();
147 final Set triggers = option.getTriggers();
148 assertContentsEqual(list("start", "go"), triggers);
149 }
150
151
152
153
154
155
156 public void testValidate() {
157 final Parent option = buildCommitCommand();
158 final WriteableCommandLine commandLine = commandLine(option, list());
159
160 try {
161 option.validate(commandLine);
162 fail("Missing an option");
163 } catch (OptionException moe) {
164 assertSame(option, moe.getOption());
165 }
166 }
167
168
169
170
171
172
173 public void testAppendUsage() {
174 final Option option = buildStartCommand();
175 final StringBuffer buffer = new StringBuffer();
176 option.appendUsage(buffer, DisplaySetting.ALL, null);
177
178 assertEquals("[start (go)]", buffer.toString());
179 }
180
181 public void testNullPreferredName() {
182 try {
183 new Command(null, "", Collections.singleton("go"), false, null, null, 0);
184 } catch (IllegalArgumentException exp) {
185 assertEquals("wrong exception name",
186 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT),
187 exp.getMessage());
188 }
189 }
190
191 public void testEmotyPreferredName() {
192 try {
193 new Command("", "", Collections.singleton("go"), false, null, null, 0);
194 } catch (IllegalArgumentException exp) {
195 assertEquals("wrong exception name",
196 ResourceHelper.getResourceHelper().getMessage(ResourceConstants.COMMAND_PREFERRED_NAME_TOO_SHORT),
197 exp.getMessage());
198 }
199 }
200
201 public void testAppendUsage_NoOptional() {
202 final Option option = buildStartCommand();
203 final StringBuffer buffer = new StringBuffer();
204 final Set settings = new HashSet(DisplaySetting.ALL);
205 settings.remove(DisplaySetting.DISPLAY_OPTIONAL);
206 option.appendUsage(buffer, settings, null);
207
208 assertEquals("start (go)", buffer.toString());
209 }
210
211 public void testAppendUsage_NoAlias() {
212 final Option option = buildStartCommand();
213 final StringBuffer buffer = new StringBuffer();
214 final Set settings = new HashSet(DisplaySetting.ALL);
215 settings.remove(DisplaySetting.DISPLAY_ALIASES);
216 option.appendUsage(buffer, settings, null);
217
218 assertEquals("[start]", buffer.toString());
219 }
220
221
222
223
224
225
226 public void testGetPreferredName() {
227 final Option option = buildStartCommand();
228 assertEquals("start", option.getPreferredName());
229 }
230
231
232
233
234
235
236 public void testGetDescription() {
237 final Option option = buildLoginCommand();
238 assertEquals("Initiates a session for the user", option.getDescription());
239 }
240
241
242
243
244
245
246 public void testHelpLines() {
247
248 }
249 }