KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
27    Note that this test requires resources for testing. These resources
28    are construct4ed from the two files P1 & P2 located in the current
29    directory. If these file names are changed then the corresponding
30    names in this submodules build.xml file should be changed also
31 */

32 import java.util.ArrayList JavaDoc;
33 import java.util.Arrays JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Properties JavaDoc;
37 import java.util.Vector JavaDoc;
38 import junit.framework.*;
39 import junit.textui.TestRunner;
40
41 /**
42  *
43  * @author jane.young@sun.com
44  * @version $Revision: 1.4 $
45  */

46
47 /**
48    Execute these tests using gmake (and Ant) by:
49    cd <framework>
50    gmake ANT_TARGETS=CommandTest.java
51 */

52
53 public class CommandTest extends TestCase {
54     public void testGetDelimeterIndex(){
55         assertEquals(3, testCommand.getDelimeterIndex("0123", "3", 0));
56     }
57     
58     public void testLocalizedString(){
59         assertEquals("Key not found", testCommand.getLocalizedString("a", (Object JavaDoc []) null));
60         assertEquals("Key not found", testCommand.getLocalizedString("InvalidCommand"));
61     }
62     
63     public void testGetSetProperty2(){
64         testCommand.setProperties(new Properties JavaDoc());
65         testCommand.setProperty("prop", "value");
66         assertEquals("value", testCommand.getProperty("prop"));
67     }
68     
69     public void testGetSetProperty1(){
70         testCommand.setProperties(new Properties JavaDoc());
71         testCommand.setProperty("prop", "value");
72         assertEquals("value", testCommand.getProperty("prop"));
73     }
74     
75     
76     public void testGetSetProperty(){
77         testCommand.setProperties(new Properties JavaDoc());
78         assertEquals(null, testCommand.getProperty("prop"));
79     }
80     
81
82     public void testSetGetIntegerOptions(){
83         testCommand.setOption("number 1", "1");
84         assertEquals(1, testCommand.getIntegerOption("number 1"));
85     }
86     
87     public void testSetBooleanOptionsList(){
88         final List JavaDoc l = new ArrayList JavaDoc();
89         testCommand.setBooleanOptions(l);
90         l.add("bool");
91         testCommand.setOption("bool", "fargle");
92         assertEquals("sampleCommand --bool=fargle", testCommand.toString());
93     }
94     
95     
96     public void testBooleanOptions(){
97         assertTrue(!testCommand.getBooleanOption("bool"));
98         testCommand.setOption("bool", "true");
99         assertEquals("true", testCommand.getOption("bool"));
100     }
101     
102         
103     public void testGetSetOptions(){
104         testCommand.setOption("key", "value");
105         assertEquals("value", testCommand.getOption("key"));
106         assertNull(testCommand.getOption("foo"));
107         
108     }
109     
110     public void testConstructorAndAccessors(){
111         assertEquals("sampleCommand", testCommand.getName());
112         assertTrue(testCommand.getOperands().isEmpty());
113         assertTrue(testCommand.getOptions().isEmpty());
114         assertEquals(null, testCommand.getUsageText());
115         assertNull(testCommand.getProperties(""));
116         assertEquals("Key not found", testCommand.getLocalizedString("fargle"));
117         assertEquals("sampleCommand", testCommand.toString());
118         
119     }
120     
121       // test replacePattern method with different sets of operands
122
public void testReplacePatternOperands() throws Exception JavaDoc{
123         final String JavaDoc[] operands = new String JavaDoc[] {"abc=xyz", "$123", "#123", "", "//////", "abc:xyz=123:456$888=\"99:99\"", "foo-bar"};
124         testCommand.setOperands(new Vector JavaDoc(Arrays.asList(operands)));
125         assertEquals("first operand should be abc=xyz", "abc=xyz", testCommand.replacePattern("{#1}"));
126         assertEquals("second operand should be $123", "$123", testCommand.replacePattern("{#2}"));
127         assertEquals("third operand should be #123", "#123", testCommand.replacePattern("{#3}"));
128         assertEquals("fourth operand should be empty", null, testCommand.replacePattern("{#4}"));
129         assertEquals("fifth operand should be //////", "//////", testCommand.replacePattern("{#5}"));
130         assertEquals("sixth operand should be abc:xyz=123:456$888=\"99:99\"",
131                      "abc:xyz=123:456$888=\"99:99\"", testCommand.replacePattern("{#6}"));
132         assertEquals("seventh operand should be foo-bar", "foo-bar", testCommand.replacePattern("{#7}"));
133         assertEquals("all operands should be",
134                      "7=foo-bar," +
135                      "6=abc:xyz=123:456$888=\"99:99\"," +
136                      "5=//////,"+
137                      "4=,"+
138                      "3=#123,"+
139                      "2=$123,"+
140                      "1=abc=xyz", testCommand.replacePattern("7={#7},"+
141                                                              "6={#6},"+
142                                                              "5={#5},"+
143                                                              "4={#4},"+
144                                                              "3={#3},"+
145                                                              "2={#2},"+
146                                                              "1={#1}"));
147     }
148
149
150       // test replacePattern method with different sets of options
151
public void testReplacePatternOptions() throws Exception JavaDoc{
152         HashMap JavaDoc options = new HashMap JavaDoc();
153         options.put("user", "admin");
154         options.put("password", "adminadmin");
155         options.put("host", "fuyako");
156         options.put("port", "4848");
157         options.put("property", "--D:${abc.xyz}=123:\"456\":88:99$01");
158         options.put("empty", "");
159         options.put("foo-bar", "foobar");
160         testCommand.setOptions(options);
161
162         assertEquals("first option should be admin", "admin", testCommand.replacePattern("{$user}"));
163         assertEquals("second option should be adminadmin", "adminadmin", testCommand.replacePattern("{$password}"));
164         assertEquals("third option should be fuyako", "fuyako", testCommand.replacePattern("{$host}"));
165         assertEquals("fourth option should be 4848", "4848", testCommand.replacePattern("{$port}"));
166         assertEquals("fifth option should be --D:${abc.xyz}=123:\"456\":88:99$01",
167                      "--D:${abc.xyz}=123:\"456\":88:99$01", testCommand.replacePattern("{$property}"));
168         assertEquals("sixth option should be empty", null, testCommand.replacePattern("{$empty}"));
169         assertEquals("seventh option should be foobar", "foobar", testCommand.replacePattern("{$foo-bar}"));
170         assertEquals("all options", "user=admin,password=adminadmin,host=fuyako,port=4848,"+
171                      "property=--D:${abc.xyz}=123:\"456\":88:99$01,empty=,foo-bar=foobar",
172                      testCommand.replacePattern("user={$user},password={$password},host={$host},"+
173                                                 "port={$port},property={$property},empty={$empty},foo-bar={$foo-bar}"));
174
175     }
176     
177   
178     public CommandTest(String JavaDoc name){
179         super(name);
180     }
181
182     Command testCommand = null;
183
184     protected void setUp() {
185         testCommand = new Command() {
186                 public void runCommand()
187                     throws CommandException, CommandValidationException
188                 {
189                 }
190                 public boolean validateOptions() throws CommandValidationException
191                 {
192                     return true;
193                 }
194             };
195         testCommand.setName("sampleCommand");
196     }
197   
198   
199
200     protected void tearDown() {
201     }
202
203     private void nyi(){
204         fail("Not Yet Implemented");
205     }
206
207     public static Test suite(){
208         TestSuite suite = new TestSuite(CommandTest.class);
209         return suite;
210     }
211
212     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
213         final TestRunner runner= new TestRunner();
214         final TestResult result = runner.doRun(CommandTest.suite(), false);
215         System.exit(result.errorCount() + result.failureCount());
216     }
217 }
218
219
Popular Tags