001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cts.test;
023:
024: import java.rmi.server.UnicastRemoteObject;
025: import java.io.ByteArrayOutputStream;
026: import java.io.ObjectOutputStream;
027: import java.io.ByteArrayInputStream;
028: import java.io.ObjectInputStream;
029: import java.util.Properties;
030: import javax.naming.InitialContext;
031: import javax.rmi.PortableRemoteObject;
032: import javax.ejb.Handle;
033: import javax.ejb.CreateException;
034:
035: import junit.framework.Test;
036:
037: import org.jboss.test.JBossTestCase;
038: import org.jboss.test.cts.interfaces.StatelessSession;
039: import org.jboss.test.cts.interfaces.StatelessSessionHome;
040: import org.jboss.test.cts.interfaces.StrictlyPooledSessionHome;
041: import org.jboss.test.cts.interfaces.StrictlyPooledSession;
042: import EDU.oswego.cs.dl.util.concurrent.CountDown;
043:
044: /** Basic conformance tests for stateless sessions
045: *
046: * @author kimptoc
047: * @author d_jencks converted to JBossTestCase and logging.
048: * @author Scott.Stark@jboss.org
049: * @version $Revision: 57211 $
050: */
051: public class StatelessSessionUnitTestCase extends JBossTestCase {
052: static final int MAX_SIZE = 20;
053: StatelessSession sessionBean;
054:
055: public StatelessSessionUnitTestCase(String name) {
056: super (name);
057: }
058:
059: protected void setUp() throws Exception {
060: InitialContext ctx = new InitialContext();
061: Object ref = ctx.lookup("ejbcts/StatelessSessionHome");
062: StatelessSessionHome home = (StatelessSessionHome) PortableRemoteObject
063: .narrow(ref, StatelessSessionHome.class);
064: sessionBean = home.create();
065: }
066:
067: protected void tearDown() throws Exception {
068: if (sessionBean != null)
069: sessionBean.remove();
070: }
071:
072: /**
073: * Method testBasicStatelessSession
074: * @throws Exception
075: */
076: public void testBasicStatelessSession() throws Exception {
077: getLog().debug("+++ testBasicStatelessSession()");
078: String result = sessionBean
079: .method1("testBasicStatelessSession");
080: // Test response
081: assertTrue(result.equals("testBasicStatelessSession"));
082: }
083:
084: /** Test of handle
085: * @throws Exception
086: */
087: public void testSessionHandle() throws Exception {
088: getLog().debug("+++ testSessionHandle()");
089: Handle beanHandle = sessionBean.getHandle();
090: ByteArrayOutputStream out = new ByteArrayOutputStream();
091: ObjectOutputStream oos = new ObjectOutputStream(out);
092: oos.writeObject(beanHandle);
093: oos.flush();
094: byte[] bytes = out.toByteArray();
095:
096: getLog().debug("Unserialize bean handle...");
097: ByteArrayInputStream in = new ByteArrayInputStream(bytes);
098: ObjectInputStream ois = new ObjectInputStream(in);
099: beanHandle = (Handle) ois.readObject();
100: StatelessSession theSession = (StatelessSession) beanHandle
101: .getEJBObject();
102: theSession.method1("Hello");
103: getLog().debug("Called method1 on handle session bean");
104: }
105:
106: /** Test of handle that is unmarshalled in a environment where
107: * new InitialContext() will not work. This must use the
108: * @throws Exception
109: */
110: public void testSessionHandleNoDefaultJNDI() throws Exception {
111: getLog().debug("+++ testSessionHandleNoDefaultJNDI()");
112:
113: /* We have to establish the JNDI env by creating a InitialContext with
114: the org.jboss.naming.NamingContextFactory. Normally this would be done
115: during the home lookup and session creation.
116: */
117: Properties homeProps = new Properties();
118: homeProps.setProperty("java.naming.factory.initial",
119: "org.jboss.naming.NamingContextFactory");
120: InitialContext ic = new InitialContext(homeProps);
121: Handle beanHandle = sessionBean.getHandle();
122: ByteArrayOutputStream out = new ByteArrayOutputStream();
123: ObjectOutputStream oos = new ObjectOutputStream(out);
124: oos.writeObject(beanHandle);
125: oos.flush();
126: byte[] bytes = out.toByteArray();
127:
128: Properties sysProps = System.getProperties();
129: Properties newProps = new Properties(sysProps);
130: newProps.setProperty("java.naming.factory.initial",
131: "badFactory");
132: newProps.setProperty("java.naming.provider.url",
133: "jnp://badhost:12345");
134: System.setProperties(newProps);
135: try {
136: getLog().debug("Unserialize bean handle...");
137: ByteArrayInputStream in = new ByteArrayInputStream(bytes);
138: ObjectInputStream ois = new ObjectInputStream(in);
139: beanHandle = (Handle) ois.readObject();
140: StatelessSession theSession = (StatelessSession) beanHandle
141: .getEJBObject();
142: theSession.method1("Hello");
143: getLog().debug("Called method1 on handle session bean");
144: } finally {
145: System.setProperties(sysProps);
146: }
147: }
148:
149: /** Test of accessing the home interface from the remote interface in an env
150: * new InitialContext() will not work.
151: * @throws Exception
152: */
153: public void testHomeFromRemoteNoDefaultJNDI() throws Exception {
154: getLog().debug("+++ testHomeFromRemoteNoDefaultJNDI()");
155:
156: // Override the JNDI variables in the System properties
157: Properties sysProps = System.getProperties();
158: Properties newProps = new Properties(sysProps);
159: newProps.setProperty("java.naming.factory.initial",
160: "badFactory");
161: newProps.setProperty("java.naming.provider.url",
162: "jnp://badhost:12345");
163: System.setProperties(newProps);
164:
165: // Do a lookup of the home and create a remote using a custom env
166: Properties env = new Properties();
167: env.setProperty("java.naming.factory.initial", super
168: .getJndiInitFactory());
169: env.setProperty("java.naming.provider.url", super .getJndiURL());
170: try {
171: InitialContext ctx = new InitialContext(env);
172: Object ref = ctx.lookup("ejbcts/StatelessSessionHome");
173: StatelessSessionHome home = (StatelessSessionHome) PortableRemoteObject
174: .narrow(ref, StatelessSessionHome.class);
175: sessionBean = home.create();
176: StatelessSessionHome home2 = (StatelessSessionHome) sessionBean
177: .getEJBHome();
178: StatelessSession bean2 = home2.create();
179: bean2.remove();
180: } finally {
181: System.setProperties(sysProps);
182: }
183: }
184:
185: public void testLocalStatelessSession() throws Exception {
186: getLog().debug("+++ testLocalStatelessSession()");
187: sessionBean.testLocalHome();
188: }
189:
190: public void testClientCallback() throws Exception {
191: getLog().debug("+++ testClientCallback()");
192: ClientCallbackImpl callback = new ClientCallbackImpl();
193: UnicastRemoteObject.exportObject(callback);
194: sessionBean.callbackTest(callback, "testClientCallback");
195: // Test callback data
196: assertTrue(callback.wasCalled());
197: UnicastRemoteObject.unexportObject(callback, true);
198: }
199:
200: public void testStrictPooling() throws Exception {
201: CountDown done = new CountDown(MAX_SIZE);
202: InitialContext ctx = new InitialContext();
203: StrictlyPooledSessionHome home = (StrictlyPooledSessionHome) ctx
204: .lookup("ejbcts/StrictlyPooledStatelessBean");
205: SessionInvoker[] threads = new SessionInvoker[MAX_SIZE];
206: for (int n = 0; n < MAX_SIZE; n++) {
207: SessionInvoker t = new SessionInvoker(home, n, done,
208: getLog());
209: threads[n] = t;
210: t.start();
211: }
212: assertTrue("Acquired done", done.attempt(1500 * MAX_SIZE));
213:
214: for (int n = 0; n < MAX_SIZE; n++) {
215: SessionInvoker t = threads[n];
216: if (t.runEx != null) {
217: t.runEx.printStackTrace();
218: fail("SessionInvoker.runEx != null");
219: }
220: }
221: }
222:
223: public void testStrictPoolingException() throws Exception {
224: log.info("+++ testStrictPoolingException");
225: InitialContext ctx = new InitialContext();
226: StrictlyPooledSessionHome home = (StrictlyPooledSessionHome) ctx
227: .lookup("ejbcts/StrictlyPooledCreateExceptionBean");
228:
229: try {
230: StrictlyPooledSession bean = home.create();
231: fail("Create did not fail, bean=" + bean);
232: } catch (CreateException e) {
233: log.debug("Saw expected create failure", e);
234: }
235:
236: for (int n = 0; n < MAX_SIZE; n++) {
237: StrictlyPooledSession bean = home.create();
238: bean.methodA();
239: bean.remove();
240: }
241: }
242:
243: public static Test suite() throws Exception {
244: return getDeploySetup(StatelessSessionUnitTestCase.class,
245: "cts.jar");
246: }
247:
248: }
|