KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21
22 import javax.management.InstanceNotFoundException JavaDoc;
23 import javax.management.MBeanServer JavaDoc;
24 import javax.management.MBeanServerConnection JavaDoc;
25 import javax.management.ObjectInstance JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import javax.management.remote.JMXConnector JavaDoc;
28 import javax.management.remote.JMXConnectorFactory JavaDoc;
29 import javax.management.remote.JMXServiceURL JavaDoc;
30
31 import org.springframework.core.JdkVersion;
32 import org.springframework.jmx.AbstractJmxTests;
33
34 /**
35  * @author Rob Harrop
36  */

37 public class ConnectorServerFactoryBeanTests extends AbstractJmxTests {
38
39     private static final String JavaDoc OBJECT_NAME = "spring:type=connector,name=test";
40
41     public void testStartupWithLocatedServer() throws Exception JavaDoc {
42         if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
43             // to avoid NoClassDefFoundError for JSSE
44
return;
45         }
46
47         ConnectorServerFactoryBean bean = new ConnectorServerFactoryBean();
48         bean.afterPropertiesSet();
49
50         try {
51             checkServerConnection(server);
52         }
53         finally {
54             bean.destroy();
55         }
56     }
57
58     public void testStartupWithSuppliedServer() throws Exception JavaDoc {
59         if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
60             // to avoid NoClassDefFoundError for JSSE
61
return;
62         }
63
64         ConnectorServerFactoryBean bean = new ConnectorServerFactoryBean();
65         bean.setServer(server);
66         bean.afterPropertiesSet();
67
68         try {
69             checkServerConnection(server);
70         }
71         finally {
72             bean.destroy();
73         }
74     }
75
76     public void testRegisterWithMBeanServer() throws Exception JavaDoc {
77         ConnectorServerFactoryBean bean = new ConnectorServerFactoryBean();
78         bean.setObjectName(OBJECT_NAME);
79         bean.afterPropertiesSet();
80
81         try {
82             // Try to get the connector bean.
83
ObjectInstance JavaDoc instance = server.getObjectInstance(ObjectName.getInstance(OBJECT_NAME));
84             assertNotNull("ObjectInstance should not be null", instance);
85         }
86         finally {
87             bean.destroy();
88         }
89     }
90
91     public void testNoRegisterWithMBeanServer() throws Exception JavaDoc {
92         ConnectorServerFactoryBean bean = new ConnectorServerFactoryBean();
93         bean.afterPropertiesSet();
94
95         try {
96             // Try to get the connector bean.
97
ObjectInstance JavaDoc instance = server.getObjectInstance(ObjectName.getInstance(OBJECT_NAME));
98             fail("Instance should not be found");
99         }
100         catch (InstanceNotFoundException JavaDoc ex) {
101             // expected
102
}
103         finally {
104             bean.destroy();
105         }
106     }
107
108     private void checkServerConnection(MBeanServer JavaDoc hostedServer) throws IOException JavaDoc, MalformedURLException JavaDoc {
109         // Try to connect using client.
110
JMXConnector JavaDoc connector = JMXConnectorFactory.connect(
111                 new JMXServiceURL JavaDoc(ConnectorServerFactoryBean.DEFAULT_SERVICE_URL));
112         assertNotNull("Client Connector should not be null", connector);
113
114         // Get the MBean server connection.
115
MBeanServerConnection JavaDoc connection = connector.getMBeanServerConnection();
116         assertNotNull("MBeanServerConnection should not be null", connection);
117
118         // Test for MBean server equality.
119
assertEquals("Registered MBean count should be the same",
120                 hostedServer.getMBeanCount(), connection.getMBeanCount());
121     }
122
123 }
124
Popular Tags