FileDocCategorySizeDatePackage
CustomMBeanConfigTest.javaAPI DocGlassfish v2 API10158Fri May 04 22:23:54 BST 2007com.sun.enterprise.management.config

CustomMBeanConfigTest

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

Fields Summary
private static final String
IMPL_CLASSNAME
private static final String
TEST_NAME_BASE
private static final String
TEST_TYPE
Constructors Summary
public CustomMBeanConfigTest()

    
		
	 
	
	    if ( checkNotOffline( "ensureDefaultInstance" ) )
	    {
	        ensureDefaultInstance( getDomainConfig() );
	    }
	
Methods Summary
private synchronized void_testAttrs()

	    final Map<String,CustomMBeanConfig> all = getCustomMBeanConfigs();
	    
	    if ( all.size() != 0 )
	    {
    	    // everything is already tested generically, but we'll
    	    // do some basic sanity checks here
    	    for( final CustomMBeanConfig config : all.values() )
    	    {
    	        sanityCheck( config );
    	    }
	    }
	    else
	    {
	        warning( "CustomMBeanConfigTest: No custom MBeans to test" );
	    }
	
private void_testGetAll()

	    final Map<String,CustomMBeanConfig> all = getCustomMBeanConfigs();
		assert( all != null );
	
public synchronized com.sun.appserv.management.config.CustomMBeanConfigcreate(com.sun.appserv.management.config.DomainConfig domainConfig, java.lang.String name, java.util.Map optional)

	    return createInstance( domainConfig, name, optional );
	
public static com.sun.appserv.management.config.CustomMBeanConfigcreateInstance(com.sun.appserv.management.config.DomainConfig domainConfig, java.lang.String name, java.util.Map optional)

	    final CustomMBeanConfig custom  =
	        domainConfig.createCustomMBeanConfig( name, IMPL_CLASSNAME,
	        createObjectName( name ),
	        false,
	        optional );
	    
	    return custom;
	
private static java.lang.StringcreateObjectName(java.lang.String name)

        return CustomMBeanConfig.JMX_DOMAIN + ":name=" + name +
            ",type=" + TEST_TYPE;
    
private static java.util.MapcreateProps()
Create some dummy properties for creating a CustomMBeanConfig

	    final String PRP = PropertiesAccess.PROPERTY_PREFIX;    // shorten
	    
	    final Map<String,String> optional  = new HashMap<String,String>();
	    
	    // these must be available in test MBean
	    optional.put( PRP + "Attr1", "hello" );
	    optional.put( PRP + "Attr2", "world" );
	    
	    return optional;
    
public static com.sun.appserv.management.config.CustomMBeanConfigensureDefaultInstance(com.sun.appserv.management.config.DomainConfig domainConfig)

	    CustomMBeanConfig   result  =
	        domainConfig.getCustomMBeanConfigMap().get( getDefaultInstanceName() );
	    
	    if ( result == null )
	    {
	        result  = createInstance( 
	            domainConfig,
	            getDefaultInstanceName(),
	            createProps() );
	    }
	    
	    return result;
	
public java.util.MapgetCustomMBeanConfigs()

	    return getDomainConfig().getCustomMBeanConfigMap();
	
public static java.lang.StringgetDefaultInstanceName()

        return getDefaultInstanceName( "CustomMBeanConfig" );
    
private java.util.SetgetRegisteredCustoms()

	    final QueryMgr  queryMgr    = getQueryMgr();
	    final Set<ObjectName>   mbeans  =
	        queryMgr.queryPatternObjectNameSet( CustomMBeanConfig.JMX_DOMAIN, "type=" + TEST_TYPE );
	    
	    return mbeans;
    
private voidremoveCustomMBean(java.lang.String name)

	    getDomainConfig().removeCustomMBeanConfig( name );
    
private voidsanityCheck(com.sun.appserv.management.config.CustomMBeanConfig config)

        final String    objectName = config.getObjectNameInConfig();
        
        final String    implClassname   = config.getImplClassname();
	
public synchronized voidtestCreateRemove()

	    if ( ! checkNotOffline( "testCreateRemove" ) )
	    {
	        return;
	    }
	    
	    final DomainConfig  domainConfig    = getDomainConfig();
	    
	    final Map<String,String> optional    = createProps();
	    
	    final Set<CustomMBeanConfig>    created = new HashSet<CustomMBeanConfig>();
	    
	    final Map<String,CustomMBeanConfig> existing    =
	        getDomainConfig().getCustomMBeanConfigMap();
	    
	    unregisterAnyTestMBeans();
        final Set<ObjectName> customsBefore = getRegisteredCustoms();
	    if ( customsBefore.size() != 0 )
	    {
	        printVerbose( "custom MBeans already registered:\n" +
	            CollectionUtil.toString( customsBefore, "\n" ) );
	    }
	    
	    final int   NUM = 3;
	    for( int i = 0; i < NUM; ++i )
	    {
	        final String    testName    = TEST_NAME_BASE + i;
	        
	        if ( existing.containsKey( testName ) ) 
	        {
    	        RefHelper.removeAllRefsTo( existing.get( testName ), true );
	            // leftover from a failed test...
	            removeCustomMBean( testName );
	        }
	        
	        final CustomMBeanConfig config  =
	            create( domainConfig, TEST_NAME_BASE + i, optional );
	        //printVerbose( "created: " + Util.getObjectName( config ) );
	        
	        assert( getCustomMBeanConfigs().get( config.getName() ) == config );
	        
	        created.add( config );
	        sanityCheck( config );
	        
	        verifyPropsAdded( config, optional ); 
	    }
	    
	    _testGetAll();
	    _testAttrs();
	    
	    for( final CustomMBeanConfig config : created )
	    {
	        //printVerbose( "removing: " + Util.getObjectName( config ) );
	        final String    name    = config.getName();
	        removeCustomMBean( name );
	        
	        assert( getCustomMBeanConfigs().get( name ) == null );
	    }
	    
	    _testGetAll();
	    
	    mySleep( 100 );
        final Set<ObjectName>   customsAfter = getRegisteredCustoms();
        customsAfter.removeAll( customsBefore );
        if ( customsAfter.size() != 0 )
        {
            warning( "after removing custom MBeans, " +
                "they are still registered (not an AMX bug):\n" +
                CollectionUtil.toString( customsAfter, "\n" ) );
        }
        unregisterAnyTestMBeans();
	
private voidunregisterAnyTestMBeans()

        final Set<ObjectName>   customs = getRegisteredCustoms();
        for( final ObjectName objectName : customs )
        {
            if ( TEST_TYPE.equals( objectName.getKeyProperty( "type" ) ) )
            {
                try
                {
                    getMBeanServerConnection().unregisterMBean( objectName );
                    printVerbose( "unregistered: " + objectName );
                }
                catch( Exception e )
                {
                }
            }
        }
    
public synchronized voidverifyPropsAdded(com.sun.appserv.management.config.CustomMBeanConfig config, java.util.Map props)

	    for( final String key : props.keySet() )
	    {
	        if ( key.startsWith( PropertiesAccess.PROPERTY_PREFIX ) )
	        {
	            final String    specifiedValue   = props.get( key ).toString();
	            final String    propName    = key.substring(
	                PropertiesAccess.PROPERTY_PREFIX.length(), key.length());
	            
	            final String    actualValue = config.getPropertyValue( propName );
	            assert( specifiedValue.equals( actualValue ) );
	        }
	    }