KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > commands > S1ASCommandTest


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

31 import com.sun.enterprise.cli.framework.*;
32 //import java.util.ArrayList;
33
//import java.util.Arrays;
34
//import java.util.HashMap;
35
//import java.util.List;
36
//import java.util.Properties;
37
import java.util.Vector JavaDoc;
38 import junit.framework.*;
39 import junit.textui.TestRunner;
40 import java.io.File JavaDoc;
41 import java.io.PrintWriter JavaDoc;
42 import java.io.OutputStreamWriter JavaDoc;
43 import java.io.FileOutputStream JavaDoc;
44 import java.util.Properties JavaDoc;
45 import java.util.Map JavaDoc;
46 import java.util.Hashtable JavaDoc;
47 import com.sun.enterprise.util.SystemPropertyConstants;
48 import com.sun.appserv.management.util.misc.ExceptionUtil;
49
50 /**
51  *
52  * @author prashanth.abbagani@sun.com
53  * @version $Revision: 1.3 $
54  */

55
56 /**
57    Execute these tests using gmake (and Ant) by:
58    cd <commands>
59    gmake ANT_TARGETS=CommandTest.java
60 */

61
62 public class S1ASCommandTest extends TestCase {
63     public void testCheckForFileExistence() throws Exception JavaDoc{
64         final File JavaDoc f = File.createTempFile("S1ASCommandTest_testCheckForFileExistence", ".tmp");
65         f.deleteOnExit();
66         String JavaDoc tempdir = System.getProperty("java.io.tmpdir");
67         assertTrue(testCommand.checkForFileExistence(tempdir, f.getName()).exists());
68     }
69
70     public void testCheckForFileExistenceWhenNoFile() throws Exception JavaDoc{
71         String JavaDoc tempdir = System.getProperty("java.io.tmpdir");
72         try{
73             File JavaDoc f = testCommand.checkForFileExistence(tempdir, "FileDoesNotExist");
74         }catch (Exception JavaDoc e){
75             assertEquals(e.getMessage(), "CLI146 FileDoesNotExist does not exist in the file system or read permission denied.");
76         }
77     }
78
79     public void testCreatePropertiesParam() throws Exception JavaDoc{
80         String JavaDoc propStr = "name=value";
81         Properties JavaDoc props = testCommand.createPropertiesParam(propStr);
82         assertEquals(props.getProperty("name"),"value");
83     }
84
85     public void testCreatePropertiesParamWithEscapes() throws Exception JavaDoc{
86         String JavaDoc propStr = "user=dbuser:password=dbpassword:DatabaseName=jdbc\\:pointbase:server=http\\://localhost\\:9292";
87         Properties JavaDoc props = testCommand.createPropertiesParam(propStr);
88         assertEquals(props.getProperty("user"),"dbuser");
89         assertEquals(props.getProperty("password"), "dbpassword");
90         assertEquals(props.getProperty("DatabaseName"), "jdbc:pointbase");
91         assertEquals(props.getProperty("server"), "http://localhost:9292");
92     }
93     
94     public void testCreatePropertiesParamInvalid() throws Exception JavaDoc{
95         String JavaDoc propStr = "name1=value1:value2";
96         try{
97             Properties JavaDoc props = testCommand.createPropertiesParam(propStr);
98         }catch (Exception JavaDoc e){
99             assertEquals(e.getMessage(), "CLI131 Invalid property syntax.");
100         }
101     }
102
103     public void testCreateStringArrayParam() throws Exception JavaDoc{
104         String JavaDoc paramStr = "value1:value2";
105         String JavaDoc[] params = testCommand.createStringArrayParam(paramStr);
106         assertEquals(params[0],"value1");
107         assertEquals(params[1],"value2");
108     }
109
110     public void testCreateStringArrayParamWithEscapes() throws Exception JavaDoc{
111         String JavaDoc paramStr = "-XX\\:NewRatio=2";
112         String JavaDoc[] params = testCommand.createStringArrayParam(paramStr);
113         assertEquals(params[0],"-XX:NewRatio=2");
114     }
115
116     public void testDisplayExceptionMessage(){
117         NullPointerException JavaDoc npe = new NullPointerException JavaDoc("a null pointer");
118         try{
119             testCommand.displayExceptionMessage(npe);
120         }
121         catch (CommandException ce){
122             assertEquals(npe, ExceptionUtil.getRootCause(ce));
123         }
124     }
125
126     public void testgetHost(){
127         testCommand.setOption("host", "localhost");
128         assertEquals(testCommand.getHost(),"localhost");
129     }
130
131     public void testgetInteractiveOptionWhenFalse() throws Exception JavaDoc{
132         final CLIDescriptorsReader cliDescriptorsReader = CLIDescriptorsReader.getInstance();
133         ValidCommand validCommand = cliDescriptorsReader.getCommand(null);
134         LocalStringsManagerFactory.setCommandLocalStringsManagerProperties(
135             CLIDescriptorsReader.getInstance().getProperties());
136         
137         testCommand.setOption("interactive", "false");
138         try{
139         testCommand.getInteractiveOption("test", "test");
140         }
141         catch (CommandValidationException cve){
142             assertEquals(cve.getMessage(), "CLI152 test is a required option.");
143         }
144     }
145
146     public void testgetObjectName() throws Exception JavaDoc{
147         Vector JavaDoc propertyValues = new Vector JavaDoc();
148         propertyValues.add("test_objectname");
149         testCommand.setProperty(testCommand.OBJECT_NAME, propertyValues);
150         assertEquals(testCommand.getObjectName(), "test_objectname");
151     }
152
153     public void testgetOperationName() throws Exception JavaDoc{
154         Vector JavaDoc propertyValues = new Vector JavaDoc();
155         propertyValues.add("testMethod");
156         testCommand.setProperty(testCommand.OPERATION, propertyValues);
157         assertEquals(testCommand.getOperationName(), "testMethod");
158     }
159
160     public void testgetTypesInfo() throws Exception JavaDoc{
161         Vector JavaDoc propertyValues = new Vector JavaDoc();
162         for (int i=0; i<=3; i++){
163             propertyValues.add("java.lang.String");
164         }
165         testCommand.setProperty(testCommand.PARAM_TYPES, propertyValues);
166         String JavaDoc[] types = testCommand.getTypesInfo();
167         for (int i=0; i<types.length; i++)
168             assertEquals(types[i], "java.lang.String");
169     }
170
171     public void testreplacePattern() throws Exception JavaDoc{
172         
173         Vector JavaDoc typesValues = new Vector JavaDoc();
174         typesValues.add("java.lang.String");
175         testCommand.setProperty(testCommand.PARAM_TYPES, typesValues);
176         testCommand.setOption("testOption", "test");
177         Vector JavaDoc paramValues = new Vector JavaDoc();
178         paramValues.add("{$testOption}");
179         testCommand.setProperty(testCommand.PARAMS, paramValues);
180         Object JavaDoc[] params = testCommand.getParamsInfo();
181         assertEquals((String JavaDoc)params[0], "test");
182     }
183
184     public void testgetParamsInfo() throws Exception JavaDoc{
185         
186         Vector JavaDoc typesValues = new Vector JavaDoc();
187         for (int i=0; i<3; i++){
188             typesValues.add("java.lang.String");
189         }
190         testCommand.setProperty(testCommand.PARAM_TYPES, typesValues);
191         
192         Vector JavaDoc paramValues = new Vector JavaDoc();
193         for (int i=0; i<3; i++){
194             paramValues.add("x"+i);
195         }
196         testCommand.setProperty(testCommand.PARAMS, paramValues);
197         Object JavaDoc[] params = testCommand.getParamsInfo();
198         for (int i=0; i<params.length; i++)
199             assertEquals((String JavaDoc)params[i], "x"+i);
200     }
201
202     public void testgetParamsInfoInvalid() throws Exception JavaDoc{
203         //#of param typess is not equal to #of param values
204
Vector JavaDoc typesValues = new Vector JavaDoc();
205         typesValues.add("java.lang.String");
206         typesValues.add("java.lang.String");
207         testCommand.setProperty(testCommand.PARAM_TYPES, typesValues);
208         
209         Vector JavaDoc paramValues = new Vector JavaDoc();
210         for (int i=0; i<3; i++){
211             paramValues.add("x"+i);
212         }
213         testCommand.setProperty(testCommand.PARAMS, paramValues);
214         try{
215             Object JavaDoc[] params = testCommand.getParamsInfo();
216         }catch (CommandException ce){
217             assertEquals(ce.getMessage(),
218                     "CLI174 Error in CLIDescriptor.xml -- The number of params doesn''t match the number of param-types. Solution: edit this command in CLIDescriptor.xml.");
219         }
220     }
221
222     public void testgetValuesFromASADMINPREFS() throws Exception JavaDoc{
223         final String JavaDoc enc = "ISO-8859-1";
224         final File JavaDoc f = new File JavaDoc(System.getProperty("java.io.tmpdir"),
225                                 testCommand.ASADMINPREFS);
226         f.deleteOnExit();
227         final PrintWriter JavaDoc pw = new PrintWriter JavaDoc (
228                 new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(f), enc));
229         pw.println("AS_ADMIN_PASSWORD=test_password");
230         pw.close();
231         System.setProperty("user.home", System.getProperty("java.io.tmpdir"));
232         assertEquals(testCommand.getValuesFromASADMINPREFS(testCommand.PASSWORD),
233                         "test_password");
234     }
235     
236     public void testgetPasswordWhenNotSet() throws Exception JavaDoc{
237         String JavaDoc pwd = testCommand.getPassword(testCommand.PASSWORD, null, null,
238                                     false, false, false, false, null, null, false,
239                                     false, false, false);
240         assertEquals(pwd, null);
241     }
242     
243     public void testgetPasswordFromPrefsFile() throws Exception JavaDoc{
244         final String JavaDoc enc = "ISO-8859-1";
245         final File JavaDoc f = new File JavaDoc(System.getProperty("java.io.tmpdir"),
246                                 testCommand.ASADMINPREFS);
247         f.deleteOnExit();
248         final PrintWriter JavaDoc pw = new PrintWriter JavaDoc (
249                 new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(f), enc));
250         pw.println("AS_ADMIN_PASSWORD=test_password");
251         pw.close();
252         System.setProperty("user.home", System.getProperty("java.io.tmpdir"));
253         //set the readPrefsFile & readPasswordOptionFromPrefs to true
254
String JavaDoc pwd = testCommand.getPassword(testCommand.PASSWORD, null, null,
255                                     false, true, true, false, null, null, false,
256                                     false, true, false);
257         assertEquals(pwd, "test_password");
258     }
259     
260     public void testgetPasswordFromCommandLine() throws Exception JavaDoc{
261         testCommand.setOption("password", "test_password");
262         String JavaDoc pwd = testCommand.getPassword(testCommand.PASSWORD, null, null,
263                                     true, false, false, false, null, null, false,
264                                     false, true, false);
265         assertEquals(pwd, "test_password");
266     }
267     
268     public void testgetPort() throws Exception JavaDoc{
269         testCommand.setOption(testCommand.PORT, "4848");
270         assertEquals(testCommand.getPort(), 4848);
271     }
272     
273     public void testgetPortInvalid() throws Exception JavaDoc{
274         try{
275             testCommand.setOption(testCommand.PORT, "xyz");
276             testCommand.getPort();
277         }catch (Exception JavaDoc e){
278             assertEquals(e.getMessage(), "CLI136 Port xyz should be a numeric value.");
279         }
280     }
281     
282     public void testgetReturnType() throws Exception JavaDoc{
283         Vector JavaDoc propertyValues = new Vector JavaDoc();
284         propertyValues.add("java.lang.String");
285         testCommand.setProperty(testCommand.RETURN_TYPE, propertyValues);
286         assertEquals(testCommand.getReturnType(), "java.lang.String");
287     }
288
289     public void testgetUserWhenNotSet() throws Exception JavaDoc{
290         try{
291             String JavaDoc adminUser = testCommand.getUser();
292         }catch(Exception JavaDoc e){
293             assertEquals(e.getMessage(), "CLI152 user is a required option.");
294         }
295     }
296     
297     public void testgetUserFromPrefsFile() throws Exception JavaDoc{
298         final String JavaDoc enc = "ISO-8859-1";
299         final File JavaDoc f = new File JavaDoc(System.getProperty("java.io.tmpdir"),
300                                 testCommand.ASADMINPREFS);
301         f.deleteOnExit();
302         final PrintWriter JavaDoc pw = new PrintWriter JavaDoc (
303                 new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(f), enc));
304         pw.println("AS_ADMIN_USER=admin");
305         pw.close();
306         System.setProperty("user.home", System.getProperty("java.io.tmpdir"));
307         //set the readPrefsFile & readPasswordOptionFromPrefs to true
308
String JavaDoc user = testCommand.getUser();
309         assertEquals(user, "admin");
310     }
311     
312     public void testgetUserFromCommandLine() throws Exception JavaDoc{
313         testCommand.setOption("user", "admin");
314         String JavaDoc user = testCommand.getUser();
315         assertEquals(user, "admin");
316     }
317     
318     public void testisPasswordValid() throws Exception JavaDoc{
319         boolean isValid = testCommand.isPasswordValid("eightOrMoreCharacters");
320         assertEquals(isValid, true);
321         isValid = testCommand.isPasswordValid("7chars");
322         assertEquals(isValid, false);
323     }
324     
325     public void testsetLoggerLevel() throws Exception JavaDoc{
326         testCommand.setOption(testCommand.TERSE, "false");
327         testCommand.setLoggerLevel();
328         assertEquals(CLILogger.getInstance().getOutputLevel(),java.util.logging.Level.FINE);
329         testCommand.setOption(testCommand.TERSE, "true");
330         testCommand.setLoggerLevel();
331         assertEquals(CLILogger.getInstance().getOutputLevel(),java.util.logging.Level.INFO);
332     }
333     
334     public S1ASCommandTest(String JavaDoc name){
335         super(name);
336     }
337
338     S1ASCommand testCommand = null;
339
340     protected void setUp() throws Exception JavaDoc{
341         //Properties systemProperties = new java.util.Propertis();
342
//systemProperties.put("com.sun.aas.configRoot",)
343
String JavaDoc configProperty = SystemPropertyConstants.CONFIG_ROOT_PROPERTY;
344         //System.out.println(configProperty + " = " + System.getProperty(configProperty));
345
final CLIDescriptorsReader cliDescriptorsReader = CLIDescriptorsReader.getInstance();
346         ValidCommand validCommand = cliDescriptorsReader.getCommand(null);
347         LocalStringsManagerFactory.setCommandLocalStringsManagerProperties(
348             CLIDescriptorsReader.getInstance().getProperties());
349         testCommand = new S1ASCommand() {
350                 public void runCommand()
351                     throws CommandException, CommandValidationException
352                 {
353                 }
354                 public boolean validateOptions() throws CommandValidationException
355                 {
356                     return true;
357                 }
358             };
359         testCommand.setName("sampleCommand");
360     }
361   
362   
363
364     protected void tearDown() {
365     }
366
367     private void nyi(){
368         fail("Not Yet Implemented");
369     }
370
371     public static Test suite(){
372         TestSuite suite = new TestSuite(S1ASCommandTest.class);
373         return suite;
374     }
375
376     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
377         final TestRunner runner= new TestRunner();
378         final TestResult result = runner.doRun(S1ASCommandTest.suite(), false);
379         System.exit(result.errorCount() + result.failureCount());
380     }
381 }
382
383
Popular Tags