KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
27 import java.util.Vector JavaDoc;
28 import junit.framework.*;
29 /**
30  *
31  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
32  * @version $Revision: 1.4 $
33  */

34
35 public class CommandFactoryTest extends TestCase {
36     public void testInvalidClass(){
37         final ValidCommand vc = new ValidCommand();
38         vc.setName("vc");
39         vc.setNumberOfOperands("three");
40         vc.setUsageText("vc 1 2 3");
41         vc.setClassName("foobar");
42         try {
43             CommandFactory.createCommand(vc, new HashMap JavaDoc(), new Vector JavaDoc());
44             fail("Expected a CommandValidationException indicating that the class could not be found");
45         }
46         catch (CommandValidationException cve){
47             assertEquals("CLI002 Unable to create command object, vc.", cve.getMessage());
48             assertEquals(ClassNotFoundException JavaDoc.class, cve.getCause().getClass());
49             assertEquals("foobar", cve.getCause().getMessage()); }
50     }
51         
52     public void testNullClass() {
53         final ValidCommand vc = new ValidCommand();
54         vc.setName("vc");
55         vc.setNumberOfOperands("three");
56         vc.setUsageText("vc 1 2 3");
57         try {
58             CommandFactory.createCommand(vc, new HashMap JavaDoc(), new Vector JavaDoc());
59             fail("Expected a CommandValidationException indicating that no classname was given");
60         }
61         catch (CommandValidationException cve){
62             assertEquals("CLI001 Invalid Command, vc.", cve.getMessage());
63         }
64     }
65     
66     public void testSimpleCommandCreation() throws Exception JavaDoc {
67         final ValidCommand vc = new ValidCommand();
68         vc.setName("vc");
69         vc.setNumberOfOperands("three");
70         vc.setUsageText("vc 1 2 3");
71         vc.setClassName("com.sun.enterprise.cli.framework.TestCommand");
72         assertNotNull(vc.getClassName());
73         final Command c = CommandFactory.createCommand(vc, new HashMap JavaDoc(), new Vector JavaDoc());
74         assertNotNull(c);
75     }
76
77     public void testNoArgsConstructor(){
78         final CommandFactory c = new CommandFactory();
79     }
80     
81     public CommandFactoryTest(String JavaDoc name){
82         super(name);
83     }
84
85     protected void setUp() {
86     }
87
88     protected void tearDown() {
89     }
90
91     private void nyi(){
92         fail("Not Yet Implemented");
93     }
94
95     public static void main(String JavaDoc args[]){
96         if (args.length == 0){
97             junit.textui.TestRunner.run(CommandFactoryTest.class);
98         } else {
99             junit.textui.TestRunner.run(makeSuite(args));
100         }
101     }
102     private static TestSuite makeSuite(String JavaDoc args[]){
103         final TestSuite ts = new TestSuite();
104         for (int i = 0; i < args.length; i++){
105             ts.addTest(new CommandFactoryTest(args[i]));
106         }
107         return ts;
108     }
109 }
110
111 class TestCommand extends Command
112 {
113     public void runCommand() throws com.sun.enterprise.cli.framework.CommandException, com.sun.enterprise.cli.framework.CommandValidationException{}
114     public boolean validateOptions() throws com.sun.enterprise.cli.framework.CommandValidationException{
115         return true;
116     }
117     
118 }
119
120
Popular Tags