23
23
import javax .management .remote .JMXConnectorServerFactory ;
24
24
import javax .management .remote .JMXServiceURL ;
25
25
26
+ import org .junit .Before ;
26
27
import org .junit .Test ;
27
28
import org .springframework .aop .support .AopUtils ;
28
29
import org .springframework .jmx .AbstractMBeanServerTests ;
29
30
import org .springframework .tests .Assume ;
30
31
import org .springframework .tests .TestGroup ;
32
+ import org .springframework .util .SocketUtils ;
31
33
32
34
import static org .junit .Assert .*;
33
35
37
39
*/
38
40
public class MBeanServerConnectionFactoryBeanTests extends AbstractMBeanServerTests {
39
41
40
- private static final String SERVICE_URL = "service:jmx:jmxmp://localhost:9878" ;
41
42
42
- private JMXServiceURL getServiceUrl () throws MalformedURLException {
43
- return new JMXServiceURL (SERVICE_URL );
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
+ System .out .println (port );
51
+ }
52
+
53
+
54
+ private JMXServiceURL getJMXServiceUrl () throws MalformedURLException {
55
+ return new JMXServiceURL (serviceUrl );
44
56
}
45
57
46
58
private JMXConnectorServer getConnectorServer () throws Exception {
47
- return JMXConnectorServerFactory .newJMXConnectorServer (getServiceUrl (), null , getServer ());
59
+ return JMXConnectorServerFactory .newJMXConnectorServer (getJMXServiceUrl (), null , getServer ());
48
60
}
49
61
62
+
50
63
@ Test
51
64
public void testTestValidConnection () throws Exception {
52
65
Assume .group (TestGroup .JMXMP );
@@ -55,7 +68,7 @@ public void testTestValidConnection() throws Exception {
55
68
56
69
try {
57
70
MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean ();
58
- bean .setServiceUrl (SERVICE_URL );
71
+ bean .setServiceUrl (serviceUrl );
59
72
bean .afterPropertiesSet ();
60
73
61
74
try {
@@ -87,7 +100,7 @@ public void testWithNoServiceUrl() throws Exception {
87
100
public void testTestWithLazyConnection () throws Exception {
88
101
Assume .group (TestGroup .JMXMP );
89
102
MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean ();
90
- bean .setServiceUrl (SERVICE_URL );
103
+ bean .setServiceUrl (serviceUrl );
91
104
bean .setConnectOnStartup (false );
92
105
bean .afterPropertiesSet ();
93
106
@@ -110,7 +123,7 @@ public void testTestWithLazyConnection() throws Exception {
110
123
@ Test
111
124
public void testWithLazyConnectionAndNoAccess () throws Exception {
112
125
MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean ();
113
- bean .setServiceUrl (SERVICE_URL );
126
+ bean .setServiceUrl (serviceUrl );
114
127
bean .setConnectOnStartup (false );
115
128
bean .afterPropertiesSet ();
116
129
0 commit comments