KickJava   Java API By Example, From Geeks To Geeks.

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


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.StatefulRemote;
12 import org.jboss.ejb3.test.clusteredsession.NodeAnswer;
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 SFSB for load-balancing and failover behaviour
23  *
24  * @author Ben.Wang@jboss.org
25  * @version $Revision: 46483 $
26  */

27 public class PassivationUnitTestCase 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 PassivationUnitTestCase (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(PassivationUnitTestCase.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     */

64    public void testStatefulPassivation()
65       throws Exception JavaDoc
66    {
67       log.info("+++ testStatefulPassivation");
68
69       // Connect to the server0 JNDI
70
InitialContext JavaDoc ctx = getInitialContext(0);
71
72       getLog().debug("==================================");
73       getLog().debug(++PassivationUnitTestCase.test +"- "
74               +"Looking up testStateful...");
75       StatefulRemote remote = (StatefulRemote) ctx.lookup("testStateful/remote");
76
77       remote.reset();
78       remote.setState("hello");
79       sleep_(11000);
80       assertEquals("hello", remote.getState());
81       assertEquals(1, remote.getPrePassivate());
82       assertEquals(1, remote.getPostActivate());
83
84       sleep_(11000);
85       remote.remove();
86
87    }
88
89    /** This is to test failover with passivation
90     */

91    public void XtestFailoverStatefulPassivation()
92       throws Exception JavaDoc
93    {
94       log.info("+++ testFiloverStatefulPassivation");
95
96       // Connect to the server0 JNDI
97
InitialContext JavaDoc ctx = getInitialContext(0);
98
99       getLog().debug("==================================");
100       getLog().debug(++PassivationUnitTestCase.test +"- "
101               +"Looking up testStateful...");
102       StatefulRemote stateful = (StatefulRemote) ctx.lookup("testStateful/remote");
103       stateful.reset();
104
105       stateful.setName("The Code");
106       NodeAnswer node1 = stateful.getNodeState ();
107       getLog ().debug ("Node 1 ID: " +node1);
108
109       assertEquals("Counter: ", 1, stateful.increment());
110       assertEquals("Counter: ", 2, stateful.increment());
111       sleep_(10000);
112
113       // Now we switch to the other node, simulating a failure on node 1
114
//
115
stateful.setUpFailover("once");
116       NodeAnswer node2 = stateful.getNodeState ();
117       assertNotNull("State node: ", node2);
118       getLog ().debug ("Node 2 ID : " +node2);
119
120       assertNotSame ("No failover has occured!", node1.nodeId, node2.nodeId);
121
122       assertEquals ("Node 1: ", "The Code", node1.answer);
123       assertEquals ("Node 2: ", "The Code", node2.answer);
124
125       stateful.resetActivationCounter(); // This will activate the bean.
126
sleep_(10000); // let it get passivated again.
127
assertEquals("Counter: ", 3, stateful.increment());
128       assertEquals("Counter: ", 4, stateful.increment());
129       assertEquals(1, stateful.getPostActivate());
130       assertEquals(1, stateful.getPrePassivate());
131
132       stateful.remove();
133    }
134
135    protected void sleep_(long msec)
136    {
137       try {
138          Thread.sleep(msec);
139       } catch (InterruptedException JavaDoc e) {
140          e.printStackTrace();
141       }
142    }
143 }
144
Popular Tags