View Javadoc

1   /*
2    * $Id: MuleAgentsTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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.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  
25      protected JmxAgent jmxAgent;
26  
27      public MuleAgentsTestCase()
28      {
29          setStartContext(false);
30      }
31  
32      @Override
33      protected void doSetUp() throws Exception
34      {
35          super.doSetUp();
36          jmxAgent = (JmxAgent) muleContext.getRegistry().lookupObject(JmxAgent.class);
37      }
38  
39      public void testRemoveNonExistentAgent() throws Exception
40      {
41          muleContext.getRegistry().unregisterAgent("DOES_NOT_EXIST");
42          // should not throw NPE
43      }
44  
45      public void testAgentsRegistrationOrder() throws Exception
46      {
47          // If you specified "JmxAgent", it was the first one in the map,
48          // but for "jmxAgent" the order was not preserved.
49          // MX4JAgent depends on JmxAgent having finished initilisation
50          // before proceeding, otherwise it is not able to find any
51          // MBeanServer.
52          jmxAgent.setName("jmxAgent");
53  
54          Mx4jAgent agentSecond = new Mx4jAgent();
55          agentSecond.setName("mx4jAgent");
56          muleContext.getRegistry().registerAgent(agentSecond);
57  
58          // should not throw an exception
59          muleContext.start();
60      }
61  
62      /**
63       * Should not bark when the MBeanServer is injected and
64       * {@code locateServer} and {@code createServer} both
65       * set to false.
66       */
67      public void testJmxAgentInjectedMBeanServer() throws Exception
68      {
69          List servers = MBeanServerFactory.findMBeanServer(null);
70          MBeanServer server;
71          server = servers == null || servers.isEmpty()
72                  ? MBeanServerFactory.createMBeanServer()
73                  : (MBeanServer) servers.get(0);
74          jmxAgent.setCreateServer(false);
75          jmxAgent.setLocateServer(false);
76          jmxAgent.setMBeanServer(server);
77  
78          muleContext.start();
79      }
80  }