View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.management.agents;
8   
9   import org.mule.module.management.agent.JmxAgent;
10  import org.mule.module.management.agent.Mx4jAgent;
11  import org.mule.tck.junit4.AbstractMuleContextTestCase;
12  
13  import java.util.List;
14  
15  import javax.management.MBeanServer;
16  import javax.management.MBeanServerFactory;
17  
18  import org.junit.Test;
19  
20  public class MuleAgentsTestCase extends AbstractMuleContextTestCase
21  {
22  
23      protected JmxAgent jmxAgent;
24  
25      public MuleAgentsTestCase()
26      {
27          setStartContext(false);
28      }
29  
30      @Override
31      protected void doSetUp() throws Exception
32      {
33          super.doSetUp();
34          jmxAgent = (JmxAgent) muleContext.getRegistry().lookupObject(JmxAgent.class);
35      }
36  
37      @Test
38      public void testRemoveNonExistentAgent() throws Exception
39      {
40          muleContext.getRegistry().unregisterAgent("DOES_NOT_EXIST");
41          // should not throw NPE
42      }
43  
44      @Test
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      @Test
68      public void testJmxAgentInjectedMBeanServer() throws Exception
69      {
70          List servers = MBeanServerFactory.findMBeanServer(null);
71          MBeanServer server;
72          server = servers == null || servers.isEmpty()
73                  ? MBeanServerFactory.createMBeanServer()
74                  : (MBeanServer) servers.get(0);
75          jmxAgent.setCreateServer(false);
76          jmxAgent.setLocateServer(false);
77          jmxAgent.setMBeanServer(server);
78  
79          muleContext.start();
80      }
81  }