FileDocCategorySizeDatePackage
ComSunAppservMonitorTest.javaAPI DocGlassfish v2 API13864Fri May 04 22:24:06 BST 2007com.sun.enterprise.management.monitor

ComSunAppservMonitorTest

public final class ComSunAppservMonitorTest extends com.sun.enterprise.management.support.ComSunAppservTest
Unit test for the com.sun.appserv monitoring MBeans relied upon by AMX.

Fields Summary
private static final Set
COM_SUN_APPSERV_MONITOR_TYPES
types to be tested as in "category=monitor,type=xxx". We can't just test all category=monitor, or even check that getStatistics() and getStatisticNames() are present, as many such MBeans don't work at all. Types taken from com.sun.enterprise.management.support.initMap().
Constructors Summary
public ComSunAppservMonitorTest()

    
Methods Summary
private booleancheckMonitor(javax.management.ObjectName objectName)

        boolean worksOK = true;
        
        final MonitorIntf   intf  = getMonitorIntf( objectName );
        if ( intf != null )
        {
            final String[]      statisticNames   = intf.getStatisticNames();
            final Set<String>   statisticNamesSet   = GSetUtil.newStringSet( statisticNames );
            
            // get all names as defined by Statistics
            final Statistic[]   statistics       = intf.getStatistics();
            final Set<String>   statisticNamesFromStatisticsSet   = new HashSet<String>();
            for( final Statistic s : statistics )
            {
                statisticNamesFromStatisticsSet.add( s.getName() );
            }
            
            if ( ! statisticNamesSet.equals( statisticNamesFromStatisticsSet ) )
            {
                final String[]  statisticNamesFromStatistics    =
                    GSetUtil.toStringArray( statisticNamesFromStatisticsSet );
                
                Arrays.sort( statisticNames );
                Arrays.sort( statisticNamesFromStatistics );
                
                printVerbose( "WARNING: MBean " + StringUtil.quote( objectName ) +
                    " returns Statistic names from getStatisticNames() " +
                    "that disagree with the names actually " +
                    "found in Statistics from getStatistics(): " +
                    "getStatisticNames() = {" + ArrayStringifier.stringify( statisticNames, "," ) +
                    "}, getStatistics() = {" +
                        ArrayStringifier.stringify( statisticNamesFromStatistics, "," ) + "}" );
                worksOK = false;
            }
        }
        
        return( worksOK );
    
private com.sun.enterprise.management.monitor.ComSunAppservMonitorTest$MonitorIntfgetMonitorIntf(javax.management.ObjectName objectName)

        final MonitorIntf   intf    = new MonitorImpl( objectName );
        boolean basicsOK = true;
        
        if ( ! hasStatisticSupport( objectName ) )
        {
            warning( "MBean " + StringUtil.quote( objectName ) +
                " doesn't have getStatisticNames() and getStatistics() methods." );
            return null;
        }
        
        try
        {
            final String[] statisticNames  = intf.getStatisticNames();
            if ( statisticNames == null )
            {
                warning( "MBean " + StringUtil.quote( objectName ) +
                    " returned null from getStatisticNames()" );
                basicsOK    = false;
            }
            if ( statisticNames.length == 0 )
            {
                warning( "MBean " + StringUtil.quote( objectName ) +
                    " returned an empty String[] from getStatisticNames()" );
                basicsOK    = false;
            }
        }
        catch( Exception e )
        {
			final Throwable rootCause	= ExceptionUtil.getRootCause( e );
			
            warning( "MBean " + StringUtil.quote( objectName ) +
                " threw an exception from getStatisticNames(): " + rootCause );
            basicsOK    = false;
        }
        
        if ( basicsOK )
        {
            try
            {
                final Statistic[] statistics  = intf.getStatistics();
                if ( statistics == null )
                {
                    warning( "MBean " + StringUtil.quote( objectName ) +
                        " returned null from getStatistics()" );
                    basicsOK    = false;
                }
                if ( statistics != null && statistics.length == 0 )
                {
                    warning( "MBean " + StringUtil.quote( objectName ) +
                        " returned an empty Statistic[] from getStatistics()" );
                    basicsOK    = false;
                }
            }
            catch( Exception e )
            {
			    final Throwable rootCause	= ExceptionUtil.getRootCause( e );
			
                warning( "MBean " + StringUtil.quote( objectName ) +
                    " threw an exception from getStatistics(): " + rootCause );
                basicsOK    = false;
            }
        }
        
        return basicsOK ? intf : null;
    
private booleanhasStatisticSupport(javax.management.ObjectName objectName)

        boolean hasSupport  = false;
        
        try
        {
            final MBeanInfo mbeanInfo   = getMBeanServerConnection().getMBeanInfo( objectName );
            boolean shouldTest  = false;
            
            final MBeanOperationInfo[]  candidates  = mbeanInfo.getOperations();
            if ( JMXUtil.findOperations( candidates, "getStatisticNames" ).length != 0 &&
                 JMXUtil.findOperations( candidates, "getStatistics" ).length != 0 )
            {
                hasSupport  = true;
            }
        }
        catch( Exception e )
        {
			final Throwable rootCause	= ExceptionUtil.getRootCause( e );
			
            warning( "hasStatisticSupport: got exception: " + rootCause );
        }
        
        return hasSupport;
    
private booleanshouldBeTested(javax.management.ObjectName objectName)

        
        
         
       
         
   
        final String    type    = objectName.getKeyProperty( "type" );
        
        return COM_SUN_APPSERV_MONITOR_TYPES.contains( type );
   
public voidtestAllMonitor()

        try
        {
            Class.forName( "com.sun.enterprise.admin.monitor.stats.CountStatisticImpl" );
        }
        catch( ClassNotFoundException e )
        {
            failure( "ComSunAppservMonitorTest.testAllMonitor: " +
            "CLASSPATH is missing Statistic classes, skipping tests. " +
            "Use 'maven run-tests' instead of 'ant run-tests'" );
            return;
        }
        
        final Map<String,ObjectName>    m   = getAllComSunAppservMonitor();
        
        final Collection<ObjectName>   objectNames = m.values();
        final Set<ObjectName>   defective   = new HashSet<ObjectName>();
        int testedCount = 0;
        for( final ObjectName objectName : objectNames )
        {
            if ( ! shouldBeTested( objectName ) )
            {
                continue;
            }
            
            ++testedCount;
            
            if ( ! checkMonitor( objectName ) )
            {
                defective.add( objectName );
            }
        }
        
        printVerbose( "ComSunAppservMonitorTest.testAllMonitor: checked " +
            testedCount + " com.sun.appserv:category=monitor MBeans for basic functionality, " +
            defective.size() + " failures." );
            
        if ( defective.size() != 0 )
        {
            // slim down the ObjectName for better readability
            final String[]  names   = new String[ defective.size() ];
            int i = 0;
            for( final ObjectName objectName : defective )
            {
                names[ i ]  = objectName.getCanonicalKeyPropertyListString();
                ++i;
            }
            
            Arrays.sort( names );
            
            final boolean verbose   = getVerbose();
            
            warning( "The following " + defective.size() +
                " com.sun.appserv MBeans don't work correctly, so " +
                "subsequent tests (eg J2EETest) may fail:\n" +
                ArrayStringifier.stringify( names, "\n") +
                (verbose ? "" : "\n(set amxtest.verbose=true for details)") );
        }