KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > core > notification > NotificationTest


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

7 package test.compliance.core.notification;
8
9 import java.rmi.registry.Registry JavaDoc;
10 import java.rmi.registry.LocateRegistry JavaDoc;
11 import java.rmi.RemoteException JavaDoc;
12 import javax.management.MBeanServerConnection JavaDoc;
13 import javax.management.MalformedObjectNameException JavaDoc;
14 import javax.management.ObjectName JavaDoc;
15 import javax.management.MBeanServer JavaDoc;
16 import javax.management.MBeanServerFactory JavaDoc;
17 import javax.management.ListenerNotFoundException JavaDoc;
18 import javax.management.remote.JMXConnector JavaDoc;
19 import javax.management.remote.JMXConnectorFactory JavaDoc;
20 import javax.management.remote.JMXServiceURL JavaDoc;
21 import javax.management.remote.JMXConnectorServer JavaDoc;
22 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
23
24 import junit.framework.TestCase;
25
26 /**
27  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
28  */

29 public class NotificationTest extends TestCase
30 {
31    private JMXConnector JavaDoc connector;
32    private JMXConnectorServer JavaDoc connectorServer;
33    private MBeanServerConnection JavaDoc server;
34    private int registryPort = 1090;
35    private String JavaDoc jndiPath = "/jmxconnector";
36
37    public void setUp() throws Exception JavaDoc
38    {
39       MBeanServer JavaDoc mbeanServer = MBeanServerFactory.createMBeanServer();
40       InvokerTest test = new InvokerTest();
41       mbeanServer.registerMBean(test, getObjectName());
42
43       // check to see if registry already created
44
Registry JavaDoc rmiRegistry = LocateRegistry.getRegistry(registryPort);
45       if(rmiRegistry != null)
46       {
47          try
48          {
49             rmiRegistry.list();
50          }
51          catch(RemoteException JavaDoc e)
52          {
53             rmiRegistry = LocateRegistry.createRegistry(registryPort);
54          }
55       }
56       else
57       {
58          rmiRegistry = LocateRegistry.createRegistry(registryPort);
59       }
60       JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("service:jmx:rmi://localhost/jndi/rmi://localhost:" + registryPort + jndiPath);
61
62       // create new connector server and start it
63
connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);
64       connectorServer.start();
65
66       connector = JMXConnectorFactory.connect(url);
67       server = connector.getMBeanServerConnection();
68
69    }
70
71    public void tearDown() throws Exception JavaDoc
72    {
73       if(connector != null)
74       {
75          connector.close();
76       }
77       if(connectorServer != null)
78       {
79          connectorServer.stop();
80       }
81    }
82
83    private ObjectName JavaDoc getObjectName() throws MalformedObjectNameException JavaDoc
84    {
85       return new ObjectName JavaDoc("test:type=InvokerTest");
86    }
87
88    public void testNotification() throws Exception JavaDoc
89    {
90       Listener listener = new Listener(10);
91       server.addNotificationListener(getObjectName(), listener, new RunTimerFilter(), "runTimer");
92       synchronized(listener)
93       {
94          listener.wait(15000);
95       }
96       server.removeNotificationListener(getObjectName(), listener);
97       int count = listener.getCount();
98       assertTrue("Received 10 notifications, count=" + count, count == 10);
99    }
100
101    /**
102     * Test the remoting of JMX Notifications with a valid listener
103     * and a bad listener that attempts to hang the service by sleeping
104     * in the notification callback.
105     *
106     * @throws Exception
107     */

108    public void testNotificationWithBadListener() throws Exception JavaDoc
109    {
110       // Add a bad listener
111
BadListener badListener = new BadListener();
112       server.addNotificationListener(getObjectName(), badListener, null, "BadListener");
113       Listener listener = new Listener(10);
114       // Add a good listener
115
server.addNotificationListener(getObjectName(), listener, new RunTimerFilter(), "runTimer");
116       // Wait 25 seconds for the good listener events to complete
117
synchronized(listener)
118       {
119          listener.wait(25000);
120       }
121       server.removeNotificationListener(getObjectName(), listener);
122       int count = listener.getCount();
123       assertTrue("Received 10 notifications from Listener, count=" + count,
124                  count == 10);
125       count = badListener.getCount();
126       assertTrue("Received >= 1 notifications from BadListener, count=" + count,
127                  count >= 1);
128       try
129       {
130          server.removeNotificationListener(getObjectName(), badListener);
131       }
132       catch(ListenerNotFoundException JavaDoc e)
133       {
134          System.out.println("The BadListener was not found");
135       }
136    }
137 }
Popular Tags