KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > core > notification > connection > ConnectionNotificationTest


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.connection;
8
9 import java.rmi.registry.LocateRegistry JavaDoc;
10 import java.rmi.registry.Registry JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
13 import javax.management.MBeanServer JavaDoc;
14 import javax.management.MBeanServerFactory JavaDoc;
15 import javax.management.Notification JavaDoc;
16 import javax.management.NotificationListener JavaDoc;
17 import javax.management.remote.JMXConnector JavaDoc;
18 import javax.management.remote.JMXConnectorFactory JavaDoc;
19 import javax.management.remote.JMXConnectorServer JavaDoc;
20 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
21 import javax.management.remote.JMXServiceURL JavaDoc;
22
23 import junit.framework.TestCase;
24
25 /**
26  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
27  */

28 public class ConnectionNotificationTest extends TestCase implements NotificationListener JavaDoc
29 {
30    private JMXConnectorServer JavaDoc connectorServer;
31    private List JavaDoc notificationList = new ArrayList JavaDoc();
32
33    public void setUp() throws Exception JavaDoc
34    {
35       MBeanServer JavaDoc mbeanServer = MBeanServerFactory.createMBeanServer();
36
37       int registryPort = Registry.REGISTRY_PORT;
38       Registry JavaDoc rmiRegistry = LocateRegistry.createRegistry(registryPort);
39
40       String JavaDoc jndiPath = "/jmxconnector";
41       JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("service:jmx:rmi://localhost/jndi/rmi://localhost:" + registryPort + jndiPath);
42
43       // create new connector server and start it
44
connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);
45       connectorServer.start();
46
47       System.out.println("Connector server started.");
48    }
49
50    public void tearDown() throws Exception JavaDoc
51    {
52       connectorServer.stop();
53    }
54
55    public void testConnectionNotifications() throws Exception JavaDoc
56    {
57       JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxconnector");
58
59       JMXConnector JavaDoc connector = JMXConnectorFactory.newJMXConnector(url, null);
60       connector.addConnectionNotificationListener(this, null, null);
61       connector.connect();
62       connector.close();
63
64       assertEquals("Should have received two notifications (one for connect and one for close).", 2, notificationList.size());
65    }
66
67    public void handleNotification(Notification JavaDoc notification, Object JavaDoc o)
68    {
69       notificationList.add(notification);
70    }
71 }
Popular Tags