1   /*
2    * $Id: AbstractMuleJmxTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.tck.AbstractMuleTestCase;
14  
15  import java.util.Iterator;
16  import java.util.List;
17  import java.util.Set;
18  
19  import javax.management.MBeanServer;
20  import javax.management.MBeanServerFactory;
21  import javax.management.ObjectInstance;
22  import javax.management.ObjectName;
23  
24  /**
25   * This base test case will create a new <code>MBean Server</code> if necessary,
26   * and will clean up any registered MBeans in its <code>tearDown()</code> method.
27   */
28  public class AbstractMuleJmxTestCase extends AbstractMuleTestCase
29  {
30      protected MBeanServer mBeanServer;
31  
32      protected void doSetUp() throws Exception
33      {
34          // simulate a running environment with Log4j MBean already registered
35          List servers = MBeanServerFactory.findMBeanServer(null);
36          if (servers.size() == 0)
37          {
38              MBeanServerFactory.createMBeanServer();
39          }
40  
41          mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
42      }
43  
44      protected void doTearDown() throws Exception
45      {
46          // unregister all MBeans
47          Set objectInstances = mBeanServer.queryMBeans(ObjectName.getInstance("*.*:*"), null);
48          for (Iterator it = objectInstances.iterator(); it.hasNext();)
49          {
50              ObjectInstance instance = (ObjectInstance)it.next();
51              mBeanServer.unregisterMBean(instance.getObjectName());
52          }
53  
54          mBeanServer = null;
55      }
56  
57      public void testDummy()
58      {
59          // this method only exists to silence the test runner
60      }
61  
62  }