FileDocCategorySizeDatePackage
TestMain.javaAPI DocGlassfish v2 API28161Fri May 04 22:23:50 BST 2007com.sun.enterprise.management

TestMain

public final class TestMain extends Object implements NotificationListener
Main class that runs all the unit tests

Fields Summary
private final com.sun.appserv.management.DomainRoot
mDomainRoot
private com.sun.appserv.management.client.HandshakeCompletedListenerImpl
mHandshakeCompletedListener
private static final String
RMI_PROTOCOL_IN_CONFIG
Constructors Summary
public TestMain(String optionalPropertiesFile, Map cmdLineParams)

	    AMXDebug.getInstance().setAll( true );
	    
	    checkAssertsOn();
	    
		final Map<String,String>    props	= getProperties( optionalPropertiesFile );
		
		final Map<String,String>    envIn = new HashMap<String,String>( props );
		envIn.putAll( cmdLineParams );
		warnUnknownProperties( envIn );
		
		final Map<String,Object>    env = new HashMap<String,Object>();
		env.putAll( envIn );
		
		println( "" );
		println( "ENVIRONMENT:\n" + MapUtil.toString( env, "\n" ) );
		println( "" );
		
		final PropertyGetter	getter	= new PropertyGetter( env );
		
    	ConnectionSource	conn    = null;
    		
		final boolean	testOffline	= getter.getboolean( TEST_OFFLINE_KEY );
	    if ( testOffline )
	    {
	        final String    domainXML   = getter.getString( DOMAIN_XML_KEY );
	        mDomainRoot = initOffline( new File( domainXML ) );
	        
	        final MBeanServer   server  = (MBeanServer)
	            Util.getExtra( mDomainRoot ).getConnectionSource().getExistingMBeanServerConnection();
	    
    	    final Set<ObjectName>    mbeans =
    	        JMXUtil.queryNames( server, Util.newObjectName( "*:*" ), null );
    	    //println( "\n\n------------------------------------------" );
    	    //println( "MBeans registered:" );
    	    //println( CollectionUtil.toString( mbeans, "\n" ) );
    	    //println( "\n\n" );
	            
	        conn    = new MBeanServerConnectionSource( server );
	    }
	    else
	    {
    		if ( getter.getboolean( CONNECT_KEY ) )
    		{
    		    final AppserverConnectionSource acs  	= getConnectionSource( getter, true );
    		
    		    if ( acs == null )
    		    {
    		        throw new IOException( "Can't connect to server" );
    		    }

    		    mDomainRoot	= acs.getDomainRoot();
    		    
    		    conn    = acs;
    		}
    		else
    		{
    	        mDomainRoot = null;
    	        conn       = null;
    	    }
	    }
		
		if ( mDomainRoot != null )
		{
		    Observer.create( mDomainRoot );
		}
		
		final boolean	expandedTesting	= testOffline ?
		    false :getter.getboolean( EXPANDED_TESTING_KEY );
		
		if ( mDomainRoot != null && expandedTesting )
		{
		    final Map<String,AppserverConnectionSource> connections =
		        getNodeAgentConnections( mDomainRoot, getter );
		    
		    env.put( NODE_AGENTS_KEY, connections );
		}
		
		
		final boolean	threaded	= getter.getboolean( RUN_THREADED_KEY );
		
		if ( getter.getboolean( VERBOSE_KEY ) )
		{
			println( "VERBOSE mode enabled" );
			if ( threaded )
			{
				println( "NOTE: timings displayed when running " +
				    "threaded tests will be impacted by other concurrent tests." );
			}
		}
		
		final List<Class<junit.framework.TestCase>> specifiedClasses	= 
		    getTestClasses( getter.getFile( TEST_CLASSES_FILE_KEY ) );
		
		final List<Class<junit.framework.TestCase>> testClasses   =
		    filterTestClasses( mDomainRoot, getter, specifiedClasses );
		
		final int iterations	= getter.getInteger( ITERATIONS_KEY ).intValue();
		iterateTests(
		    testClasses,
		    iterations,
		    conn,
		    threaded,
		    Collections.unmodifiableMap( env ) );
		
		
		println( "" );
		println( "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" );
		println( ">>>> Please inspect amxtest.coverage <<<<" );
		println( "                   ^                     " );
		println( "                   ^                     " );
		println( "                   ^                     " );
		println( "                   ^                     " );
	
Methods Summary
private com.sun.appserv.management.client.AppserverConnectionSource_getConnectionSource(com.sun.enterprise.management.TestMain$PropertyGetter getter, java.lang.String host, int port)

		final String	user	= getter.getString( USER_KEY );
		final String	password	= getter.getString( PASSWORD_KEY );
		final File		trustStore	= getter.getFile( TRUSTSTORE_KEY);
		final String	trustStorePassword	= getter.getString( TRUSTSTORE_PASSWORD_KEY);
		final boolean	useTLS	=  getter.getboolean( USE_TLS_KEY );
		
		final TLSParams	tlsParams	= useTLS ?
			createTLSParams( trustStore, trustStorePassword) : null;
		
		AppserverConnectionSource	conn = null;
		
		try
		{
    		conn 	= connect( host, port, user, password, tlsParams );
    		if ( mHandshakeCompletedListener != null )
    		{
    			assert( mHandshakeCompletedListener.getLastEvent() != null );
    			println( "HandshakeCompletedEvent: " +
    				toString( mHandshakeCompletedListener.getLastEvent() ) );
    		}
		}
		catch( IOException e )
		{
		    if ( useTLS )
		    {
		        // try without TLS
		        println( "Attempting connection without TLS..." );
    		    conn 	= connect( host, port, user, password, null );
		    }
		}
		
		if ( conn != null )
		{
		    conn.getJMXConnector( false ).addConnectionNotificationListener( this, null, conn );
		}
		
		return( conn );
	
private com.sun.appserv.management.client.AppserverConnectionSource_getConnectionSource(com.sun.enterprise.management.TestMain$PropertyGetter getter)

		final String	host	= getter.getString( HOST_KEY );
		final int		port	= getter.getint( PORT_KEY );
		
		return _getConnectionSource( getter, host, port );
	
private static java.util.MapargsToMap(java.lang.String[] args)

		final Map<String,String>   params  = new HashMap<String,String>();
		
		params.put( DEFAULT_PROPERTIES_FILE, args[ 0 ] );
		
	    for( int i = 1; i < args.length; ++i )
	    {
	        final String    pair= args[ i ];
	        final int delimIndex  = pair.indexOf( "=" );
	        String    name  = null;
	        String    value  = null;
	        if ( delimIndex < 0 )
	        {
	            name    = pair;
	            value   = null;
	        }
	        else
	        {
	            name    = pair.substring( 0, delimIndex);
	            value   = pair.substring( name.length() + 1, pair.length() );
	            
	        }
	        params.put( name, value );
	    }
	    
	    return params;
	
protected static voidcheckAssertsOn()

		try
		{
			assert( false );
			throw new Error( "Assertions must be enabled for unit tests" );
		}
		catch( AssertionError a )
		{
		}
	
private java.lang.String[]classesToStrings(java.util.Set classes)

		final String[]  names   = new String[ classes.size() ];
		
		int i = 0;
		for ( final Class<?> c : classes )
		{
		    names[ i ] = c.getName();
		    ++i;
		}
		return names;
	
public static com.sun.appserv.management.client.AppserverConnectionSourceconnect(java.lang.String host, int port, java.lang.String user, java.lang.String password, com.sun.appserv.management.client.TLSParams tlsParams)

param
host hostname or IP address of Domain Admin Server
param
port RMI administrative port
param
user admin user
param
password admin user password
param
tlsParams TLS parameters, may be null
return
AppserverConnectionSource

		final String info = "host=" + host + ", port=" + port +
			", user=" + user + ", password=" + password +
			", tls=" + (tlsParams != null);
			
		println( "Connecting: " + info  + "...");
		
		final AppserverConnectionSource conn	=
			new AppserverConnectionSource( AppserverConnectionSource.PROTOCOL_RMI,
				host, port, user, password, tlsParams, null);

		conn.getJMXConnector( false );
		//println( "Connected: " + info );
		
		return( conn );
	
private com.sun.appserv.management.client.TLSParamscreateTLSParams(java.io.File trustStoreFile, java.lang.String password)

		final char[] trustStorePassword	= password.toCharArray();
					
		mHandshakeCompletedListener	= new HandshakeCompletedListenerImpl();
		final TestClientTrustStoreTrustManager trustMgr =
			new TestClientTrustStoreTrustManager( trustStoreFile, trustStorePassword);

		final TLSParams	tlsParams = new TLSParams( trustMgr, mHandshakeCompletedListener );

		return( tlsParams );
	
private java.util.ListfilterTestClasses(com.sun.appserv.management.DomainRoot domainRoot, com.sun.enterprise.management.TestMain$PropertyGetter getter, java.util.List classes)

		final boolean	offline	= getter.getboolean( TEST_OFFLINE_KEY );
		
		final SystemInfo    systemInfo  = domainRoot == null ? null : domainRoot.getSystemInfo();
		
		final boolean   clustersSupported   = systemInfo == null ?
		    false : systemInfo.supportsFeature( SystemInfo.CLUSTERS_FEATURE );
		    
		final boolean   multipleServersSupported   = systemInfo == null ?
		    false : systemInfo.supportsFeature( SystemInfo.MULTIPLE_SERVERS_FEATURE );
		    
		final boolean   monitorsSupported   = ! offline;
		
		final List<Class<junit.framework.TestCase>>   included   = new ArrayList<Class<junit.framework.TestCase>>();
		final List<Class<junit.framework.TestCase>>   omitted    = new ArrayList<Class<junit.framework.TestCase>>();
		for( final Class<junit.framework.TestCase> c : classes )
		{
		    boolean include   = true;
		    
		    final Capabilities  capabilities = getCapabilities( c );
		    
	        if (  (! monitorsSupported ) &&
	            AMXMonitorTestBase.class.isAssignableFrom( c ) )
	        {
	            include = false;
	        }
		    else if ( offline && ! capabilities.getOfflineCapable() )
	        {
	            include = false;
	        }
		    else if ( ClusterSupportRequired.class.isAssignableFrom( c ) &&
		        ! clustersSupported )
		    {
	            include = false;
		    }
		    else if ( MultipleServerSupportRequired.class.isAssignableFrom( c ) &&
		        ! multipleServersSupported )
		    {
	            include = false;
		    }
		    
		    if ( include )
		    {
		        included.add( c );
		    }
		    else
		    {
		        omitted.add( c );
		    }
		}
		
		return included;
    
private CapabilitiesgetCapabilities(java.lang.Class c)

	    Capabilities    capabilities    = AMXTestBase.getDefaultCapabilities();
	    
	    try
	    {
		    final Method getCapabilities	= c.getDeclaredMethod( "getCapabilities", (Class[])null );
		    
		    capabilities    = (Capabilities)getCapabilities.invoke( null, (Object[])null );
		}
		catch( Exception e )
		{
		}
		
		return capabilities;
    
private com.sun.appserv.management.client.AppserverConnectionSourcegetConnectionSource(com.sun.enterprise.management.TestMain$PropertyGetter getter, boolean retry)

		AppserverConnectionSource	conn	= null;
		
		final long PAUSE_MILLIS = 3*1000;
		
		for( int i = 0; i < 5; ++i )
		{
			try
			{
				conn	= _getConnectionSource( getter );
				break;
			}
			catch( Exception e )
			{
				final Throwable rootCause	= ExceptionUtil.getRootCause( e );
				
				if ( rootCause instanceof java.net.ConnectException )
				{
					println( "ConnectException: " + rootCause.getMessage() +
						"...retry..." );
					Thread.sleep( PAUSE_MILLIS );
					continue;
				}
				throw e;
			}
		}

		return( conn );
	
private final com.sun.appserv.management.DomainRootgetDomainRoot()

		return( mDomainRoot );
	
public java.util.MapgetNodeAgentConnections(com.sun.appserv.management.DomainRoot domainRoot, com.sun.enterprise.management.TestMain$PropertyGetter getter)

	
	     
	
	             
	          
	
	    final Map<String,NodeAgentConfig>   nodeAgentConfigs =
	        domainRoot.getDomainConfig().getNodeAgentConfigMap();
	    
	    final Map<String,AppserverConnectionSource> nodeAgentConnections =
	        new HashMap<String,AppserverConnectionSource>();
	    
	    println( "" );
	    println( "Contacting node agents..." );
	    
	    for( final NodeAgentConfig nodeAgentConfig : nodeAgentConfigs.values() )
	    {
	        final String    nodeAgentName   = nodeAgentConfig.getName();
	        
	        final JMXConnectorConfig    connConfig  = nodeAgentConfig.getJMXConnectorConfig();
	        
	        if ( ! connConfig.getEnabled() )
	        {
	            println( nodeAgentName + ": DISABLED CONNECTOR");
	            continue;
	        }
	        
	        final String    address    = connConfig.getAddress();
	        final int       port    = Integer.parseInt( connConfig.getPort() );
	        final boolean   tlsEnabled  = connConfig.getSecurityEnabled();
	        final String    protocol    = connConfig.getProtocol();
	        
	        if ( ! RMI_PROTOCOL_IN_CONFIG.equals( protocol ) )
	        {
	            println( nodeAgentName + ": UNSUPPORTED CONNECTOR PROTOCOL: " + protocol );
	            continue;
	        }
	        
	        // See if we can connect
	        try
	        {
	            final AppserverConnectionSource asConn    =
	                _getConnectionSource( getter, address, port );
	            final MBeanServerConnection conn    = asConn.getMBeanServerConnection( false );
	            final boolean   alive   =
	                conn.isRegistered( JMXUtil.getMBeanServerDelegateObjectName() );
	            assert( alive );
	            
	            nodeAgentConnections.put( nodeAgentName, asConn );
	            println( nodeAgentName + ": ALIVE" );
	        }
	        catch( Exception e )
	        {
	            println( "Node agent " + nodeAgentConfig.getName() + 
	                " could not be contacted: " + e.getClass().getName() );
	            println( nodeAgentName + ": COULD NOT BE CONTACTED" );
	            continue;
	        }
	    }
	    
	    println( "" );
	    
	    return nodeAgentConnections;
	
private final java.util.MapgetProperties(java.lang.String file)
Read connect properties from a file.

		Map<String,String>	props	= PropertyKeys.getDefaults();
		
		props.remove( TEST_CLASSES_FILE_KEY );
		
		if ( file != null )
		{
			println( "Reading properties from: " + StringUtil.quote( file ) );
			
			final String propsString	= FileUtils.fileToString( new File( file ) );
			final Properties fromFile  = new Properties();
			fromFile.load( new ByteArrayInputStream( propsString.getBytes() ) );
			
			props   = MapUtil.toStringStringMap( fromFile );
		}
		else
		{
			println( "Using default properties." );
		}
		
		return( props );
	
private java.util.ListgetTestClasses(java.io.File testsFile)

		List<Class<junit.framework.TestCase>> testClasses	= null;

		if ( testsFile == null  )
		{
			testClasses	= Tests.getTestClasses();
			println( "NO TEST FILE SPECIFIED--TESTING ALL CLASSES in " + Tests.class.getName());
		}
		else
		{
			println( "Reading test classes from: " + StringUtil.quote( testsFile.toString() ));
			
			final String	fileString	= FileUtils.fileToString( testsFile );
			final String	temp	= fileString.replaceAll( "\r\n", "\n" ).replaceAll( "\r", "\n" );
			final String[]	classnames	= temp.split( "\n" );
			
			testClasses	= new ArrayList<Class<junit.framework.TestCase>>();
			
			for( int i = 0; i < classnames.length; ++i )
			{
				final String	classname	= classnames[ i ].trim();
						
				if ( classname.length() != 0 && ! classname.startsWith( "#" ) )
				{
					try
					{
						final Class<junit.framework.TestCase> theClass	=
						    TypeCast.asClass( ClassUtil.getClassFromName( classname ) );
						
						testClasses.add( theClass );
					}
					catch( Throwable t )
					{
					    final String msg    = "Can't load test class: " + classname;
					    
						throw new Error( msg, t);
					}
				}
			}
			
			warnUntestedClasses( testClasses );
			warnDisabledTests();
		}
		
		return( testClasses );
	
public voidhandleNotification(javax.management.Notification notifIn, java.lang.Object handback)

		if ( notifIn instanceof JMXConnectionNotification )
		{
			final String type	= notifIn.getType();
			if ( type.equals( JMXConnectionNotification.FAILED ) )
			{
				System.err.println( "\n\n### JMXConnection FAILED: " + handback + "\n\n" );
			}
			else if ( type.equals( JMXConnectionNotification.CLOSED ) )
			{
				System.err.println( "\n\n### JMXConnection CLOSED: " + handback + "\n\n" );
			}
			else if ( type.equals( JMXConnectionNotification.OPENED ) )
			{
				System.err.println( "\n\n### JMXConnection OPENED: " + handback + "\n\n" );
			}
			else if ( type.equals( JMXConnectionNotification.NOTIFS_LOST ) )
			{
				System.err.println( "\n\n### JMXConnection NOTIFS_LOST: " + handback + "\n\n" + notifIn );
				Observer.getInstance().notifsLost();
			}
		}
	
private static com.sun.appserv.management.DomainRootinitOffline(java.io.File domainXML)

	    final MBeanServer   server  = MBeanServerFactory.createMBeanServer( "test" );
	    assert( domainXML.exists() && domainXML.length() != 0 );
	    
	    final OfflineConfigIniter initer    = new OfflineConfigIniter( server, domainXML );
	    final DomainRoot    domainRoot  = initer.getDomainRoot();
	    
	    return domainRoot;
	
private static booleanisHelp(java.lang.String s)

		return( s.equals( "help" ) || s.equals( "--help" ) || s.equals( "-?" ) );
	
private voiditerateTests(java.util.List testClasses, int iterations, com.sun.appserv.management.client.ConnectionSource conn, boolean threaded, java.util.Map env)

		for( int i = 0; i < iterations; ++i )
		{
			if ( iterations != 1 )
			{
			    println( "#########################################################" );
				println( "\n### ITERATION " + (i+1) );
			    println( "#########################################################" );
			}

			final long	start	= System.currentTimeMillis();
	    
			final TestRunner	runner	= new TestRunner( conn );
			runner.runAll( testClasses, threaded,  env );
			
			final long	elapsed	= System.currentTimeMillis() - start;
			println( "Time to run tests: " + (elapsed/1000) + " seconds" );
		}
	
public static voidmain(java.lang.String[] args)

	    checkAssertsOn();
		// for friendlier output via Stringifiers
		new StringifierRegistryIniter( StringifierRegistryImpl.DEFAULT );
	    
		
		if ( args.length == 0 ||
			( args.length == 1 && isHelp( args[ 0 ] ) ) )
		{
			printUsage();
			System.exit( 255 );
		}
		
		final Map<String,String>    cmdLineParams   = argsToMap( args );
		
		try
		{
			new TestMain( args.length == 0 ? null : args[ 0 ], cmdLineParams);
		}
		catch( Throwable t )
		{
			final Throwable rootCause	= ExceptionUtil.getRootCause( t );
			
			if ( rootCause instanceof java.net.ConnectException )
			{
				System.err.println( "\nERROR: The connection to the server could not be made" );
			}
			else
			{
				System.err.println( "\nERROR: exception of type: " + rootCause.getClass().getName() );
				rootCause.printStackTrace();
			}
			System.exit( -1 );
		}
	
private voidprintItems(java.lang.String[] items, java.lang.String prefix)

		for( int i = 0; i < items.length; ++i )
		{
			println( prefix + items[ i ] );
		}
	
private static voidprintUsage()

		println( "USAGE: java " + TestMain.class.getName() + " <properties-file> [name=value [name=value]*]" );
		
		final String	example	= MapUtil.toString( PropertyKeys.getDefaults(), "\n" ) +
			"\n\nAdditional properties may be included and will be placed into a Map " +
			"for use by any unit test.";
		println( "Properties file format:\n" + example );
		println( "" );
		println( "The optional property " + StringUtil.quote(TEST_CLASSES_FILE_KEY) +
			" may contain the name of a file which specifies which test classes to run. " +
			"Files should be listed with fully-qualified classnames, one per line.  " +
			"The # character may be used to comment-out classnames."
		    );
	    println( "" );
	    println( "Additional properties may also be passed directly on the command line." );
	    println( "These override any properties found in the specified properties file." );
	    println( "[all properties intended for permanent use should be defined in PropertyKeys.java]" );
	    println( "EXAMPLE:" );
	    println( "java TestMain amxtest.properties amxtest.verbose=true my-temp=true" );
	
private static voidprintln(java.lang.Object o)

		System.out.println( o );
	
public static java.lang.StringtoString(java.lang.Object o)

		return( SmartStringifier.toString( o ) );
	
private voidwarnDisabledTests()

	    final String WARNING =
"----------------------------------------\n" +
"-                                      -\n" +
"- NOTE:                                -\n" +
"- Generic tests currently disabled for -\n" +
"- AMX MBeans which reside in non-DAS   -\n" +
"- server instances eg Logging, CallFlow.-\n" +
"- Denoted by 'remoteIncomplete'        -\n" +
"-                                      -\n" +
"-                                      -\n" +
"----------------------------------------";

	    println( WARNING );
	
private voidwarnUnknownProperties(java.util.Map props)

		final Map<String,String>	known	= new HashMap<String,String>( getDefaults() );
		final Map<String,String>	unknown	= new HashMap<String,String>( props );
		
		unknown.keySet().removeAll( known.keySet() );
		if ( unknown.keySet().size() != 0 )
		{
			println( "\nNOTE: the following properties are not recognized but " +
			"will be included in the environment for use by unit tests:");
			println( MapUtil.toString( unknown, "\n" ) );
			println( "" );
		}
	
private voidwarnUntestedClasses(java.util.List actual)

		final Set<Class<junit.framework.TestCase>>	actualSet   = GSetUtil.newSet( actual );
		final Set<Class<junit.framework.TestCase>>	allSet      = GSetUtil.newSet( Tests.getTestClasses() );
		
		final Set<Class<junit.framework.TestCase>>	untested	= GSetUtil.newSet( allSet );
		untested.removeAll( actualSet );
		if ( untested.size() != 0 )
		{
			println( "\nWARNING: the following tests WILL NOT BE RUN:" );
			final String[]  names   = classesToStrings( untested );
			for( int i = 0; i < names.length; ++i )
			{
			    names[i]    = "!" + names[i] + "!";   // indicate not being run
			}

			println( ArrayStringifier.stringify( names, "\n" ) );
			println( "" );
		}
		
		final Set<Class<junit.framework.TestCase>>	extras	= GSetUtil.newSet( actualSet );
		extras.removeAll( actualSet );
		if ( extras.size() != 0 )
		{
			println( "\nNOTE: the following non-default tests WILL BE RUN:" );
			final String[]  names   = classesToStrings( extras );
			
			println( ArrayStringifier.stringify( names, "\n" ) );
			println( "" );
		}