KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > ValidCommandTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.cli.framework;
25
26 import junit.framework.*;
27 import java.util.Vector JavaDoc;
28 import junit.textui.TestRunner;
29 import junit.framework.TestResult;
30 /**
31  *
32  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
33  * @version $Revision: 1.4 $
34  */

35
36 public class ValidCommandTest extends TestCase {
37     public static void assertFalse(boolean v){
38          assertTrue(!v);
39     }
40
41     public void testToStringNonEmpty(){
42         final ValidCommand vc = new ValidCommand();
43         vc.addValidOption(new ValidOption("vo", "type", 0, "default"));
44         vc.addRequiredOption(new ValidOption("ro", "type", 0, "default"));
45 // vc.setProperty("key", "value");
46
assertEquals("null null | {vo type default,} | ro type default,} | | } | null {}", vc.toString());
47     }
48     
49         
50
51     public void testToString(){
52         final ValidCommand vc = new ValidCommand();
53         assertEquals("null null | {} | } | | } | null {}", vc.toString());
54     }
55     
56     public void testHasProperty(){
57         final ValidCommand vc = new ValidCommand();
58         vc.setProperty("key", "name");
59         assertTrue(vc.hasProperty("key"));
60         assertFalse(vc.hasProperty("name"));
61     }
62     
63     public void testHasRequiredObject(){
64         final ValidCommand vc = new ValidCommand();
65         final ValidOption vo = new ValidOption("name", "type", 0, "default");
66         final ValidOption ro = new ValidOption("req", "type", 0, "default");
67         vc.addValidOption(vo);
68         vc.addRequiredOption(ro);
69         assertTrue(vc.hasRequiredOption(ro));
70         assertFalse(vc.hasRequiredOption(vo));
71     }
72         
73     public void testHasRequiredObjectByName(){
74         final ValidCommand vc = new ValidCommand();
75         final ValidOption vo = new ValidOption("name", "type", 0, "default");
76         final ValidOption ro = new ValidOption("req", "type", 0, "default");
77         vc.addValidOption(vo);
78         vc.addRequiredOption(ro);
79         vc.addRequiredOption(new ValidOption("np", "t", 0, "d"));
80         assertTrue(vc.hasRequiredOption("req"));
81         assertFalse(vc.hasRequiredOption("name"));
82     }
83     
84     public void testHasValidObject(){
85         final ValidCommand vc = new ValidCommand();
86         final ValidOption vo = new ValidOption("name", "type", 0, "default");
87         vc.addValidOption(vo);
88         assertTrue(vc.hasValidOption(vo));
89         assertFalse(vc.hasValidOption(new ValidOption()));
90         assertFalse(vc.hasValidOption((ValidOption) null));
91     }
92     
93     
94         
95     public void testHasValidObjectNullName(){
96         final ValidCommand vc = new ValidCommand();
97         final ValidOption vo = new ValidOption();
98         vc.addValidOption(vo);
99         assertFalse(vc.hasValidOption((String JavaDoc) null));
100     }
101
102     public void testHasValidObjectByNameNoOptions(){
103         final ValidCommand vc = new ValidCommand();
104         assertFalse(vc.hasValidOption("name"));
105     }
106     
107     public void testHasValidObjectByName(){
108         final ValidCommand vc = new ValidCommand();
109         final ValidOption vo = new ValidOption("name", "type", 0, "default");
110         vc.addValidOption(vo);
111         vc.addValidOption(new ValidOption("np", "far", 0, "default"));
112         assertTrue(vc.hasValidOption("name"));
113         assertFalse(vc.hasValidOption("foo"));
114         assertFalse(vc.hasValidOption((String JavaDoc) null));
115     }
116     
117     public void testCantDeleteRequiredOption(){
118         final ValidCommand vc = new ValidCommand();
119         final ValidOption vo = new ValidOption("name", "type", 0, "default");
120         vc.addValidOption(vo);
121         vc.addRequiredOption(vo);
122         assertTrue(vc.getValidOptions().contains(vo));
123         assertTrue(vc.getRequiredOptions().contains(vo));
124         vc.deleteOption(vo);
125         assertTrue(vc.getValidOptions().isEmpty());
126         assertTrue(vc.getRequiredOptions().contains(vo));
127     }
128
129     public void testDeleteNullOption(){
130         final ValidCommand vc = new ValidCommand();
131         final ValidOption vo = new ValidOption("name", "type", 0, "default");
132         vc.addValidOption(vo);
133         assertTrue(vc.getValidOptions().contains(vo));
134         vc.deleteOption(null);
135         assertTrue(vc.getValidOptions().contains(vo));
136     }
137     public void testDeleteOption(){
138         final ValidCommand vc = new ValidCommand();
139         final ValidOption vo = new ValidOption("name", "type", 0, "default");
140         vc.addValidOption(vo);
141         assertTrue(vc.getValidOptions().contains(vo));
142         vc.deleteOption(vo);
143         assertTrue(vc.getValidOptions().isEmpty());
144     }
145
146     public void testAddRequiredOptionTwice(){
147         final ValidCommand vc = new ValidCommand();
148         final ValidOption vo = new ValidOption("name", "type", 0, "default");
149         vc.addRequiredOption(vo);
150         vc.addRequiredOption(vo);
151         assertTrue(vc.getRequiredOptions().contains(vo));
152         assertEquals(1, vc.getRequiredOptions().size());
153         assertTrue(vc.getValidOptions().isEmpty());
154     }
155         
156     public void testAddNullRequiredOption(){
157         final ValidCommand vc = new ValidCommand();
158         vc.addRequiredOption(null);
159         assertEquals(0, vc.getRequiredOptions().size());
160     }
161     
162     public void testAddRequiredOption(){
163         final ValidCommand vc = new ValidCommand();
164         final ValidOption vo = new ValidOption("name", "type", 0, "default");
165         vc.addRequiredOption(vo);
166         assertTrue(vc.getRequiredOptions().contains(vo));
167         assertEquals(1, vc.getRequiredOptions().size());
168         assertTrue(vc.getValidOptions().isEmpty());
169     }
170     
171         
172     public void testAddSimpleValidOption(){
173         final ValidCommand vc = new ValidCommand();
174         final ValidOption vo = new ValidOption();
175         vc.addValidOption(vo);
176         assertTrue(vc.getValidOptions().contains(vo));
177         assertEquals(1, vc.getValidOptions().size());
178         assertTrue(vc.getRequiredOptions().isEmpty());
179     }
180
181     public void testAddValidOptionTwice(){
182         final ValidCommand vc = new ValidCommand();
183         final ValidOption vo = new ValidOption("name", "type", 0, "default");
184         vc.addValidOption(vo);
185         vc.addValidOption(vo);
186         assertTrue(vc.getValidOptions().contains(vo));
187         assertEquals(1, vc.getValidOptions().size());
188         assertTrue(vc.getRequiredOptions().isEmpty());
189     }
190
191     public void testAddNullValidOption(){
192         final ValidCommand vc = new ValidCommand();
193         vc.addValidOption(null);
194         assertEquals(0, vc.getValidOptions().size());
195     }
196     
197     public void testAddValidOptionWithNamedOption(){
198         final ValidCommand vc = new ValidCommand();
199         final ValidOption vo = new ValidOption("name", "type", 0, "default");
200         vc.addValidOption(vo);
201         assertTrue(vc.getValidOptions().contains(vo));
202         assertEquals(1, vc.getValidOptions().size());
203         assertTrue(vc.getRequiredOptions().isEmpty());
204     }
205     
206     public void testGetOptionsContainsOptions(){
207         final ValidCommand vc = new ValidCommand();
208         final Vector JavaDoc v = new Vector JavaDoc();
209         final ValidOption vo = new ValidOption();
210         v.add(vo);
211         vc.setValidOptions(v);
212         final Vector JavaDoc r = new Vector JavaDoc();
213         final ValidOption ro = new ValidOption();
214         r.add(ro);
215         vc.setRequiredOptions(r);
216         assertTrue(vc.getOptions().contains(vo));
217         assertTrue(vc.getOptions().contains(ro));
218         assertEquals(2, vc.getOptions().size());
219     }
220     
221
222     public void testGetOptionsSameVector(){
223         final ValidCommand vc = new ValidCommand();
224         final Vector JavaDoc v = new Vector JavaDoc();
225         vc.setValidOptions(v);
226         vc.setRequiredOptions(v);
227         assertTrue(vc.getOptions().isEmpty());
228     }
229     
230     public void testGetOptionsIsEmpty(){
231         final ValidCommand vc = new ValidCommand();
232         final Vector JavaDoc v = new Vector JavaDoc();
233         final Vector JavaDoc r = new Vector JavaDoc();
234         vc.setValidOptions(v);
235         vc.setRequiredOptions(r);
236         assertTrue(vc.getOptions().isEmpty());
237     }
238
239     public void testGetNonExistentOption(){
240         final ValidCommand vc = new ValidCommand();
241         final Vector JavaDoc v = new Vector JavaDoc();
242         final ValidOption vo = new ValidOption("name", "type", 0, "default");
243         v.add(vo);
244         vc.setValidOptions(v);
245         assertNull(vc.getOption("Name"));
246     }
247         
248     
249     public void testValidOptionsFromMany(){
250         final ValidCommand vc = new ValidCommand();
251         final Vector JavaDoc v = new Vector JavaDoc();
252         final ValidOption vo = new ValidOption("name", "type", 0, "default");
253         v.add(vo);
254         v.add(new ValidOption("foo", "far", 1, "default"));
255         vc.setValidOptions(v);
256         assertEquals(vo, vc.getOption("name"));
257     }
258
259     public void testGetOptionFromEmptySet(){
260         final ValidCommand vc = new ValidCommand();
261         final Vector JavaDoc v = new Vector JavaDoc();
262         assertNull(vc.getOption("anything"));
263     }
264     
265     
266     public void testValidOptionsNullCase(){
267         final ValidCommand vc = new ValidCommand();
268         final Vector JavaDoc v = new Vector JavaDoc();
269         final ValidOption vo = new ValidOption();
270         v.add(vo);
271         vc.setValidOptions(v);
272         assertEquals(null, vc.getOption((String JavaDoc)null));
273     }
274     
275     public void testValidOptionsWithAString(){
276         final ValidCommand vc = new ValidCommand();
277         final Vector JavaDoc v = new Vector JavaDoc();
278         final ValidOption vo = new ValidOption("opt1", "string", 1, "opt1");
279         v.add(vo);
280         vc.setValidOptions(v);
281         assertEquals(vo, vc.getOption("opt1"));
282     }
283
284     public void testReplaceAllOptionsWithValidOption() {
285         final ValidCommand vc = new ValidCommand();
286         vc.addValidOption(new ValidOption("option1", "string", 1, "default"));
287         vc.addValidOption(new ValidOption("option2", "boolean", 0, "true"));
288         vc.addRequiredOption(new ValidOption("option3", "string", 1, "default"));
289         vc.addRequiredOption(new ValidOption("option4", "string", 1, "4848"));
290         ValidOption ro = new ValidOption("option2", "boolean", 0, "false");
291         vc.replaceAllOptions(ro);
292         Vector JavaDoc vtr = vc.getValidOptions();
293         ValidOption vo = (ValidOption)vtr.get(1);
294         assertEquals("false", vo.getDefaultValue());
295     }
296     
297
298     public void testReplaceAllOptionsWithRquiredOption() {
299         final ValidCommand vc = new ValidCommand();
300         vc.addValidOption(new ValidOption("option1", "string", 1, "default"));
301         vc.addValidOption(new ValidOption("option2", "boolean", 0, "true"));
302         vc.addRequiredOption(new ValidOption("option3", "string", 1, "default"));
303         vc.addRequiredOption(new ValidOption("option4", "string", 1, "4848"));
304         ValidOption ro = new ValidOption("option4", "string", 0, "4949");
305         vc.replaceAllOptions(ro);
306         Vector JavaDoc vtr = vc.getRequiredOptions();
307         ValidOption vo = (ValidOption)vtr.get(1);
308         assertEquals("4949", vo.getDefaultValue());
309     }
310
311     
312     public void testReplaceAllOptionsWithDeprecatedOption() {
313         final ValidCommand vc = new ValidCommand();
314         vc.addValidOption(new ValidOption("option1", "string", 1, "default"));
315         vc.addValidOption(new ValidOption("option2", "boolean", 0, "true"));
316         vc.addRequiredOption(new ValidOption("option3", "string", 1, "default"));
317         vc.addRequiredOption(new ValidOption("option4", "string", 1, "4848"));
318         vc.addDeprecatedOption(new ValidOption("option5", "string", 1, "admin"));
319         vc.addDeprecatedOption(new ValidOption("option6", "string", 1, "test"));
320
321         ValidOption ro = new ValidOption("option5", "boolean", 2, "true");
322         vc.replaceAllOptions(ro);
323         Vector JavaDoc vtr = vc.getDeprecatedOptions();
324         ValidOption vo = (ValidOption)vtr.get(0);
325         assertEquals("boolean", vo.getType());
326         assertEquals(2, vo.isValueRequired());
327         assertEquals("true", vo.getDefaultValue());
328     }
329
330
331     public void testMutatorsAndAccessors(){
332         final ValidCommand vc = new ValidCommand();
333         final String JavaDoc name = "name";
334         vc.setName(name);
335         assertEquals(name, vc.getName());
336         final String JavaDoc nops = "nops";
337         vc.setNumberOfOperands(nops);
338         assertEquals(nops, vc.getNumberOfOperands());
339         final Vector JavaDoc vo = new Vector JavaDoc();
340         vc.setValidOptions(vo);
341         assertEquals(vo, vc.getValidOptions());
342         final Vector JavaDoc ro = new Vector JavaDoc();
343         vc.setRequiredOptions(ro);
344         assertEquals(ro, vc.getRequiredOptions());
345         final String JavaDoc clazz = "class";
346         vc.setClassName(clazz);
347         assertEquals(clazz, vc.getClassName());
348         final String JavaDoc use = "use";
349         vc.setUsageText(use);
350         assertEquals(use, vc.getUsageText());
351         vc.setProperty("key", "value");
352         assertEquals("value", vc.getProperty("key"));
353     }
354     
355     public void testComplexConstruction(){
356         final Vector JavaDoc vo = new Vector JavaDoc();
357         final Vector JavaDoc ro = new Vector JavaDoc();
358         final Vector JavaDoc dos = new Vector JavaDoc();
359         
360         final ValidCommand vc = new ValidCommand("name", "number of ops", vo, ro, dos, "usage");
361         assertEquals("name", vc.getName());
362         assertEquals("number of ops", vc.getNumberOfOperands());
363         assertEquals(vo, vc.getValidOptions());
364         assertEquals(ro, vc.getRequiredOptions());
365         assertEquals("usage", vc.getUsageText());
366     }
367     
368     public void testBasicConstruction() {
369         final ValidCommand vc = new ValidCommand();
370         assertNotNull(vc.getValidOptions());
371         assertTrue(vc.getValidOptions().isEmpty());
372         assertNotNull(vc.getRequiredOptions());
373         assertTrue(vc.getRequiredOptions().isEmpty());
374         assertNotNull(vc.getProperties());
375         assertTrue(vc.getProperties().isEmpty());
376     }
377
378     public ValidCommandTest(String JavaDoc name){
379         super(name);
380     }
381
382     protected void setUp() {
383     }
384
385     protected void tearDown() {
386     }
387
388     private void nyi(){
389         fail("Not Yet Implemented");
390     }
391
392     public static void main(String JavaDoc args[]){
393         if (args.length == 0){
394             junit.textui.TestRunner.run(ValidCommandTest.class);
395         } else {
396             junit.textui.TestRunner.run(makeSuite(args));
397         }
398     }
399     private static TestSuite makeSuite(String JavaDoc args[]){
400         final TestSuite ts = new TestSuite();
401         for (int i = 0; i < args.length; i++){
402             ts.addTest(new ValidCommandTest(args[i]));
403         }
404         return ts;
405     }
406 }
407
Popular Tags