001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package test.javax.management.remote;
010:
011: import java.io.IOException;
012: import java.lang.reflect.InvocationHandler;
013: import java.lang.reflect.Method;
014: import java.lang.reflect.Proxy;
015: import java.net.MalformedURLException;
016: import java.util.Map;
017: import javax.management.MBeanServer;
018: import javax.management.remote.JMXConnector;
019: import javax.management.remote.JMXConnectorFactory;
020: import javax.management.remote.JMXConnectorServer;
021: import javax.management.remote.JMXConnectorServerFactory;
022: import javax.management.remote.JMXServiceURL;
023: import javax.management.remote.MBeanServerForwarder;
024:
025: import test.MX4JTestCase;
026:
027: /**
028: * @version $Revision: 1.11 $
029: */
030: public abstract class JMXConnectorServerTestCase extends MX4JTestCase {
031: public JMXConnectorServerTestCase(String name) {
032: super (name);
033: }
034:
035: public abstract JMXServiceURL createJMXConnectorServerAddress()
036: throws MalformedURLException;
037:
038: public abstract Map getEnvironment();
039:
040: public void testNewJMXConnectorServerWithNullURL() throws Exception {
041: try {
042: JMXConnectorServerFactory.newJMXConnectorServer(null, null,
043: null);
044: fail();
045: } catch (NullPointerException x) {
046: }
047: }
048:
049: public void testNewJMXConnectorServerWithFactory() throws Exception {
050: JMXServiceURL url = createJMXConnectorServerAddress();
051: JMXConnectorServer server = JMXConnectorServerFactory
052: .newJMXConnectorServer(url, getEnvironment(), null);
053: if (server == null)
054: fail();
055: }
056:
057: public void testStartWithoutMBeanServer() throws Exception {
058: JMXServiceURL url = createJMXConnectorServerAddress();
059: JMXConnectorServer server = JMXConnectorServerFactory
060: .newJMXConnectorServer(url, getEnvironment(), null);
061: try {
062: server.start();
063: fail();
064: } catch (IllegalStateException x) {
065: }
066: }
067:
068: public void testMBeanServerForwarderNoMBeanServer()
069: throws Exception {
070: JMXServiceURL url = createJMXConnectorServerAddress();
071: JMXConnectorServer server = JMXConnectorServerFactory
072: .newJMXConnectorServer(url, getEnvironment(), null);
073: MBeanServerForwarder forwarder = (MBeanServerForwarder) Proxy
074: .newProxyInstance(getClass().getClassLoader(),
075: new Class[] { MBeanServerForwarder.class },
076: new InvocationHandler() {
077: public Object invoke(Object proxy,
078: Method method, Object[] args)
079: throws Throwable {
080: return null;
081: }
082: });
083:
084: try {
085: server.setMBeanServerForwarder(forwarder);
086: fail("No MBeanServer to forward to");
087: } catch (IllegalStateException x) {
088: }
089: }
090:
091: public void testMBeanServerForwarder() throws Exception {
092: JMXServiceURL url = createJMXConnectorServerAddress();
093: JMXConnectorServer server = JMXConnectorServerFactory
094: .newJMXConnectorServer(url, getEnvironment(),
095: newMBeanServer());
096: MBeanServerForwarder forwarder = (MBeanServerForwarder) Proxy
097: .newProxyInstance(getClass().getClassLoader(),
098: new Class[] { MBeanServerForwarder.class },
099: new InvocationHandler() {
100: public Object invoke(Object proxy,
101: Method method, Object[] args)
102: throws Throwable {
103: return null;
104: }
105: });
106:
107: server.setMBeanServerForwarder(forwarder);
108:
109: try {
110: server.start();
111: if (server.getMBeanServer() != forwarder)
112: fail();
113: } finally {
114: server.stop();
115: }
116: }
117:
118: public void testStart() throws Exception {
119: JMXConnectorServer cntorServer = null;
120: try {
121: JMXServiceURL url = createJMXConnectorServerAddress();
122: cntorServer = JMXConnectorServerFactory
123: .newJMXConnectorServer(url, getEnvironment(),
124: newMBeanServer());
125: cntorServer.start();
126: } finally {
127: if (cntorServer != null)
128: cntorServer.stop();
129: }
130: }
131:
132: public void testStartStart() throws Exception {
133: JMXConnectorServer cntorServer = null;
134: try {
135: JMXServiceURL url = createJMXConnectorServerAddress();
136: cntorServer = JMXConnectorServerFactory
137: .newJMXConnectorServer(url, getEnvironment(),
138: newMBeanServer());
139: cntorServer.start();
140: cntorServer.start();
141: } finally {
142: if (cntorServer != null)
143: cntorServer.stop();
144: }
145: }
146:
147: public void testStartStop() throws Exception {
148: JMXServiceURL url = createJMXConnectorServerAddress();
149: JMXConnectorServer cntorServer = JMXConnectorServerFactory
150: .newJMXConnectorServer(url, getEnvironment(),
151: newMBeanServer());
152: cntorServer.start();
153: cntorServer.stop();
154: }
155:
156: public void testStartStopStart() throws Exception {
157: JMXServiceURL url = createJMXConnectorServerAddress();
158: JMXConnectorServer cntorServer = JMXConnectorServerFactory
159: .newJMXConnectorServer(url, getEnvironment(),
160: newMBeanServer());
161: cntorServer.start();
162: cntorServer.stop();
163: try {
164: cntorServer.start();
165: fail();
166: } catch (IOException x) {
167: }
168: }
169:
170: public void testStartStopStop() throws Exception {
171: JMXServiceURL url = createJMXConnectorServerAddress();
172: JMXConnectorServer cntorServer = JMXConnectorServerFactory
173: .newJMXConnectorServer(url, getEnvironment(),
174: newMBeanServer());
175: cntorServer.start();
176: cntorServer.stop();
177: cntorServer.stop();
178: }
179:
180: public void testTwoConnectorServers() throws Exception {
181: JMXConnectorServer cntorServer1 = null;
182: JMXConnectorServer cntorServer2 = null;
183: JMXConnector cntor10 = null;
184: JMXConnector cntor11 = null;
185: JMXConnector cntor12 = null;
186: JMXConnector cntor20 = null;
187: try {
188: JMXServiceURL url1 = createJMXConnectorServerAddress();
189: JMXServiceURL url2 = new JMXServiceURL(url1.getProtocol(),
190: url1.getHost(), (url1.getPort() > 0) ? (url1
191: .getPort() + 1) : 0, url1.getURLPath());
192:
193: MBeanServer server = newMBeanServer();
194: cntorServer1 = JMXConnectorServerFactory
195: .newJMXConnectorServer(url1, getEnvironment(),
196: server);
197: cntorServer2 = JMXConnectorServerFactory
198: .newJMXConnectorServer(url2, getEnvironment(),
199: server);
200: cntorServer1.start();
201: cntorServer2.start();
202:
203: cntor10 = JMXConnectorFactory.connect(cntorServer1
204: .getAddress(), getEnvironment());
205: cntor11 = JMXConnectorFactory.connect(cntorServer1
206: .getAddress(), getEnvironment());
207: cntor12 = JMXConnectorFactory.connect(cntorServer1
208: .getAddress(), getEnvironment());
209: cntor20 = JMXConnectorFactory.connect(cntorServer2
210: .getAddress(), getEnvironment());
211:
212: if (cntor10.getConnectionId().equals(
213: cntor11.getConnectionId()))
214: fail();
215: if (cntor10.getConnectionId().equals(
216: cntor12.getConnectionId()))
217: fail();
218: if (cntor10.getConnectionId().equals(
219: cntor20.getConnectionId()))
220: fail();
221:
222: } finally {
223: if (cntor20 != null)
224: cntor20.close();
225: if (cntor12 != null)
226: cntor12.close();
227: if (cntor11 != null)
228: cntor11.close();
229: if (cntor10 != null)
230: cntor10.close();
231:
232: if (cntorServer2 != null)
233: cntorServer2.stop();
234: if (cntorServer1 != null)
235: cntorServer1.stop();
236: }
237: }
238:
239: public void testStartWithProviderClassLoader() throws Exception {
240: JMXConnectorServer cntorServer = null;
241: try {
242: JMXServiceURL url = createJMXConnectorServerAddress();
243: MBeanServer server = newMBeanServer();
244: Map serverEnv = getEnvironment();
245: serverEnv
246: .put(
247: JMXConnectorServerFactory.PROTOCOL_PROVIDER_CLASS_LOADER,
248: getClass().getClassLoader());
249: Thread.currentThread().setContextClassLoader(
250: getClass().getClassLoader().getParent());
251: cntorServer = JMXConnectorServerFactory
252: .newJMXConnectorServer(url, serverEnv, server);
253: cntorServer.start();
254: } finally {
255: if (cntorServer != null)
256: cntorServer.stop();
257: }
258: }
259: }
|