001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: A_ClientView.java 7106 2005-07-26 15:11:53Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.session;
027:
028: import java.io.ByteArrayInputStream;
029: import java.io.ByteArrayOutputStream;
030: import java.io.ObjectInputStream;
031: import java.io.ObjectOutputStream;
032: import java.rmi.NoSuchObjectException;
033: import java.rmi.RemoteException;
034:
035: import javax.ejb.EJBException;
036: import javax.ejb.EJBObject;
037: import javax.ejb.RemoveException;
038: import javax.ejb.EJBMetaData;
039: import javax.ejb.Handle;
040: import javax.ejb.HomeHandle;
041: import javax.rmi.PortableRemoteObject;
042:
043: import org.objectweb.jonas.jtests.beans.local.Target;
044: import org.objectweb.jonas.jtests.beans.local.TargetSLHome;
045: import org.objectweb.jonas.jtests.util.JTestCase;
046:
047: /**
048: * This Class defines Test cases that can be run either SF or SL bean.
049: * These tests are related to the "Client View of a Session Bean"
050: * A session bean that supports both Local & Remote interface is used
051: * (TargetSL oe TargetSF in beans/local)
052: * But here are testcases that can be run via a non remote suite.
053: *
054: * @see EJB2.0 specification Chapter 6.
055: * @author Ph Coq, Ph Durieux
056: */
057:
058: public abstract class A_ClientView extends JTestCase {
059:
060: public A_ClientView(String name) {
061: super (name);
062: }
063:
064: /**
065: * @return TargetSLHome, that can be either SF or SL bean.
066: */
067: public abstract TargetSLHome getHome() throws Exception;
068:
069: /**
070: * Lookup a Home Object
071: *
072: * This test verify that we can look up a Home Object
073: * via the root context
074: */
075:
076: public void testHomeLookup() throws Exception {
077: TargetSLHome h = null;
078: h = getHome();
079: assertTrue(h != null);
080: }
081:
082: /**
083: * This test verify we can create and remove a session bean
084: */
085: public void testCreateRemove() throws Exception {
086: Target tr = getHome().create();
087: tr.remove();
088: }
089:
090: /**
091: * This test verify we can create and remove a session bean
092: */
093: public void testLongCreateRemove() throws Exception {
094: for (int i = 0; i < 500; i++) {
095: Target tr = getHome().create();
096: tr.remove();
097: }
098: }
099:
100: /**
101: * This test verify we can create, access to the bean via
102: * its remote interface and remove it
103: * It verifies we cannot access to the bean after remove
104: */
105: public void testBusinessMethod1() throws Exception {
106: Target tr = getHome().create();
107: assertEquals(10, tr.getTen());
108: tr.remove();
109: try {
110: tr.method2("Bye");
111: fail("NoSuchObjectException must be raised");
112: // } catch (NoSuchObjectException e) {
113: } catch (NoSuchObjectException e) {
114: }
115: }
116:
117: /**
118: * Same test than testBusinessMethod1 but two calls to businessmethod
119: */
120: public void testBusinessMethod2() throws Exception {
121: Target tr = getHome().create();
122: assertEquals(10, tr.getTen());
123: assertEquals(10, tr.getTen());
124: tr.remove();
125: }
126:
127: /**
128: * test 3 Remote Business Methods
129: */
130: public void testBusinessMethod3() throws Exception {
131: Target tr = getHome().create();
132: assertEquals(10, tr.getTen());
133: tr.method2("Hello");
134: assertEquals(10, tr.getTen());
135: tr.remove();
136: }
137:
138: /**
139: * test EJBObject.isIdentical on the same bean
140: */
141: public void testIsIdenticalOnSameBean() throws Exception {
142: Target tr = getHome().create();
143: assertTrue(tr.isIdentical(tr));
144: tr.remove();
145: }
146:
147: /**
148: * test EJBObject.getPrimary Key
149: * This test verify that a Remote Exception must be catched
150: */
151: public void testGetPrimaryKey() throws Exception {
152: Target tr = getHome().create();
153: try {
154: tr.getPrimaryKey();
155: fail("getPrimaryKey should raise RemoteException on a session bean");
156: } catch (RemoteException e) {
157: } finally {
158: tr.remove();
159: }
160: }
161:
162: /**
163: * test EJBHome.getEJBMetaData().getPrimaryKeyClass()
164: * This test verify that a EJBException must be catched
165: */
166: public void testGetPrimaryKeyClass() throws Exception {
167: try {
168: getHome().getEJBMetaData().getPrimaryKeyClass();
169: fail("getEJBMetaData().getPrimaryKeyClass should raise EJBException on a session bean home");
170: } catch (EJBException e) {
171: }
172: }
173:
174: /**
175: * test that EJBHome.remove(pk) should raise RemoteException (API) or
176: * RemoveException (spec EJB 2.0 6.3.2)
177: */
178: public void testRemoveByPK() throws Exception {
179: Object pk = null;
180: try {
181: getHome().remove(pk);
182: fail("getPrimaryKey should raise Exception on a session bean");
183: } catch (RemoteException e) {
184: } catch (RemoveException e) {
185: }
186: }
187:
188: /**
189: * test that EJBHome.getEJBMetaData()
190: * test we can obtain an handle for a session bean
191: */
192: public void testGetEJBMetaData() throws Exception {
193: EJBMetaData hh = getHome().getEJBMetaData();
194: }
195:
196: /**
197: * test that EJBHome.getHomeHandle()
198: * test we can obtain a handle for a session bean's home interface
199: */
200: public void testGetHomeHandle() throws Exception {
201: HomeHandle hh = getHome().getHomeHandle();
202: }
203:
204: /**
205: * test serialize/deserialize handle.
206: * @throws Exception
207: */
208: public void testSerializeHandle() throws Exception {
209: TargetSLHome tgh = getHome();
210: Target tr = tgh.create();
211: Handle h = tr.getHandle();
212: ObjectInputStream is = null;
213: ObjectOutputStream os = null;
214: ByteArrayOutputStream baos = new ByteArrayOutputStream();
215: os = new ObjectOutputStream(baos);
216: os.writeObject(h);
217: byte[] b = baos.toByteArray();
218: ByteArrayInputStream bais = new ByteArrayInputStream(b);
219: is = new ObjectInputStream(bais);
220: Handle deserializedHandle = (Handle) is.readObject();
221: EJBObject ejbObject = deserializedHandle.getEJBObject();
222: Target beanRef2 = (Target) PortableRemoteObject.narrow(
223: ejbObject, Target.class);
224: }
225:
226: /**
227: * test that EJBHome.removeHandle()
228: * test we can remove a session via its handle
229: * it verifies the bean cannot be accessed after remove and
230: * java.rmi.NoSuchObjectException is raised
231: */
232: public void testRemoveHandle() throws Exception {
233: TargetSLHome tgh = getHome();
234: Target tr = tgh.create();
235: Handle h = tr.getHandle();
236: tgh.remove(h);
237: try {
238: tr.method2("Bye");
239: fail("Cannot remove a session bean via its handle");
240: } catch (NoSuchObjectException e) {
241:
242: }
243: }
244:
245: }
|