1 7 package test.compliance.core.notification.connection; 8 9 import java.rmi.registry.LocateRegistry ; 10 import java.rmi.registry.Registry ; 11 import java.util.ArrayList ; 12 import java.util.List ; 13 import javax.management.MBeanServer ; 14 import javax.management.MBeanServerFactory ; 15 import javax.management.Notification ; 16 import javax.management.NotificationListener ; 17 import javax.management.remote.JMXConnector ; 18 import javax.management.remote.JMXConnectorFactory ; 19 import javax.management.remote.JMXConnectorServer ; 20 import javax.management.remote.JMXConnectorServerFactory ; 21 import javax.management.remote.JMXServiceURL ; 22 23 import junit.framework.TestCase; 24 25 28 public class ConnectionNotificationTest extends TestCase implements NotificationListener 29 { 30 private JMXConnectorServer connectorServer; 31 private List notificationList = new ArrayList (); 32 33 public void setUp() throws Exception 34 { 35 MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer(); 36 37 int registryPort = Registry.REGISTRY_PORT; 38 Registry rmiRegistry = LocateRegistry.createRegistry(registryPort); 39 40 String jndiPath = "/jmxconnector"; 41 JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi://localhost/jndi/rmi://localhost:" + registryPort + jndiPath); 42 43 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 51 { 52 connectorServer.stop(); 53 } 54 55 public void testConnectionNotifications() throws Exception 56 { 57 JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxconnector"); 58 59 JMXConnector 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 notification, Object o) 68 { 69 notificationList.add(notification); 70 } 71 } | Popular Tags |