1 7 package org.jboss.test.remoting.detection.jndi; 8 9 import java.net.InetAddress ; 10 import javax.management.MBeanServer ; 11 import javax.management.MBeanServerFactory ; 12 import javax.management.ObjectName ; 13 import org.apache.log4j.Level; 14 import org.jboss.jrunit.extensions.ServerTestCase; 15 import org.jboss.remoting.InvokerLocator; 16 import org.jboss.remoting.detection.jndi.JNDIDetector; 17 import org.jboss.remoting.network.NetworkInstance; 18 import org.jboss.remoting.network.NetworkRegistry; 19 import org.jboss.remoting.transport.Connector; 20 import org.jboss.test.remoting.TestUtil; 21 import org.jboss.test.remoting.transport.mock.MockServerInvocationHandler; 22 23 28 public class JNDIDetectorTest2 extends ServerTestCase 29 { 30 private int detectorPort = 1099; 31 private String contextFactory = "org.jnp.interfaces.NamingContextFactory"; 32 private String urlPackage = "org.jboss.naming:org.jnp.interfaces"; 33 34 private JNDIDetector detector; 35 private Connector connector; 36 private NetworkRegistry registry; 37 38 public void setUp() throws Exception 39 { 40 String detectorHost = InetAddress.getLocalHost().getHostName(); 41 42 detector = new JNDIDetector(); 43 detector.setCleanDetectionNumber(2); 44 45 System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis())); 46 System.out.println("jboss.identity = " + System.getProperty("jboss.identity")); 47 48 MBeanServer server = MBeanServerFactory.createMBeanServer(); 49 50 registry = JNDIDetectorTest2.TestNetworkRegistry.createNetworkRegistry(); 52 server.registerMBean(registry, new ObjectName ("remoting:type=NetworkRegistry")); 53 54 int port = TestUtil.getRandomPort(); 55 System.out.println("port = " + port); 56 57 InvokerLocator locator = new InvokerLocator("socket://localhost:" + port); 58 59 System.out.println("Starting remoting server with locator uri of: " + locator.getLocatorURI()); 60 connector = new Connector(); 61 connector.setInvokerLocator(locator.getLocatorURI()); 62 connector.create(); 63 64 MockServerInvocationHandler handler = new MockServerInvocationHandler(); 65 connector.addInvocationHandler("mock", handler); 66 67 ObjectName obj = new ObjectName ("jboss.remoting:type=Connector,transport=" + locator.getProtocol()); 68 server.registerMBean(connector, obj); 69 connector.start(); 71 72 server.registerMBean(detector, new ObjectName ("remoting:type=JNDIDetector")); 74 75 detector.setPort(detectorPort); 77 detector.setHost(detectorHost); 78 detector.setContextFactory(contextFactory); 79 detector.setURLPackage(urlPackage); 80 detector.start(); 81 82 } 83 84 public void testDetector2() throws Exception 85 { 86 Thread.currentThread().sleep(5000); 87 88 NetworkInstance[] server = registry.getServers(); 89 if(server != null && server.length > 0) 90 { 91 System.out.println("PASS - got more than zero servers detected."); 92 assertTrue("Detected server.", true); 93 } 94 else 95 { 96 System.out.println("FAIL - did not detect any other server."); 97 assertTrue("Did not detect any servers.", false); 98 } 99 100 if(connector != null) 101 { 102 connector.stop(); 103 connector.destroy(); 104 connector = null; 105 } 106 if(detector != null) 107 { 108 detector.stop(); 109 detector = null; 110 } 111 } 112 113 public void tearDown() throws Exception 114 { 115 if(connector != null) 116 { 117 connector.stop(); 118 connector.destroy(); 119 connector = null; 120 } 121 if(detector != null) 122 { 123 detector.stop(); 124 detector = null; 125 } 126 127 } 128 129 public static void main(String [] args) 130 { 131 org.apache.log4j.BasicConfigurator.configure(); 132 org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG); 133 134 try 135 { 136 JNDIDetectorTest2 test = new JNDIDetectorTest2(); 137 test.setUp(); 138 test.testDetector2(); 139 test.tearDown(); 140 } 141 catch(Exception e) 142 { 143 e.printStackTrace(); 144 } 145 146 } 147 148 private static class TestNetworkRegistry extends NetworkRegistry 149 { 150 public static NetworkRegistry createNetworkRegistry() 151 { 152 return new JNDIDetectorTest2.TestNetworkRegistry(); 153 } 154 } 155 156 } | Popular Tags |