1   /*
2    * $Id: MuleAgentsTestCase.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.agents;
12  
13  import org.mule.tck.AbstractMuleTestCase;
14  import org.mule.umo.manager.UMOManager;
15  
16  import java.util.List;
17  
18  import javax.management.MBeanServer;
19  import javax.management.MBeanServerFactory;
20  
21  public class MuleAgentsTestCase extends AbstractMuleTestCase
22  {
23      private UMOManager manager;
24  
25      /**
26       * Print the name of this test to standard output
27       */
28      protected void doSetUp() throws Exception
29      {
30          manager = getManager(true);
31      }
32  
33  
34      protected void doTearDown () throws Exception {
35          if (manager != null)
36          {
37              manager.dispose();
38          }
39      }
40  
41      public void testRemoveNonExistentAgent() throws Exception
42      {
43          manager.unregisterAgent("DOES_NOT_EXIST");
44          // should not throw NPE
45      }
46  
47      public void testAgentsRegistrationOrder() throws Exception
48      {
49          manager.setId("MuleAgentsTestCase.agentsRegistrationOrder");
50          JmxAgent agentFirst = new JmxAgent();
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          agentFirst.setName("jmxAgent");
57          manager.registerAgent(agentFirst);
58  
59          Mx4jAgent agentSecond = new Mx4jAgent();
60          agentSecond.setName("mx4jAgent");
61          manager.registerAgent(agentSecond);
62  
63          manager.start();
64  
65          // should not throw an exception
66      }
67  
68      /**
69       * Should not bark when the MBeanServer is injected and
70       * {@code locateServer} and {@code createServer} both
71       * set to false.
72       */
73      public void testJmxAgentInjectedMBeanServer() throws Exception
74      {
75          manager.setId("MuleAgentsTestCase.jmxAgentInjectedMBeanServer");
76          JmxAgent jmxAgent = new JmxAgent();
77          List servers = MBeanServerFactory.findMBeanServer(null);
78          MBeanServer server;
79          server = servers == null || servers.isEmpty()
80                  ? MBeanServerFactory.createMBeanServer()
81                  : (MBeanServer) servers.get(0);
82          jmxAgent.setCreateServer(false);
83          jmxAgent.setLocateServer(false);
84          jmxAgent.setMBeanServer(server);
85          manager.registerAgent(jmxAgent);
86          manager.start();
87      }
88  }