View Javadoc

1   /*
2    * $Id: AbstractMuleJmxTestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
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;
12  
13  import org.mule.module.management.agent.RmiRegistryAgent;
14  import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory;
15  import org.mule.module.management.support.JmxSupport;
16  import org.mule.module.management.support.JmxSupportFactory;
17  import org.mule.tck.junit4.AbstractMuleContextTestCase;
18  
19  import java.lang.management.ManagementFactory;
20  import java.util.Set;
21  
22  import javax.management.MBeanServer;
23  import javax.management.ObjectInstance;
24  
25  import org.junit.Test;
26  
27  /**
28   * This base test case will create a new <code>MBean Server</code> if necessary,
29   * and will clean up any registered MBeans in its <code>tearDown()</code> method.
30   */
31  public abstract class AbstractMuleJmxTestCase extends AbstractMuleContextTestCase
32  {
33      protected MBeanServer mBeanServer;
34      protected JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
35      protected JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
36  
37      protected void doSetUp() throws Exception
38      {
39          RmiRegistryAgent rmiRegistryAgent = new RmiRegistryAgent();
40          rmiRegistryAgent.setMuleContext(muleContext);
41          rmiRegistryAgent.initialise();
42          muleContext.getRegistry().registerAgent(rmiRegistryAgent);
43  
44          mBeanServer = ManagementFactory.getPlatformMBeanServer();
45  
46      }
47  
48      protected void unregisterMBeansByMask(final String mask) throws Exception
49      {
50          Set<ObjectInstance> objectInstances = mBeanServer.queryMBeans(jmxSupport.getObjectName(mask), null);
51          for (ObjectInstance instance : objectInstances)
52          {
53              try
54              {
55                  mBeanServer.unregisterMBean(instance.getObjectName());
56              }
57              catch (Exception e)
58              {
59                  // ignore
60              }
61          }
62      }
63  
64      protected void doTearDown() throws Exception
65      {
66          String domainName = jmxSupport.getDomainName(muleContext);
67          unregisterMBeansByMask(domainName + ":*");
68          unregisterMBeansByMask(domainName + ".1:*");
69          unregisterMBeansByMask(domainName + ".2:*");
70          mBeanServer = null;
71      }
72  
73      @Test
74      public void testDummy()
75      {
76          // this method only exists to silence the test runner
77      }
78  
79  }