1   /*
2    * $Id: AbstractMuleJmxTestCase.java 11371 2008-03-15 03:12:09Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.RegistryContext;
14  import org.mule.module.management.agent.RmiRegistryAgent;
15  import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory;
16  import org.mule.module.management.support.JmxSupport;
17  import org.mule.module.management.support.JmxSupportFactory;
18  import org.mule.tck.AbstractMuleTestCase;
19  
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Set;
23  
24  import javax.management.MBeanServer;
25  import javax.management.MBeanServerFactory;
26  import javax.management.ObjectInstance;
27  
28  /**
29   * This base test case will create a new <code>MBean Server</code> if necessary,
30   * and will clean up any registered MBeans in its <code>tearDown()</code> method.
31   */
32  public abstract class AbstractMuleJmxTestCase extends AbstractMuleTestCase
33  {
34      protected MBeanServer mBeanServer;
35      protected JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
36      protected JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport(); 
37  
38      protected void doSetUp() throws Exception
39      {
40          RmiRegistryAgent rmiRegistryAgent = new RmiRegistryAgent();
41          rmiRegistryAgent.setMuleContext(muleContext);
42          rmiRegistryAgent.initialise();
43          RegistryContext.getRegistry().registerAgent(rmiRegistryAgent);
44          
45          // simulate a running environment with Log4j MBean already registered
46          List servers = MBeanServerFactory.findMBeanServer(null);
47          if (servers.size() == 0)
48          {
49              MBeanServerFactory.createMBeanServer();
50          }
51  
52          mBeanServer = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
53      }
54  
55      protected void unregisterMBeansByMask(final String mask) throws Exception
56      {
57          Set objectInstances = mBeanServer.queryMBeans(jmxSupport.getObjectName(mask), null);
58          for (Iterator it = objectInstances.iterator(); it.hasNext();)
59          {
60              ObjectInstance instance = (ObjectInstance) it.next();
61              mBeanServer.unregisterMBean(instance.getObjectName());
62          }
63      }
64  
65      protected void doTearDown() throws Exception
66      {
67          // Don't unregister MBean's here as ManamagmentContext disposal disposes agents which unregister
68          // their MBeans and give errors if they can't find the MBeans they registered.
69          // Any MBean's that are registered manually in TestCase should be unregistered in the same test case.
70  
71          // Release MBeanServer so MBeanServer instance can't get passed over from one
72          // test to another in same circumstances.
73          MBeanServerFactory.releaseMBeanServer(mBeanServer);
74          mBeanServer = null;
75      }
76  
77      public void testDummy()
78      {
79          // this method only exists to silence the test runner
80      }
81  
82  }