KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > clusteredsession > unit > BeanUnitTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.ejb3.test.clusteredsession.unit;
9
10 import org.jboss.test.JBossClusteredTestCase;
11
12 import javax.naming.Context JavaDoc;
13 import javax.naming.InitialContext JavaDoc;
14 import java.util.Date JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import junit.framework.Test;
18 import org.jboss.ejb3.test.clusteredsession.StatefulRemote;
19 import org.jboss.ejb3.test.clusteredsession.NodeAnswer;
20
21 /**
22  * Test SFSB for load-balancing and failover behaviour
23  *
24  * @author Ben.Wang@jboss.org
25  * @version $Revision: 46483 $
26  */

27 public class BeanUnitTestCase extends JBossClusteredTestCase
28 {
29    static boolean deployed = false;
30    public static int test = 0;
31    static Date JavaDoc startDate = new Date JavaDoc();
32
33    protected final String JavaDoc namingFactory =
34    System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
35
36    protected final String JavaDoc providerURL =
37    System.getProperty(Context.PROVIDER_URL);
38
39    public BeanUnitTestCase (String JavaDoc name)
40    {
41       super(name);
42    }
43
44    public static Test suite() throws Exception JavaDoc
45    {
46       final String JavaDoc jarName = "clusteredsession-test.jar";
47       Test t1 = JBossClusteredTestCase.getDeploySetup(BeanUnitTestCase.class,
48               jarName);
49       return t1;
50    }
51
52    protected InitialContext JavaDoc getInitialContext(int node) throws Exception JavaDoc {
53       // Connect to the server0 JNDI
54
String JavaDoc[] urls = getNamingURLs();
55       Properties JavaDoc env1 = new Properties JavaDoc();
56       env1.setProperty(Context.INITIAL_CONTEXT_FACTORY,
57          "org.jnp.interfaces.NamingContextFactory");
58       env1.setProperty(Context.PROVIDER_URL, urls[node]);
59       return new InitialContext JavaDoc(env1);
60    }
61
62    public void testBasic()
63    throws Exception JavaDoc
64    {
65       getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "
66               +"Trying the context...");
67
68       // Connect to the server0 JNDI
69
InitialContext JavaDoc ctx = getInitialContext(0);
70
71       getLog().debug("Test Stateful Bean");
72       getLog().debug("==================================");
73       getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "
74               +"Looking up testBasic...");
75       StatefulRemote stateful = (StatefulRemote) ctx.lookup("testStateful/remote");
76
77       stateful.setName("The Code");
78       _sleep(300);
79
80       NodeAnswer node1 = stateful.getNodeState ();
81       getLog ().debug ("Node 1 ID: " +node1);
82
83       assertEquals("Counter: ", 1, stateful.increment());
84       assertEquals("Counter: ", 2, stateful.increment());
85
86       stateful.remove();
87       getLog().debug("ok");
88    }
89
90    public void testStatefulBeanCounterFailover()
91    throws Exception JavaDoc
92    {
93       getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "+"Trying the context...");
94
95       // Connect to the server0 JNDI
96
InitialContext JavaDoc ctx = getInitialContext(0);
97
98       getLog().debug("Test Stateful Bean Failover");
99       getLog().debug("==================================");
100       getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "
101               +"Looking up testStateful...");
102       StatefulRemote stateful = (StatefulRemote) ctx.lookup("testStateful/remote");
103
104       stateful.setName("The Code");
105       NodeAnswer node1 = stateful.getNodeState ();
106       getLog ().debug ("Node 1 ID: " +node1);
107
108       assertEquals("Counter: ", 1, stateful.increment());
109       assertEquals("Counter: ", 2, stateful.increment());
110       _sleep(300);
111
112       // Now we switch to the other node, simulating a failure on node 1
113
//
114
stateful.setUpFailover("once");
115       NodeAnswer node2 = stateful.getNodeState ();
116       assertNotNull("State node: ", node2);
117       getLog ().debug ("Node 2 ID : " +node2);
118
119       assertNotSame ("No failover has occured!", node1.nodeId, node2.nodeId);
120
121       assertEquals ("Node 1: ", "The Code", node1.answer);
122       assertEquals ("Node 2: ", "The Code", node2.answer);
123
124       assertEquals("Counter: ", 3, stateful.increment());
125       assertEquals("Counter: ", 4, stateful.increment());
126
127       stateful.remove();
128       getLog().debug("ok");
129    }
130
131    public void testStatefulBeanFailover()
132    throws Exception JavaDoc
133    {
134       getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "+"Trying the context...");
135
136       // Connect to the server0 JNDI
137
InitialContext JavaDoc ctx = getInitialContext(0);
138
139       getLog().debug("Test Stateful Bean Failover");
140       getLog().debug("==================================");
141       getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "
142               +"Looking up testStateful...");
143       StatefulRemote stateful = (StatefulRemote) ctx.lookup("testStateful/remote");
144
145       stateful.setName("Bupple-Dupple");
146       _sleep(300);
147
148       NodeAnswer node1 = stateful.getNodeState ();
149       getLog ().debug ("Node 1 ID: " +node1);
150
151       // Now we switch to the other node, simulating a failure on node 1
152
//
153
stateful.setUpFailover("once");
154       NodeAnswer node2 = stateful.getNodeState ();
155       assertNotNull("State node: ", node2);
156       getLog ().debug ("Node 2 ID : " +node2);
157
158       assertNotSame ("No failover has occured!", node1.nodeId, node2.nodeId);
159
160       assertEquals ("Node 1: ", "Bupple-Dupple", node1.answer);
161       assertEquals ("Node 2: ", "Bupple-Dupple", node2.answer);
162
163       // we change our name to see if it replicates to node 1
164
//
165
stateful.setName ("Changed");
166       _sleep(300);
167
168       // now we travel back on node 1
169
//
170
stateful.setUpFailover("once");
171       node1 = stateful.getNodeState ();
172       assertNotNull("State node id should not be null: ", node1.nodeId);
173       getLog ().debug (node1);
174
175       _sleep(300);
176       assertNotSame ("No failover has occured!", node1.nodeId, node2.nodeId);
177
178       assertEquals ("Value is not identical on replicated node", "Changed", node1.answer );
179
180       stateful.remove();
181       getLog().debug("ok");
182    }
183
184    protected void _sleep(long time)
185    {
186       try {
187          Thread.sleep(time);
188       } catch (InterruptedException JavaDoc e) {
189          e.printStackTrace();
190       }
191    }
192 }
193
Popular Tags