1 7 package test.compliance.core.notification; 8 9 import java.rmi.registry.Registry ; 10 import java.rmi.registry.LocateRegistry ; 11 import java.rmi.RemoteException ; 12 import javax.management.MBeanServerConnection ; 13 import javax.management.MalformedObjectNameException ; 14 import javax.management.ObjectName ; 15 import javax.management.MBeanServer ; 16 import javax.management.MBeanServerFactory ; 17 import javax.management.ListenerNotFoundException ; 18 import javax.management.remote.JMXConnector ; 19 import javax.management.remote.JMXConnectorFactory ; 20 import javax.management.remote.JMXServiceURL ; 21 import javax.management.remote.JMXConnectorServer ; 22 import javax.management.remote.JMXConnectorServerFactory ; 23 24 import junit.framework.TestCase; 25 26 29 public class NotificationTest extends TestCase 30 { 31 private JMXConnector connector; 32 private JMXConnectorServer connectorServer; 33 private MBeanServerConnection server; 34 private int registryPort = 1090; 35 private String jndiPath = "/jmxconnector"; 36 37 public void setUp() throws Exception 38 { 39 MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer(); 40 InvokerTest test = new InvokerTest(); 41 mbeanServer.registerMBean(test, getObjectName()); 42 43 Registry rmiRegistry = LocateRegistry.getRegistry(registryPort); 45 if(rmiRegistry != null) 46 { 47 try 48 { 49 rmiRegistry.list(); 50 } 51 catch(RemoteException e) 52 { 53 rmiRegistry = LocateRegistry.createRegistry(registryPort); 54 } 55 } 56 else 57 { 58 rmiRegistry = LocateRegistry.createRegistry(registryPort); 59 } 60 JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi://localhost/jndi/rmi://localhost:" + registryPort + jndiPath); 61 62 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 72 { 73 if(connector != null) 74 { 75 connector.close(); 76 } 77 if(connectorServer != null) 78 { 79 connectorServer.stop(); 80 } 81 } 82 83 private ObjectName getObjectName() throws MalformedObjectNameException 84 { 85 return new ObjectName ("test:type=InvokerTest"); 86 } 87 88 public void testNotification() throws Exception 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 108 public void testNotificationWithBadListener() throws Exception 109 { 110 BadListener badListener = new BadListener(); 112 server.addNotificationListener(getObjectName(), badListener, null, "BadListener"); 113 Listener listener = new Listener(10); 114 server.addNotificationListener(getObjectName(), listener, new RunTimerFilter(), "runTimer"); 116 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 e) 133 { 134 System.out.println("The BadListener was not found"); 135 } 136 } 137 } | Popular Tags |