1 7 package org.jboss.remoting.detection.multicast; 8 9 import junit.framework.TestCase; 10 import org.jboss.remoting.InvokerLocator; 11 import org.jboss.remoting.network.NetworkRegistry; 12 import org.jboss.remoting.transport.Connector; 13 import org.w3c.dom.Document ; 14 15 import javax.management.MBeanServer ; 16 import javax.management.MBeanServerFactory ; 17 import javax.management.ObjectName ; 18 import javax.xml.parsers.DocumentBuilderFactory ; 19 import java.io.ByteArrayInputStream ; 20 21 26 public class MulticastUnitTestCase extends TestCase 27 { 28 public MulticastUnitTestCase(String name) 29 { 30 super(name); 31 } 32 33 public void testDetectors() throws Exception 34 { 35 MulticastDetector detector1 = new MulticastDetector(); 36 MulticastDetector detector2 = new MulticastDetector(); 37 38 Connector connector1 = new Connector(); 39 Connector connector2 = new Connector(); 40 41 NetworkRegistry reg1 = setupServers(detector1, connector1); 42 NetworkRegistry reg2 = setupServers(detector2, connector2); 43 44 Thread.currentThread().sleep(2000); 46 47 int reg1Count = reg1.getServers().length; 49 int reg2Count = reg2.getServers().length; 50 51 assertTrue(reg1Count == 1 && reg2Count == 1); 53 54 if(reg1Count == 1 && reg2Count == 1) 55 { 56 System.out.println("PASSED - both registries have found the detectors."); 57 } 58 else 59 { 60 System.out.println("FAILED - registries not populated with remote detectors."); 61 } 62 63 connector1.stop(); 65 connector1 = null; 67 connector2.stop(); 68 connector2.stop(); 69 connector2 = null; 70 detector1.stop(); 71 72 Thread.currentThread().sleep(12000); 74 75 reg1Count = reg2.getServers().length; 77 78 assertTrue(reg1Count == 0); 80 81 if(reg1Count == 0) 82 { 83 System.out.println("PASSED - 2nd detector stopped and no longer in registry."); 84 } 85 else 86 { 87 System.out.println("FAILED - 2nd detector stopped but still in registry."); 88 } 89 90 detector2.stop(); 92 } 95 96 97 private synchronized NetworkRegistry setupServers(MulticastDetector detector, Connector connector) 98 { 99 NetworkRegistry registry = null; 100 System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis())); 101 System.out.println("jboss.identity = " + System.getProperty("jboss.identity")); 102 103 try 104 { 105 MBeanServer server = MBeanServerFactory.createMBeanServer(); 106 107 registry = TestNetworkRegistry.createNetworkRegistry(); 109 server.registerMBean(registry, new ObjectName ("remoting:type=NetworkRegistry")); 110 111 int port = 8085; 113 System.out.println("port = " + port); 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.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 private static class TestNetworkRegistry extends NetworkRegistry 144 { 145 public static NetworkRegistry createNetworkRegistry() 146 { 147 return new TestNetworkRegistry(); 148 } 149 } 150 151 } 152 | Popular Tags |