1   /*
2    * $Id: JmxAgentTestCase.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.MuleManager;
14  import org.mule.tck.AbstractMuleTestCase;
15  
16  import java.util.Arrays;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import javax.management.MBeanServerConnection;
21  import javax.management.remote.JMXConnector;
22  import javax.management.remote.JMXConnectorFactory;
23  import javax.management.remote.JMXServiceURL;
24  import javax.management.remote.rmi.RMIConnectorServer;
25  
26  public class JmxAgentTestCase extends AbstractMuleTestCase
27  {
28      private static final String[] VALID_AUTH_TOKEN = {"mule", "mulepassword"};
29      private static final String DOMAIN = "JmxAgentTest";
30  
31      private MuleManager manager;
32      private JmxAgent jmxAgent;
33  
34      protected void doSetUp () throws Exception
35      {
36          super.doSetUp();
37          RmiRegistryAgent rmiRegistryAgent = new RmiRegistryAgent();
38          jmxAgent = new JmxAgent();
39          jmxAgent.setConnectorServerUrl(JmxAgent.DEFAULT_REMOTING_URI);
40          manager = (MuleManager) getManager(true);
41          manager.registerAgent(rmiRegistryAgent);
42          manager.setId(DOMAIN);
43      }
44  
45      public void testDefaultProperties() throws Exception
46      {
47          jmxAgent.setCredentials(getValidCredentials());
48          manager.registerAgent(jmxAgent);
49          manager.start();
50      }
51  
52      public void testSuccessfulRemoteConnection() throws Exception
53      {
54          configureProperties();
55          jmxAgent.setCredentials(getValidCredentials());
56          manager.registerAgent(jmxAgent);
57          manager.start();
58  
59          JMXServiceURL serviceUrl = new JMXServiceURL(JmxAgent.DEFAULT_REMOTING_URI);
60          Map props = new HashMap(1);
61          props.put(JMXConnector.CREDENTIALS, VALID_AUTH_TOKEN);
62          JMXConnector connector = JMXConnectorFactory.connect(serviceUrl, props);
63          MBeanServerConnection connection = connector.getMBeanServerConnection();
64          // is it the right server?
65          assertTrue(Arrays.asList(connection.getDomains()).contains("Mule." + DOMAIN));
66      }
67  
68      public void testNoCredentialsProvided() throws Exception
69      {
70          configureProperties();
71          jmxAgent.setCredentials(getValidCredentials());
72          manager.registerAgent(jmxAgent);
73          manager.start();
74  
75          JMXServiceURL serviceUrl = new JMXServiceURL(JmxAgent.DEFAULT_REMOTING_URI);
76          try
77          {
78              JMXConnector connector = JMXConnectorFactory.connect(serviceUrl);
79          }
80          catch (SecurityException e)
81          {
82              // expected
83          }
84      }
85  
86      public void testNonRestrictedAccess() throws Exception
87      {
88          configureProperties();
89          jmxAgent.setCredentials(null);
90          manager.registerAgent(jmxAgent);
91          manager.start();
92  
93          JMXServiceURL serviceUrl = new JMXServiceURL(JmxAgent.DEFAULT_REMOTING_URI);
94          JMXConnector connector = JMXConnectorFactory.connect(serviceUrl);
95          MBeanServerConnection connection = connector.getMBeanServerConnection();
96          // is it the right server?
97          assertTrue(Arrays.asList(connection.getDomains()).contains("Mule." + DOMAIN));
98      }
99  
100     protected Map getValidCredentials ()
101     {
102         final Map credentials = new HashMap(1);
103         credentials.put(VALID_AUTH_TOKEN[0], VALID_AUTH_TOKEN[1]);
104 
105         return credentials;
106     }
107 
108     protected void configureProperties ()
109     {
110         // make multi-NIC dev box happy by sticking RMI clients to a single
111         // local ip address
112         Map props = new HashMap();
113         props.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE,
114                   new FixedHostRmiClientSocketFactory("127.0.0.1"));
115         jmxAgent.setConnectorServerProperties(props);
116     }
117 }