KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > server > MBeanDelegateTEST


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package test.compliance.server;
9
10 import javax.management.MBeanServer;
11 import javax.management.MBeanServerFactory;
12 import javax.management.Notification;
13 import javax.management.NotificationListener;
14 import javax.management.ObjectName;
15
16 import junit.framework.TestCase;
17 import test.compliance.server.support.Test;
18
19 /**
20  * Tests for the MBean server delegate.
21  *
22  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
23  *
24  * @version $Revision: 1.5 $
25  */

26 public class MBeanDelegateTEST extends TestCase
27 {
28
29    public MBeanDelegateTEST(String s)
30    {
31       super(s);
32    }
33    
34    class MyNotificationListener implements NotificationListener {
35
36       int notificationCount = 0;
37       
38       public void handleNotification(Notification notification, Object handback)
39       {
40          try
41          {
42             notificationCount++;
43          }
44          catch (Exception e)
45          {
46             fail("Unexpected error: " + e.toString());
47          }
48       }
49    }
50
51    public synchronized void testRegistrationAndUnregistrationNotification() throws Exception
52    {
53       MBeanServer server = MBeanServerFactory.createMBeanServer();
54       MyNotificationListener listener = new MyNotificationListener();
55       
56       server.addNotificationListener(
57             new ObjectName("JMImplementation:type=MBeanServerDelegate"),
58             listener, null, null
59       );
60     
61       // force registration notification
62
server.registerMBean(new Test(), new ObjectName("test:foo=bar"));
63     
64       // force unregistration notification
65
server.unregisterMBean(new ObjectName("test:foo=bar"));
66       
67       // wait for notif to arrive max 5 secs
68
for (int i = 0; i < 10; ++i)
69       {
70          wait(500);
71          
72          if (listener.notificationCount > 1)
73             break;
74       }
75       
76       assertTrue(listener.notificationCount == 2);
77    }
78
79 }
80
Popular Tags