KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jmx > compliance > server > MBeanDelegateTEST


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jmx.compliance.server;
23
24 import javax.management.MBeanServer JavaDoc;
25 import javax.management.MBeanServerFactory JavaDoc;
26 import javax.management.Notification JavaDoc;
27 import javax.management.NotificationListener JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29
30 import junit.framework.TestCase;
31
32 import org.jboss.test.jmx.compliance.server.support.Test;
33
34 /**
35  * Tests for the MBean server delegate.
36  *
37  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
38  *
39  * @version $Revision: 37459 $
40  */

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