KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > MBeanRegistrationTest


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.javax.management;
10
11 import javax.management.*;
12 import javax.management.loading.MLet JavaDoc;
13
14 import test.MX4JTestCase;
15 import test.MutableBoolean;
16 import test.MutableObject;
17 import test.javax.management.support.RegistrationSupport;
18
19 /**
20  * @version $Revision: 1.13 $
21  */

22 public class MBeanRegistrationTest extends MX4JTestCase
23 {
24    public static interface BarMBean
25    {
26       int getBeer();
27
28       void getBEER();
29
30       int getBeer(String JavaDoc name);
31
32       String JavaDoc[] get();
33    }
34
35    public static class Bar implements BarMBean
36    {
37       public Bar()
38       {
39       }
40
41       public String JavaDoc[] get()
42       {
43          return new String JavaDoc[0];
44       }
45
46       public int getBeer()
47       {
48          return 0;
49       }
50
51       public void getBEER()
52       {
53          throw new java.lang.Error JavaDoc("No BEER here");
54       }
55
56       public int getBeer(String JavaDoc name)
57       {
58          return 0;
59       }
60
61    }
62
63    public MBeanRegistrationTest(String JavaDoc s)
64    {
65       super(s);
66    }
67
68    public void testNullObjectName() throws Exception JavaDoc
69    {
70       MBeanServer server = newMBeanServer();
71       int count = server.getMBeanCount().intValue();
72       Object JavaDoc nullObjectName = new RegistrationSupport.NullObjectName();
73       try
74       {
75          server.registerMBean(nullObjectName, null);
76          fail("MBean cannot be registered");
77       }
78       catch (RuntimeOperationsException ignored)
79       {
80       }
81       // Check that was not registered
82
if (server.getMBeanCount().intValue() != count)
83       {
84          fail("MBean with null ObjectName was registered");
85       }
86    }
87
88    public void testPreRegisterException() throws Exception JavaDoc
89    {
90       MBeanServer server = newMBeanServer();
91       int count = server.getMBeanCount().intValue();
92       Object JavaDoc preRegisterException = new RegistrationSupport.PreRegisterException();
93       try
94       {
95          server.registerMBean(preRegisterException, null);
96          fail("MBean cannot be registered");
97       }
98       catch (MBeanRegistrationException ignored)
99       {
100       }
101       // Check that was not registered
102
if (server.getMBeanCount().intValue() != count)
103       {
104          fail("MBean threw exception in preRegister, but was registered");
105       }
106    }
107
108    public void testPostRegisterException() throws Exception JavaDoc
109    {
110       MBeanServer server = newMBeanServer();
111       int count = server.getMBeanCount().intValue();
112       Object JavaDoc postRegisterException = new RegistrationSupport.PostRegisterException();
113       ObjectName name = new ObjectName(":test=postRegister");
114       try
115       {
116          server.registerMBean(postRegisterException, name);
117          fail("MBean must throw an exception");
118       }
119       catch (RuntimeMBeanException ignored)
120       {
121       }
122       // Check that was registered
123
if (server.getMBeanCount().intValue() != count + 1)
124       {
125          fail("MBean threw exception in postRegister, but was NOT registered");
126       }
127    }
128
129    public void testPreDeregisterException() throws Exception JavaDoc
130    {
131       MBeanServer server = newMBeanServer();
132       int count = server.getMBeanCount().intValue();
133       Object JavaDoc preDeregisterException = new RegistrationSupport.PreDeregisterException();
134       ObjectName name = new ObjectName("simon:mbean=test");
135       server.registerMBean(preDeregisterException, name);
136       if (server.getMBeanCount().intValue() != count + 1)
137       {
138          fail("MBean was not registered");
139       }
140       try
141       {
142          server.unregisterMBean(name);
143          fail("MBean cannot be unregistered");
144       }
145       catch (MBeanRegistrationException ignored)
146       {
147       }
148       if (server.getMBeanCount().intValue() != count + 1)
149       {
150          fail("MBean was unregistered");
151       }
152    }
153
154    public void testPostDeregisterException() throws Exception JavaDoc
155    {
156       MBeanServer server = newMBeanServer();
157       int count = server.getMBeanCount().intValue();
158       Object JavaDoc postDeregisterException = new RegistrationSupport.PostDeregisterException();
159       ObjectName name = new ObjectName("simon:mbean=test");
160       server.registerMBean(postDeregisterException, name);
161       if (server.getMBeanCount().intValue() != count + 1)
162       {
163          fail("MBean was not registered");
164       }
165       try
166       {
167          server.unregisterMBean(name);
168          fail("MBean must throw an exception");
169       }
170       catch (RuntimeMBeanException ignored)
171       {
172       }
173       if (server.getMBeanCount().intValue() != count)
174       {
175          fail("MBean was NOT unregistered");
176       }
177    }
178
179    public void testRegistration() throws Exception JavaDoc
180    {
181       MBeanServer server = newMBeanServer();
182       int count = server.getMBeanCount().intValue();
183       final MutableBoolean bool1 = new MutableBoolean(false);
184       final MutableBoolean bool2 = new MutableBoolean(false);
185       Object JavaDoc empty = new RegistrationSupport.Empty(bool1, bool2);
186       final ObjectName name = new ObjectName("simon:mbean=empty");
187       server.registerMBean(empty, name);
188       // Check registration
189
if (!bool1.get())
190       {
191          fail("postRegister called with wrong argument value for successful registration");
192       }
193       if (server.getMBeanCount().intValue() != count + 1)
194       {
195          fail("MBean was not registered");
196       }
197    }
198
199    public void testDuplicateRegistration() throws Exception JavaDoc
200    {
201       MBeanServer server = newMBeanServer();
202       int count = server.getMBeanCount().intValue();
203       final MutableBoolean bool1 = new MutableBoolean(false);
204       final MutableBoolean bool2 = new MutableBoolean(false);
205       Object JavaDoc empty = new RegistrationSupport.Empty(bool1, bool2);
206       final ObjectName name = new ObjectName("simon:mbean=empty");
207       server.registerMBean(empty, name);
208       if (server.getMBeanCount().intValue() != count + 1)
209       {
210          fail("MBean was not registered");
211       }
212
213       Object JavaDoc duplicate = new RegistrationSupport.EmptyDuplicate(name, bool1);
214       try
215       {
216          server.registerMBean(duplicate, null);
217          fail("MBean with same name cannot be registered");
218       }
219       catch (InstanceAlreadyExistsException ignored)
220       {
221       }
222       // Check that postRegister was called correctly
223
if (bool1.get())
224       {
225          fail("postRegister called with wrong argument value for unsuccessful registration");
226       }
227       if (server.getMBeanCount().intValue() != count + 1)
228       {
229          fail("MBean was registered, and it shouldn't");
230       }
231    }
232
233    public void testDeregistration() throws Exception JavaDoc
234    {
235       MBeanServer server = newMBeanServer();
236       int count = server.getMBeanCount().intValue();
237       final MutableBoolean bool1 = new MutableBoolean(false);
238       final MutableBoolean bool2 = new MutableBoolean(false);
239       Object JavaDoc empty = new RegistrationSupport.Empty(bool1, bool2);
240       final ObjectName name = new ObjectName("simon:mbean=empty");
241       server.registerMBean(empty, name);
242       if (server.getMBeanCount().intValue() != count + 1)
243       {
244          fail("MBean was not registered");
245       }
246
247       bool1.set(true);
248       bool2.set(true);
249       server.unregisterMBean(name);
250       if (server.getMBeanCount().intValue() != count)
251       {
252          fail("MBean was not unregistered");
253       }
254       if (bool1.get() || bool2.get())
255       {
256          fail("preDeregister or postDeregister are not called");
257       }
258    }
259
260    public void testDuplicateDeregistration() throws Exception JavaDoc
261    {
262       MBeanServer server = newMBeanServer();
263       int count = server.getMBeanCount().intValue();
264       final MutableBoolean bool1 = new MutableBoolean(false);
265       final MutableBoolean bool2 = new MutableBoolean(false);
266       Object JavaDoc empty = new RegistrationSupport.Empty(bool1, bool2);
267       final ObjectName name = new ObjectName("simon:mbean=empty");
268       server.registerMBean(empty, name);
269       if (server.getMBeanCount().intValue() != count + 1)
270       {
271          fail("MBean was not registered");
272       }
273
274       bool1.set(true);
275       bool2.set(true);
276       server.unregisterMBean(name);
277       if (server.getMBeanCount().intValue() != count)
278       {
279          fail("MBean was not unregistered");
280       }
281       if (bool1.get() || bool2.get())
282       {
283          fail("preDeregister or postDeregister are not called");
284       }
285
286       // Try again
287
try
288       {
289          server.unregisterMBean(name);
290          fail("Already unregistered MBean can be unregistered");
291       }
292       catch (InstanceNotFoundException ignored)
293       {
294       }
295    }
296
297    public void testNotificationDuringRegistrationForStdMBean() throws Exception JavaDoc
298    {
299       final MBeanServer server = newMBeanServer();
300       Object JavaDoc mbean = new RegistrationSupport.Std();
301       final ObjectName name = new ObjectName(":mbean=std");
302       server.addNotificationListener(new ObjectName("JMImplementation:type=MBeanServerDelegate"), new NotificationListener()
303       {
304          public void handleNotification(Notification notification, Object JavaDoc handback)
305          {
306             invokeOperationsDuringRegistration(server, name, notification);
307          }
308       }, null, null);
309
310       server.registerMBean(mbean, name);
311    }
312
313    public void testNotificationDuringRegistrationForDynMBean() throws Exception JavaDoc
314    {
315       final MBeanServer server = newMBeanServer();
316       Object JavaDoc mbean = new RegistrationSupport.Dyn();
317       final ObjectName name = new ObjectName(":mbean=dyn");
318       server.addNotificationListener(new ObjectName("JMImplementation:type=MBeanServerDelegate"), new NotificationListener()
319       {
320          public void handleNotification(Notification notification, Object JavaDoc handback)
321          {
322             invokeOperationsDuringRegistration(server, name, notification);
323          }
324       }, null, null);
325
326       server.registerMBean(mbean, name);
327    }
328
329    private void invokeOperationsDuringRegistration(MBeanServer server, ObjectName name, Notification notification)
330    {
331       if (notification != null)
332       {
333          MBeanServerNotification notif = (MBeanServerNotification)notification;
334          ObjectName registered = notif.getMBeanName();
335          if (!registered.equals(name)) fail("Notification for the wrong MBean: " + registered + ", should be " + name);
336          if (!MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(notif.getType())) fail("Expecting a registration notification");
337       }
338
339       try
340       {
341          MBeanInfo info = server.getMBeanInfo(name);
342          if (info.getClassName() == null) fail("MBeanInfo not initialized correctly");
343          if (info.getOperations().length == 0) fail("MBeanInfo not initialized correctly");
344
345          ObjectInstance instance = server.getObjectInstance(name);
346          if (instance == null) fail("ObjectInstance should be already initialized");
347
348          boolean isRegistered = server.isRegistered(name);
349          if (!isRegistered) fail("MBean is registered");
350
351          // Must be able to invoke it with no exceptions
352
server.invoke(name, RegistrationSupport.StdMBean.class.getMethods()[0].getName(), null, null);
353       }
354       catch (Exception JavaDoc x)
355       {
356          fail("MBean metadata structures are not yet ready, but they should be: " + x);
357       }
358    }
359
360    public void testInvokeMBeanServerOperationsInCallbacks() throws Exception JavaDoc
361    {
362       MBeanServer server = newMBeanServer();
363       Object JavaDoc mbean = new InvokeDuringCallbacks();
364       ObjectName name = ObjectName.getInstance(":name=invoke");
365       server.registerMBean(mbean, name);
366       server.unregisterMBean(name);
367    }
368
369
370    public interface InvokeDuringCallbacksMBean
371    {
372       public void method();
373    }
374
375    public class InvokeDuringCallbacks implements InvokeDuringCallbacksMBean, MBeanRegistration
376    {
377       private MBeanServer server;
378       private ObjectName name;
379
380       public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception JavaDoc
381       {
382          this.server = server;
383          this.name = name;
384          return name;
385       }
386
387       public void postRegister(Boolean JavaDoc registrationDone)
388       {
389          invokeOperationsDuringRegistration(server, name, null);
390       }
391
392       public void preDeregister() throws Exception JavaDoc
393       {
394          invokeOperationsDuringRegistration(server, name, null);
395       }
396
397       public void postDeregister()
398       {
399       }
400
401       public void method()
402       {
403       }
404    }
405
406    public void testDistinguishAttributesOperations() throws Exception JavaDoc
407    {
408       MBeanServer server = newMBeanServer();
409       ObjectName objname = new ObjectName("tests:id=distinguishAttributesOperations");
410       Bar b = new Bar();
411       server.registerMBean(b, objname);
412       MBeanInfo info = server.getMBeanInfo(objname);
413       assertTrue("Expecting one attribute", info.getAttributes().length == 1);
414       try
415       {
416          assertTrue("No 'Beer' attribute", ((Integer JavaDoc)server.getAttribute(objname, "Beer")).intValue() == 0);
417          String JavaDoc[] getresult = (String JavaDoc[])server.invoke(objname, "get", new Object JavaDoc[0], new String JavaDoc[0]);
418          assertTrue("Expecting zero length result", getresult.length == 0);
419          server.getAttribute(objname, "BEER");
420          fail("Expecting AttributeNotFoundException");
421       }
422       catch (AttributeNotFoundException x)
423       {
424          assertTrue(true);
425       }
426       assertTrue("Expecting three operations", info.getOperations().length == 3);
427    }
428
429    public void testListenerRegistrationUnregistrationDuringCallbacks() throws Exception JavaDoc
430    {
431       MBeanServer server = newMBeanServer();
432       MutableObject holder = new MutableObject(null);
433       Object JavaDoc mbean = new RegistrationSupport.ListenerRegistrar(holder);
434       ObjectName name = ObjectName.getInstance("test:type=notifications");
435       server.registerMBean(mbean, name);
436
437       // Register a new MBean, the holder must be notified
438
ObjectName mlet = ObjectName.getInstance("test:type=mlet");
439       server.createMBean(MLet JavaDoc.class.getName(), mlet, null);
440
441       Notification notification = (Notification)holder.get();
442       assertNotNull(notification);
443       assertEquals(notification.getType(), MBeanServerNotification.REGISTRATION_NOTIFICATION);
444       holder.set(null);
445
446       server.unregisterMBean(mlet);
447
448       notification = (Notification)holder.get();
449       assertNotNull(notification);
450       assertEquals(notification.getType(), MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
451       holder.set(null);
452
453       // Unregisters also the listeners (in postDeregister)
454
server.unregisterMBean(name);
455       notification = (Notification)holder.get();
456       assertNotNull(notification);
457       assertEquals(notification.getType(), MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
458       holder.set(null);
459
460       server.createMBean(MLet JavaDoc.class.getName(), mlet, null);
461       notification = (Notification)holder.get();
462       assertNull(notification);
463
464       server.unregisterMBean(mlet);
465       notification = (Notification)holder.get();
466       assertNull(notification);
467    }
468
469    public void testAbstractClass() throws Exception JavaDoc
470    {
471       MBeanServer server = newMBeanServer();
472       try {
473          server.createMBean(Foo.class.getName(), null);
474          fail();
475       }
476       catch (NotCompliantMBeanException e)
477       {
478          // ok
479
}
480       catch (Exception JavaDoc e)
481       {
482          e.printStackTrace();
483          fail();
484       }
485    }
486
487    public static interface FooMBean
488    {
489       void something();
490    }
491
492    public static abstract class Foo implements FooMBean
493    {
494       public void something() {
495       }
496    }
497 }
498
Popular Tags