KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > session > test > EjbRemoveUnitTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.session.test;
23
24 import java.rmi.RemoteException JavaDoc;
25
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28
29 import org.jboss.test.JBossTestCase;
30 import org.jboss.test.session.interfaces.CountedSession;
31 import org.jboss.test.session.interfaces.CountedSessionHome;
32 import org.jboss.test.session.interfaces.CounterSession;
33 import org.jboss.test.session.interfaces.CounterSessionHome;
34
35 /**
36  * Test that ejbRemove is called.
37  *
38  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
39  * @version $Revision: 56759 $
40  */

41 public class EjbRemoveUnitTestCase extends JBossTestCase
42 {
43    public static Test suite() throws Exception JavaDoc
44    {
45       // JBAS-3607, the execution order of tests in this test case is important
46
// so it must be defined explicitly when running under some JVMs
47
TestSuite suite = new TestSuite();
48       suite.addTest(new EjbRemoveUnitTestCase("testEjbRemoveCalledForEveryCall"));
49       suite.addTest(new EjbRemoveUnitTestCase("testEjbRemoveNotCalledForEveryCall"));
50       suite.addTest(new EjbRemoveUnitTestCase("testEjbRemoveMultiThread"));
51       
52       return JBossTestCase.getDeploySetup(suite, "test-session-remove.jar");
53    }
54    
55    public EjbRemoveUnitTestCase(String JavaDoc name)
56    {
57       super(name);
58    }
59
60    /**
61     * In this test, pooling is disabled (MaximumSize==0) so call
62     * to the CountedSession bean should create a new instance,
63     * (ejbCreate()) use it but then throw it away (ejbRemove())
64     * ather than putting it back to the pool.
65     * @throws Exception
66     */

67    public void testEjbRemoveCalledForEveryCall() throws Exception JavaDoc
68    {
69       CounterSessionHome counterHome = (CounterSessionHome)getInitialContext().lookup("CounterSession");
70       CountedSessionHome countedHome = (CountedSessionHome)getInitialContext().lookup("CountedSession");
71       
72       CounterSession counter = counterHome.create();
73       counter.clearCounters();
74       
75       CountedSession counted = countedHome.create();
76       assertEquals("createCounter", 1, counter.getCreateCounter());
77       assertEquals("removeCounter", 1, counter.getRemoveCounter());
78       
79       counted.doSomething(0);
80       assertEquals("createCounter", 2, counter.getCreateCounter());
81       assertEquals("removeCounter", 2, counter.getRemoveCounter());
82       
83       counted.remove();
84       assertEquals("createCounter", 3, counter.getCreateCounter());
85       assertEquals("removeCounter", 3, counter.getRemoveCounter());
86    }
87    
88    /**
89     * In this test, pooling is enabled (Maximum==5) so after the
90     * initial create() call, the same instance should be used
91     * from the pool, and only removed when the app gets undeployed
92     * @throws Exception
93     */

94    public void testEjbRemoveNotCalledForEveryCall() throws Exception JavaDoc
95    {
96       CounterSessionHome counterHome = (CounterSessionHome)getInitialContext().lookup("CounterSession");
97       CountedSessionHome countedHome = (CountedSessionHome)getInitialContext().lookup("CountedSession2");
98       
99       CounterSession counter = counterHome.create();
100       counter.clearCounters();
101       
102       CountedSession counted = countedHome.create();
103       assertEquals("createCounter", 1, counter.getCreateCounter());
104       assertEquals("removeCounter", 0, counter.getRemoveCounter());
105       
106       counted.doSomething(0);
107       assertEquals("createCounter", 1, counter.getCreateCounter());
108       assertEquals("removeCounter", 0, counter.getRemoveCounter());
109       
110       counted.remove();
111       assertEquals("createCounter", 1, counter.getCreateCounter());
112       assertEquals("removeCounter", 0, counter.getRemoveCounter());
113    }
114
115    public void testEjbRemoveMultiThread() throws Exception JavaDoc
116    {
117       CounterSessionHome counterHome = (CounterSessionHome)getInitialContext().lookup("CounterSession");
118       CountedSessionHome countedHome = (CountedSessionHome)getInitialContext().lookup("CountedSession2");
119       
120       CounterSession counter = counterHome.create();
121       counter.clearCounters();
122       
123       final CountedSession counted = countedHome.create();
124       
125       Runnable JavaDoc runnable = new Runnable JavaDoc() {
126          public void run()
127          {
128             try
129             {
130                // introduce 1sec delay
131
counted.doSomething(1000);
132             }
133             catch (RemoteException JavaDoc e)
134             {
135                // ignore
136
}
137          }
138       };
139
140       for (int i = 0; i < 10; i++)
141       {
142          new Thread JavaDoc(runnable).start();
143       }
144       
145       // since the session pool is Maximum==5, using 10 concurrent
146
// requests ensures at least 5 instances will have to be created
147
// (ejbCreate() to handle the load. Those 5 extra instances, will also have
148
// to be destroyed (ejbRemove()) upon return, because the pool will
149
// only store the first 5
150

151       // wait for all 10 threads to finish
152
Thread.sleep(2000);
153       
154       assertTrue("createCounter >= 5", counter.getCreateCounter() >= 5);
155       assertTrue("removeCounter == 5", counter.getRemoveCounter() == 5);
156    }
157 }
158
Popular Tags