1 7 package org.jboss.remoting.detection.jndi; 8 9 import org.apache.log4j.Level; 10 import org.jboss.dtf.DistributedTestCase; 11 import org.jboss.dtf.MultipleTestRunner; 12 import org.jboss.remoting.InvokerLocator; 13 import org.jboss.remoting.network.NetworkInstance; 14 import org.jboss.remoting.network.NetworkRegistry; 15 import org.jboss.remoting.transport.Connector; 16 import org.w3c.dom.Document ; 17 18 import javax.management.MBeanServer ; 19 import javax.management.MBeanServerFactory ; 20 import javax.management.ObjectName ; 21 import javax.xml.parsers.DocumentBuilderFactory ; 22 import java.io.ByteArrayInputStream ; 23 import java.util.Random ; 24 25 34 public class JNDIDetectorTest1 extends DistributedTestCase 35 { 36 private int detectorPort = 1099; 38 private String detectorHost = "localhost"; 39 private String contextFactory = "org.jnp.interfaces.NamingContextFactory"; 40 private String urlPackage = "org.jboss.naming:org.jnp.interfaces"; 41 42 private final Object waitObj = new Object (); 43 44 45 public JNDIDetectorTest1(String name) 46 { 47 super(name); 48 } 49 50 public JNDIDetectorTest1(String name, int port, String host) 51 { 52 super(name); 53 this.detectorPort = port; 54 this.detectorHost = host; 55 } 56 57 public JNDIDetectorTest1(int instances, int port, String host) 58 { 59 super("JNDIDetectorTest1"); 60 this.detectorPort = port; 61 this.detectorHost = host; 62 init(instances); 63 } 64 65 66 public int getDetectorPort() 67 { 68 return detectorPort; 69 } 70 71 public void setDetectorPort(int detectorPort) 72 { 73 this.detectorPort = detectorPort; 74 } 75 76 public String getDetectorHost() 77 { 78 return detectorHost; 79 } 80 81 public void setDetectorHost(String detectorHost) 82 { 83 this.detectorHost = detectorHost; 84 } 85 86 public void testDetectors() 87 { 88 try 89 { 90 91 JNDIDetector detector = new JNDIDetector(); 92 detector.setCleanDetectionNumber(2); 93 94 Connector connector = new Connector(); 95 96 NetworkRegistry reg = setupServers(detector, connector); 97 98 101 Thread.currentThread().sleep(4000); 103 104 int regCount = reg.getServers().length; 106 107 assertTrue("JNDIDetector should have found at least one other registry.", 109 regCount == 1); 110 System.err.println("regCount = " + regCount); 111 NetworkInstance[] instances = reg.getServers(); 112 for(int x = 0; x < instances.length; x++) 113 { 114 System.err.println("Network instance " + x + " = " + instances[x]); 115 } 116 117 shutdown(); 118 119 System.err.println("returning from shutdown()"); 120 121 synchronized(waitObj) 124 { 125 waitObj.wait(90000); 126 } 127 128 System.err.println("done waiting."); 129 130 regCount = reg.getServers().length; 131 System.out.println("stopped. now regCount = " + regCount); 132 instances = reg.getServers(); 133 for(int x = 0; x < instances.length; x++) 134 { 135 System.out.println("Network instance " + x + " = " + instances[x]); 136 } 137 138 assertTrue("JNDIDetector should not find any other registries.", regCount == 0); 140 connector.stop(); 142 connector = null; 144 detector.stop(); 145 detector = null; 146 } 147 catch(Exception e) 148 { 149 e.printStackTrace(); 150 } 151 } 152 153 154 private synchronized NetworkRegistry setupServers(JNDIDetector detector, Connector connector) 155 { 156 NetworkRegistry registry = null; 157 System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis())); 158 System.out.println("jboss.identity = " + System.getProperty("jboss.identity")); 159 160 try 161 { 162 MBeanServer server = MBeanServerFactory.createMBeanServer(); 163 164 registry = TestNetworkRegistry.createNetworkRegistry(); 166 server.registerMBean(registry, new ObjectName ("remoting:type=NetworkRegistry")); 167 168 int port = Math.abs(new Random ().nextInt(2000)); 169 System.out.println("port = " + port); 170 171 InvokerLocator locator = new InvokerLocator("socket://localhost:" + port); 172 StringBuffer buf = new StringBuffer (); 173 buf.append("<?xml version=\"1.0\"?>\n"); 174 buf.append("<handlers>\n"); 175 buf.append(" <handler subsystem=\"mock\">org.jboss.remoting.transport.mock.MockServerInvocationHandler</handler>\n"); 176 buf.append("</handlers>\n"); 177 Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream (buf.toString().getBytes())); 178 connector.setInvokerLocator(locator.getLocatorURI()); 179 connector.setConfiguration(xml.getDocumentElement()); 180 ObjectName obj = new ObjectName ("jboss.remoting:type=Connector,transport=" + locator.getProtocol()); 181 server.registerMBean(connector, obj); 182 connector.start(); 184 185 server.registerMBean(detector, new ObjectName ("remoting:type=JNDIDetector")); 187 188 detector.setPort(detectorPort); 190 detector.setHost(detectorHost); 191 detector.setContextFactory(contextFactory); 192 detector.setURLPackage(urlPackage); 193 detector.start(); 194 } 195 catch(Exception e) 196 { 197 e.printStackTrace(); 198 } 199 200 return registry; 201 } 202 203 public static void main(String [] args) 204 { 205 org.apache.log4j.BasicConfigurator.configure(); 206 org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG); 207 208 JNDIDetectorTest1 test = null; 209 if(args.length == 2) 210 { 211 String sPort = args[0]; 212 int port = Integer.parseInt(sPort); 213 String host = args[1]; 214 test = new JNDIDetectorTest1("JNDIDetectorTest1", port, host); 215 } 216 else if(args.length == 3) 217 { 218 String sInstances = args[0]; 219 int instances = Integer.parseInt(sInstances); 220 String sPort = args[1]; 221 int port = Integer.parseInt(sPort); 222 String host = args[2]; 223 test = new JNDIDetectorTest1(instances, port, host); 224 } 225 else 226 { 227 test = new JNDIDetectorTest1("JNDIDetectorTest1"); 228 } 229 230 MultipleTestRunner runner = new MultipleTestRunner(); 231 runner.doRun(test, true); 232 233 235 System.exit(0); 236 237 } 238 239 private static class TestNetworkRegistry extends NetworkRegistry 240 { 241 public static NetworkRegistry createNetworkRegistry() 242 { 243 return new TestNetworkRegistry(); 244 } 245 } 246 247 } 248 | Popular Tags |