KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jmx > support > MBeanServerConnectionFactoryBeanTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16
17 package org.springframework.jmx.support;
18
19 import java.net.MalformedURLException JavaDoc;
20
21 import javax.management.MBeanServerConnection JavaDoc;
22 import javax.management.remote.JMXConnectorServer JavaDoc;
23 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
24 import javax.management.remote.JMXServiceURL JavaDoc;
25
26 import org.springframework.core.JdkVersion;
27 import org.springframework.jmx.AbstractJmxTests;
28
29 /**
30  * @author Rob Harrop
31  */

32 public class MBeanServerConnectionFactoryBeanTests extends AbstractJmxTests {
33
34     private static final String JavaDoc SERVICE_URL = "service:jmx:jmxmp://localhost:9876";
35
36     private JMXServiceURL JavaDoc getServiceUrl() throws MalformedURLException JavaDoc {
37         return new JMXServiceURL JavaDoc(SERVICE_URL);
38     }
39
40     private JMXConnectorServer JavaDoc getConnectorServer() throws Exception JavaDoc {
41         return JMXConnectorServerFactory.newJMXConnectorServer(getServiceUrl(), null, server);
42     }
43
44     public void testValidConnection() throws Exception JavaDoc {
45         if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
46             // to avoid NoClassDefFoundError for JSSE
47
return;
48         }
49
50         JMXConnectorServer JavaDoc connectorServer = getConnectorServer();
51         connectorServer.start();
52
53         MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
54         bean.setServiceUrl(SERVICE_URL);
55         bean.afterPropertiesSet();
56
57         MBeanServerConnection JavaDoc connection = (MBeanServerConnection JavaDoc) bean.getObject();
58         assertNotNull("Connection should not be null", connection);
59
60         // perform simple mbean count test
61
assertEquals("MBean count should be the same", server.getMBeanCount(), connection.getMBeanCount());
62     }
63
64     public void testWithNoServiceUrl() throws Exception JavaDoc {
65         MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
66         try {
67             bean.afterPropertiesSet();
68             fail("IllegalArgumentException should be raised when no service url is provided");
69         }
70         catch (IllegalArgumentException JavaDoc ex) {
71             // expected
72
}
73     }
74
75 }
76
Popular Tags