KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > detection > jndi > JNDIDetectorUnitTestCase


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.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 JavaDoc;
15
16 import javax.management.MBeanServer JavaDoc;
17 import javax.management.MBeanServerFactory JavaDoc;
18 import javax.management.ObjectName JavaDoc;
19 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.net.InetAddress JavaDoc;
22
23 /**
24  * Just tests that detector A sees detector B when B comes online then off.
25  *
26  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
27  */

28 public class JNDIDetectorUnitTestCase extends TestCase
29 {
30    // Setup for JNDI Server
31
private int detectorPort = 1088;
32    private String JavaDoc detectorHost = "localhost";
33    private String JavaDoc contextFactory = "org.jnp.interfaces.NamingContextFactory";
34    private String JavaDoc urlPackage = "org.jboss.naming:org.jnp.interfaces";
35
36    public JNDIDetectorUnitTestCase(String JavaDoc name)
37    {
38       super(name);
39    }
40
41    public void testDetectors() throws Exception JavaDoc
42    {
43       detectorHost = InetAddress.getLocalHost().getHostName();
44
45       // start JNDI server
46
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       // Need to allow heartbeat so have detection
63
Thread.currentThread().sleep(4000);
64
65       //Should now have an entry for both of the registries
66
int reg1Count = reg1.getServers().length;
67       int reg2Count = reg2.getServers().length;
68
69       // Actual junit test
70
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       // stop the 2nd detector, so see if 1st one detects it is missing
82
connector1.stop();
83       //connector1.destroy();
84
connector1 = null;
85       connector2.stop();
86       connector2.stop();
87       connector2 = null;
88       detector1.stop();
89
90       // sleep for a few seconds so the 1st detector can discover 2nd one down
91
Thread.currentThread().sleep(6000);
92
93       // 1st one should be empty
94
reg1Count = reg2.getServers().length;
95
96       // Actual junit test
97
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       // cleanup
109
detector2.stop();
110       //connector2.stop();
111
//connector2.destroy();
112
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 JavaDoc server = MBeanServerFactory.createMBeanServer();
125
126          //registry = NetworkRegistry.getInstance();
127
registry = TestNetworkRegistry.createNetworkRegistry();
128          server.registerMBean(registry, new ObjectName JavaDoc("remoting:type=NetworkRegistry"));
129
130          //int port = Math.abs(new Random().nextInt(2000));
131
int port = 8085;
132          System.out.println("port = " + port);
133
134          InvokerLocator locator = new InvokerLocator("socket://localhost:" + port);
135          StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream JavaDoc(buf.toString().getBytes()));
141          connector.setInvokerLocator(locator.getLocatorURI());
142          connector.setConfiguration(xml.getDocumentElement());
143          ObjectName JavaDoc obj = new ObjectName JavaDoc("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
144          server.registerMBean(connector, obj);
145          //connector.create();
146
connector.start();
147
148          //Need to set new domain for identity
149
server.registerMBean(detector, new ObjectName JavaDoc("remoting:type=JNDIDetector"));
150
151          // set config info for detector and start it.
152
detector.setPort(detectorPort);
153          detector.setHost(detectorHost);
154          detector.setContextFactory(contextFactory);
155          detector.setURLPackage(urlPackage);
156          detector.start();
157       }
158       catch(Exception JavaDoc 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