KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > stateful > 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.stateful.unit;
9
10 import org.jboss.test.JBossTestCase;
11 import org.jboss.ejb3.test.stateful.nested.ParentStatefulRemote;
12
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import java.util.Date JavaDoc;
16
17 import junit.framework.Test;
18
19 /**
20  * Test nested SFSB for repeated passivation
21  *
22  * @author Ben.Wang@jboss.org
23  * @version $Revision: 45372 $
24  */

25 public class NestedBeanUnitTestCase extends JBossTestCase
26 {
27    static boolean deployed = false;
28    public static int test = 0;
29    static Date JavaDoc startDate = new Date JavaDoc();
30
31    protected final String JavaDoc namingFactory =
32    System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
33
34    protected final String JavaDoc providerURL =
35    System.getProperty(Context.PROVIDER_URL);
36
37    public NestedBeanUnitTestCase (String JavaDoc name)
38    {
39       super(name);
40    }
41
42    public static Test suite() throws Exception JavaDoc
43    {
44       return getDeploySetup(NestedBeanUnitTestCase.class, "stateful-test.jar");
45    }
46
47    public void testBasic()
48    throws Exception JavaDoc
49    {
50       getLog().debug(++NestedBeanUnitTestCase.test +"- "
51               +"Trying the context...");
52
53       // Connect to the server0 JNDI
54
InitialContext JavaDoc ctx = getInitialContext();
55
56       getLog().debug("Test Stateful Bean");
57       getLog().debug("==================================");
58       getLog().debug(++NestedBeanUnitTestCase.test +"- "
59               +"Looking up testBasic...");
60       ParentStatefulRemote stateful = (ParentStatefulRemote) ctx.lookup("testParentStateful/remote");
61
62       assertEquals("Counter: ", 1, stateful.increment());
63       assertEquals("Counter: ", 2, stateful.increment());
64
65       stateful.remove();
66       getLog().debug("ok");
67    }
68
69    public void testStatefulPassivation()
70    throws Exception JavaDoc
71    {
72       getLog().debug(++NestedBeanUnitTestCase.test +"- "+"Trying the context...");
73
74       InitialContext JavaDoc ctx = getInitialContext();
75
76       getLog().debug("Test Stateful Bean passivation");
77       getLog().debug("==================================");
78       getLog().debug(++NestedBeanUnitTestCase.test +"- "
79               +"Looking up testParentStateful...");
80       ParentStatefulRemote stateful = (ParentStatefulRemote) ctx.lookup("testParentStateful/remote");
81
82       assertEquals("Counter: ", 1, stateful.increment());
83       assertEquals("Counter: ", 2, stateful.increment());
84       _sleep(10000); // should passivated
85

86       assertEquals("Counter: ", 3, stateful.increment());
87       assertEquals("Counter: ", 4, stateful.increment());
88       assertEquals(1, stateful.getPrePassivate());
89       assertEquals(1, stateful.getPostActivate());
90
91       stateful.remove();
92       getLog().debug("ok");
93    }
94
95    public void testStatefulRepeatedPassivation()
96    throws Exception JavaDoc
97    {
98       getLog().debug(++NestedBeanUnitTestCase.test +"- "+"Trying the context...");
99
100       InitialContext JavaDoc ctx = getInitialContext();
101
102       getLog().debug("Test Stateful Bean passivation");
103       getLog().debug("==================================");
104       getLog().debug(++NestedBeanUnitTestCase.test +"- "
105               +"Looking up testParentStateful...");
106       ParentStatefulRemote stateful = (ParentStatefulRemote) ctx.lookup("testParentStateful/remote");
107       stateful.reset();
108
109       assertEquals("Counter: ", 1, stateful.increment());
110       assertEquals("Counter: ", 2, stateful.increment());
111       _sleep(10000); // should passivated
112

113       assertEquals("Counter: ", 3, stateful.increment());
114       assertEquals("Counter: ", 4, stateful.increment());
115       assertEquals(1, stateful.getPrePassivate());
116       assertEquals(1, stateful.getPostActivate());
117       _sleep(10000); // should passivated
118

119       assertEquals("Counter: ", 5, stateful.increment());
120       assertEquals("Counter: ", 6, stateful.increment());
121       assertEquals(2, stateful.getPrePassivate());
122       assertEquals(2, stateful.getPostActivate());
123
124       stateful.remove();
125       getLog().debug("ok");
126    }
127
128    protected void _sleep(long time)
129    {
130       try {
131          Thread.sleep(time);
132       } catch (InterruptedException JavaDoc e) {
133          e.printStackTrace();
134       }
135    }
136 }
137
Popular Tags