1 23 package com.sun.enterprise.cli.commands; 24 25 31 import com.sun.enterprise.cli.framework.*; 32 import java.util.Vector ; 38 import junit.framework.*; 39 import junit.textui.TestRunner; 40 import java.io.File ; 41 import java.io.PrintWriter ; 42 import java.io.OutputStreamWriter ; 43 import java.io.FileOutputStream ; 44 import java.util.Properties ; 45 import java.util.Map ; 46 import java.util.Hashtable ; 47 import com.sun.enterprise.util.SystemPropertyConstants; 48 import com.sun.appserv.management.util.misc.ExceptionUtil; 49 50 55 56 61 62 public class S1ASCommandTest extends TestCase { 63 public void testCheckForFileExistence() throws Exception { 64 final File f = File.createTempFile("S1ASCommandTest_testCheckForFileExistence", ".tmp"); 65 f.deleteOnExit(); 66 String tempdir = System.getProperty("java.io.tmpdir"); 67 assertTrue(testCommand.checkForFileExistence(tempdir, f.getName()).exists()); 68 } 69 70 public void testCheckForFileExistenceWhenNoFile() throws Exception { 71 String tempdir = System.getProperty("java.io.tmpdir"); 72 try{ 73 File f = testCommand.checkForFileExistence(tempdir, "FileDoesNotExist"); 74 }catch (Exception 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 { 80 String propStr = "name=value"; 81 Properties props = testCommand.createPropertiesParam(propStr); 82 assertEquals(props.getProperty("name"),"value"); 83 } 84 85 public void testCreatePropertiesParamWithEscapes() throws Exception { 86 String propStr = "user=dbuser:password=dbpassword:DatabaseName=jdbc\\:pointbase:server=http\\://localhost\\:9292"; 87 Properties 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 { 95 String propStr = "name1=value1:value2"; 96 try{ 97 Properties props = testCommand.createPropertiesParam(propStr); 98 }catch (Exception e){ 99 assertEquals(e.getMessage(), "CLI131 Invalid property syntax."); 100 } 101 } 102 103 public void testCreateStringArrayParam() throws Exception { 104 String paramStr = "value1:value2"; 105 String [] params = testCommand.createStringArrayParam(paramStr); 106 assertEquals(params[0],"value1"); 107 assertEquals(params[1],"value2"); 108 } 109 110 public void testCreateStringArrayParamWithEscapes() throws Exception { 111 String paramStr = "-XX\\:NewRatio=2"; 112 String [] params = testCommand.createStringArrayParam(paramStr); 113 assertEquals(params[0],"-XX:NewRatio=2"); 114 } 115 116 public void testDisplayExceptionMessage(){ 117 NullPointerException npe = new NullPointerException ("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 { 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 { 147 Vector propertyValues = new Vector (); 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 { 154 Vector propertyValues = new Vector (); 155 propertyValues.add("testMethod"); 156 testCommand.setProperty(testCommand.OPERATION, propertyValues); 157 assertEquals(testCommand.getOperationName(), "testMethod"); 158 } 159 160 public void testgetTypesInfo() throws Exception { 161 Vector propertyValues = new Vector (); 162 for (int i=0; i<=3; i++){ 163 propertyValues.add("java.lang.String"); 164 } 165 testCommand.setProperty(testCommand.PARAM_TYPES, propertyValues); 166 String [] 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 { 172 173 Vector typesValues = new Vector (); 174 typesValues.add("java.lang.String"); 175 testCommand.setProperty(testCommand.PARAM_TYPES, typesValues); 176 testCommand.setOption("testOption", "test"); 177 Vector paramValues = new Vector (); 178 paramValues.add("{$testOption}"); 179 testCommand.setProperty(testCommand.PARAMS, paramValues); 180 Object [] params = testCommand.getParamsInfo(); 181 assertEquals((String )params[0], "test"); 182 } 183 184 public void testgetParamsInfo() throws Exception { 185 186 Vector typesValues = new Vector (); 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 paramValues = new Vector (); 193 for (int i=0; i<3; i++){ 194 paramValues.add("x"+i); 195 } 196 testCommand.setProperty(testCommand.PARAMS, paramValues); 197 Object [] params = testCommand.getParamsInfo(); 198 for (int i=0; i<params.length; i++) 199 assertEquals((String )params[i], "x"+i); 200 } 201 202 public void testgetParamsInfoInvalid() throws Exception { 203 Vector typesValues = new Vector (); 205 typesValues.add("java.lang.String"); 206 typesValues.add("java.lang.String"); 207 testCommand.setProperty(testCommand.PARAM_TYPES, typesValues); 208 209 Vector paramValues = new Vector (); 210 for (int i=0; i<3; i++){ 211 paramValues.add("x"+i); 212 } 213 testCommand.setProperty(testCommand.PARAMS, paramValues); 214 try{ 215 Object [] 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 { 223 final String enc = "ISO-8859-1"; 224 final File f = new File (System.getProperty("java.io.tmpdir"), 225 testCommand.ASADMINPREFS); 226 f.deleteOnExit(); 227 final PrintWriter pw = new PrintWriter ( 228 new OutputStreamWriter (new FileOutputStream (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 { 237 String 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 { 244 final String enc = "ISO-8859-1"; 245 final File f = new File (System.getProperty("java.io.tmpdir"), 246 testCommand.ASADMINPREFS); 247 f.deleteOnExit(); 248 final PrintWriter pw = new PrintWriter ( 249 new OutputStreamWriter (new FileOutputStream (f), enc)); 250 pw.println("AS_ADMIN_PASSWORD=test_password"); 251 pw.close(); 252 System.setProperty("user.home", System.getProperty("java.io.tmpdir")); 253 String 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 { 261 testCommand.setOption("password", "test_password"); 262 String 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 { 269 testCommand.setOption(testCommand.PORT, "4848"); 270 assertEquals(testCommand.getPort(), 4848); 271 } 272 273 public void testgetPortInvalid() throws Exception { 274 try{ 275 testCommand.setOption(testCommand.PORT, "xyz"); 276 testCommand.getPort(); 277 }catch (Exception e){ 278 assertEquals(e.getMessage(), "CLI136 Port xyz should be a numeric value."); 279 } 280 } 281 282 public void testgetReturnType() throws Exception { 283 Vector propertyValues = new Vector (); 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 { 290 try{ 291 String adminUser = testCommand.getUser(); 292 }catch(Exception e){ 293 assertEquals(e.getMessage(), "CLI152 user is a required option."); 294 } 295 } 296 297 public void testgetUserFromPrefsFile() throws Exception { 298 final String enc = "ISO-8859-1"; 299 final File f = new File (System.getProperty("java.io.tmpdir"), 300 testCommand.ASADMINPREFS); 301 f.deleteOnExit(); 302 final PrintWriter pw = new PrintWriter ( 303 new OutputStreamWriter (new FileOutputStream (f), enc)); 304 pw.println("AS_ADMIN_USER=admin"); 305 pw.close(); 306 System.setProperty("user.home", System.getProperty("java.io.tmpdir")); 307 String user = testCommand.getUser(); 309 assertEquals(user, "admin"); 310 } 311 312 public void testgetUserFromCommandLine() throws Exception { 313 testCommand.setOption("user", "admin"); 314 String user = testCommand.getUser(); 315 assertEquals(user, "admin"); 316 } 317 318 public void testisPasswordValid() throws Exception { 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 { 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 name){ 335 super(name); 336 } 337 338 S1ASCommand testCommand = null; 339 340 protected void setUp() throws Exception { 341 String configProperty = SystemPropertyConstants.CONFIG_ROOT_PROPERTY; 344 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 args[]) throws Exception { 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 |