View Javadoc

1   /*
2    * $Id: MBeansRegistrationTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.management.mbeans;
12  
13  import org.mule.module.management.agent.JmxAgent;
14  import org.mule.module.management.agent.JmxServerNotificationAgent;
15  import org.mule.module.management.mbean.ConnectorService;
16  import org.mule.module.management.mbean.EndpointService;
17  import org.mule.module.management.mbean.ModelService;
18  import org.mule.module.management.mbean.MuleConfigurationService;
19  import org.mule.module.management.mbean.MuleService;
20  import org.mule.module.management.mbean.RouterStats;
21  import org.mule.module.management.mbean.ServiceService;
22  import org.mule.module.management.mbean.StatisticsService;
23  import org.mule.module.management.support.JmxSupport;
24  import org.mule.tck.FunctionalTestCase;
25  
26  import java.util.ArrayList;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Set;
30  
31  import javax.management.MBeanServer;
32  import javax.management.MalformedObjectNameException;
33  import javax.management.ObjectInstance;
34  
35  /**
36   * Verify that expected MBeans are registered based on the config.
37   */
38  public class MBeansRegistrationTestCase extends FunctionalTestCase
39  {
40      private MBeanServer mBeanServer;
41      private String domainName;
42      private JmxSupport jmxSupport;
43  
44      @Override
45      protected void doSetUp() throws Exception
46      {
47          super.doSetUp();
48          JmxAgent jmxAgent = (JmxAgent) muleContext.getRegistry().lookupAgent("jmx-agent");
49          jmxSupport = jmxAgent.getJmxSupportFactory().getJmxSupport();
50          domainName = jmxSupport.getDomainName(muleContext);
51          mBeanServer = jmxAgent.getMBeanServer();
52      }
53  
54      protected String getConfigResources()
55      {
56          return "mbeans-test.xml";
57      }
58  
59      /**
60       * Verify that all expected MBeans are registered for a default config
61       */
62      public void testDefaultMBeansRegistration() throws Exception
63      {
64          List<String> mbeanClasses = getMBeanClasses();
65          
66          assertTrue(mbeanClasses.contains(JmxServerNotificationAgent.class.getName() + "$BroadcastNotificationService"));
67          assertTrue(mbeanClasses.contains(JmxServerNotificationAgent.class.getName() + "$NotificationListener"));
68          assertTrue(mbeanClasses.contains(MuleService.class.getName()));
69          assertTrue(mbeanClasses.contains(MuleConfigurationService.class.getName()));
70          assertTrue(mbeanClasses.contains(StatisticsService.class.getName()));
71          assertTrue(mbeanClasses.contains(ModelService.class.getName()));
72  
73          // Only if registerMx4jAdapter="true"
74          assertTrue(mbeanClasses.contains(mx4j.tools.adaptor.http.HttpAdaptor.class.getName()));
75      }
76  
77      /**
78       * Verify that all expected MBeans are registered for connectors, services, routers, and endpoints.
79       */
80      public void testServiceMBeansRegistration() throws Exception
81      {
82          List<String> mbeanClasses = getMBeanClasses();
83          
84          assertTrue(mbeanClasses.contains(ConnectorService.class.getName()));
85          assertTrue(mbeanClasses.contains(ModelService.class.getName()));
86          assertTrue(mbeanClasses.contains(ServiceService.class.getName()));
87          assertTrue(mbeanClasses.contains(RouterStats.class.getName()));
88          assertTrue(mbeanClasses.contains(EndpointService.class.getName()));
89      }
90  
91      /**
92       * Verify that all MBeans were unregistered during disposal phase.
93       */
94      public void testMBeansUnregistration() throws Exception
95      {
96          muleContext.dispose();
97          assertEquals("No MBeans should be registered after disposal of MuleContext", 0, getMBeanClasses().size());
98      }
99      
100     protected List<String> getMBeanClasses() throws MalformedObjectNameException
101     {
102         Set<ObjectInstance> mbeans = getMBeans();
103         Iterator it = mbeans.iterator();
104         List<String> mbeanClasses = new ArrayList<String>();
105         while (it.hasNext())
106         {
107             mbeanClasses.add(((ObjectInstance) it.next()).getClassName());
108         }
109         return mbeanClasses;
110     }
111     
112     protected Set<ObjectInstance> getMBeans() throws MalformedObjectNameException
113     {
114         return mBeanServer.queryMBeans(jmxSupport.getObjectName(domainName + ":*"), null);        
115     }
116 }