View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.management;
8   
9   import org.mule.module.management.agent.RmiRegistryAgent;
10  import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory;
11  import org.mule.module.management.support.JmxSupport;
12  import org.mule.module.management.support.JmxSupportFactory;
13  import org.mule.tck.junit4.AbstractMuleContextTestCase;
14  
15  import java.lang.management.ManagementFactory;
16  import java.util.Set;
17  
18  import javax.management.MBeanServer;
19  import javax.management.ObjectInstance;
20  
21  import org.junit.Test;
22  
23  /**
24   * This base test case will create a new <code>MBean Server</code> if necessary,
25   * and will clean up any registered MBeans in its <code>tearDown()</code> method.
26   */
27  public abstract class AbstractMuleJmxTestCase extends AbstractMuleContextTestCase
28  {
29      protected MBeanServer mBeanServer;
30      protected JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
31      protected JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
32  
33      protected void doSetUp() throws Exception
34      {
35          RmiRegistryAgent rmiRegistryAgent = new RmiRegistryAgent();
36          rmiRegistryAgent.setMuleContext(muleContext);
37          rmiRegistryAgent.initialise();
38          muleContext.getRegistry().registerAgent(rmiRegistryAgent);
39  
40          mBeanServer = ManagementFactory.getPlatformMBeanServer();
41  
42      }
43  
44      protected void unregisterMBeansByMask(final String mask) throws Exception
45      {
46          Set<ObjectInstance> objectInstances = mBeanServer.queryMBeans(jmxSupport.getObjectName(mask), null);
47          for (ObjectInstance instance : objectInstances)
48          {
49              try
50              {
51                  mBeanServer.unregisterMBean(instance.getObjectName());
52              }
53              catch (Exception e)
54              {
55                  // ignore
56              }
57          }
58      }
59  
60      protected void doTearDown() throws Exception
61      {
62          String domainName = jmxSupport.getDomainName(muleContext);
63          unregisterMBeansByMask(domainName + ":*");
64          unregisterMBeansByMask(domainName + ".1:*");
65          unregisterMBeansByMask(domainName + ".2:*");
66          mBeanServer = null;
67      }
68  
69      @Test
70      public void testDummy()
71      {
72          // this method only exists to silence the test runner
73      }
74  
75  }