KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cts > test > StatelessSessionUnitTestCase


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.cts.test;
23
24
25 import java.rmi.server.UnicastRemoteObject JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.util.Properties JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.rmi.PortableRemoteObject JavaDoc;
33 import javax.ejb.Handle JavaDoc;
34 import javax.ejb.CreateException JavaDoc;
35
36 import junit.framework.Test;
37
38 import org.jboss.test.JBossTestCase;
39 import org.jboss.test.cts.interfaces.StatelessSession;
40 import org.jboss.test.cts.interfaces.StatelessSessionHome;
41 import org.jboss.test.cts.interfaces.StrictlyPooledSessionHome;
42 import org.jboss.test.cts.interfaces.StrictlyPooledSession;
43 import EDU.oswego.cs.dl.util.concurrent.CountDown;
44
45 /** Basic conformance tests for stateless sessions
46  *
47  * @author kimptoc
48  * @author d_jencks converted to JBossTestCase and logging.
49  * @author Scott.Stark@jboss.org
50  * @version $Revision: 37406 $
51  */

52 public class StatelessSessionUnitTestCase
53       extends JBossTestCase
54 {
55    static final int MAX_SIZE = 20;
56    StatelessSession sessionBean;
57
58    public StatelessSessionUnitTestCase(String JavaDoc name)
59    {
60       super(name);
61    }
62
63    protected void setUp() throws Exception JavaDoc
64    {
65       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
66       Object JavaDoc ref = ctx.lookup("ejbcts/StatelessSessionHome");
67       StatelessSessionHome home = (StatelessSessionHome)
68             PortableRemoteObject.narrow(ref, StatelessSessionHome.class);
69       sessionBean = home.create();
70    }
71
72    protected void tearDown() throws Exception JavaDoc
73    {
74       if (sessionBean != null)
75          sessionBean.remove();
76    }
77
78    /**
79     * Method testBasicStatelessSession
80     * @throws Exception
81     */

82    public void testBasicStatelessSession()
83          throws Exception JavaDoc
84    {
85       getLog().debug("+++ testBasicStatelessSession()");
86       String JavaDoc result = sessionBean.method1("testBasicStatelessSession");
87       // Test response
88
assertTrue(result.equals("testBasicStatelessSession"));
89    }
90
91    /** Test of handle
92     * @throws Exception
93     */

94    public void testSessionHandle()
95          throws Exception JavaDoc
96    {
97       getLog().debug("+++ testSessionHandle()");
98       Handle JavaDoc beanHandle = sessionBean.getHandle();
99       ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
100       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(out);
101       oos.writeObject(beanHandle);
102       oos.flush();
103       byte[] bytes = out.toByteArray();
104
105       getLog().debug("Unserialize bean handle...");
106       ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(bytes);
107       ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(in);
108       beanHandle = (Handle JavaDoc) ois.readObject();
109       StatelessSession theSession = (StatelessSession) beanHandle.getEJBObject();
110       theSession.method1("Hello");
111       getLog().debug("Called method1 on handle session bean");
112    }
113
114    /** Test of handle that is unmarshalled in a environment where
115     * new InitialContext() will not work. This must use the
116     * @throws Exception
117     */

118    public void testSessionHandleNoDefaultJNDI()
119          throws Exception JavaDoc
120    {
121       getLog().debug("+++ testSessionHandleNoDefaultJNDI()");
122
123       /* We have to establish the JNDI env by creating a InitialContext with
124       the org.jboss.naming.NamingContextFactory. Normally this would be done
125       during the home lookup and session creation.
126       */

127       Properties JavaDoc homeProps = new Properties JavaDoc();
128       homeProps.setProperty("java.naming.factory.initial", "org.jboss.naming.NamingContextFactory");
129       InitialContext JavaDoc ic = new InitialContext JavaDoc(homeProps);
130       Handle JavaDoc beanHandle = sessionBean.getHandle();
131       ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
132       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(out);
133       oos.writeObject(beanHandle);
134       oos.flush();
135       byte[] bytes = out.toByteArray();
136
137       Properties JavaDoc sysProps = System.getProperties();
138       Properties JavaDoc newProps = new Properties JavaDoc(sysProps);
139       newProps.setProperty("java.naming.factory.initial", "badFactory");
140       newProps.setProperty("java.naming.provider.url", "jnp://badhost:12345");
141       System.setProperties(newProps);
142       try
143       {
144          getLog().debug("Unserialize bean handle...");
145          ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(bytes);
146          ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(in);
147          beanHandle = (Handle JavaDoc) ois.readObject();
148          StatelessSession theSession = (StatelessSession) beanHandle.getEJBObject();
149          theSession.method1("Hello");
150          getLog().debug("Called method1 on handle session bean");
151       }
152       finally
153       {
154          System.setProperties(sysProps);
155       }
156    }
157
158    /** Test of accessing the home interface from the remote interface in an env
159     * new InitialContext() will not work.
160     * @throws Exception
161     */

162    public void testHomeFromRemoteNoDefaultJNDI()
163          throws Exception JavaDoc
164    {
165       getLog().debug("+++ testHomeFromRemoteNoDefaultJNDI()");
166
167       // Override the JNDI variables in the System properties
168
Properties JavaDoc sysProps = System.getProperties();
169       Properties JavaDoc newProps = new Properties JavaDoc(sysProps);
170       newProps.setProperty("java.naming.factory.initial", "badFactory");
171       newProps.setProperty("java.naming.provider.url", "jnp://badhost:12345");
172       System.setProperties(newProps);
173
174       // Do a lookup of the home and create a remote using a custom env
175
Properties JavaDoc env = new Properties JavaDoc();
176       env.setProperty("java.naming.factory.initial", super.getJndiInitFactory());
177       env.setProperty("java.naming.provider.url", super.getJndiURL());
178       try
179       {
180          InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
181          Object JavaDoc ref = ctx.lookup("ejbcts/StatelessSessionHome");
182          StatelessSessionHome home = (StatelessSessionHome)
183                PortableRemoteObject.narrow(ref, StatelessSessionHome.class);
184          sessionBean = home.create();
185          StatelessSessionHome home2 = (StatelessSessionHome) sessionBean.getEJBHome();
186          StatelessSession bean2 = home2.create();
187          bean2.remove();
188       }
189       finally
190       {
191          System.setProperties(sysProps);
192       }
193    }
194
195    public void testLocalStatelessSession()
196          throws Exception JavaDoc
197    {
198       getLog().debug("+++ testLocalStatelessSession()");
199       sessionBean.testLocalHome();
200    }
201
202    public void testClientCallback()
203          throws Exception JavaDoc
204    {
205       getLog().debug("+++ testClientCallback()");
206       ClientCallbackImpl callback = new ClientCallbackImpl();
207       UnicastRemoteObject.exportObject(callback);
208       sessionBean.callbackTest(callback, "testClientCallback");
209       // Test callback data
210
assertTrue(callback.wasCalled());
211       UnicastRemoteObject.unexportObject(callback, true);
212    }
213
214    public void testStrictPooling() throws Exception JavaDoc
215    {
216       CountDown done = new CountDown(MAX_SIZE);
217       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
218       StrictlyPooledSessionHome home = (StrictlyPooledSessionHome)
219             ctx.lookup("ejbcts/StrictlyPooledStatelessBean");
220       SessionInvoker[] threads = new SessionInvoker[MAX_SIZE];
221       for (int n = 0; n < MAX_SIZE; n++)
222       {
223          SessionInvoker t = new SessionInvoker(home, n, done, getLog());
224          threads[n] = t;
225          t.start();
226       }
227       assertTrue("Acquired done", done.attempt(1500 * MAX_SIZE));
228
229       for (int n = 0; n < MAX_SIZE; n++)
230       {
231          SessionInvoker t = threads[n];
232          if (t.runEx != null)
233          {
234             t.runEx.printStackTrace();
235             fail("SessionInvoker.runEx != null");
236          }
237       }
238    }
239
240    public void testStrictPoolingException() throws Exception JavaDoc
241    {
242       log.info("+++ testStrictPoolingException");
243       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
244       StrictlyPooledSessionHome home = (StrictlyPooledSessionHome)
245             ctx.lookup("ejbcts/StrictlyPooledCreateExceptionBean");
246
247       try
248       {
249          StrictlyPooledSession bean = home.create();
250          fail("Create did not fail, bean="+bean);
251       }
252       catch(CreateException JavaDoc e)
253       {
254          log.debug("Saw expected create failure", e);
255       }
256
257       for (int n = 0; n < MAX_SIZE; n++)
258       {
259          StrictlyPooledSession bean = home.create();
260          bean.methodA();
261          bean.remove();
262       }
263    }
264
265    public static Test suite() throws Exception JavaDoc
266    {
267       return getDeploySetup(StatelessSessionUnitTestCase.class, "cts.jar");
268    }
269
270 }
271
Popular Tags