1 7 package org.jboss.test.remoting.detection.metadata; 8 9 import java.io.ByteArrayInputStream ; 10 import java.util.List ; 11 import javax.management.MBeanServer ; 12 import javax.management.MBeanServerFactory ; 13 import javax.management.Notification ; 14 import javax.management.NotificationListener ; 15 import javax.management.ObjectName ; 16 import javax.xml.parsers.DocumentBuilderFactory ; 17 import org.jboss.remoting.InvokerLocator; 18 import org.jboss.remoting.detection.ServerInvokerMetadata; 19 import org.jboss.remoting.detection.multicast.MulticastDetector; 20 import org.jboss.remoting.network.NetworkNotification; 21 import org.jboss.remoting.network.NetworkRegistry; 22 import org.jboss.remoting.transport.Connector; 23 import org.jboss.test.remoting.TestUtil; 24 import org.w3c.dom.Document ; 25 26 import junit.framework.TestCase; 27 28 34 public class MetadataTestCase extends TestCase implements NotificationListener 35 { 36 private String subSystem = null; 37 38 public MetadataTestCase(String name) 39 { 40 super(name); 41 } 42 43 public void testDetectors() throws Exception 44 { 45 MulticastDetector detector1 = new MulticastDetector(); 46 MulticastDetector detector2 = new MulticastDetector(); 47 48 Connector connector1 = new Connector(); 49 Connector connector2 = new Connector(); 50 51 NetworkRegistry reg1 = setupServers(detector1, connector1); 52 List mbeanServers = MBeanServerFactory.findMBeanServer(null); 54 MBeanServer mbeanSvr = (MBeanServer ) mbeanServers.get(0); 55 mbeanSvr.addNotificationListener(new ObjectName ("remoting:type=NetworkRegistry"), 56 this, null, null); 57 58 NetworkRegistry reg2 = setupServers(detector2, connector2); 59 60 Thread.currentThread().sleep(2000); 62 63 int reg1Count = reg1.getServers().length; 65 int reg2Count = reg2.getServers().length; 66 67 assertTrue(reg1Count == 1 && reg2Count == 1); 69 70 if(reg1Count == 1 && reg2Count == 1) 71 { 72 System.out.println("PASSED - both registries have found the detectors."); 73 } 74 else 75 { 76 System.out.println("FAILED - registries not populated with remote detectors."); 77 } 78 79 assertEquals("MOCK", subSystem); 81 82 connector1.stop(); 84 connector1.destroy(); 85 connector1 = null; 86 connector2.stop(); 87 connector2.destroy(); 88 connector2 = null; 89 detector1.stop(); 90 detector2.stop(); 91 } 94 95 96 private synchronized NetworkRegistry setupServers(MulticastDetector detector, Connector connector) 97 { 98 NetworkRegistry registry = null; 99 System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis())); 100 System.out.println("jboss.identity = " + System.getProperty("jboss.identity")); 101 102 try 103 { 104 MBeanServer server = MBeanServerFactory.createMBeanServer(); 105 106 registry = TestNetworkRegistry.createNetworkRegistry(); 108 server.registerMBean(registry, new ObjectName ("remoting:type=NetworkRegistry")); 109 110 int port = TestUtil.getRandomPort(); 112 System.out.println("port = " + port); 113 114 115 InvokerLocator locator = new InvokerLocator("socket://localhost:" + port); 116 StringBuffer buf = new StringBuffer (); 117 buf.append("<?xml version=\"1.0\"?>\n"); 118 buf.append("<handlers>\n"); 119 buf.append(" <handler subsystem=\"mock\">org.jboss.test.remoting.transport.mock.MockServerInvocationHandler</handler>\n"); 120 buf.append("</handlers>\n"); 121 Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream (buf.toString().getBytes())); 122 connector.setInvokerLocator(locator.getLocatorURI()); 123 connector.setConfiguration(xml.getDocumentElement()); 124 ObjectName obj = new ObjectName ("jboss.remoting:type=Connector,transport=" + locator.getProtocol()); 125 server.registerMBean(connector, obj); 126 connector.start(); 128 129 server.registerMBean(detector, new ObjectName ("remoting:type=JNDIDetector")); 131 132 detector.start(); 134 } 135 catch(Exception e) 136 { 137 e.printStackTrace(); 138 } 139 140 return registry; 141 } 142 143 public void handleNotification(Notification notification, Object o) 144 { 145 System.out.println("Received notification: " + notification); 146 if(notification instanceof NetworkNotification) 147 { 148 NetworkNotification netNot = (NetworkNotification) notification; 149 ServerInvokerMetadata[] serverMetadata = netNot.getServerInvokers(); 150 subSystem = serverMetadata[0].getSubSystems()[0]; 151 } 152 } 153 154 private static class TestNetworkRegistry extends NetworkRegistry 155 { 156 public static NetworkRegistry createNetworkRegistry() 157 { 158 return new TestNetworkRegistry(); 159 } 160 } 161 162 } 163 | Popular Tags |