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.remoting.InvokerLocator; 15 import org.jboss.remoting.detection.jndi.JNDIDetector; 16 import org.jboss.remoting.network.NetworkInstance; 17 import org.jboss.remoting.network.NetworkRegistry; 18 import org.jboss.remoting.transport.Connector; 19 import org.jboss.test.remoting.TestUtil; 20 import org.jboss.test.remoting.transport.mock.MockServerInvocationHandler; 21 import org.jnp.server.Main; 22 23 import junit.framework.TestCase; 24 25 34 public class JNDIDetectorTest1 extends TestCase 35 { 36 private int detectorPort = 1099; 37 private String contextFactory = "org.jnp.interfaces.NamingContextFactory"; 38 private String urlPackage = "org.jboss.naming:org.jnp.interfaces"; 39 40 private JNDIDetector detector; 41 private Connector connector; 42 private NetworkRegistry registry; 43 44 public void testDetector() throws Exception 45 { 46 Thread.currentThread().sleep(5000); 47 48 NetworkInstance[] server = registry.getServers(); 49 if(server != null && server.length > 0) 50 { 51 System.out.println("PASS - more than zero servers detected."); 52 assertTrue("Got detection.", true); 53 } 54 else 55 { 56 System.out.println("FAIL - no servers detected"); 57 assertTrue("Did not detect servers.", false); 58 } 59 60 Thread.currentThread().sleep(60000); 61 62 server = registry.getServers(); 63 if(server == null || server.length == 0) 64 { 65 System.out.println("PASS - no servers detected."); 66 assertTrue("No servers detected.", true); 67 } 68 else 69 { 70 System.out.println("FAIL - all servers should have been removed."); 71 assertTrue("Found a server, but should have been removed.", false); 72 } 73 74 } 75 76 public void setUp() throws Exception 77 { 78 String detectorHost = InetAddress.getLocalHost().getHostName(); 79 80 detector = new JNDIDetector(); 81 detector.setCleanDetectionNumber(2); 82 83 System.setProperty("jboss.identity", String.valueOf(System.currentTimeMillis())); 84 System.out.println("jboss.identity = " + System.getProperty("jboss.identity")); 85 86 MBeanServer server = MBeanServerFactory.createMBeanServer(); 87 88 registry = JNDIDetectorTest1.TestNetworkRegistry.createNetworkRegistry(); 90 server.registerMBean(registry, new ObjectName ("remoting:type=NetworkRegistry")); 91 92 int port = TestUtil.getRandomPort(); 93 System.out.println("port = " + port); 94 95 InvokerLocator locator = new InvokerLocator("socket://localhost:" + port); 96 97 System.out.println("Starting remoting server with locator uri of: " + locator.getLocatorURI()); 98 connector = new Connector(); 99 connector.setInvokerLocator(locator.getLocatorURI()); 100 connector.create(); 101 102 MockServerInvocationHandler handler = new MockServerInvocationHandler(); 103 connector.addInvocationHandler("mock", handler); 104 105 ObjectName obj = new ObjectName ("jboss.remoting:type=Connector,transport=" + locator.getProtocol()); 106 server.registerMBean(connector, obj); 107 connector.start(); 109 110 server.registerMBean(detector, new ObjectName ("remoting:type=JNDIDetector")); 112 113 detector.setPort(detectorPort); 115 detector.setHost(detectorHost); 116 detector.setContextFactory(contextFactory); 117 detector.setURLPackage(urlPackage); 118 detector.start(); 119 120 } 121 122 public void tearDown() throws Exception 123 { 124 if(detector != null) 125 { 126 detector.stop(); 127 } 128 if(connector != null) 129 { 130 connector.stop(); 131 connector.destroy(); 132 } 133 } 134 135 public static void main(String [] args) 136 { 137 org.apache.log4j.BasicConfigurator.configure(); 138 org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG); 139 140 try 141 { 142 int port = 1099; 144 String host = InetAddress.getLocalHost().getHostName(); 146 Main JNDIServer = new Main(); 147 JNDIServer.setPort(port); 148 JNDIServer.setBindAddress(host); 149 JNDIServer.start(); 150 System.out.println("Started JNDI server on " + host + ":" + port); 151 152 JNDIDetectorTest1 test = new JNDIDetectorTest1(); 153 test.setUp(); 154 test.testDetector(); 155 test.tearDown(); 156 } 157 catch(Exception e) 158 { 159 e.printStackTrace(); 160 } 161 162 } 163 164 private static class TestNetworkRegistry extends NetworkRegistry 165 { 166 public static NetworkRegistry createNetworkRegistry() 167 { 168 return new JNDIDetectorTest1.TestNetworkRegistry(); 169 } 170 } 171 172 } | Popular Tags |