View Javadoc

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