FileDocCategorySizeDatePackage
GenericTest.javaAPI DocGlassfish v2 API20114Fri May 04 22:23:54 BST 2007com.sun.enterprise.management.base

GenericTest

public final class GenericTest extends com.sun.enterprise.management.AMXTestBase

Fields Summary
final boolean
mDoInfo
final boolean
mDoAttributes
final boolean
mDoOperations
final boolean
mwarnings
private static final String
SECTION_LINE
Constructors Summary
public GenericTest()

		mDoInfo				= true;
		mDoAttributes		= true;
		mDoOperations		= true;
		mwarnings		= true;
	
Methods Summary
voidcheckGetterResult(javax.management.MBeanServerConnection conn, javax.management.ObjectName callee, javax.management.MBeanOperationInfo operationInfo, java.lang.Object resultOfCall)

		if ( resultOfCall instanceof ObjectName )
		{
			final ObjectName	name	= (ObjectName)resultOfCall;
			
			checkObjectNameReturnValue( conn, callee, operationInfo, name );
		}
		else if ( resultOfCall instanceof ObjectName[] )
		{
			final ObjectName[]	names	= (ObjectName[])resultOfCall;
			
			for( int i = 0; i < names.length; ++i )
			{
				checkObjectNameReturnValue( conn, callee, operationInfo, names[ i ]);
			}
		}
	
voidcheckObjectNameReturnValue(javax.management.MBeanServerConnection conn, javax.management.ObjectName callee, javax.management.MBeanOperationInfo operationInfo, javax.management.ObjectName resultOfCall)

		try
		{
			printVerbose( "checking MBean info for: " + resultOfCall );
			final MBeanInfo	mbeanInfo	= conn.getMBeanInfo( resultOfCall );
		}
		catch( InstanceNotFoundException e )
		{
			trace( "WARNING: MBean " + resultOfCall + " returned from " +
				operationInfo.getReturnType() + " " + operationInfo.getName() + "() does not exist" );
				
		}
		catch( Exception e )
		{
			trace( "WARNING: MBean " + resultOfCall + " returned from " +
				operationInfo.getReturnType() + " " + operationInfo.getName() +
				"() can't supply MBeanInfo: " + getExceptionMsg( e )
				);
			
			if ( e instanceof IOException )
			{
				throw (IOException)e;
			}
		}
	
private voiddisplayAttributeFailuresOrWarnings(boolean failure, javax.management.ObjectName objectName, javax.management.MBeanAttributeInfo[] infos, java.util.Map problems)

		trace( "" );
		trace( problems.keySet().size() + (failure ? " Failures: " : " Warnings: ") + objectName  );
		
		int	i = 0;
		for( final String attrName : problems.keySet() )
		{
			final Throwable	t = problems.get( attrName );
			
			final MBeanAttributeInfo	info	= findAttributeInfo( infos, attrName );
			
			final String prefix	= "(" + (i+1) + ")" + " getting Attribute \"" + attrName + "\" of type " +
				info.getType() + " ";
				
			if ( t == null )
			{
				trace( prefix + "returned null" );
			}
			else
			{
				trace( prefix + getExceptionMsg( t ) );
			}
			++i;
		}
	
javax.management.MBeanAttributeInfofindAttributeInfo(javax.management.MBeanAttributeInfo[] infos, java.lang.String attrName)

		MBeanAttributeInfo	info	= null;
		
		for( int i = 0; i < infos.length; ++i )
		{
			if ( infos[ i ] != null && infos[ i ].getName().equals( attrName ) )
			{
				info	= infos[ i ];
				break;
			}
		}
		
		assert( info != null );
		return( info );
	
public static com.sun.enterprise.management.CapabilitiesgetCapabilities()

	    return getOfflineCapableCapabilities( true );
	
private java.lang.StringgetExceptionMsg(java.lang.Throwable e)

		String	msg	= null;
		
		if ( e instanceof IOException )
		{
			msg	= "received an exception of class " + e.getClass().getName();
			
			if ( shouldPrintStackTraces() )
			{
				msg	= msg + "Stack trace = \n" + 
				ExceptionUtil.getStackTrace( ExceptionUtil.getRootCause( e ) );
			}
		}
		else
		{
			msg	= "threw an Exception of type " +
				e.getClass().getName() + ", message =  " + e.getMessage();
			
			if ( shouldPrintStackTraces() )
			{
				msg	= msg + "\n" + ExceptionUtil.getStackTrace( e );
			}
		}
		
		final Throwable rootCause	= ExceptionUtil.getRootCause( e );
		
		if ( rootCause != e )
		{
			msg	= msg + "...\nRoot cause was exception of type " + e.getClass().getName() + ", message = " +
				rootCause.getMessage();
				
			
			if ( shouldPrintStackTraces() )
			{
				msg	= msg + "\n" + ExceptionUtil.getStackTrace( rootCause );
			}
		}
		
		return( msg );
	
private voidprintDuplicateAttributes(javax.management.ObjectName objectName, javax.management.MBeanAttributeInfo[] attrInfos, java.lang.String name)

	
	
	
		 
	       
	
		String	msg	= "MBean " + quote( objectName ) + " has the same Attribute listed more than once:\n";
		
		for( int i = 0; i < attrInfos.length; ++i )
		{
			final MBeanAttributeInfo	a	= attrInfos[ i ];
				
			if ( a.getName().equals( name ) )
			{
				msg	= msg + name + ": " + a.getType() + ", " + quote( a.getDescription() );
			}
		}
		
		warning( msg );
	
booleanshouldPrintStackTraces()

		return( true );
	
public voidtestGenerically()

		final Set<ObjectName>	all	= getTestUtil().getAllObjectNames();
		
		final ObjectName[]	allObjectNames	= new ObjectName[ all.size() ];
		all.toArray( allObjectNames );
		validate( allObjectNames );
	
private booleanvalidate(javax.management.ObjectName objectName)

		boolean	valid	= true;

		MBeanServerConnection conn	= getConnection();
		
		MBeanInfo	info	= null;
		try
		{
			info	= conn.getMBeanInfo( objectName );
		}
		catch( Exception e )
		{
			valid	= false;
			warning( " during getMBeanInfo() for: " + objectName + "\n" +
				" message = " + e.getMessage() );
			// abort--the connection has died
			throw e;
		}
		
		if ( mDoInfo && ! validateMBeanInfo( objectName, info ) )
		{
			trace( "validateMBeanInfo failed for: " + objectName );
			valid	= false;
		}
		
		
		if ( mDoAttributes &&
			! validateAttributes( objectName, info.getAttributes() ) )
		{
			trace( "validateAttributes failed for: " + objectName );
			valid	= false;
		}
		
		if ( mDoOperations &&
			! validateGetters( objectName, info.getOperations() ) )
		{
			trace( "validateGetters failed for: " + objectName );
			valid	= false;
		}
		
		return( valid );
	
private voidvalidate(javax.management.ObjectName[] objectNames)

		int	failureCount	= 0;
		
		trace( "Validating: " );
		if ( mDoInfo )
		{
			trace( "- MBeanInfo" );
		}
		if ( mDoAttributes )
		{
			trace( "- Attributes" );
		}
		if ( mDoOperations )
		{
			trace( "- Operations (getters)" );
		}
		
		trace( "" );
		
		for( int i = 0; i < objectNames.length; ++i )
		{
			final ObjectName	objectName	= objectNames[ i ];
			
			printVerbose( "Validating: " + objectName );
			
			if ( ! shouldTest( objectName ) )
			{
				notTested( objectName );
				continue;
			}
			
			final boolean	valid	= validate( objectName );
			if ( ! valid )
			{
				++failureCount;
			}
		}
		
		trace( "Total mbeans failing: " + failureCount );
	
private booleanvalidateAttributeTypes(javax.management.ObjectName objectName, javax.management.AttributeList attrs, javax.management.MBeanAttributeInfo[] attrInfos)

		boolean	valid	= true;
		
		final Map<String,MBeanAttributeInfo>	attrInfosMap	= JMXUtil.attributeInfosToMap( attrInfos );
		
		final Iterator	iter	= attrs.iterator();
		while ( iter.hasNext() )
		{
			final Attribute	attr	= (Attribute)iter.next();
			
			final String	name	= attr.getName();
			final Object	value	= attr.getValue();
			final MBeanAttributeInfo	attrInfo = (MBeanAttributeInfo)attrInfosMap.get( name );
			if ( attrInfo == null )
			{
				valid	= false;
				warning( "MBean " + objectName + " returned an Attribute not " +
					"declared in its MBeanInfo: " + name );
			}
			else if ( value != null )
			{
				final String	typeName	= attrInfo.getType();
				final Class<?>		infoClass	= ClassUtil.getClassFromName( typeName );
				final Class<?>		valueClass	= value.getClass();
				
				if ( infoClass == null )
				{
					valid	= false;
					warning( "Can't find class for: " + typeName );
				}
				else if ( ! infoClass.isAssignableFrom( valueClass ) )
				{
					final Class<?> objectClass	= ClassUtil.PrimitiveClassToObjectClass( infoClass );
					
					if ( ! objectClass.isAssignableFrom( valueClass ) )
					{
						valid	= false;
						warning( "MBean " + objectName + " returned Attribute " +
							name + "=" + value +
							" of class " + value.getClass().getName() +
							" not matching its MBeanInfo: " + infoClass.getName() );
					}
				}
			}
		}
		
		return( valid );
	
private booleanvalidateAttributes(javax.management.ObjectName objectName, javax.management.MBeanAttributeInfo[] attrInfos)

		boolean	valid	= true;
		
		final MBeanAttributeInfo[]	readableInfos	= JMXUtil.filterAttributeInfos( attrInfos,
				ReadWriteAttributeFilter.READABLE_FILTER );
		final String[]	attrNames	= JMXUtil.getAttributeNames( readableInfos );
		Arrays.sort( attrNames  );
		
		if ( attrNames.length != 0 )
		{
			// if we can fetch all the attributes, then the MBean is OK;
			// try this first for efficiency
			try
			{
                //trace( objectName.getKeyProperty( "j2eeType" ) + ": " + attrNames.length );
				final AttributeList	attrs	= getConnection().getAttributes( objectName, attrNames );
				
				if ( attrs == null )
				{
					warning( "MBean " + quote( objectName ) + " returned NULL for its AttributeList" );
					valid	= false;
				}
				else if ( attrs.size() != readableInfos.length )
				{
					// mismatch between claimed number of attributes and actual
					final ArrayStringifier	as	= new ArrayStringifier( ", ", true );
					final String claimedString	= as.stringify( attrNames );
					
					final Set<String>	actualSet	= JMXUtil.attributeListToValueMap( attrs ).keySet();
					final Set<String>	missingSet	= ArrayConversion.arrayToSet( attrNames );
					missingSet.removeAll( actualSet );
					
					final String[]	missingNames	= (String[])ArrayConversion.setToArray( missingSet, true );
					Arrays.sort( missingNames  );
					final String	missingString	= as.stringify( missingNames );
					
					warning( "MBean " + quote( objectName ) +
						" did not supply the " +
						missingNames.length + " attributes " + missingString );
				}
				
				valid	= validateAttributeTypes( objectName, attrs, readableInfos  );
			}
			catch( Exception e )
			{
				trace( SECTION_LINE );
				final String	msg	= "getAttributes() failed on " + quote( objectName ) + ", exception =\n" + e;
				
				if ( e instanceof NotSerializableException )
				{
					warning( msg );
				}
				else
				{
					warning( msg );
					valid	= false;
				}
				// do them one-at-a time to see where failure occurs
				final Map<String,Throwable>	failures	= new HashMap<String,Throwable>();
				final Map<String,Throwable>	warnings	= new HashMap<String,Throwable>();
				
				validateAttributesSingly( objectName, attrNames, failures, warnings );
				
				trace( "Validating attributes one-at-a-time using getAttribute() for " + quote( objectName ));
				if ( failures.size() == 0 && warnings.size() == 0 )
				{
					warning( " during getAttributes(" +
						ArrayStringifier.stringify( attrNames, "," ) + ") for: " + objectName +
						" (but Attributes work when queried one-at-a-time).\nIt " +
						getExceptionMsg( e ) );
				}
				
				if ( failures.size() != 0 )
				{
					displayAttributeFailuresOrWarnings( true, objectName, readableInfos, failures );
				}
				
				if ( warnings.size() != 0 )
				{
					displayAttributeFailuresOrWarnings( false, objectName, readableInfos, warnings );
				}
				
				trace( SECTION_LINE );
			}
		}
		else
		{
			valid	= true;
		}
		
		if ( ! validateUniqueAttributeNames( objectName, attrInfos ) )
		{
			valid	= false;
		}
		
		if ( ! validateMissingAndEmptyAttributeNames( objectName ) )
		{
			valid	= false;
		}
		
		return( valid );
	
private java.util.MapvalidateAttributesSingly(javax.management.ObjectName objectName, java.lang.String[] attrNames, java.util.Map failures, java.util.Map warnings)

		MBeanServerConnection conn	= getConnection();
			
		for( int i = 0; i < attrNames.length; ++i )
		{
			final String	attrName	= attrNames[ i ];
			
			try
			{
				final Object	a	= conn.getAttribute( objectName, attrName );
				
				if ( a == null )
				{
					// null is legal, apparently
				}
			}
			catch( NotSerializableException e )
			{
				warnings.put( attrName, e );
			}
			catch( IOException e )
			{
				failures.put( attrName, e );
			}
			catch( Exception e )
			{
				failures.put( attrName, e );
			}
		}
		
		return( failures );
	
private booleanvalidateGetters(javax.management.ObjectName objectName, javax.management.MBeanOperationInfo[] operationInfos)

		boolean	valid	= true;
		MBeanServerConnection conn	= getConnection();
		
		for( int i = 0; i < operationInfos.length; ++i )
		{
			final MBeanOperationInfo	info	= operationInfos[ i ];
			
			if ( JMXUtil.isGetter( info ) )
			{
				boolean	opValid	= false;
					
				try
				{
					printVerbose( "invoking getter: " + info.getName() + "()" );
					final Object	result	= conn.invoke( objectName, info.getName(), null, null );
					
					checkGetterResult( conn, 
					objectName, info, result );
				}
				catch( Exception e )
				{
					warning( "Failure: calling " + info.getName() + "() on " + objectName +
						": " + getExceptionMsg( e ) );
						
					if ( e instanceof IOException)
					{
						throw( (IOException)e );
					}
					valid	= false;
				}
			}
		}
		
		return( valid );
	
private booleanvalidateMBeanInfo(javax.management.ObjectName objectName, javax.management.MBeanInfo info)

		boolean	valid	= true;
		
		if ( ArrayUtil.arrayContainsNulls( info.getAttributes() ) )
		{
			warning( "MBean has nulls in its MBeanAttributeInfo[]: " + objectName );
			valid	= false;
		}
		
		if ( ArrayUtil.arrayContainsNulls( info.getConstructors() ) )
		{
			warning( "MBean has nulls in its MBeanConstructorInfo[]: " + objectName );
			valid	= false;
		}
		
		if ( ArrayUtil.arrayContainsNulls( info.getOperations() ) )
		{
			warning( "MBean has nulls in its MBeanOperationInfo[]: " + objectName );
			valid	= false;
		}
		
		if ( ArrayUtil.arrayContainsNulls( info.getNotifications() ) )
		{
			warning( "MBean has nulls in its MBeanNotificationInfo[]: " + objectName );
			valid	= false;
		}
		
		return( valid );
	
private booleanvalidateMissingAndEmptyAttributeNames(javax.management.ObjectName objectName)

		boolean						valid	= true;
		final MBeanServerConnection	conn	= getConnection();
		
		AttributeList	attrs	= null;
		try
		{
			attrs	= conn.getAttributes( objectName, new String[0] );
			if ( attrs == null )
			{
				warning( "MBean " + quote( objectName ) +
					" returned NULL for an empty AttributeList" );
				valid	= false;
			}
			else if ( attrs.size() != 0 )
			{
				warning( "MBean " + quote( objectName ) +
					" returned attributes for an empty AttributeList" );
				valid	= false;
			}
		}
		catch( Exception e )
		{
			valid	= false;
			
			warning( "MBean " + quote( objectName ) +
				" threw an exception getting an empty attribute list" );
		}
		
		try
		{
			final String	notFoundName	= "bogus." + System.currentTimeMillis();
			attrs	= conn.getAttributes( objectName, new String[] { notFoundName });
			if ( attrs == null )
			{
				warning( "MBean " + quote( objectName ) +
					" returned NULL for a missing Attribute" );
				valid	= false;
			}
			else if ( attrs.size() != 0 )
			{
				warning( "MBean " + quote( objectName ) +
					" returned attributes for a non-existent name" );
				valid	= false;
			}
		}
		catch( Exception e )
		{
			valid	= false;
			
			warning( "MBean " + quote( objectName ) +
				" threw an exception when getAttributes() was called with a " +
				"non-existent Attribute, exception class = " +
				e.getClass().getName() );
		}
		
		return( valid );
	
private booleanvalidateUniqueAttributeNames(javax.management.ObjectName objectName, javax.management.MBeanAttributeInfo[] attrInfos)

		boolean	valid	= true;
		final MBeanAttributeInfo[]	infos	=
			JMXUtil.filterAttributeInfos( attrInfos, ReadWriteAttributeFilter.READABLE_FILTER );
		final String[]	names	= JMXUtil.getAttributeNames( infos );
		
		if ( ArrayConversion.arrayToSet( names ).size() != attrInfos.length )
		{
			final Set<String>	set		= new HashSet<String>();
			
			for( int i = 0; i < names.length; ++i )
			{
				final String	name	= names[ i ];
			
				if ( set.contains( name ) )
				{
					valid	= false;
					
					printDuplicateAttributes( objectName, attrInfos, name );
				}
				else
				{
					set.add( name );
				}
			}
			set.clear();
		}
		
		return( valid );