KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > simplecluster > unit > SimpleClusterUnitTestCase


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

8
9 package org.jboss.ejb3.test.simplecluster.unit;
10
11 //import java.rmi.*;
12

13
14 import java.util.Properties JavaDoc;
15
16 import javax.management.ObjectName JavaDoc;
17 import javax.naming.InitialContext JavaDoc;
18
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21
22 import org.jboss.ejb3.test.simplecluster.Session;
23 import org.jboss.ejb3.test.simplecluster.StatefulRemote;
24 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
25 import org.jboss.test.JBossClusteredTestCase;
26 import org.jboss.test.JBossTestClusteredSetup;
27
28 /**
29  * Sample client for the jboss container.
30  *
31  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
32  * @version $Id: SimpleClusterUnitTestCase.java,v 1.1.2.3 2005/06/21 04:47:47 bill Exp $
33  */

34
35 public class SimpleClusterUnitTestCase
36 extends JBossClusteredTestCase
37 {
38    org.apache.log4j.Category log = getLog();
39
40    static String JavaDoc stoppedAddress;
41    static Properties JavaDoc prop = new Properties JavaDoc();
42
43
44    public SimpleClusterUnitTestCase(String JavaDoc name)
45    {
46       super(name);
47
48       prop.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
49       prop.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
50    }
51
52    protected static void sleepThread(long msecs)
53    {
54       try {
55          Thread.sleep(msecs);
56       } catch (InterruptedException JavaDoc e) {
57          e.printStackTrace();
58       }
59    }
60
61
62    public void testAll() throws Exception JavaDoc
63    {
64       System.out.println("Starting test" + new java.util.Date JavaDoc());
65       String JavaDoc node0 = System.getProperty("jbosstest.cluster.node0");
66       String JavaDoc node1 = System.getProperty("jbosstest.cluster.node1");
67
68       prop.put("java.naming.provider.url", "jnp://" + node0 + ":1099");
69
70       InitialContext JavaDoc ctx = new InitialContext JavaDoc(prop);
71
72       StatefulRemote stateful = (StatefulRemote) ctx.lookup(StatefulRemote.class.getName());
73       Session stateless = (Session)ctx.lookup(Session.class.getName());
74
75       int last = 0;
76       for (int i = 0 ; i < 20 ; i++)
77       {
78          stateless.test();
79          int current = stateful.increment();
80          System.out.println("Got value: " + current);
81          assertEquals("Wrong return value", current, last + 1);
82          last = current;
83          sleepThread(500);
84
85          if (i == 10)
86          {
87             takeDownActiveInstance(stateful, prop);
88             sleepThread(2000);
89          }
90
91       }
92    }
93    
94    protected void takeDownActiveInstance(StatefulRemote stateful, Properties JavaDoc prop) throws Exception JavaDoc
95    {
96       String JavaDoc address = stateful.getHostAddress();
97       stopJBossInstance(address);
98       stoppedAddress = address;
99    }
100
101    protected static void stopJBossInstance(String JavaDoc address)throws Exception JavaDoc
102    {
103       prop.put("java.naming.provider.url", "jnp://" + address + ":1099");
104       InitialContext JavaDoc ctx = new InitialContext JavaDoc(prop);
105       RMIAdaptor server = (RMIAdaptor)ctx.lookup("jmx/rmi/RMIAdaptor");
106       server.invoke(new ObjectName JavaDoc("jboss.system:type=Server"), "shutdown", new Object JavaDoc[0], new String JavaDoc[0]);
107       
108       sleepThread(10000);
109    }
110    
111    public static Test suite() throws Exception JavaDoc
112    {
113       Class JavaDoc clazz = SimpleClusterUnitTestCase.class;
114       final String JavaDoc jarName = "simplecluster-test.ejb3";
115       TestSuite suite = new TestSuite();
116       
117       //Set up tests manually to make sure testServerFound happens first
118
//tearDown after this test will stop both servers causing testServerNotFound
119
//to fail if it happens last
120
suite.addTest(new SimpleClusterUnitTestCase("testServerFound"));
121       suite.addTest(new SimpleClusterUnitTestCase("testAll"));
122       
123       JBossTestClusteredSetup wrapper = new JBossTestClusteredSetup(suite)
124       {
125          protected void setUp() throws Exception JavaDoc
126          {
127             if (jarName == null) return;
128             deploymentException = null;
129             try
130             {
131                this.deploy(jarName);
132                this.getLog().debug("deployed package: " + jarName);
133             }
134             catch (Exception JavaDoc ex)
135             {
136                // Throw this in testServerFound() instead.
137
deploymentException = ex;
138             }
139                 
140             // wait a few seconds so that the cluster stabilize
141
synchronized (this)
142             {
143                wait(2000);
144             }
145          }
146
147          protected void tearDown() throws Exception JavaDoc
148          {
149             String JavaDoc node0 = System.getProperty("jbosstest.cluster.node0");
150             String JavaDoc node1 = System.getProperty("jbosstest.cluster.node1");
151             if (node0.equals(stoppedAddress))
152             {
153                stopJBossInstance(node1);
154             }
155             else if (node1.equals(stoppedAddress))
156             {
157                stopJBossInstance(node0);
158             }
159          }
160       };
161       
162       return wrapper;
163    }
164 }
165
Popular Tags