View Javadoc

1   /*
2    * $Id: JmxAgentTestCase.java 22387 2011-07-12 03:53:36Z 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.api.context.MuleContextBuilder;
14  import org.mule.component.simple.EchoComponent;
15  import org.mule.config.DefaultMuleConfiguration;
16  import org.mule.module.management.agent.FixedHostRmiClientSocketFactory;
17  import org.mule.module.management.agent.JmxAgent;
18  import org.mule.module.management.agent.RmiRegistryAgent;
19  import org.mule.tck.junit4.AbstractMuleContextTestCase;
20  
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.management.InstanceAlreadyExistsException;
27  import javax.management.MBeanRegistrationException;
28  import javax.management.MBeanServerConnection;
29  import javax.management.MalformedObjectNameException;
30  import javax.management.NotCompliantMBeanException;
31  import javax.management.remote.JMXConnector;
32  import javax.management.remote.JMXConnectorFactory;
33  import javax.management.remote.JMXServiceURL;
34  import javax.management.remote.rmi.RMIConnectorServer;
35  
36  import org.junit.Test;
37  
38  import static org.junit.Assert.assertTrue;
39  import static org.junit.Assert.fail;
40  
41  public class JmxAgentTestCase extends AbstractMuleContextTestCase
42  {
43      private static final String[] VALID_AUTH_TOKEN = {"mule", "mulepassword"};
44      private static final String DOMAIN = "JmxAgentTest";
45  
46      private JMXServiceURL serviceUrl;
47      private JmxAgent jmxAgent;
48  
49      @Override
50      protected void configureMuleContext(MuleContextBuilder contextBuilder)
51      {
52          super.configureMuleContext(contextBuilder);
53  
54          DefaultMuleConfiguration config = new DefaultMuleConfiguration();
55          config.setId(DOMAIN);
56          contextBuilder.setMuleConfiguration(config);
57      }
58  
59      @Override
60      protected void doSetUp() throws Exception
61      {
62          super.doSetUp();
63          serviceUrl = new JMXServiceURL(JmxAgent.DEFAULT_REMOTING_URI);
64          muleContext.getRegistry().registerAgent(new RmiRegistryAgent());
65          jmxAgent = muleContext.getRegistry().lookupObject(JmxAgent.class);
66          jmxAgent.setConnectorServerUrl(JmxAgent.DEFAULT_REMOTING_URI);
67      }
68  
69      @Override
70      protected void doTearDown()
71      {
72          if (jmxAgent != null)
73          {
74              jmxAgent.dispose();
75          }
76      }
77  
78      @Test
79      public void testDefaultProperties() throws Exception
80      {
81          jmxAgent.setCredentials(getValidCredentials());
82          muleContext.start();
83      }
84  
85      @Test
86      public void testSuccessfulRemoteConnection() throws Exception
87      {
88          configureProperties();
89          jmxAgent.setCredentials(getValidCredentials());
90          muleContext.start();
91  
92          JMXConnector connector = null;
93          try
94          {
95              Map<String, ?> props = Collections.singletonMap(JMXConnector.CREDENTIALS, VALID_AUTH_TOKEN);
96              connector = JMXConnectorFactory.connect(serviceUrl, props);
97              MBeanServerConnection connection = connector.getMBeanServerConnection();
98              // is it the right server?
99              assertTrue(Arrays.asList(connection.getDomains()).toString(),
100                     Arrays.asList(connection.getDomains()).contains("Mule." + DOMAIN));
101         }
102         finally
103         {
104             if (connector != null)
105             {
106                 connector.close();
107             }
108         }
109     }
110 
111     @Test
112     public void testNoCredentialsProvided() throws Exception
113     {
114         configureProperties();
115         jmxAgent.setCredentials(getValidCredentials());
116         muleContext.start();
117 
118         JMXConnector connector = null;
119         try
120         {
121             connector = JMXConnectorFactory.connect(serviceUrl);
122             fail("expected SecurityException");
123         }
124         catch (SecurityException e)
125         {
126             // expected
127         }
128         finally
129         {
130             if (connector != null)
131             {
132                 connector.close();
133             }
134         }
135     }
136 
137     @Test
138     public void testNonRestrictedAccess() throws Exception
139     {
140         configureProperties();
141         jmxAgent.setCredentials(null);
142         muleContext.start();
143 
144         JMXConnector connector = null;
145         try
146         {
147             connector = JMXConnectorFactory.connect(serviceUrl);
148             MBeanServerConnection connection = connector.getMBeanServerConnection();
149             // is it the right server?
150             assertTrue(Arrays.asList(connection.getDomains()).contains("Mule." + DOMAIN));
151         }
152         finally
153         {
154             if (connector != null)
155             {
156                 connector.close();
157             }
158         }
159     }
160 
161     protected Map<String, String> getValidCredentials()
162     {
163         final Map<String, String> credentials = new HashMap<String, String>(1);
164         credentials.put(VALID_AUTH_TOKEN[0], VALID_AUTH_TOKEN[1]);
165 
166         return credentials;
167     }
168 
169     protected void configureProperties()
170     {
171         // make multi-NIC dev box happy by sticking RMI clients to a single
172         // local ip address
173         Map<String, Object> props = new HashMap<String, Object>();
174         props.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE,
175                   new FixedHostRmiClientSocketFactory("127.0.0.1"));
176         jmxAgent.setConnectorServerProperties(props);
177     }
178 
179     @Test
180     public void testServiceNameContainsColon() throws Exception
181     {
182         // create a service with an invalid name. It is registered in the registry as side effect
183         // so the JmxAgent will pick it up while registring services
184         getTestService("invalid:service:name", EchoComponent.class);
185 
186         // when registering services, the one we just put into the registry will be exposed
187         // to the local MBean server, too. If a MalformedObjectNameException is thrown during
188         // this operation, this test will fail
189         TestJmxAgent agent = new TestJmxAgent();
190         agent.setMuleContext(muleContext);
191         agent.initialise();
192 
193         agent.registerServiceServices();
194     }
195 
196     private static class TestJmxAgent extends JmxAgent
197     {
198         /**
199          * Open up method for test access
200          */
201         @Override
202         public void registerServiceServices() throws NotCompliantMBeanException, MBeanRegistrationException, InstanceAlreadyExistsException, MalformedObjectNameException
203         {
204             super.registerServiceServices();
205         }
206     }
207 }