1 7 package org.jboss.test.remoting.detection.multicast; 8 9 import java.io.ByteArrayInputStream ; 10 import javax.management.MBeanServer ; 11 import javax.management.MBeanServerFactory ; 12 import javax.management.ObjectName ; 13 import javax.xml.parsers.DocumentBuilderFactory ; 14 import org.jboss.remoting.InvokerLocator; 15 import org.jboss.remoting.detection.multicast.MulticastDetector; 16 import org.jboss.remoting.network.NetworkRegistry; 17 import org.jboss.remoting.transport.Connector; 18 import org.jboss.test.remoting.TestUtil; 19 import org.w3c.dom.Document ; 20 21 import junit.framework.TestCase; 22 23 28 public class MulticastUnitTestCase extends TestCase 29 { 30 public MulticastUnitTestCase(String name) 31 { 32 super(name); 33 } 34 35 public void testDetectors() throws Exception 36 { 37 MulticastDetector detector1 = new MulticastDetector(); 38 MulticastDetector detector2 = new MulticastDetector(); 39 40 Connector connector1 = new Connector(); 41 Connector connector2 = new Connector(); 42 43 NetworkRegistry reg1 = setupServers(detector1, connector1); 44 NetworkRegistry reg2 = setupServers(detector2, connector2); 45 46 Thread.currentThread().sleep(2000); 48 49 int reg1Count = reg1.getServers().length; 51 int reg2Count = reg2.getServers().length; 52 53 assertTrue(reg1Count == 1 && reg2Count == 1); 55 56 if(reg1Count == 1 && reg2Count == 1) 57 { 58 System.out.println("PASSED - both registries have found the detectors."); 59 } 60 else 61 { 62 System.out.println("FAILED - registries not populated with remote detectors."); 63 } 64 65 connector1.stop(); 67 connector1.destroy(); 68 connector1 = null; 69 connector2.stop(); 70 connector2.destroy(); 71 connector2 = null; 72 detector1.stop(); 73 74 Thread.currentThread().sleep(60000); 76 77 reg1Count = reg2.getServers().length; 79 80 assertTrue(reg1Count == 0); 82 83 if(reg1Count == 0) 84 { 85 System.out.println("PASSED - 2nd detector stopped and no longer in registry."); 86 } 87 else 88 { 89 System.out.println("FAILED - 2nd detector stopped but still in registry."); 90 } 91 92 detector2.stop(); 94 } 97 98 99 private synchronized NetworkRegistry setupServers(MulticastDetector detector, Connector connector) 100 { 101 NetworkRegistry registry = null; 102 System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis())); 103 System.out.println("jboss.identity = " + System.getProperty("jboss.identity")); 104 105 try 106 { 107 MBeanServer server = MBeanServerFactory.createMBeanServer(); 108 109 registry = TestNetworkRegistry.createNetworkRegistry(); 111 server.registerMBean(registry, new ObjectName ("remoting:type=NetworkRegistry")); 112 113 int port = TestUtil.getRandomPort(); 115 System.out.println("port = " + port); 116 117 118 InvokerLocator locator = new InvokerLocator("socket://localhost:" + port); 119 StringBuffer buf = new StringBuffer (); 120 buf.append("<?xml version=\"1.0\"?>\n"); 121 buf.append("<handlers>\n"); 122 buf.append(" <handler subsystem=\"mock\">org.jboss.test.remoting.transport.mock.MockServerInvocationHandler</handler>\n"); 123 buf.append("</handlers>\n"); 124 Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream (buf.toString().getBytes())); 125 connector.setInvokerLocator(locator.getLocatorURI()); 126 connector.setConfiguration(xml.getDocumentElement()); 127 ObjectName obj = new ObjectName ("jboss.remoting:type=Connector,transport=" + locator.getProtocol()); 128 server.registerMBean(connector, obj); 129 connector.start(); 131 132 server.registerMBean(detector, new ObjectName ("remoting:type=JNDIDetector")); 134 135 detector.start(); 137 } 138 catch(Exception e) 139 { 140 e.printStackTrace(); 141 } 142 143 return registry; 144 } 145 146 private static class TestNetworkRegistry extends NetworkRegistry 147 { 148 public static NetworkRegistry createNetworkRegistry() 149 { 150 return new TestNetworkRegistry(); 151 } 152 } 153 154 } 155 | Popular Tags |