KickJava   Java API By Example, From Geeks To Geeks.

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


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.Test;
10 import junit.framework.TestSuite;
11 import junit.textui.TestRunner;
12 import org.apache.log4j.Level;
13 import org.jboss.dtf.DistributedTestCase;
14 import org.jgroups.Address;
15 import org.jnp.server.Main;
16
17 import java.net.InetAddress JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 /**
23  * This should be used as the main test case for JNDI detector.
24  * It will start two JNDIDetectors in seperate instances. The first
25  * will detect the second and then the second will shutdown and the first
26  * will detect that the second is no longer present. This also requires
27  * this class to start an instance of the JNP
28  *
29  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>
30  */

31 public class JNDIDetectorTestCase extends DistributedTestCase
32 {
33    private List JavaDoc results = new ArrayList JavaDoc();
34
35    public JNDIDetectorTestCase(String JavaDoc name)
36    {
37       super(name);
38    }
39
40    protected void setUp() throws Exception JavaDoc
41    {
42       int instances = 3;
43       int port = 1099;
44       String JavaDoc host = InetAddress.getLocalHost().getHostName();
45
46       startJNP(port, host);
47
48       String JavaDoc clientcmd = "java -cp " + System.getProperty("java.class.path") +
49 // " -Djboss.mx.instanceid.dir=" + System.getProperty("jboss.mx.instanceid.local.dir") +
50
" " + JNDIDetectorTest1.class.getName() + " " + instances + " " +
51                          port + " " + host;
52       System.out.println("clientcmd: " + clientcmd);
53       String JavaDoc svrcmd = "java -cp " + System.getProperty("java.class.path") +
54 // " -Djboss.mx.instanceid.dir=" + System.getProperty("jboss.mx.instanceid.remote.dir") +
55
" " + JNDIDetectorTest2.class.getName() + " " + instances + " " +
56                       port + " " + host;
57       System.out.println("svrcmd: " + svrcmd);
58
59       final Process JavaDoc local = Runtime.getRuntime().exec(clientcmd);
60       final Process JavaDoc remote = Runtime.getRuntime().exec(svrcmd);
61
62    }
63
64    /**
65     * Have to start a JNDI server so both the JNDIDetectorTests have
66     * a server to connect to.
67     *
68     * @param port
69     * @param host
70     * @throws Exception
71     */

72    private void startJNP(int port, String JavaDoc host) throws Exception JavaDoc
73    {
74       // start JNDI server
75
Main JNDIServer = new Main();
76       JNDIServer.setPort(port);
77       JNDIServer.setBindAddress(host);
78       JNDIServer.start();
79       System.out.println("Started JNDI server on " + host + ":" + port);
80    }
81
82    protected void tearDown() throws Exception JavaDoc
83    {
84       //NO OP
85
}
86
87    public void testInvokers()
88    {
89       try
90       {
91          startup(3);
92          System.out.println("startup() called");
93          shutdown();
94          System.out.println("shutdown() called");
95       }
96       catch(Exception JavaDoc e)
97       {
98          e.printStackTrace();
99          assertTrue("Problem starting or stopping client/server processes.", false);
100       }
101       finally
102       {
103          // assert results
104
try
105          {
106             Thread.currentThread().sleep(120000);
107          }
108          catch(InterruptedException JavaDoc e)
109          {
110             e.printStackTrace();
111          }
112          System.out.println("results.size() = " + results.size());
113          if(results.size() > 0)
114          {
115             Iterator JavaDoc itr = results.iterator();
116             while(itr.hasNext())
117             {
118                String JavaDoc message = (String JavaDoc) itr.next();
119                assertTrue(message, false);
120             }
121          }
122          else
123          {
124             assertTrue("No test failures or errors.", true);
125          }
126
127       }
128    }
129
130    /**
131     * **********************************
132     * Driver callback for JUnit asserts *
133     * ***********************************
134     */

135    public void receiveAssert(Address source, String JavaDoc message)
136    {
137       super.receiveAssert(source, message);
138       results.add("Assert source: " + source + "\tmessage: " + message);
139    }
140
141    public static Test suite()
142    {
143       return new TestSuite(JNDIDetectorTestCase.class);
144    }
145
146    public static void main(String JavaDoc[] args)
147    {
148
149       org.apache.log4j.BasicConfigurator.configure();
150       org.apache.log4j.Category.getRoot().setLevel(Level.DEBUG);
151
152       //MultipleTestRunner runner = new MultipleTestRunner();
153
//runner.doRun(test, true);
154
TestRunner.run(suite());
155 // System.exit(0);
156
}
157
158
159 }
Popular Tags