FileDocCategorySizeDatePackage
CLISupportMBeanImplTest.javaAPI DocGlassfish v2 API26657Fri May 04 22:24:52 BST 2007com.sun.cli.jmx.support

CLISupportMBeanImplTest

public final class CLISupportMBeanImplTest extends TestCase

Fields Summary
MBeanServer
mServer
com.sun.cli.jmx.support.CLISupportMBeanProxy
mProxy
private static final String
ALIAS_BASE
private static final String
CLI_TEST_ALIAS_NAME
private static final String
ALL_ALIAS
private static final String
SIMPLE_TESTEE_ALIAS
private static final String
SUPPORT_TESTEE_ALIAS
private static final String[]
ALL_TARGETS
private static final String[]
SIMPLE_TESTEE_TARGET
private static final String[]
SUPPORT_TESTEE_TARGET
private static final String
ARGS_11
private static final Class[]
GENERICALLY_TESTABLE_CLASSES
private static final com.sun.cli.jmx.test.CLISupportTestee
CLI_SUPPORT_TESTEE
Constructors Summary
public CLISupportMBeanImplTest()


	
		
	  
	
	
Methods Summary
private voidcheckEqualArray(java.lang.Object[] expected, java.lang.Object[] actual)

		assertEquals( expected.getClass(), actual.getClass() );
		assertEquals( expected.length, actual.length );
		
		for( int i = 0; i < expected.length; ++i )
		{
			if ( expected[ i ] == null )
			{
				assertEquals( null, actual[ i ] );
			}
			else
			{
				assertEquals( expected[ i ].getClass(), actual[ i ].getClass() );
			
				if ( ClassUtil.objectIsArray( expected[ i ] ) )
				{
					if ( ClassUtil.objectIsPrimitiveArray( expected[ i ] ) )
					{
						final Object []	e	= ArrayConversion.toAppropriateType( expected[ i ] );
						final Object []	a	= ArrayConversion.toAppropriateType( actual[ i ] );
						
						checkEqualArray( e, a );
					}
					else
					{
						checkEqualArray( (Object [])expected[ i ], (Object [])actual[ i ] );
					}
				}
				else
				{
					assertEquals( expected[ i ], actual[ i ] );
				}
			}
		}
	
private javax.management.MBeanServercreateAgent()

		return( MBeanServerFactory.createMBeanServer( "Test" ) );
	
private voiddeleteTestAliases()

		assert( mProxy != null );
		
		final String []	aliases	= mProxy.listAliases( false );
		assert( aliases != null );
		
		for( int i = 0; i < aliases.length; ++i )
		{
			final String	name	= aliases[ i ];
			
			if ( name.startsWith( ALIAS_BASE ) )
			{
				mProxy.deleteAlias( name );
			}
		}
	
private voiddoIntegerTest(java.lang.String arg, java.lang.Object expected)

		final Integer		result	= (Integer)doTest( "testInteger", arg );
		assertEquals( expected, result );
	
private voiddoObjectArrayTest(java.lang.String arg, java.lang.Object[] expected)

		final Object []		result	= (Object [])doTest( "testObjectArray", arg );
		checkEqualArray( expected, result );
	
private voiddoObjectTest(java.lang.String arg, java.lang.Object expected)

		final Object		result	= doTest( "testObject", arg );
		assertEquals( expected, result );
	
private voiddoStringTest(java.lang.String arg, java.lang.String expected)

		final String		result	= (String)doTest( "testString", arg );
		assertEquals( expected, result );
	
private java.lang.ObjectdoTest(java.lang.String operationName, java.lang.String arg)

		final InvokeResult	invokeResult	= invokeSimpleTestee( operationName, arg );
		
		if ( invokeResult.getResultType() != InvokeResult.SUCCESS )
		{
			fail( "invocation failed for " + operationName + "(" + arg + ")" );
		}
		
		return( invokeResult.mResult );
	
private java.lang.StringgetCastType(java.lang.String type)

		String	result	= type;
		
		if ( ClassUtil.classnameIsArray( result ) )
		{
			final Class	theClass	= ClassUtil.getClassFromName(result);
			
			final Class elementClass	= ClassUtil.getInnerArrayElementClass( theClass );
			
			result	= elementClass.getName();
		}
		
		return( result );
	
private com.sun.cli.jmx.support.InvokeResult[]invoke(java.lang.String operationName, java.lang.String args, java.lang.String[] targets)

		final InvokeResult []	results = mProxy.mbeanInvoke( operationName, args, targets );
		
		return( results );
	
private com.sun.cli.jmx.support.InvokeResultinvokeSimpleTestee(java.lang.String operationName, java.lang.String args)

		final InvokeResult []	results = invoke( operationName, args, SIMPLE_TESTEE_TARGET );
		assert( results.length == 1 );
		
		return( results[ 0 ] );
	
private com.sun.cli.jmx.support.InvokeResultinvokeSupportTestee(java.lang.String operationName, java.lang.String args)

		final InvokeResult []	results = invoke( operationName, args, SUPPORT_TESTEE_TARGET );
		assert( results.length == 1 );
		
		return( results[ 0 ] );
	
private booleanisGenericallyTestable(javax.management.MBeanOperationInfo operationInfo)

		boolean	isTestable	= true;
		
		final MBeanParameterInfo []	paramInfos	= operationInfo.getSignature();
		final int					numParams	= paramInfos.length;
		for( int i = 0; i < numParams; ++i )
		{
			final Class	theClass	= ClassUtil.getClassFromName( paramInfos[i].getType() );
			
			if ( ! isGenericallyTestableClass( theClass ) )
			{
				isTestable	= false;
				break;
			}
		}
		
		return( isTestable );
	
private booleanisGenericallyTestableClass(java.lang.Class theClass)

		 
	    
		 
	
		boolean	isTestable	= false;
		
		Class	testClass	= theClass;
		if ( ClassUtil.classIsArray( theClass ) )
		{
			// we can test all arrays of supported types
			testClass	= ClassUtil.getInnerArrayElementClass( theClass );
		}
		
		final Class []	classes	= GENERICALLY_TESTABLE_CLASSES;
		final int	numClasses	= classes.length;
		for( int i = 0; i < numClasses; ++i )
		{
			if ( testClass == classes[ i ] )
			{
				isTestable	= true;
				break;
			}
		}
		
		if ( ! isTestable  )
		{
			assert( testClass == java.util.Properties.class );
		}
		
		return( isTestable );
	
private java.lang.StringmakeArgList(java.lang.String[] args)

		final int			numArgs	= args.length;
		String				result	= null;
		
		if ( numArgs != 0 )
		{
			final StringBuffer	buf	= new StringBuffer();
		
			for( int i = 0; i < numArgs; ++i )
			{
				buf.append( args[ i ] );
				buf.append( "," );
			}
			// strip trailing ","
			buf.setLength( buf.length() - 1 );
			
			result	= new String( buf ) ;
		}
		
		return( result );
	
private voidregisterMBean(javax.management.MBeanServer conn, java.lang.Object mbean, java.lang.String name)

		conn.registerMBean( mbean, new ObjectName( name ) );
	
public voidsetUp()

		 
	  
	
		mServer	= createAgent(  );
		
		// CLI_SUPPORT_TESTEE is very expensive to create, so we'll always reuse the same one
		registerMBean( mServer, CLI_SUPPORT_TESTEE, CLISupportStrings.CLI_SUPPORT_TESTEE_TARGET );
		
		registerMBean( mServer, new CLISupportSimpleTestee( ),
			CLISupportStrings.CLI_SIMPLE_TESTEE_TARGET );
		
		final AliasMgr	aliasMgr = new AliasMgr( new AliasMgrHashMapImpl() );
		aliasMgr.createAlias( ALL_ALIAS, "*" );
		aliasMgr.createAlias( SIMPLE_TESTEE_ALIAS, CLISupportStrings.CLI_SIMPLE_TESTEE_TARGET );
		aliasMgr.createAlias( SUPPORT_TESTEE_ALIAS, CLISupportStrings.CLI_SUPPORT_TESTEE_TARGET );
		
		final CLISupportMBeanImpl	cliSupport = new CLISupportMBeanImpl( mServer, aliasMgr );
		
		mProxy	= new CLISupportMBeanProxy( aliasMgr, cliSupport );
		
		verifySetup( mProxy );
	
public voidtearDown()

		mProxy.mbeanUnregister( mProxy.resolveAlias( SUPPORT_TESTEE_ALIAS ) );
		mProxy.mbeanUnregister( mProxy.resolveAlias( SIMPLE_TESTEE_ALIAS ) );
		mProxy	= null;
		mServer	= null;
	
public voidtest11MixedArgs()

		invokeSimpleTestee( "test11MixedArgs", ARGS_11 );
	
public voidtest11ObjectArgs()

    
		 
	   
	
		invokeSimpleTestee( "test11ObjectArgs", ARGS_11 );
	
public voidtestAliases()

		deleteTestAliases();
		
		// create an alias for each MBean
		final ObjectName []	names		= mProxy.mbeanFind( new String [] { StandardAliases.ALL_ALIAS } );
		final int			numNames	= names.length;
		
		// create  test alias for each existing MBean
		for( int i = 0; i < numNames; ++i )
		{
			final String	aliasName	= ALIAS_BASE + (i+1);
			mProxy.createAlias( aliasName, names[ i ].toString() );
		}
		
		// now verify that each of them resolves correctly
		for( int i = 0; i < numNames; ++i )
		{
			final String	aliasName	= ALIAS_BASE + (i+1);
			
			final String	aliasValue	= mProxy.resolveAlias( aliasName );
			if ( aliasValue == null )
			{
				fail( "can't resolve alias after creating it: " + aliasName );
			}
			
			if ( ! names[ i ].toString().equals( aliasValue ))
			{
				fail( "alias does not resolve to value it was created with: " + aliasName );
			}
		}
		
		// create an alias consisting of all aliases
		final String	ALL_ALIASES_NAME	= ALIAS_BASE + "all";
		final String []	aliases	= mProxy.listAliases( false );
		final String	allAliases	= ArrayStringifier.stringify( aliases, " " );
		mProxy.createAlias( ALL_ALIASES_NAME, allAliases );
		
		// create a recursive alias
		String	allAliasesName	= ALL_ALIASES_NAME;
		for( int i = 0; i < 5; ++i )
		{
			mProxy.createAlias( allAliasesName + i, allAliasesName );
			allAliasesName	= allAliasesName + i;
		}
		
		// verify that the alias to all of them produces the same set of names as we started with
		final ObjectName []	resolvedNames	= mProxy.resolveTargets( new String [] { allAliasesName } );
		//p( "all aliases = " + ArrayStringifier.stringify( resolvedNames, "\n" ) );
		if ( resolvedNames.length !=  numNames )
		{
			fail( "alias resolution produce wrong number of results" );
		}
		
		deleteTestAliases();
	
public voidtestArrayWithCastAndElementsWithCast()

		doObjectArrayTest( "(Number){(BigInteger)0,(BigDecimal)10.0}",
				new Number [] { new BigInteger( "0" ), new BigDecimal( "10.0" ) } );
		
		// now see that incompatible casts are rejected
		final String	input	= "(Number){(String)hello}";
		
		final InvokeResult	invokeResult	= invokeSimpleTestee( "testObjectArray", input );
		if ( invokeResult.getResultType() == InvokeResult.SUCCESS )
		{
			fail( "expected this construct to fail: " + input );
		}
	
public voidtestArrayWithEmptyElements()

		final String	s	= new String( "" );
		
		doObjectArrayTest( "(String){,,,}", new String [] { s,s,s,s } );
		doObjectArrayTest( "{,,,}", new Object [] { s,s,s,s } );
	
public voidtestArrayWithNullElements()

		doObjectArrayTest( "{null,null}", new Object [] { null, null } );
		
		doObjectArrayTest( "{(String)null,(Object)null}", new Object [] { null, null } );
	
public voidtestCaseInsensitivity()

		final InvokeResult	result	= invokeSimpleTestee( "testcasesensitivity2", null );
		assertEquals( "testCaseSensitivity2", result.mResult );
	
public voidtestCaseSensitivity()

		InvokeResult	result;
		
		result	= invokeSimpleTestee( "testcasesensitivity1", null );
		assertEquals( "testcasesensitivity1", result.mResult );
		
		result	= invokeSimpleTestee( "testCASESENSITIVITY1", null );
		assertEquals( "testCASESENSITIVITY1", result.mResult );
		
		result	= invokeSimpleTestee( "testCaseSensitivity1", null );
		assertEquals( "testCaseSensitivity1", result.mResult );
		
		try
		{
			result	= invokeSimpleTestee( "testcaseSensitivity1", null );
			if ( result.getResultType() == InvokeResult.SUCCESS )
			{
				fail( "expected ambiguous match to prevent execution of method: " + result.mResult );
			}
		}
		catch( Exception e )
		{
			// good, expected this
		}
	
public voidtestEmptyArray()

		doObjectArrayTest( "{}", new Object [ 0 ]);
	
public voidtestEmptyObjectArrayWithCast()

		doObjectArrayTest( "(Object){}", new Object [ 0 ]);
		doObjectArrayTest( "(String){}", new String [ 0 ]);
		doObjectArrayTest( "(Character){}", new Character [ 0 ]);
		doObjectArrayTest( "(Boolean){}", new Boolean [ 0 ]);
		doObjectArrayTest( "(Byte){}", new Byte [ 0 ]);
		doObjectArrayTest( "(Short){}", new Short [ 0 ]);
		doObjectArrayTest( "(Integer){}", new Integer [ 0 ]);
		doObjectArrayTest( "(Long){}", new Long [ 0 ]);
		doObjectArrayTest( "(Float){}", new Float [ 0 ]);
		doObjectArrayTest( "(Double){}", new Double [ 0 ]);
		doObjectArrayTest( "(BigInteger){}", new BigInteger [ 0 ]);
		doObjectArrayTest( "(BigDecimal){}", new BigDecimal [ 0 ]);
		doObjectArrayTest( "(Number){}", new Number [ 0 ]);
	
public voidtestEmptyQuotedString()

		doStringTest( "\"\"", "");
	
public voidtestEmptyQuotedStringWithCast()

		doStringTest( "(String)\"\"", "");
	
public voidtestEmptyStringWithCast()

		doStringTest( "(String)", "");
	
public voidtestEscapedString()

		final String	result	= "\"\n\r\t\"";
		
		doStringTest( "\\\"\\n\\r\\t\\\"", result);
	
private voidtestGeneric(boolean namedTest, javax.management.ObjectName objectName)

		final MBeanInfo				info	= mServer.getMBeanInfo( objectName );
		final MBeanOperationInfo []	opInfos	= info.getOperations();
		
		int	notTestedCount	= 0;
		for( int i = 0; i < opInfos.length; ++i )
		{
			try
			{
				if ( isGenericallyTestable( opInfos[ i ] ) )
				{
					final InvokeResult.ResultType resultType	=
						testOperationGenerically( namedTest, objectName, opInfos[ i ] );
						
					if ( resultType != InvokeResult.SUCCESS )
					{
						fail( "invocation failure on: " + SmartStringifier.toString( opInfos[ i ] ) );
					}
				}
				else
				{
					++notTestedCount;
				}
			}
			catch( Exception e )
			{
				fail( "FAILURE: " + SmartStringifier.toString( opInfos[ i ] ) );
			}
		}
	
public voidtestGenericNamed()

		final ObjectName []	allObjects	= mProxy.mbeanFind( SUPPORT_TESTEE_ALIAS );
		assert( allObjects.length == 1 );
		
		testGeneric(  false, allObjects[ 0 ] );
	
public voidtestGenericOrdered()

		final ObjectName []	allObjects	= mProxy.mbeanFind( SUPPORT_TESTEE_ALIAS );
		assert( allObjects.length == 1 );
		
		testGeneric(  false, allObjects[ 0 ] );
	
public voidtestInteger()

		doIntegerTest("(Integer)10", Integer.valueOf(10));
		doIntegerTest("-10", Integer.valueOf(-10));
	
public voidtestMBeanCreateDelete()

		final String	name	= "test:name=testtemp";
		final String	className	= "com.sun.cli.jmx.test.CLISupportSimpleTestee";
		
		mProxy.mbeanCreate( name, className, null );
		
		ObjectName []	results		= mProxy.mbeanFind( new String [] { name } );
		assertEquals( 1, results.length );
		
		mProxy.mbeanUnregister( name );
		results		= mProxy.mbeanFind( new String [] { name } );
		assertEquals( 0, results.length );
		
		
	
public voidtestMBeanFind()

		final ObjectName []	results		= mProxy.mbeanFind( ALL_TARGETS );
		
		assertEquals( 3, results.length );
	
public voidtestMBeanGet()

		final ResultsForGetSet []	results	= mProxy.mbeanGet( "*", SIMPLE_TESTEE_TARGET );
		
		assertEquals( 1, results.length );
		assertEquals( 2, results[ 0 ].getAttributes().size() );
	
public voidtestMBeanInspect()

		final InspectRequest	request	= new InspectRequest();
		
		final InspectResult []	results	= mProxy.mbeanInspect( request, ALL_TARGETS );
		
		assertEquals( 3, results.length );
	
public voidtestMBeanSet()

		final ResultsForGetSet []	results	= mProxy.mbeanSet( "NotifMillis=1000", SIMPLE_TESTEE_TARGET );
		
		assertEquals( 1, results.length );
		assertEquals( 1, results[ 0 ].getAttributes().size() );
		
		final AttributeList	attrs	= mProxy.mbeanGet( "NotifMillis", SIMPLE_TESTEE_TARGET )[ 0 ].getAttributes();
		final Attribute expected	= new Attribute( "NotifMillis", Long.valueOf(1000));
		assertEquals( expected, attrs.get( 0 ) );
	
public voidtestMixedArrayWithCasts()

		doObjectArrayTest("{(Character)c,(Boolean)true,(Byte)99,(Short)-999,(Integer)0,(Long)99999,(BigInteger)99999999999999999999999999,(BigDecimal)123456789123456789.123456789123456789,\"hello\",(String)hello,hello,}",
                                   new Object[]{Character.valueOf('c"),
                                           Boolean.valueOf("true"),
                                           Byte.valueOf((byte) 99),
                                           Short.valueOf((short) -999),
                                           Integer.valueOf(0),
                                           Long.valueOf(99999),
                                           new BigInteger("99999999999999999999999999"),
                                           new BigDecimal("123456789123456789.123456789123456789"),
                                           "hello", "hello", "hello", ""});
	
public voidtestNamedInvoke()

		invokeSupportTestee( "testNamed", "p1=hello" );
		
		invokeSupportTestee( "testNamed", "p1=hello,p2=there" );
		
		invokeSupportTestee( "testNamed", "p1=hello,p2=there,p3=!!!" );
		
		invokeSupportTestee( "testNamed", "p1=hello,p2=there,p3=!!!,p4=foobar" );
	
public voidtestNullIllegalForSimpleType()

		try
		{
			final InvokeResult	result	= invokeSimpleTestee( "test_int", "null" );
			if ( result.getResultType() == InvokeResult.SUCCESS )
			{
				fail( "expected failure trying to pass 'null' for an int" );
			}
		}
		catch( Exception e )
		{
		}
	
public voidtestNullIntegerWithCast()

		doIntegerTest( "(Integer)null", null);
	
public voidtestNullIntegerWithoutCast()

		doIntegerTest( "null", null);
	
public voidtestNullObjectWithCast()

		doObjectTest( "(Object)null", null);
	
public voidtestNullObjectWithoutCast()

		doObjectTest( "null", null);
	
public voidtestNullStringWithCast()

		doStringTest( "(String)null", null);
	
public voidtestNullStringWithoutCast()

		doStringTest( "null", null);
	
public voidtestNumericStringNoCast()

		doStringTest( "10", "10");
	
private InvokeResult.ResultTypetestOperationGenerically(boolean namedArgs, javax.management.ObjectName targetName, javax.management.MBeanOperationInfo operationInfo)

		final MBeanParameterInfo []	paramInfos	= operationInfo.getSignature();
		final int					numParams	= paramInfos.length;
		
		final String []	strings	= new String [ numParams ];
		final String	operationName	= operationInfo.getName();
		
		// create an object of the correct type for each parameter.
		// The actual value is not important.
		for( int i = 0; i < numParams; ++i )
		{
			final MBeanParameterInfo	paramInfo	= paramInfos[ i ];
			final String				paramType	= paramInfos[ i ].getType();
			final Class					theClass	= ClassUtil.getClassFromName( paramType );
			
			final Object paramObject	= ClassUtil.InstantiateDefault( theClass );
			final String paramString	= SmartStringifier.toString( paramObject );
			final String castString		= "(" + getCastType( paramType ) + ")";
			
			final String paramName		= namedArgs ? (paramInfo.getName() + '=") : "";
			
			strings[ i ]	= paramName + castString + paramString;
		}
		
		// convert the arguments to strings
		final String	argString	= makeArgList( strings );
		
		final String []	args	= new String [] { targetName.toString() };
		
		final InvokeResult []	results	= (InvokeResult [])mProxy.mbeanInvoke( operationName, argString, args );
		final InvokeResult	result	= results[ 0 ];
		
		if ( result.getResultType() == InvokeResult.SUCCESS )
		{
			// p( "SUCCESS: " + operationName + "(" + SmartStringifier.toString( paramInfos ) + ")");
		}
		else
		{
			final String paramInfosString	= SmartStringifier.toString( paramInfos );
			
			result.mThrowable.printStackTrace();
		}
		
		return( result.getResultType() );
	
public voidtestPropertiesOnly()

		final String	operationName	= "testPropertiesOnly";
		final String	argString	= "foo=bar";
		final InvokeResult []	results	= (InvokeResult [])
			mProxy.mbeanInvoke( operationName, argString, SUPPORT_TESTEE_TARGET );
			
		final InvokeResult	result	= results[ 0 ];
		
		if ( result.getResultType() != InvokeResult.SUCCESS )
		{
			throw new TestFailedException( "invocation failed: " + operationName + "(" + argString + ")" );
		}
		
	
public voidtestStringAsObject()

		doObjectTest( "(Object)hello", "hello");
	
public voidtestStringContaining_word_null()

		doStringTest( "\"null\"", "null");
	
public voidtestURI()

		final URI	input	= new URI( "service:jmx:jmxmp://localhost:" );
		final InvokeResult	result	= invokeSimpleTestee( "testURI", input.toString() );
		assertEquals( input, result.mResult );
	
public voidtestURL()

		final URL	input	= new URL( "http://www.sun.com?foo=bar&bar=foo" );
		final InvokeResult	result	= invokeSimpleTestee( "testURL", input.toString() );
		assertEquals( input, result.mResult );
	
private voidverifySetup(com.sun.cli.jmx.support.CLISupportMBeanProxy proxy)

		// must be at least one MBean
		final ObjectName []	all	= proxy.resolveTargets( ALL_TARGETS );
		assert( all.length != 0 );
		
		// verify that the AliasMgr and CLI are available.
		final String []	aliases	= proxy.listAliases( false );
		assert( aliases.length != 0 );
		
		// verify that required aliases are in place
		assert( proxy.resolveAlias( ALL_ALIAS ) != null );
		assert( proxy.resolveAlias( SIMPLE_TESTEE_ALIAS ) != null );
		assert( proxy.resolveAlias( SUPPORT_TESTEE_ALIAS ) != null );