1 7 package org.jboss.remoting.detection.jndi; 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.jnp.server.Main; 14 import org.w3c.dom.Document ; 15 16 import javax.management.MBeanServer ; 17 import javax.management.MBeanServerFactory ; 18 import javax.management.ObjectName ; 19 import javax.xml.parsers.DocumentBuilderFactory ; 20 import java.io.ByteArrayInputStream ; 21 import java.net.InetAddress ; 22 23 28 public class JNDIDetectorUnitTestCase extends TestCase 29 { 30 private int detectorPort = 1088; 32 private String detectorHost = "localhost"; 33 private String contextFactory = "org.jnp.interfaces.NamingContextFactory"; 34 private String urlPackage = "org.jboss.naming:org.jnp.interfaces"; 35 36 public JNDIDetectorUnitTestCase(String name) 37 { 38 super(name); 39 } 40 41 public void testDetectors() throws Exception 42 { 43 detectorHost = InetAddress.getLocalHost().getHostName(); 44 45 Main JNDIServer = new Main(); 47 JNDIServer.setPort(detectorPort); 48 JNDIServer.setBindAddress(detectorHost); 49 JNDIServer.start(); 50 51 Thread.currentThread().sleep(4000); 52 53 JNDIDetector detector1 = new JNDIDetector(); 54 JNDIDetector detector2 = new JNDIDetector(); 55 56 Connector connector1 = new Connector(); 57 Connector connector2 = new Connector(); 58 59 NetworkRegistry reg1 = setupServers(detector1, connector1); 60 NetworkRegistry reg2 = setupServers(detector2, connector2); 61 62 Thread.currentThread().sleep(4000); 64 65 int reg1Count = reg1.getServers().length; 67 int reg2Count = reg2.getServers().length; 68 69 assertTrue(reg1Count == 1 && reg2Count == 1); 71 72 if(reg1Count == 1 && reg2Count == 1) 73 { 74 System.out.println("PASSED - both registries have found the detectors."); 75 } 76 else 77 { 78 System.out.println("FAILED - registries not populated with remote detectors."); 79 } 80 81 connector1.stop(); 83 connector1 = null; 85 connector2.stop(); 86 connector2.stop(); 87 connector2 = null; 88 detector1.stop(); 89 90 Thread.currentThread().sleep(6000); 92 93 reg1Count = reg2.getServers().length; 95 96 assertTrue(reg1Count == 0); 98 99 if(reg1Count == 0) 100 { 101 System.out.println("PASSED - 2nd detector stopped and no longer in registry."); 102 } 103 else 104 { 105 System.out.println("FAILED - 2nd detector stopped but still in registry."); 106 } 107 108 detector2.stop(); 110 JNDIServer.stop(); 113 } 114 115 116 private synchronized NetworkRegistry setupServers(JNDIDetector detector, Connector connector) 117 { 118 NetworkRegistry registry = null; 119 System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis())); 120 System.out.println("jboss.identity = " + System.getProperty("jboss.identity")); 121 122 try 123 { 124 MBeanServer server = MBeanServerFactory.createMBeanServer(); 125 126 registry = TestNetworkRegistry.createNetworkRegistry(); 128 server.registerMBean(registry, new ObjectName ("remoting:type=NetworkRegistry")); 129 130 int port = 8085; 132 System.out.println("port = " + port); 133 134 InvokerLocator locator = new InvokerLocator("socket://localhost:" + port); 135 StringBuffer buf = new StringBuffer (); 136 buf.append("<?xml version=\"1.0\"?>\n"); 137 buf.append("<handlers>\n"); 138 buf.append(" <handler subsystem=\"mock\">org.jboss.remoting.transport.mock.MockServerInvocationHandler</handler>\n"); 139 buf.append("</handlers>\n"); 140 Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream (buf.toString().getBytes())); 141 connector.setInvokerLocator(locator.getLocatorURI()); 142 connector.setConfiguration(xml.getDocumentElement()); 143 ObjectName obj = new ObjectName ("jboss.remoting:type=Connector,transport=" + locator.getProtocol()); 144 server.registerMBean(connector, obj); 145 connector.start(); 147 148 server.registerMBean(detector, new ObjectName ("remoting:type=JNDIDetector")); 150 151 detector.setPort(detectorPort); 153 detector.setHost(detectorHost); 154 detector.setContextFactory(contextFactory); 155 detector.setURLPackage(urlPackage); 156 detector.start(); 157 } 158 catch(Exception e) 159 { 160 e.printStackTrace(); 161 } 162 163 return registry; 164 } 165 166 private static class TestNetworkRegistry extends NetworkRegistry 167 { 168 public static NetworkRegistry createNetworkRegistry() 169 { 170 return new TestNetworkRegistry(); 171 } 172 } 173 174 } 175 | Popular Tags |