View Javadoc

1   /*
2    * $Id: JmxAgentAuthenticationTestCase.java 22450 2011-07-19 08:20:41Z 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.config;
12  
13  import org.mule.tck.junit4.FunctionalTestCase;
14  import org.mule.tck.junit4.rule.DynamicPort;
15  
16  import java.io.IOException;
17  import java.net.MalformedURLException;
18  import java.util.Collections;
19  import java.util.Map;
20  
21  import javax.management.MBeanServerConnection;
22  import javax.management.ObjectInstance;
23  import javax.management.ObjectName;
24  import javax.management.remote.JMXConnector;
25  import javax.management.remote.JMXConnectorFactory;
26  import javax.management.remote.JMXServiceURL;
27  
28  import org.junit.Rule;
29  import org.junit.Test;
30  
31  import static org.junit.Assert.assertNotNull;
32  
33  public class JmxAgentAuthenticationTestCase extends FunctionalTestCase
34  {
35  
36      @Rule
37      public DynamicPort dynamicPort = new DynamicPort("port1");
38  
39      @Override
40      protected String getConfigResources()
41      {
42          return "jmx-authentication-config.xml";
43      }
44  
45      @Test(expected = SecurityException.class)
46      public void testAccessJmxServerWithoutCredentials() throws Exception
47      {
48          JMXServiceURL serviceUrl = createServiceUrl();
49          JMXConnectorFactory.connect(serviceUrl);
50      }
51  
52      @Test
53      public void testAccessJmxServerWithValidCredentials() throws Exception
54      {
55          JMXConnector connector = connectToJmx("jsmith", "foo");
56  
57          // to test the connection, access an MBean that is present by default
58          MBeanServerConnection connection = connector.getMBeanServerConnection();
59          ObjectName name = new ObjectName("java.lang:type=Runtime");
60          ObjectInstance instance = connection.getObjectInstance(name);
61          assertNotNull(instance);
62      }
63  
64      @Test(expected = SecurityException.class)
65      public void testAccessJmxServerWithInvalidCredentials() throws Exception
66      {
67          connectToJmx("invalid", "user");
68      }
69  
70      private JMXConnector connectToJmx(String user, String password) throws IOException
71      {
72          JMXServiceURL serviceUrl = createServiceUrl();
73  
74          String[] authToken = new String[] {user, password};
75          Map<String, ?> environment = Collections.singletonMap(JMXConnector.CREDENTIALS, authToken);
76  
77          return JMXConnectorFactory.connect(serviceUrl, environment);
78      }
79  
80      private JMXServiceURL createServiceUrl() throws MalformedURLException
81      {
82          String url = String.format("service:jmx:rmi:///jndi/rmi://localhost:%d/server",
83                                     dynamicPort.getNumber());
84          return new JMXServiceURL(url);
85      }
86  }