1
2
3
4
5
6
7
8
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 = (JmxAgent) muleContext.getRegistry().lookupObject(JmxAgent.class);
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.start();
68 }
69
70 public void testSuccessfulRemoteConnection() throws Exception
71 {
72 configureProperties();
73 jmxAgent.setCredentials(getValidCredentials());
74 muleContext.start();
75
76 JMXConnector connector = null;
77 try
78 {
79 Map props = Collections.singletonMap(JMXConnector.CREDENTIALS, VALID_AUTH_TOKEN);
80 connector = JMXConnectorFactory.connect(serviceUrl, props);
81 MBeanServerConnection connection = connector.getMBeanServerConnection();
82
83 assertTrue(Arrays.asList(connection.getDomains()).toString(),
84 Arrays.asList(connection.getDomains()).contains("Mule." + DOMAIN));
85 }
86 finally
87 {
88 if (connector != null)
89 {
90 connector.close();
91 }
92 }
93 }
94
95 public void testNoCredentialsProvided() throws Exception
96 {
97 configureProperties();
98 jmxAgent.setCredentials(getValidCredentials());
99 muleContext.start();
100
101 JMXConnector connector = null;
102 try
103 {
104 connector = JMXConnectorFactory.connect(serviceUrl);
105 fail("expected SecurityException");
106 }
107 catch (SecurityException e)
108 {
109
110 }
111 finally
112 {
113 if (connector != null)
114 {
115 connector.close();
116 }
117 }
118 }
119
120 public void testNonRestrictedAccess() throws Exception
121 {
122 configureProperties();
123 jmxAgent.setCredentials(null);
124 muleContext.start();
125
126 JMXConnector connector = null;
127 try
128 {
129 connector = JMXConnectorFactory.connect(serviceUrl);
130 MBeanServerConnection connection = connector.getMBeanServerConnection();
131
132 assertTrue(Arrays.asList(connection.getDomains()).contains("Mule." + DOMAIN));
133 }
134 finally
135 {
136 if (connector != null)
137 {
138 connector.close();
139 }
140 }
141 }
142
143 protected Map getValidCredentials()
144 {
145 final Map credentials = new HashMap(1);
146 credentials.put(VALID_AUTH_TOKEN[0], VALID_AUTH_TOKEN[1]);
147
148 return credentials;
149 }
150
151 protected void configureProperties()
152 {
153
154
155 Map props = new HashMap();
156 props.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE,
157 new FixedHostRmiClientSocketFactory("127.0.0.1"));
158 jmxAgent.setConnectorServerProperties(props);
159 }
160
161 }