FileDocCategorySizeDatePackage
MonitoringRegistrationHelperTest.javaAPI DocGlassfish v2 API6697Fri May 04 22:25:54 BST 2007com.sun.enterprise.admin.monitor.registry.spi

MonitoringRegistrationHelperTest

public class MonitoringRegistrationHelperTest extends TestCase implements MonitoringLevelListener
Unit test for MonitoringRegistrationHelper class.
author
Shreedhar Ganapathy

Fields Summary
MonitoringRegistry
mrh
MBeanServer
server
Constructors Summary
public MonitoringRegistrationHelperTest(String testName)

        super(testName);
    
Methods Summary
public voidchangeLevel(MonitoringLevel from, MonitoringLevel to, MonitoredObjectType type)

    
public voidchangeLevel(MonitoringLevel from, MonitoringLevel to, javax.management.j2ee.statistics.Stats handback)

    
private javax.management.ObjectNamegetObjectName()

        ObjectName obj =null;
        try{
            obj = new ObjectName("defaultDomain:category=monitor,type=jvm,name=jvm");
        }catch (Exception e){
            e.printStackTrace();
        }
        return obj;
    
public static voidmain(java.lang.String[] args)

        junit.textui.TestRunner.run(suite());
    
public voidsetLevel(MonitoringLevel level)

        
    
protected voidsetUp()

        server = (MBeanServer) (MBeanServerFactory.createMBeanServer(null));
        mrh = MonitoringRegistrationHelper.getInstance();
    
public static junit.framework.Testsuite()

        TestSuite suite = new TestSuite(MonitoringRegistrationHelperTest.class);
        return suite;
    
protected voidtearDown()

        
    
public voidtestCreation()

        assertNotNull(mrh);
    
public voidtestRegisterStats()

        try{
            ObjectName obj = getObjectName();
            Stats stats = new S1ASJVMStatsImplMock();
            try{
                mrh.registerJVMStats((JVMStats)stats, this); 
            }
            catch(Exception e){
                e.printStackTrace();
                assertEquals("MonitoringRegistrationException", e.getClass().getName());
            }

            assertEquals(1, server.getMBeanCount().intValue());
            assertEquals(true, server.isInstanceOf(obj,  "GeneratedMonitoringMBeanImpl"));
        }catch(Exception e){
            e.getLocalizedMessage();
        }            
    
public voidtestRegisteredMBean()

        try{
            assertNotNull(server);            
            ObjectName obj = getObjectName(); 
            Stats stats = new S1ASJVMStatsImplMock();
            try{
                mrh.registerJVMStats((JVMStats)stats, this); 
            }
            catch(Exception e){
                e.printStackTrace();
                assertEquals("MonitoringRegistrationException", e.getClass().getName());
            }
            //assertEquals(true, server.isRegistered(obj));
            MBeanInfo mInfo = (MBeanInfo)server.getMBeanInfo(obj);
            assertNotNull(mInfo);
            MBeanAttributeInfo[] attrInfo = mInfo.getAttributes();
            assertNotNull(attrInfo);
            for(int i=0; i<attrInfo.length;i++){
                String attr = attrInfo[i].getName();
                System.out.println("getting attribute:"+attr); 
                Object n =  server.getAttribute(obj,attr);
                try{
                    assertEquals(Long.class, n.getClass());
                }
                catch(Error ex){                    
                        ex.getLocalizedMessage();
                        assertEquals(String.class, n.getClass());
                }
                catch(Exception e){
                    assertNull(n);
                }
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            assertEquals(NullPointerException.class, ex.getClass());
        }        
    
public voidtestUnregisterStats()

        try{
            ObjectName obj = getObjectName();
            mrh.unregisterJVMStats();
            assertEquals(false, server.isRegistered(obj));     
            assertEquals(0, server.queryMBeans(obj, null).size());
        }catch(Exception e){
            e.getLocalizedMessage();
        }