KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.jboss.ejb3.test.clusteredsession.NodeAnswer;
12 import org.jboss.ejb3.test.clusteredsession.nested.ParentStatefulRemote;
13
14 import javax.naming.Context JavaDoc;
15 import javax.naming.InitialContext JavaDoc;
16 import java.util.Date JavaDoc;
17 import java.util.Properties JavaDoc;
18
19 import junit.framework.Test;
20
21 /**
22  * Test nested SFSB for load-balancing and failover behaviour
23  *
24  * @author Ben.Wang@jboss.org
25  * @version $Revision: 46483 $
26  */

27 public class NestedBeanUnitTestCase 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 NestedBeanUnitTestCase (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(NestedBeanUnitTestCase.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
63    public void testBasic()
64    throws Exception JavaDoc
65    {
66       getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "
67               +"Trying the context...");
68
69       // Connect to the server0 JNDI
70
InitialContext JavaDoc ctx = getInitialContext(0);
71
72       getLog().debug("Test Nested Stateful Bean");
73       getLog().debug("==================================");
74       getLog().debug(++org.jboss.ejb3.test.clusteredsession.unit.BeanUnitTestCase.test +"- "
75               +"Looking up testBasic...");
76       ParentStatefulRemote stateful = (ParentStatefulRemote) ctx.lookup("testParentStateful/remote");
77
78       stateful.reset();
79       stateful.setName("The Code");
80       _sleep(300);
81
82       NodeAnswer node1 = stateful.getNodeState ();
83       getLog ().debug ("Node 1 ID: " +node1);
84
85       assertEquals("Counter: ", 1, stateful.increment());
86       assertEquals("Counter: ", 2, stateful.increment());
87
88       stateful.remove();
89       getLog().debug("ok");
90       _sleep(11000);
91    }
92
93    public void testPassivation()
94    throws Exception JavaDoc
95    {
96       getLog().debug(++NestedBeanUnitTestCase.test +"- "
97               +"Trying the context...");
98
99       // Connect to the server0 JNDI
100
InitialContext JavaDoc ctx = getInitialContext(0);
101
102       getLog().debug("Test Nested Stateful Bean");
103       getLog().debug("==================================");
104       getLog().debug(++NestedBeanUnitTestCase.test +"- "
105               +"Looking up remote...");
106       ParentStatefulRemote stateful = (ParentStatefulRemote) ctx.lookup("testParentStateful/remote");
107
108       stateful.setName("The Code");
109       _sleep(300);
110
111       NodeAnswer node1 = stateful.getNodeState ();
112       getLog ().debug ("Node 1 ID: " +node1);
113 // _sleep(300000);
114
assertEquals("Counter: ", 1, stateful.increment());
115       assertEquals("Counter: ", 2, stateful.increment());
116       assertEquals("SLSB state: ", "test", stateful.getStatelessState().getAnswer());
117       stateful.reset();
118       _sleep(11000);
119       assertEquals("Counter: ", 3, stateful.increment());
120       assertEquals("SLSB state: ", "test", stateful.getStatelessState().getAnswer());
121       assertEquals(1, stateful.getPrePassivate());
122       assertEquals(1, stateful.getPostActivate());
123       assertEquals(1, stateful.getNestedPrePassivate());
124       assertEquals(1, stateful.getNestedPostActivate());
125
126       stateful.remove();
127       getLog().debug("ok: done");
128       _sleep(12000);
129    }
130
131    public void testStatefulBeanCounterFailover()
132    throws Exception JavaDoc
133    {
134       getLog().debug(++NestedBeanUnitTestCase.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(++NestedBeanUnitTestCase.test +"- "
142               +"Looking up testParentStateful...");
143       ParentStatefulRemote stateful = (ParentStatefulRemote) ctx.lookup("testParentStateful/remote");
144
145       stateful.setName("The Code");
146       NodeAnswer node1 = stateful.getNodeState ();
147       getLog ().debug ("Node 1 ID: " +node1);
148
149       assertEquals("Counter: ", 1, stateful.increment());
150       assertEquals("Counter: ", 2, stateful.increment());
151       _sleep(300);
152
153       // Now we switch to the other node, simulating a failure on node 1
154
//
155
stateful.setUpFailover("once");
156       NodeAnswer node2 = stateful.getNodeState ();
157       assertNotNull("State node: ", node2);
158       getLog ().debug ("Node 2 ID : " +node2);
159
160       assertNotSame ("No failover has occured!", node1.nodeId, node2.nodeId);
161
162       assertEquals ("Node 1: ", "The Code", node1.answer);
163       assertEquals ("Node 2: ", "The Code", node2.answer);
164
165       assertEquals("Counter: ", 3, stateful.increment());
166       assertEquals("Counter: ", 4, stateful.increment());
167
168       stateful.remove();
169       getLog().debug("ok");
170    }
171
172    protected void _sleep(long time)
173    {
174       try {
175          Thread.sleep(time);
176       } catch (InterruptedException JavaDoc e) {
177          e.printStackTrace();
178       }
179    }
180 }
181
Popular Tags