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.rmi;
010:
011: import java.io.File;
012: import java.io.Serializable;
013: import java.net.URL;
014: import java.util.Set;
015: import javax.management.Attribute;
016: import javax.management.MBeanServer;
017: import javax.management.MBeanServerConnection;
018: import javax.management.Notification;
019: import javax.management.NotificationFilter;
020: import javax.management.NotificationListener;
021: import javax.management.ObjectInstance;
022: import javax.management.ObjectName;
023: import javax.management.loading.PrivateMLet;
024: import javax.management.remote.JMXConnector;
025: import javax.management.remote.JMXConnectorFactory;
026: import javax.management.remote.JMXConnectorServer;
027: import javax.management.remote.JMXConnectorServerFactory;
028: import javax.management.remote.JMXServiceURL;
029:
030: import test.MX4JTestCase;
031: import test.javax.management.remote.support.Marshalling;
032: import test.javax.management.remote.support.Unknown;
033:
034: /**
035: * @version $Revision: 1.5 $
036: */
037: public class RMIMarshallingTest extends MX4JTestCase {
038: private MBeanServer server = null;
039: private MBeanServerConnection conn = null;
040: private JMXConnectorServer cntorServer = null;
041: private JMXConnector cntor = null;
042: private ObjectName mbeanName;
043: private ObjectName mbeanLoaderName;
044:
045: public RMIMarshallingTest(String s) {
046: super (s);
047: }
048:
049: public void setUp() throws Exception {
050: super .setUp();
051: // Create a classloader that sees only the MBean and its parameter classes (Unknown)
052: File mbeanJar = new File("dist/test/mx4j-tests.jar");
053: PrivateMLet mbeanLoader = new PrivateMLet(new URL[] { mbeanJar
054: .toURL() }, getClass().getClassLoader().getParent(),
055: false);
056: mbeanLoaderName = ObjectName.getInstance("marshal:type=mlet");
057:
058: server = newMBeanServer();
059: server.registerMBean(mbeanLoader, mbeanLoaderName);
060:
061: JMXServiceURL url = new JMXServiceURL("rmi", "localhost", 0);
062: cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(
063: url, null, server);
064: cntorServer.start();
065:
066: cntor = JMXConnectorFactory.connect(cntorServer.getAddress());
067: conn = cntor.getMBeanServerConnection();
068: mbeanName = ObjectName.getInstance("marshal:type=mbean");
069:
070: }
071:
072: public void tearDown() throws Exception {
073: if (cntor != null)
074: cntor.close();
075: if (cntorServer != null)
076: cntorServer.stop();
077: }
078:
079: protected static class MockNotificationListener implements
080: NotificationListener, Serializable {
081: long numberOfNotifications = 0;
082:
083: public void handleNotification(Notification notification,
084: Object handback) {
085: // System.out.println("[MockNotificationListener] Notification: "+notification+" Handback: "+handback);
086: assertEquals(notification.getSequenceNumber(),
087: numberOfNotifications);
088: assertEquals(notification.getType(), Unknown.class
089: .getName());
090: numberOfNotifications++;
091: synchronized (this ) {
092: this .notify();
093: }
094: }
095:
096: public synchronized void waitOnNotification(long timeout,
097: long sequence) throws InterruptedException {
098: long to;
099: if (timeout > 0)
100: to = System.currentTimeMillis() + timeout;
101: else
102: to = -1;
103: while (numberOfNotifications < sequence) // Check for missed notification
104: {
105: this .wait(timeout);
106: if (to > -1 && System.currentTimeMillis() >= to) // Check if waited for full timeout
107: {
108: Thread.currentThread().interrupt();
109: break;
110: }
111: }
112: }
113:
114: }
115:
116: protected static class MockNotificationFilter implements
117: NotificationFilter, Serializable {
118:
119: public boolean isNotificationEnabled(Notification notification) {
120: // System.out.println("[MockNotificationFilter] Notification: "+notification);
121: return true;
122: }
123:
124: }
125:
126: private void createMBean() throws Exception {
127: ObjectInstance inst = conn.createMBean(Marshalling.class
128: .getName(), mbeanName, new Object[] { new Unknown() },
129: new String[] { Unknown.class.getName() });
130: }
131:
132: public void testCreateMBean() throws Exception {
133: conn.createMBean(Marshalling.class.getName(), mbeanName);
134: checkRegistration();
135:
136: conn.createMBean(Marshalling.class.getName(), mbeanName,
137: new Object[] { new Unknown() },
138: new String[] { Unknown.class.getName() });
139: checkRegistration();
140: }
141:
142: private void checkRegistration() throws Exception {
143: // Check registrations
144: if (!conn.isRegistered(mbeanName))
145: fail();
146: if (!server.isRegistered(mbeanName))
147: fail();
148: conn.unregisterMBean(mbeanName);
149: }
150:
151: public void testInstanceOf() throws Exception {
152: createMBean();
153: // Check instanceof
154: if (!conn.isInstanceOf(mbeanName, Marshalling.class.getName()))
155: fail();
156: if (!server
157: .isInstanceOf(mbeanName, Marshalling.class.getName()))
158: fail();
159: }
160:
161: public void testInvocationUnknownReturn() throws Exception {
162: createMBean();
163: // Check invocation
164: Object returned = conn.invoke(mbeanName, "unknownReturnValue",
165: new Object[0], new String[0]);
166: if (!returned.getClass().getName().equals(
167: Unknown.class.getName()))
168: fail();
169: returned = server.invoke(mbeanName, "unknownReturnValue",
170: new Object[0], new String[0]);
171: if (!returned.getClass().getName().equals(
172: Unknown.class.getName()))
173: fail();
174: Object remoteUnk = conn.invoke(mbeanName, "unknownArgument",
175: new Object[] { new Unknown() },
176: new String[] { Unknown.class.getName() });
177: Object localUnk = server.invoke(mbeanName, "unknownArgument",
178: new Object[] { new Unknown() },
179: new String[] { Unknown.class.getName() });
180: assertEquals(remoteUnk, localUnk);
181: }
182:
183: public void testUnregisterMBean() throws Exception {
184: createMBean();
185: // Check unregistration
186: conn.unregisterMBean(mbeanName);
187: if (conn.isRegistered(mbeanName))
188: fail();
189: if (server.isRegistered(mbeanName))
190: fail();
191:
192: }
193:
194: public void testNotifications() throws Exception {
195: createMBean();
196: MockNotificationListener listener = new MockNotificationListener();
197: conn.addNotificationListener(mbeanName, listener,
198: new MockNotificationFilter(), new Object());
199:
200: Thread.sleep(1000L);
201: Attribute att = new Attribute("UnknownAttribute", new Unknown());
202:
203: conn.setAttribute(mbeanName, att);
204:
205: Thread.sleep(1000L);
206: // This triggers a notification
207: try {
208: listener.waitOnNotification(1000L, 1);
209: } catch (InterruptedException ignore) {
210: }
211:
212: assertEquals(1, listener.numberOfNotifications);
213:
214: conn.removeNotificationListener(mbeanName, listener);
215:
216: Thread.sleep(1000L);
217: conn.setAttribute(mbeanName, att);
218:
219: Thread.sleep(1000L);
220: // This triggers a notification
221: try {
222: listener.waitOnNotification(1000L, 2);
223: } catch (InterruptedException ignore) {
224: }
225:
226: assertEquals(1, listener.numberOfNotifications);
227: }
228:
229: public void testQuery() throws Exception {
230: createMBean();
231: ObjectName pattern = mbeanName;
232: ObjectName query = mbeanName;
233: Set beans = conn.queryMBeans(pattern, query);
234: Object[] set = beans.toArray();
235: assertEquals(1, set.length);
236: // System.out.println("set[0]: "+set[0]+" class: "+set[0].getClass());
237: ObjectInstance inst = (ObjectInstance) set[0];
238: assertTrue(inst.getClassName().equals(
239: Marshalling.class.getName()));
240:
241: beans = conn.queryNames(pattern, query);
242: set = beans.toArray();
243: assertEquals(1, set.length);
244: // System.out.println("set[0]: "+set[0]+" class: "+set[0].getClass());
245: ObjectName nm = (ObjectName) set[0];
246: assertTrue(nm.equals(mbeanName));
247: }
248:
249: public void testAttributes() throws Exception {
250: createMBean();
251: Unknown value = new Unknown();
252: Attribute att = new Attribute("UnknownAttribute", value);
253: conn.setAttribute(mbeanName, att);
254: Object returned = conn.getAttribute(mbeanName,
255: "UnknownAttribute");
256: assertTrue(returned.getClass().getName().equals(
257: Unknown.class.getName()));
258: }
259: }
|