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;
010:
011: import java.io.File;
012: import java.net.URL;
013: import java.util.Arrays;
014: import java.util.Set;
015:
016: import javax.management.JMRuntimeException;
017: import javax.management.ListenerNotFoundException;
018: import javax.management.MBeanException;
019: import javax.management.MBeanServer;
020: import javax.management.MBeanServerDelegate;
021: import javax.management.MBeanServerFactory;
022: import javax.management.Notification;
023: import javax.management.NotificationBroadcasterSupport;
024: import javax.management.NotificationListener;
025: import javax.management.ObjectName;
026: import javax.management.RuntimeOperationsException;
027:
028: import test.MX4JTestCase;
029: import test.MutableInteger;
030: import test.javax.management.support.ClassLoaderSupport;
031: import test.javax.management.support.ComplianceSupport;
032: import test.javax.management.support.MBeanThrowingExceptions;
033: import test.javax.management.support.NotificationSupport;
034: import test.javax.management.support.PostRegistrationSupport;
035:
036: /**
037: * @version $Revision: 1.12 $
038: */
039: public class MBeanServerTest extends MX4JTestCase {
040: public MBeanServerTest(String s) {
041: super (s);
042: }
043:
044: public void testDefaultDomainConversion() throws Exception {
045: String domain = "test";
046: MBeanServer server = MBeanServerFactory.newMBeanServer(domain);
047:
048: // Every operation with default domain must match the one with the
049: // explicit domain
050:
051: ObjectName defaultName = new ObjectName(":key=value");
052: ObjectName explicitName = new ObjectName(server
053: .getDefaultDomain(), "key", "value");
054:
055: // A broadcaster mbean
056: MBeanServerDelegate broadcaster = new MBeanServerDelegate();
057:
058: // Register with the explicit object name
059: server.registerMBean(broadcaster, explicitName);
060:
061: // Query
062: Set set = server.queryNames(defaultName, null);
063: if (set.size() != 1) {
064: fail("Default domain not handled in ObjectNames");
065: }
066:
067: // Register and remove a listener
068: NotificationListener listener = new NotificationListener() {
069: public void handleNotification(Notification notification,
070: Object handback) {
071: }
072: };
073: server.addNotificationListener(defaultName, listener, null,
074: null);
075: // Remove throws if the listener is not found
076: server.removeNotificationListener(defaultName, listener);
077:
078: // Invoke operations
079: server.getAttribute(defaultName, "MBeanServerId");
080:
081: // Metadata
082: server.getMBeanInfo(defaultName);
083: server.getObjectInstance(defaultName);
084: if (!server.isRegistered(defaultName)) {
085: fail("Default domain not handled in ObjectNames");
086: }
087: server.isInstanceOf(defaultName,
088: "javax.management.MBeanServerDelegateMBean");
089: }
090:
091: public void testRegistrationOfJMImplementationDomain()
092: throws Exception {
093: MBeanServer server = MBeanServerFactory.newMBeanServer();
094:
095: // Test that MBeans with reserved object names cannot be registered
096: Object mbean = new ComplianceSupport.BasicStandard();
097: ObjectName reserved = new ObjectName(
098: "JMImplementation:simon=true");
099: try {
100: server.registerMBean(mbean, reserved);
101: fail("MBeans with reserved object names cannot be registered");
102: } catch (JMRuntimeException ignored) {
103: }
104: }
105:
106: public void testDeregistrationOfJMImplementationDomain()
107: throws Exception {
108: MBeanServer server = MBeanServerFactory.newMBeanServer();
109:
110: // Test that the delegate MBean cannot be unregistered
111: ObjectName delegate = new ObjectName(
112: "JMImplementation:type=MBeanServerDelegate");
113: try {
114: server.unregisterMBean(delegate);
115: fail("Delegate MBean cannot be unregistered");
116: } catch (RuntimeOperationsException ignored) {
117: }
118: }
119:
120: public void testDelegateID() throws Exception {
121: ObjectName delegate = new ObjectName(
122: "JMImplementation:type=MBeanServerDelegate");
123: MBeanServer server1 = MBeanServerFactory.newMBeanServer();
124: MBeanServer server2 = MBeanServerFactory.newMBeanServer();
125: String id1 = (String) server1.getAttribute(delegate,
126: "MBeanServerId");
127: String id2 = (String) server2.getAttribute(delegate,
128: "MBeanServerId");
129:
130: // Be sure they're different
131: if (id1.equals(id2))
132: fail("MBeanServer ID must differ");
133: }
134:
135: public void testAddRemoveListenerOnMultipleMBeans()
136: throws Exception {
137: MBeanServer server = MBeanServerFactory.newMBeanServer();
138:
139: ObjectName name1 = new ObjectName("domain:key=mbean1");
140: ObjectName name2 = new ObjectName("domain:key=mbean2");
141:
142: NotificationBroadcasterSupport mbean1 = new NotificationSupport.Emitter();
143: NotificationBroadcasterSupport mbean2 = new NotificationSupport.Emitter();
144:
145: server.registerMBean(mbean1, name1);
146: server.registerMBean(mbean2, name2);
147:
148: final MutableInteger integer = new MutableInteger(0);
149: NotificationListener listener = new NotificationListener() {
150: public void handleNotification(Notification notification,
151: Object handback) {
152: integer.set(integer.get() + 1);
153: }
154: };
155:
156: server.addNotificationListener(name1, listener, null, null);
157: server.addNotificationListener(name2, listener, null, null);
158:
159: Notification notification = new Notification("test", mbean1, 1);
160: mbean1.sendNotification(notification);
161:
162: // Be sure the listener is called
163: assertEquals("Listener is not called", integer.get(), 1);
164:
165: mbean2.sendNotification(notification);
166:
167: // Be sure the listener is called
168: assertEquals("Listener is not called", integer.get(), 2);
169:
170: // Remove one listener
171: server.removeNotificationListener(name1, listener);
172:
173: // Be sure it is not called
174: mbean1.sendNotification(notification);
175: assertEquals("Listener is called", integer.get(), 2);
176:
177: // Be sure it is called
178: mbean2.sendNotification(notification);
179: assertEquals("Listener is not called", integer.get(), 3);
180:
181: try {
182: server.removeNotificationListener(name1, listener);
183: fail("Listener has been removed");
184: } catch (ListenerNotFoundException ignored) {
185: }
186:
187: // Remove also the second listener
188: server.removeNotificationListener(name2, listener);
189:
190: // Be sure it is not called
191: mbean2.sendNotification(notification);
192: assertEquals("Listener is called", integer.get(), 3);
193: }
194:
195: public void testAddRemoveMixedListenerOnMultipleMBeans()
196: throws Exception {
197: MBeanServer server = MBeanServerFactory.newMBeanServer();
198:
199: ObjectName name1 = new ObjectName("domain:key=mbean1");
200: ObjectName name2 = new ObjectName("domain:key=mbean2");
201:
202: NotificationBroadcasterSupport mbean1 = new NotificationSupport.Emitter();
203: NotificationBroadcasterSupport mbean2 = new NotificationSupport.Emitter();
204:
205: server.registerMBean(mbean1, name1);
206: server.registerMBean(mbean2, name2);
207:
208: final MutableInteger integer = new MutableInteger(0);
209: NotificationListener listener = new NotificationListener() {
210: public void handleNotification(Notification notification,
211: Object handback) {
212: integer.set(integer.get() + 1);
213: }
214: };
215:
216: server.addNotificationListener(name1, listener, null, null);
217: server.addNotificationListener(name2, listener, null, null);
218: mbean2.addNotificationListener(listener, null, null);
219:
220: Notification notification = new Notification("test", mbean1, 1);
221: mbean1.sendNotification(notification);
222:
223: // Be sure the listener is called
224: assertEquals("Listener is not called", integer.get(), 1);
225:
226: mbean2.sendNotification(notification);
227:
228: // Be sure the listeners are called
229: assertEquals("Listeners are not called", integer.get(), 3);
230:
231: // Remove one listener
232: server.removeNotificationListener(name2, listener);
233:
234: // Be sure the listener is called
235: mbean2.sendNotification(notification);
236: assertEquals("Listener is not called", integer.get(), 4);
237:
238: // Be sure it is called
239: mbean1.sendNotification(notification);
240: assertEquals("Listener is not called", integer.get(), 5);
241:
242: server.removeNotificationListener(name1, listener);
243:
244: // Be sure it is not called
245: mbean1.sendNotification(notification);
246: assertEquals("Listener is called", integer.get(), 5);
247:
248: // Be sure it is called
249: mbean2.sendNotification(notification);
250: assertEquals("Listener is not called", integer.get(), 6);
251:
252: try {
253: server.removeNotificationListener(name2, listener);
254: fail("Listener has been removed");
255: } catch (ListenerNotFoundException ignored) {
256: }
257:
258: // Remove also the second listener
259: mbean2.removeNotificationListener(listener);
260:
261: // Be sure it is not called
262: mbean2.sendNotification(notification);
263: assertEquals("Listener is called", integer.get(), 6);
264: }
265:
266: public void testObjectInstanceOnPostRegister() throws Exception {
267: MBeanServer server = MBeanServerFactory.newMBeanServer();
268: PostRegistrationSupport mbean = new PostRegistrationSupport();
269: ObjectName name = new ObjectName(":mbean=postRegistration");
270: server.registerMBean(mbean, name);
271: }
272:
273: public void testGetDomains() throws Exception {
274: MBeanServer server = MBeanServerFactory.newMBeanServer();
275:
276: String[] domains = server.getDomains();
277: if (domains.length != 1)
278: fail("Fresh new MBeanServer contains MBeans not in the JMImplementation domain");
279: if (!"JMImplementation".equals(domains[0]))
280: fail("Fresh new MBeanServer contains MBeans not in the JMImplementation domain");
281:
282: Object mbean = new ComplianceSupport.BasicStandard();
283:
284: ObjectName name1 = new ObjectName("domain1", "mbean", "1");
285: server.registerMBean(mbean, name1);
286: domains = server.getDomains();
287: Arrays.sort(domains, null);
288:
289: if (domains.length != 2)
290: fail("New MBean domain is not present in getDomains()");
291: if (!"domain1".equals(domains[1]))
292: fail("New MBean domain is not present in getDomains()");
293:
294: ObjectName name2 = new ObjectName("domain1", "mbean", "2");
295: server.registerMBean(mbean, name2);
296: domains = server.getDomains();
297: Arrays.sort(domains, null);
298:
299: if (domains.length != 2)
300: fail("Existing MBean domain should not be duplicated in getDomains()");
301: if (!"domain1".equals(domains[1]))
302: fail("Existing MBean domain should not be duplicated in getDomains()");
303:
304: server.unregisterMBean(name2);
305: domains = server.getDomains();
306: Arrays.sort(domains, null);
307:
308: if (domains.length != 2)
309: fail("Unregistering still existing MBean domain should not be removed from getDomains()");
310: if (!"domain1".equals(domains[1]))
311: fail("Unregistering still existing MBean domain should not be removed from getDomains()");
312:
313: server.unregisterMBean(name1);
314: domains = server.getDomains();
315: Arrays.sort(domains, null);
316:
317: if (domains.length != 1)
318: fail("Unregistering MBean domain should be removed from getDomains()");
319: if (!"JMImplementation".equals(domains[0]))
320: fail("Unregistering MBean domain should be removed from getDomains()");
321: }
322:
323: public void testInstantiate() throws Exception {
324: MBeanServer server = newMBeanServer();
325:
326: String className = ComplianceSupport.BasicStandard.class
327: .getName();
328: Object mbean1 = server.instantiate(className, null,
329: new Object[0], new String[0]);
330:
331: // Register one classloader mbean
332: File file = new File("dist/test/mx4j-tests.jar");
333: ClassLoader parent = getClass().getClassLoader().getParent();
334: ClassLoaderSupport loader = new ClassLoaderSupport(
335: new URL[] { file.toURL() }, parent);
336: ObjectName loaderName = new ObjectName(":type=ClassLoader");
337: server.registerMBean(loader, loaderName);
338:
339: Object mbean2 = server.instantiate(className, loaderName,
340: new Object[0], new String[0]);
341:
342: // Now mbean1 should be of a different class from mbean2
343: if (mbean1.getClass().equals(mbean2.getClass()))
344: fail("MBean classes should be different");
345:
346: Object mbean3 = server.instantiate(className, new Object[0],
347: new String[0]);
348:
349: // Since JMX 1.2, the CLR has the cl of the MBeanServer in its classpath.
350: if (!mbean1.getClass().equals(mbean3.getClass()))
351: fail("MBean classes should be equal");
352:
353: server.unregisterMBean(loaderName);
354:
355: Object mbean4 = server.instantiate(className, new Object[0],
356: new String[0]);
357: if (!mbean1.getClass().equals(mbean4.getClass()))
358: fail("MBean classes should be equal");
359: }
360:
361: public void testWrapExceptionsThrownByMBeanMethods()
362: throws Exception {
363: MBeanServer server = newMBeanServer();
364:
365: MBeanThrowingExceptions mbean = new MBeanThrowingExceptions();
366: ObjectName objectName = ObjectName
367: .getInstance(":name=exceptions");
368: server.registerMBean(mbean, objectName);
369:
370: try {
371: server.invoke(objectName, "throwReflectionException", null,
372: null);
373: fail();
374: } catch (MBeanException x) {
375: }
376: }
377: }
|