KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > 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.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 JavaDoc;
17
18 import javax.management.MBeanServer JavaDoc;
19 import javax.management.MBeanServerFactory JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.util.Random JavaDoc;
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 DistributedTestCase
35 {
36    // Setup for JNDI Server
37
private int detectorPort = 1099;
38    private String JavaDoc detectorHost = "localhost";
39    private String JavaDoc contextFactory = "org.jnp.interfaces.NamingContextFactory";
40    private String JavaDoc urlPackage = "org.jboss.naming:org.jnp.interfaces";
41
42    private final Object JavaDoc waitObj = new Object JavaDoc();
43
44
45    public JNDIDetectorTest1(String JavaDoc name)
46    {
47       super(name);
48    }
49
50    public JNDIDetectorTest1(String JavaDoc name, int port, String JavaDoc host)
51    {
52       super(name);
53       this.detectorPort = port;
54       this.detectorHost = host;
55    }
56
57    public JNDIDetectorTest1(int instances, int port, String JavaDoc 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 JavaDoc getDetectorHost()
77    {
78       return detectorHost;
79    }
80
81    public void setDetectorHost(String JavaDoc 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          // should already be called from setUp()
99
//startup();
100

101          // Need to allow heartbeat so have detection
102
Thread.currentThread().sleep(4000);
103
104          //Should now have an entry for both of the registries
105
int regCount = reg.getServers().length;
106
107          // Actual junit test
108
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          // sleep for a few seconds so the 1st detector can discover 2nd one down
122
//Thread.currentThread().sleep(20000);
123
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          // Actual junit test
139
assertTrue("JNDIDetector should not find any other registries.", regCount == 0);
140          // stop the 2nd detector, so see if 1st one detects it is missing
141
connector.stop();
142          //connector.destroy();
143
connector = null;
144          detector.stop();
145          detector = null;
146       }
147       catch(Exception JavaDoc 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 JavaDoc server = MBeanServerFactory.createMBeanServer();
163
164          //registry = NetworkRegistry.getInstance();
165
registry = TestNetworkRegistry.createNetworkRegistry();
166          server.registerMBean(registry, new ObjectName JavaDoc("remoting:type=NetworkRegistry"));
167
168          int port = Math.abs(new Random JavaDoc().nextInt(2000));
169          System.out.println("port = " + port);
170
171          InvokerLocator locator = new InvokerLocator("socket://localhost:" + port);
172          StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream JavaDoc(buf.toString().getBytes()));
178          connector.setInvokerLocator(locator.getLocatorURI());
179          connector.setConfiguration(xml.getDocumentElement());
180          ObjectName JavaDoc obj = new ObjectName JavaDoc("jboss.remoting:type=Connector,transport=" + locator.getProtocol());
181          server.registerMBean(connector, obj);
182          //connector.create();
183
connector.start();
184
185          //Need to set new domain for identity
186
server.registerMBean(detector, new ObjectName JavaDoc("remoting:type=JNDIDetector"));
187
188          // set config info for detector and start it.
189
detector.setPort(detectorPort);
190          detector.setHost(detectorHost);
191          detector.setContextFactory(contextFactory);
192          detector.setURLPackage(urlPackage);
193          detector.start();
194       }
195       catch(Exception JavaDoc e)
196       {
197          e.printStackTrace();
198       }
199
200       return registry;
201    }
202
203    public static void main(String JavaDoc[] 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 JavaDoc sPort = args[0];
212          int port = Integer.parseInt(sPort);
213          String JavaDoc host = args[1];
214          test = new JNDIDetectorTest1("JNDIDetectorTest1", port, host);
215       }
216       else if(args.length == 3)
217       {
218          String JavaDoc sInstances = args[0];
219          int instances = Integer.parseInt(sInstances);
220          String JavaDoc sPort = args[1];
221          int port = Integer.parseInt(sPort);
222          String JavaDoc 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       //test.testDetectors();
234

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