View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author Rob Harrop
38   * @author Juergen Hoeller
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  				// perform simple MBean count test
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  			// expected
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 }