FileDocCategorySizeDatePackage
JMXTestBase.javaAPI DocGlassfish v2 API7960Fri May 04 22:24:06 BST 2007com.sun.enterprise.management.util.jmx

JMXTestBase

public class JMXTestBase extends TestCase
Base class for AMX unit tests.

Fields Summary
private static MBeanServerConnection
sConn
private static Map
sEnv
protected final String
NEWLINE
Constructors Summary
public JMXTestBase()

        super( "JMXTestBase" );
        
        NEWLINE = StringUtil.NEWLINE();
        
        checkAssertsOn();
	
public JMXTestBase(String name)

 		super( name );
        NEWLINE = StringUtil.NEWLINE();
		checkAssertsOn();
	
Methods Summary
protected voidcheckAssertsOn()

		try
		{
			assert( false );
			throw new Error( "Assertions must be enabled for unit tests" );
		}
		catch( AssertionError a )
		{
		}
	
protected voidfailure(java.lang.String msg)

		trace( "\nFAILURE: " + msg + "\n" );
		assert( false ) : msg;
		throw new Error( msg );
	
public static java.lang.BooleangetEnvBoolean(java.lang.String key, java.lang.Boolean defaultValue)

		Boolean	result	= defaultValue;
		final String	s	= getEnvString( key, null );
		if ( s != null )
		{
			result	= Boolean.valueOf( s );
		}

		return( result  );
	
public static java.lang.IntegergetEnvInteger(java.lang.String key, java.lang.Integer defaultValue)

		final String	s	= getEnvString( key, null);
		Integer	result	= defaultValue;
		if ( s != null )
		{
			result	= new Integer( s.trim() );
		}

		return( result );
	
public static java.lang.StringgetEnvString(java.lang.String key, java.lang.String defaultValue)

		final String	s	= (String)getEnvValue( key );

		return( s == null ? defaultValue : s  );
	
public static synchronized java.lang.ObjectgetEnvValue(java.lang.String key)

		return( sEnv == null ? null : sEnv.get( key ) );
	
public static javax.management.MBeanServerConnectiongetGlobalConnection()

		return( getMBeanServerConnection() );
	
public static javax.management.MBeanServerConnectiongetMBeanServerConnection()

		return( sConn );
	
protected booleangetVerbose()

		final String	value	= (String)getEnvValue( PropertyKeys.VERBOSE_KEY );
		
		return( value != null && Boolean.valueOf( value ).booleanValue() );
	
private static synchronized voidinitEnv()

		if ( sEnv == null )
		{
			sEnv	= new HashMap<String,Object>();
		}
	
protected TnewProxy(javax.management.ObjectName target, java.lang.Class interfaceClass)

        try
        {
	        assert getMBeanServerConnection().isRegistered( target );
	    }
	    catch( java.io.IOException e )
	    {
	        throw new RuntimeException( e );
	    }
	    
    	return interfaceClass.cast( MBeanServerInvocationHandler.newProxyInstance(
    				getMBeanServerConnection(), target, interfaceClass, true ) );
    
protected longnow()

		return( System.currentTimeMillis() );
	
protected final voidprintElapsed(java.lang.String msg, long start)

		printVerbose( msg + ": " + (now() - start) + "ms" );
	
protected final voidprintElapsed(java.lang.String msg, int numItems, long start)

		printVerbose( msg + ", " + numItems + " MBeans: " + (now() - start) + "ms" );
	
protected final voidprintElapsedIter(java.lang.String msg, long start, long iterations)

		printVerbose( msg + "(" + iterations + " iterations): " + (now() - start) + "ms" );
	
protected voidprintVerbose(java.lang.Object o)

		if ( getVerbose() )
		{
			trace( o );
		}
	
protected voidprintln(java.lang.Object o)

		System.out.println( SmartStringifier.toString( o ) );
	
protected final java.lang.Stringquote(java.lang.Object o)

		return( StringUtil.quote( SmartStringifier.toString( o ) ) );
	
protected voidregisterMBean(java.lang.Object mbean, java.lang.String name)

		if ( sConn instanceof MBeanServer )
		{
			((MBeanServer)sConn).registerMBean( mbean, new ObjectName( name ) );
		}
		else
		{
			throw new IllegalArgumentException( "test connection is not an MBeanServer" );
		}
	
public static synchronized voidsetEnvValue(java.lang.String key, java.lang.Object value)

		initEnv();
		sEnv.put( key, value );
	
public static synchronized voidsetEnvValues(java.util.Map m)

		initEnv();
		sEnv.putAll( m );
	
public static voidsetGlobalConnection(javax.management.MBeanServerConnection conn)

		sConn	= conn;
	
public voidsetUp()

	    checkAssertsOn();
		assert( sConn != null );
	
public voidtearDown()

		// do NOT destroy the MBeanServer or its contents
	
protected java.lang.StringtoString(javax.management.ObjectName objectName)

	    return JMXUtil.toString( objectName );
	
protected java.lang.StringtoString(java.lang.Object o)

		String	result	= null;
		
		if ( o instanceof Collection )
		{
			result	= CollectionUtil.toString( (Collection)o, "\n" );
		}
		else
		{
			result	= SmartStringifier.toString( o );
		}
		
		return( result );
	
protected static voidtrace(java.lang.Object o)

		System.out.println( SmartStringifier.toString( o ) );
	
protected voidwarning(java.lang.String msg)

		trace( "\nWARNING: " + msg + "\n" );