2323import javax .management .remote .JMXConnectorServerFactory ;
2424import javax .management .remote .JMXServiceURL ;
2525
26+ import org .junit .Before ;
2627import org .junit .Test ;
2728import org .springframework .aop .support .AopUtils ;
2829import org .springframework .jmx .AbstractMBeanServerTests ;
2930import org .springframework .tests .Assume ;
3031import org .springframework .tests .TestGroup ;
32+ import org .springframework .util .SocketUtils ;
3133
3234import static org .junit .Assert .*;
3335
3739 */
3840public class MBeanServerConnectionFactoryBeanTests extends AbstractMBeanServerTests {
3941
40- private static final String SERVICE_URL = "service:jmx:jmxmp://localhost:9878" ;
4142
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 );
4456 }
4557
4658 private JMXConnectorServer getConnectorServer () throws Exception {
47- return JMXConnectorServerFactory .newJMXConnectorServer (getServiceUrl (), null , getServer ());
59+ return JMXConnectorServerFactory .newJMXConnectorServer (getJMXServiceUrl (), null , getServer ());
4860 }
4961
62+
5063 @ Test
5164 public void testTestValidConnection () throws Exception {
5265 Assume .group (TestGroup .JMXMP );
@@ -55,7 +68,7 @@ public void testTestValidConnection() throws Exception {
5568
5669 try {
5770 MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean ();
58- bean .setServiceUrl (SERVICE_URL );
71+ bean .setServiceUrl (serviceUrl );
5972 bean .afterPropertiesSet ();
6073
6174 try {
@@ -87,7 +100,7 @@ public void testWithNoServiceUrl() throws Exception {
87100 public void testTestWithLazyConnection () throws Exception {
88101 Assume .group (TestGroup .JMXMP );
89102 MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean ();
90- bean .setServiceUrl (SERVICE_URL );
103+ bean .setServiceUrl (serviceUrl );
91104 bean .setConnectOnStartup (false );
92105 bean .afterPropertiesSet ();
93106
@@ -110,7 +123,7 @@ public void testTestWithLazyConnection() throws Exception {
110123 @ Test
111124 public void testWithLazyConnectionAndNoAccess () throws Exception {
112125 MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean ();
113- bean .setServiceUrl (SERVICE_URL );
126+ bean .setServiceUrl (serviceUrl );
114127 bean .setConnectOnStartup (false );
115128 bean .afterPropertiesSet ();
116129
0 commit comments