1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.jmx.support;
18
19 import java.net.MalformedURLException;
20 import javax.management.MBeanServerConnection;
21 import javax.management.remote.JMXConnectorServer;
22 import javax.management.remote.JMXConnectorServerFactory;
23 import javax.management.remote.JMXServiceURL;
24
25 import org.junit.Before;
26 import org.junit.Test;
27
28 import org.springframework.aop.support.AopUtils;
29 import org.springframework.jmx.AbstractMBeanServerTests;
30 import org.springframework.tests.Assume;
31 import org.springframework.tests.TestGroup;
32 import org.springframework.util.SocketUtils;
33
34 import static org.junit.Assert.*;
35
36
37
38
39
40 public class MBeanServerConnectionFactoryBeanTests extends AbstractMBeanServerTests {
41
42
43 private String serviceUrl;
44
45
46 @Before
47 public void getUrl() {
48 int port = SocketUtils.findAvailableTcpPort(9800, 9900);
49 this.serviceUrl = "service:jmx:jmxmp://localhost:" + port;
50 }
51
52
53 private JMXServiceURL getJMXServiceUrl() throws MalformedURLException {
54 return new JMXServiceURL(serviceUrl);
55 }
56
57 private JMXConnectorServer getConnectorServer() throws Exception {
58 return JMXConnectorServerFactory.newJMXConnectorServer(getJMXServiceUrl(), null, getServer());
59 }
60
61
62 @Test
63 public void testTestValidConnection() throws Exception {
64 Assume.group(TestGroup.JMXMP);
65 JMXConnectorServer connectorServer = getConnectorServer();
66 connectorServer.start();
67
68 try {
69 MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
70 bean.setServiceUrl(serviceUrl);
71 bean.afterPropertiesSet();
72
73 try {
74 MBeanServerConnection connection = bean.getObject();
75 assertNotNull("Connection should not be null", connection);
76
77
78 assertEquals("MBean count should be the same", getServer().getMBeanCount(), connection.getMBeanCount());
79 } finally {
80 bean.destroy();
81 }
82 } finally {
83 connectorServer.stop();
84 }
85 }
86
87 @Test
88 public void testWithNoServiceUrl() throws Exception {
89 MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
90 try {
91 bean.afterPropertiesSet();
92 fail("IllegalArgumentException should be raised when no service url is provided");
93 } catch (IllegalArgumentException ex) {
94
95 }
96 }
97
98 @Test
99 public void testTestWithLazyConnection() throws Exception {
100 Assume.group(TestGroup.JMXMP);
101 MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
102 bean.setServiceUrl(serviceUrl);
103 bean.setConnectOnStartup(false);
104 bean.afterPropertiesSet();
105
106 MBeanServerConnection connection = bean.getObject();
107 assertTrue(AopUtils.isAopProxy(connection));
108
109 JMXConnectorServer connector = null;
110 try {
111 connector = getConnectorServer();
112 connector.start();
113 assertEquals("Incorrect MBean count", getServer().getMBeanCount(), connection.getMBeanCount());
114 } finally {
115 bean.destroy();
116 if (connector != null) {
117 connector.stop();
118 }
119 }
120 }
121
122 @Test
123 public void testWithLazyConnectionAndNoAccess() throws Exception {
124 MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
125 bean.setServiceUrl(serviceUrl);
126 bean.setConnectOnStartup(false);
127 bean.afterPropertiesSet();
128
129 MBeanServerConnection connection = bean.getObject();
130 assertTrue(AopUtils.isAopProxy(connection));
131 bean.destroy();
132 }
133
134 }