1 23 24 29 30 package com.sun.cli.jmx.test; 31 32 import java.lang.reflect.Array ; 35 36 import javax.management.*; 39 import javax.management.remote.JMXServiceURL ; 40 41 import com.sun.jmx.trace.Trace; 42 43 import com.sun.cli.util.stringifier.*; 44 import com.sun.cli.util.ClassUtil; 45 46 import com.sun.cli.jmx.support.CLISupportStrings; 47 import com.sun.cli.jmx.support.StandardAliases; 48 import com.sun.cli.jmx.support.CLISupportMBeanProxy; 49 import com.sun.cli.jmx.support.InspectRequest; 50 import com.sun.cli.jmx.support.InspectResult; 51 import com.sun.cli.jmx.support.InvokeResult; 52 import com.sun.cli.jmx.support.ResultsForGetSet; 53 import com.sun.cli.jmx.support.InspectResultStringifier; 54 import com.sun.cli.jmx.support.ResultsForGetSetStringifier; 55 import com.sun.cli.jmx.support.InvokeResultStringifier; 56 57 58 public final class CLISupportTester 59 { 60 final MBeanServerConnection mServer; 61 final CLISupportMBeanProxy mProxy; 62 final TestLog mLog; 63 64 final static private String ALIAS_BASE = "test-alias-"; 65 private final static String CLI_TEST_ALIAS_NAME = ALIAS_BASE + "generic-test"; 66 67 public 68 CLISupportTester( MBeanServerConnection conn, CLISupportMBeanProxy proxy ) 69 throws Exception 70 { 71 this( conn, proxy, new StdOutTestLog() ); 72 } 73 74 public 75 CLISupportTester( MBeanServerConnection conn, CLISupportMBeanProxy proxy, TestLog logger) 76 throws Exception 77 { 78 mServer = conn; 79 mLog = logger; 80 81 mProxy = proxy; 82 } 83 84 private void 85 p( Object arg ) 86 { 87 mLog.println( arg ); 88 } 89 90 private void 91 p( Object arg, boolean newline) 92 { 93 if ( newline ) 94 { 95 p( arg ); 96 } 97 else 98 { 99 mLog.print( arg ); 100 } 101 } 102 103 private void 104 begin( String msg ) 105 { 106 p( msg + "...", false); 107 } 108 109 private static String 110 quote( Object str ) 111 { 112 return( "\"" + str.toString() + "\"" ); 113 } 114 115 116 private void 117 TestMBeanList( CLISupportMBeanProxy test ) throws Exception 118 { 119 final String [] testStrings = new String [] { StandardAliases.ALL_ALIAS }; 120 121 final ObjectName [] results = test.mbeanFind( testStrings ); 122 final int numObjects = Array.getLength( results ); 123 124 final ArrayStringifier testStringsStringifier = new ArrayStringifier( "," ); 125 126 p( "\nmbean-list results: " + numObjects + 127 " mbeans for expr " + testStringsStringifier.stringify( testStrings ) ); 128 129 final String str = ArrayStringifier.stringify( results, "\n", ObjectStringifier.DEFAULT); 130 p ( str ); 131 132 p( "TestMBeanList...DONE: listed " + numObjects + " for " + testStrings[ 0 ] ); 133 } 134 135 private void 136 TestMBeanInspect( CLISupportMBeanProxy test, String [] patterns ) throws Exception 137 { 138 begin( "TestMBeanInspect" ); 139 140 final InspectRequest request = new InspectRequest(); 141 142 final InspectResult [] results = test.mbeanInspect( request, patterns ); 143 final int numResults = Array.getLength( results ); 144 145 final String summary = "" + numResults + 146 " mbeans for expr " + ArrayStringifier.stringify( patterns, ","); 147 148 final String str = ArrayStringifier.stringify( results, "\n\n", new InspectResultStringifier()); 149 150 p( "DONE: inspected " + summary); 151 } 152 153 private void 154 TestMBeanGet( CLISupportMBeanProxy test, String [] targets ) throws Exception 155 { 156 begin( "TestMBeanGet" ); 157 158 final ResultsForGetSet [] results = test.mbeanGet( "*", targets ); 159 final int numResults = Array.getLength( results ); 160 161 final String summary = "" + numResults + 162 " mbeans for expr " + ArrayStringifier.stringify( targets, ","); 163 164 final ResultsForGetSetStringifier resultStringifier = 165 new ResultsForGetSetStringifier( ); 166 167 final String str = ArrayStringifier.stringify( results, "\n\n", resultStringifier); 168 169 p ( str ); 170 171 p( "DONE: inspected " + summary); 172 } 173 174 175 private String 176 InvokeResultsToString( InvokeResult [] results) 177 { 178 return( ArrayStringifier.stringify( results, "\n", new InvokeResultStringifier() ) ); 179 } 180 181 182 private void 183 testInvoke( String operationName, String args, String [] targets ) 184 throws Exception 185 { 186 InvokeResult [] results = null; 187 188 results = mProxy.mbeanInvoke( operationName, args, targets ); 189 190 p( InvokeResultsToString( results ) + "\n" ); 191 } 192 193 private void 194 TestNamedInvoke( CLISupportMBeanProxy test, String [] targets ) throws Exception 195 { 196 begin( "TestNamedInvoke" ); 197 198 testInvoke( "testNamed", "p1=hello", targets ); 199 200 testInvoke( "testNamed", "p1=hello,p2=there", targets ); 201 202 testInvoke( "testNamed", "p1=hello,p2=there,p3=!!!", targets ); 203 204 testInvoke( "testNamed", "p1=hello,p2=there,p3=!!!,p4=foobar", targets ); 205 206 p( "DONE "); 207 } 208 209 210 private void 211 TestMBeanInvoke( CLISupportMBeanProxy test, String [] targets ) throws Exception 212 { 213 TestNamedInvoke( test, targets ); 214 } 215 216 217 218 private void 219 deleteTestAliases() throws Exception 220 { 221 final String [] aliases = mProxy.listAliases( false ); 222 223 for( int i = 0; i < aliases.length; ++i ) 224 { 225 final String name = aliases[ i ]; 226 227 if ( name.startsWith( ALIAS_BASE ) ) 228 { 229 p( "deleteTestAliases: deleting: " + name ); 230 mProxy.deleteAlias( name ); 231 } 232 } 233 } 234 235 private void 236 TestAliases( ) throws Exception 237 { 238 begin( "TestAliases" ); 239 240 deleteTestAliases(); 241 242 int failureCount = 0; 243 244 final ObjectName [] names = mProxy.mbeanFind( new String [] { StandardAliases.ALL_ALIAS } ); 246 final int numNames = Array.getLength( names ); 247 248 for( int i = 0; i < numNames; ++i ) 250 { 251 final String aliasName = ALIAS_BASE + (i+1); 252 mProxy.createAlias( aliasName, names[ i ].toString() ); 253 } 254 255 for( int i = 0; i < numNames; ++i ) 257 { 258 final String aliasName = ALIAS_BASE + (i+1); 259 260 final String aliasValue = mProxy.resolveAlias( aliasName ); 261 if ( aliasValue == null || ! names[ i ].toString().equals( aliasValue )) 262 { 263 ++failureCount; 264 p( "FAILURE: alias " + aliasName + ": " + 265 quote( aliasValue ) + " != " + quote( names[ i ].toString() ) ); 266 } 267 } 268 269 final String ALL_ALIASES_NAME = ALIAS_BASE + "all"; 271 final String [] aliases = mProxy.listAliases( false ); 272 final String allAliases = ArrayStringifier.stringify( aliases, " " ); 273 mProxy.createAlias( ALL_ALIASES_NAME, allAliases ); 274 275 String allAliasesName = ALL_ALIASES_NAME; 277 for( int i = 0; i < 5; ++i ) 278 { 279 mProxy.createAlias( allAliasesName + i, allAliasesName ); 280 allAliasesName = allAliasesName + i; 281 } 282 283 final ObjectName [] resolvedNames = mProxy.resolveTargets( new String [] { allAliasesName } ); 285 if ( Array.getLength( resolvedNames ) != numNames ) 287 { 288 ++failureCount; 289 } 290 291 deleteTestAliases(); 292 293 if ( failureCount == 0 ) 294 { 295 p( "DONE" ); 296 } 297 else 298 { 299 p( "FAILURES = " + failureCount ); 300 } 301 } 302 303 306 private String 307 MakeArgList( final String [] args ) 308 { 309 final int numArgs = Array.getLength( args ); 310 String result = null; 311 312 if ( numArgs != 0 ) 313 { 314 final StringBuffer buf = new StringBuffer (); 315 316 for( int i = 0; i < numArgs; ++i ) 317 { 318 buf.append( args[ i ] ); 319 buf.append( "," ); 320 } 321 buf.setLength( buf.length() - 1 ); 323 324 result = new String ( buf ) ; 325 } 326 327 return( result ); 328 } 329 330 331 private String 332 getCastType( String type ) 333 throws ClassNotFoundException 334 { 335 String result = type; 336 337 if ( ClassUtil.classnameIsArray( result ) ) 338 { 339 final Class theClass = ClassUtil.getClassFromName(result); 340 341 final Class elementClass = ClassUtil.getInnerArrayElementClass( theClass ); 342 343 result = elementClass.getName(); 344 } 345 346 return( result ); 347 } 348 349 350 351 private InvokeResult.ResultType 352 TestOperationGenerically( 353 final CLISupportMBeanProxy test, 354 final boolean namedArgs, 355 final ObjectName targetName, 356 final MBeanOperationInfo operationInfo ) 357 throws Exception 358 { 359 final MBeanParameterInfo [] paramInfos = operationInfo.getSignature(); 360 final int numParams = Array.getLength( paramInfos ); 361 362 final String [] strings = new String [ numParams ]; 363 final String operationName = operationInfo.getName(); 364 365 for( int i = 0; i < numParams; ++i ) 368 { 369 final MBeanParameterInfo paramInfo = paramInfos[ i ]; 370 final String paramType = paramInfos[ i ].getType(); 371 final Class theClass = ClassUtil.getClassFromName( paramType ); 372 373 final Object paramObject = ClassUtil.InstantiateDefault( theClass ); 374 final String paramString = SmartStringifier.toString( paramObject ); 375 final String castString = "(" + getCastType( paramType ) + ")"; 376 377 final String paramName = namedArgs ? (paramInfo.getName() + '=') : ""; 378 379 strings[ i ] = paramName + castString + paramString; 380 } 381 382 final String argString = MakeArgList( strings ); 384 385 final String [] args = new String [] { targetName.toString() }; 386 387 final InvokeResult [] results = (InvokeResult [])test.mbeanInvoke( operationName, argString, args ); 388 final InvokeResult result = results[ 0 ]; 389 390 if ( result.getResultType() == InvokeResult.SUCCESS ) 391 { 392 } 394 else 395 { 396 final String paramInfosString = SmartStringifier.toString( paramInfos ); 397 398 p( "FAILURE: " + operationName + "(" + paramInfosString + ")" + 399 " with " + argString ); 400 result.mThrowable.printStackTrace(); 401 } 402 403 return( result.getResultType() ); 404 } 405 406 static private final Class [] GENERICALLY_TESTABLE_CLASSES = 407 { 408 boolean.class, 409 char.class, 410 byte.class, short.class, int.class, long.class, 411 float.class, double.class, 412 Boolean .class, 413 Character .class, 414 Byte .class, Short .class, Integer .class, Long .class, 415 Float .class, 416 Double .class, 417 Number .class, 418 String .class, 419 Object .class, 420 java.math.BigDecimal .class, 421 java.math.BigInteger .class, 422 java.net.URL .class, 423 java.net.URI .class 424 425 }; 426 private boolean 427 IsGenericallyTestableClass( final Class theClass ) 428 throws ClassNotFoundException 429 { 430 boolean isTestable = false; 431 432 Class testClass = theClass; 433 if ( ClassUtil.classIsArray( theClass ) ) 434 { 435 testClass = ClassUtil.getInnerArrayElementClass( theClass ); 437 } 438 439 final Class [] classes = GENERICALLY_TESTABLE_CLASSES; 440 final int numClasses = Array.getLength( classes ); 441 for( int i = 0; i < numClasses; ++i ) 442 { 443 if ( testClass == classes[ i ] ) 444 { 445 isTestable = true; 446 break; 447 } 448 } 449 450 if ( ! isTestable ) 451 { 452 assert( testClass == java.util.Properties .class ); 453 } 454 455 return( isTestable ); 456 } 457 458 private boolean 459 IsGenericallyTestable( final MBeanOperationInfo operationInfo ) 460 throws ClassNotFoundException 461 { 462 boolean isTestable = true; 463 464 final MBeanParameterInfo [] paramInfos = operationInfo.getSignature(); 465 final int numParams = Array.getLength( paramInfos ); 466 for( int i = 0; i < numParams; ++i ) 467 { 468 final Class theClass = ClassUtil.getClassFromName( paramInfos[i].getType() ); 469 470 if ( ! IsGenericallyTestableClass( theClass ) ) 471 { 472 isTestable = false; 473 break; 474 } 475 } 476 477 return( isTestable ); 478 } 479 480 481 private void 482 TestGeneric( CLISupportMBeanProxy test, boolean namedTest, ObjectName objectName ) throws Exception 483 { 484 final MBeanInfo info = mServer.getMBeanInfo( objectName ); 485 final MBeanOperationInfo [] opInfo = info.getOperations(); 486 487 begin( "TestGeneric" ); 488 489 int successCount = 0; 490 int failureCount = 0; 491 int notTestedCount = 0; 492 for( int i = 0; i < Array.getLength( opInfo ); ++i ) 493 { 494 try 495 { 496 if ( IsGenericallyTestable( opInfo[ i ] ) ) 497 { 498 final InvokeResult.ResultType resultType = TestOperationGenerically( test,namedTest, objectName, opInfo[ i ] ); 499 if ( resultType == InvokeResult.SUCCESS ) 500 { 501 ++successCount; 502 } 503 else 504 { 505 ++failureCount; 506 } 507 } 508 else 509 { 510 ++notTestedCount; 511 } 512 } 513 catch( Exception e ) 514 { 515 p( "FAILURE: " + SmartStringifier.toString( opInfo[ i ] ) ); 516 } 517 } 518 519 p( "DONE " + (namedTest ? "NAMED":"ORDERED") + 520 ": SUCCESSES = " + successCount + 521 ", FAILURES = " + failureCount + 522 ", NOT TESTABLE = " + notTestedCount ); 523 } 524 525 private void 526 TestGeneric( CLISupportMBeanProxy test, String [] targets ) throws Exception 527 { 528 final ObjectName [] allObjects = test.mbeanFind( targets ); 529 530 assert( allObjects.length >= 1 ); 531 532 for( int i = 0; i < allObjects.length; ++i ) 533 { 534 TestGeneric( test, false, allObjects[ i ] ); 535 } 536 537 for( int i = 0; i < allObjects.length; ++i ) 538 { 539 TestGeneric( test, true, allObjects[ i ] ); 540 } 541 } 542 543 private void 544 VerifySetup( CLISupportMBeanProxy proxy ) throws Exception 545 { 546 final ObjectName [] all = proxy.resolveTargets( new String [] { "*" } ); 548 assert( all.length != 0 ); 549 550 final String [] aliases = proxy.listAliases( false ); 552 assert( aliases.length != 0 ); 553 554 assert( proxy.resolveAlias( StandardAliases.ALL_ALIAS ) != null ); 556 assert( proxy.resolveAlias( StandardAliases.CLI_ALIAS ) != null ); 557 assert( proxy.resolveAlias( StandardAliases.ALIAS_MGR_ALIAS ) != null ); 558 559 } 560 561 562 public void 563 Run() throws Exception 564 { 565 final CLISupportMBeanProxy proxy = mProxy; 566 567 try 568 { 569 final String [] all = new String [] { StandardAliases.ALL_ALIAS }; 571 572 VerifySetup( proxy ); 573 574 TestAliases( ); 575 576 TestMBeanList( proxy ); 577 578 TestMBeanGet( proxy, all ); 579 580 TestMBeanInspect( proxy, all ); 581 582 proxy.deleteAlias( CLI_TEST_ALIAS_NAME ); 583 proxy.createAlias( CLI_TEST_ALIAS_NAME, CLISupportStrings.CLI_SUPPORT_TESTEE_TARGET ); 584 final String [] testMBean = new String [] { CLI_TEST_ALIAS_NAME }; 585 586 TestMBeanInvoke( proxy, testMBean ); 587 588 TestGeneric( proxy, testMBean); 589 590 591 p( "DONE" ); 592 593 594 } 595 catch( Exception e ) 596 { 597 e.printStackTrace(); 598 } 599 } 600 601 602 }; 603 604 605 | Popular Tags |