1   /*
2    * $Id: JmxAgentTestCase.java 11371 2008-03-15 03:12:09Z tcarlson $
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.api.context.MuleContextBuilder;
14  import org.mule.config.DefaultMuleConfiguration;
15  import org.mule.module.management.agent.FixedHostRmiClientSocketFactory;
16  import org.mule.module.management.agent.JmxAgent;
17  import org.mule.module.management.agent.RmiRegistryAgent;
18  import org.mule.tck.AbstractMuleTestCase;
19  
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.management.MBeanServerConnection;
26  import javax.management.remote.JMXConnector;
27  import javax.management.remote.JMXConnectorFactory;
28  import javax.management.remote.JMXServiceURL;
29  import javax.management.remote.rmi.RMIConnectorServer;
30  
31  public class JmxAgentTestCase extends AbstractMuleTestCase
32  {
33  
34      private static final String[] VALID_AUTH_TOKEN = {"mule", "mulepassword"};
35      private static final String DOMAIN = "JmxAgentTest";
36      
37      private JMXServiceURL serviceUrl;
38      private JmxAgent jmxAgent;
39  
40      //@Override
41      protected void configureMuleContext(MuleContextBuilder contextBuilder)
42      {
43          super.configureMuleContext(contextBuilder);
44  
45          DefaultMuleConfiguration config = new DefaultMuleConfiguration();
46          config.setId(DOMAIN);
47          contextBuilder.setMuleConfiguration(config);
48      }
49  
50      protected void doSetUp() throws Exception
51      {
52          super.doSetUp();
53          serviceUrl = new JMXServiceURL(JmxAgent.DEFAULT_REMOTING_URI);
54          muleContext.getRegistry().registerAgent(new RmiRegistryAgent());
55          jmxAgent = new JmxAgent();
56          jmxAgent.setConnectorServerUrl(JmxAgent.DEFAULT_REMOTING_URI);
57      }
58  
59      protected void doTearDown()
60      {
61          jmxAgent.dispose();
62      }
63  
64      public void testDefaultProperties() throws Exception
65      {
66          jmxAgent.setCredentials(getValidCredentials());
67          muleContext.getRegistry().registerAgent(jmxAgent);
68          muleContext.start();
69      }
70  
71      public void testSuccessfulRemoteConnection() throws Exception
72      {
73          configureProperties();
74          jmxAgent.setCredentials(getValidCredentials());
75          muleContext.getRegistry().registerAgent(jmxAgent);
76          muleContext.start();
77  
78          JMXConnector connector = null;
79          try
80          {
81              Map props = Collections.singletonMap(JMXConnector.CREDENTIALS, VALID_AUTH_TOKEN);
82              connector = JMXConnectorFactory.connect(serviceUrl, props);
83              MBeanServerConnection connection = connector.getMBeanServerConnection();
84              // is it the right server?
85              assertTrue(Arrays.asList(connection.getDomains()).toString(),
86                      Arrays.asList(connection.getDomains()).contains("Mule." + DOMAIN));
87          }
88          finally
89          {
90              if (connector != null)
91              {
92                  connector.close();
93              }
94          }
95      }
96  
97      public void testNoCredentialsProvided() throws Exception
98      {
99          configureProperties();
100         jmxAgent.setCredentials(getValidCredentials());
101         muleContext.getRegistry().registerAgent(jmxAgent);
102         muleContext.start();
103 
104         JMXConnector connector = null;
105         try
106         {
107             connector = JMXConnectorFactory.connect(serviceUrl);
108             fail("expected SecurityException");
109         }
110         catch (SecurityException e)
111         {
112             // expected
113         }
114         finally
115         {
116             if (connector != null)
117             {
118                 connector.close();
119             }            
120         }
121     }
122 
123     public void testNonRestrictedAccess() throws Exception
124     {
125         configureProperties();
126         jmxAgent.setCredentials(null);
127         muleContext.getRegistry().registerAgent(jmxAgent);
128         muleContext.start();
129 
130         JMXConnector connector = null;
131         try
132         {
133             connector = JMXConnectorFactory.connect(serviceUrl);
134             MBeanServerConnection connection = connector.getMBeanServerConnection();
135             // is it the right server?
136             assertTrue(Arrays.asList(connection.getDomains()).contains("Mule." + DOMAIN));
137         }
138         finally
139         {
140             if (connector != null)
141             {
142                 connector.close();
143             }
144         }
145     }
146 
147     protected Map getValidCredentials()
148     {
149         final Map credentials = new HashMap(1);
150         credentials.put(VALID_AUTH_TOKEN[0], VALID_AUTH_TOKEN[1]);
151 
152         return credentials;
153     }
154 
155     protected void configureProperties()
156     {
157         // make multi-NIC dev box happy by sticking RMI clients to a single
158         // local ip address
159         Map props = new HashMap();
160         props.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE,
161                   new FixedHostRmiClientSocketFactory("127.0.0.1"));
162         jmxAgent.setConnectorServerProperties(props);
163     }
164     
165 }