KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > detection > jndi > JNDIDetectorTest1


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.remoting.detection.jndi;
8
9 import java.net.InetAddress JavaDoc;
10 import javax.management.MBeanServer JavaDoc;
11 import javax.management.MBeanServerFactory JavaDoc;
12 import javax.management.ObjectName JavaDoc;
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 /**
26  * Just tests that detector A sees detector B when B comes online then off.
27  * This is a JUnit test, but will need to run JNDIDetectorTest2 at same time
28  * in order to work properly. Can also just run JNDIDetectorTestCase as
29  * test harness to run both JNDIDetectorTest1 and JNDIDetectorTest2 (as regular JUnit test).
30  * See the main for the arguments required (based on DistributedTestCase confines).
31  *
32  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
33  */

34 public class JNDIDetectorTest1 extends TestCase
35 {
36    private int detectorPort = 1099;
37    private String JavaDoc contextFactory = "org.jnp.interfaces.NamingContextFactory";
38    private String JavaDoc 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 JavaDoc
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 JavaDoc
77    {
78       String JavaDoc 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 JavaDoc server = MBeanServerFactory.createMBeanServer();
87
88       //registry = NetworkRegistry.getInstance();
89
registry = JNDIDetectorTest1.TestNetworkRegistry.createNetworkRegistry();
90       server.registerMBean(registry, new ObjectName JavaDoc("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 JavaDoc obj = new ObjectName JavaDoc("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
106       server.registerMBean(connector, obj);
107       //connector.create();
108
connector.start();
109
110       //Need to set new domain for identity
111
server.registerMBean(detector, new ObjectName JavaDoc("remoting:type=JNDIDetector"));
112
113       // set config info for detector and start it.
114
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 JavaDoc
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 JavaDoc[] args)
136    {
137       org.apache.log4j.BasicConfigurator.configure();
138       org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
139
140       try
141       {
142          // start JNDI server
143
int port = 1099;
144          //String host = "localhost";
145
String JavaDoc 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 JavaDoc 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