1   /*
2    * $Id: MuleAgentsTestCase.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.agents;
12  
13  import org.mule.module.management.agent.JmxAgent;
14  import org.mule.module.management.agent.Mx4jAgent;
15  import org.mule.tck.AbstractMuleTestCase;
16  
17  import java.util.List;
18  
19  import javax.management.MBeanServer;
20  import javax.management.MBeanServerFactory;
21  
22  public class MuleAgentsTestCase extends AbstractMuleTestCase
23  {
24      public MuleAgentsTestCase()
25      {
26          setStartContext(true);
27      }
28  
29      public void testRemoveNonExistentAgent() throws Exception
30      {
31          muleContext.getRegistry().unregisterAgent("DOES_NOT_EXIST");
32          // should not throw NPE
33      }
34  
35      public void testAgentsRegistrationOrder() throws Exception
36      {
37          JmxAgent agentFirst = new JmxAgent();
38          // If you specified "JmxAgent", it was the first one in the map,
39          // but for "jmxAgent" the order was not preserved.
40          // MX4JAgent depends on JmxAgent having finished initilisation
41          // before proceeding, otherwise it is not able to find any
42          // MBeanServer.
43          agentFirst.setName("jmxAgent");
44          muleContext.getRegistry().registerAgent(agentFirst);
45  
46          Mx4jAgent agentSecond = new Mx4jAgent();
47          agentSecond.setName("mx4jAgent");
48          muleContext.getRegistry().registerAgent(agentSecond);
49  
50          // should not throw an exception
51      }
52  
53      /**
54       * Should not bark when the MBeanServer is injected and
55       * {@code locateServer} and {@code createServer} both
56       * set to false.
57       */
58      public void testJmxAgentInjectedMBeanServer() throws Exception
59      {
60          JmxAgent jmxAgent = new JmxAgent();
61          List servers = MBeanServerFactory.findMBeanServer(null);
62          MBeanServer server;
63          server = servers == null || servers.isEmpty()
64                  ? MBeanServerFactory.createMBeanServer()
65                  : (MBeanServer) servers.get(0);
66          jmxAgent.setCreateServer(false);
67          jmxAgent.setLocateServer(false);
68          jmxAgent.setMBeanServer(server);
69          muleContext.getRegistry().registerAgent(jmxAgent);
70      }
71  }