KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > session > A_ClientView


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: A_ClientView.java,v 1.7 2005/07/26 15:11:53 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.session;
27
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32 import java.rmi.NoSuchObjectException JavaDoc;
33 import java.rmi.RemoteException JavaDoc;
34
35 import javax.ejb.EJBException JavaDoc;
36 import javax.ejb.EJBObject JavaDoc;
37 import javax.ejb.RemoveException JavaDoc;
38 import javax.ejb.EJBMetaData JavaDoc;
39 import javax.ejb.Handle JavaDoc;
40 import javax.ejb.HomeHandle JavaDoc;
41 import javax.rmi.PortableRemoteObject JavaDoc;
42
43 import org.objectweb.jonas.jtests.beans.local.Target;
44 import org.objectweb.jonas.jtests.beans.local.TargetSLHome;
45 import org.objectweb.jonas.jtests.util.JTestCase;
46
47
48 /**
49  * This Class defines Test cases that can be run either SF or SL bean.
50  * These tests are related to the "Client View of a Session Bean"
51  * A session bean that supports both Local & Remote interface is used
52  * (TargetSL oe TargetSF in beans/local)
53  * But here are testcases that can be run via a non remote suite.
54  *
55  * @see EJB2.0 specification Chapter 6.
56  * @author Ph Coq, Ph Durieux
57  */

58
59 public abstract class A_ClientView extends JTestCase {
60
61     public A_ClientView(String JavaDoc name) {
62         super(name);
63     }
64
65     /**
66      * @return TargetSLHome, that can be either SF or SL bean.
67      */

68     public abstract TargetSLHome getHome() throws Exception JavaDoc;
69
70
71
72     /**
73      * Lookup a Home Object
74      *
75      * This test verify that we can look up a Home Object
76      * via the root context
77      */

78
79     public void testHomeLookup() throws Exception JavaDoc {
80         TargetSLHome h = null;
81         h = getHome();
82         assertTrue(h != null);
83     }
84
85
86
87
88     /**
89      * This test verify we can create and remove a session bean
90      */

91     public void testCreateRemove() throws Exception JavaDoc {
92         Target tr = getHome().create();
93         tr.remove();
94     }
95
96
97
98     /**
99      * This test verify we can create and remove a session bean
100      */

101     public void testLongCreateRemove() throws Exception JavaDoc {
102         for (int i = 0; i < 500; i++) {
103             Target tr = getHome().create();
104             tr.remove();
105         }
106     }
107
108
109     /**
110      * This test verify we can create, access to the bean via
111      * its remote interface and remove it
112      * It verifies we cannot access to the bean after remove
113      */

114     public void testBusinessMethod1() throws Exception JavaDoc {
115         Target tr = getHome().create();
116         assertEquals(10, tr.getTen());
117         tr.remove();
118         try {
119             tr.method2("Bye");
120             fail("NoSuchObjectException must be raised");
121             // } catch (NoSuchObjectException e) {
122
} catch (NoSuchObjectException JavaDoc e) {
123         }
124     }
125
126
127     /**
128      * Same test than testBusinessMethod1 but two calls to businessmethod
129      */

130     public void testBusinessMethod2() throws Exception JavaDoc {
131         Target tr = getHome().create();
132         assertEquals(10, tr.getTen());
133         assertEquals(10, tr.getTen());
134         tr.remove();
135     }
136
137
138
139     /**
140      * test 3 Remote Business Methods
141      */

142     public void testBusinessMethod3() throws Exception JavaDoc {
143         Target tr = getHome().create();
144         assertEquals(10, tr.getTen());
145         tr.method2("Hello");
146         assertEquals(10, tr.getTen());
147         tr.remove();
148     }
149
150
151     /**
152      * test EJBObject.isIdentical on the same bean
153      */

154     public void testIsIdenticalOnSameBean() throws Exception JavaDoc {
155         Target tr = getHome().create();
156         assertTrue(tr.isIdentical(tr));
157         tr.remove();
158     }
159
160
161     /**
162      * test EJBObject.getPrimary Key
163      * This test verify that a Remote Exception must be catched
164      */

165     public void testGetPrimaryKey() throws Exception JavaDoc {
166         Target tr = getHome().create();
167         try {
168             tr.getPrimaryKey();
169             fail("getPrimaryKey should raise RemoteException on a session bean");
170         } catch (RemoteException JavaDoc e) {
171         } finally {
172             tr.remove();
173         }
174     }
175
176
177     /**
178      * test EJBHome.getEJBMetaData().getPrimaryKeyClass()
179      * This test verify that a EJBException must be catched
180      */

181     public void testGetPrimaryKeyClass() throws Exception JavaDoc {
182         try {
183             getHome().getEJBMetaData().getPrimaryKeyClass();
184             fail("getEJBMetaData().getPrimaryKeyClass should raise EJBException on a session bean home");
185         } catch (EJBException JavaDoc e) {
186         }
187     }
188
189
190     /**
191      * test that EJBHome.remove(pk) should raise RemoteException (API) or
192      * RemoveException (spec EJB 2.0 6.3.2)
193      */

194     public void testRemoveByPK() throws Exception JavaDoc {
195         Object JavaDoc pk = null;
196         try {
197             getHome().remove(pk);
198             fail("getPrimaryKey should raise Exception on a session bean");
199         } catch (RemoteException JavaDoc e) {
200         } catch (RemoveException JavaDoc e) {
201         }
202     }
203
204
205     /**
206      * test that EJBHome.getEJBMetaData()
207      * test we can obtain an handle for a session bean
208      */

209     public void testGetEJBMetaData() throws Exception JavaDoc {
210         EJBMetaData JavaDoc hh = getHome().getEJBMetaData();
211     }
212
213     /**
214      * test that EJBHome.getHomeHandle()
215      * test we can obtain a handle for a session bean's home interface
216      */

217     public void testGetHomeHandle() throws Exception JavaDoc {
218         HomeHandle JavaDoc hh = getHome().getHomeHandle();
219     }
220
221     /**
222      * test serialize/deserialize handle.
223      * @throws Exception
224      */

225     public void testSerializeHandle() throws Exception JavaDoc {
226         TargetSLHome tgh = getHome();
227         Target tr = tgh.create();
228         Handle JavaDoc h = tr.getHandle();
229         ObjectInputStream JavaDoc is = null;
230         ObjectOutputStream JavaDoc os = null;
231         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
232         os = new ObjectOutputStream JavaDoc(baos);
233         os.writeObject(h);
234         byte[] b = baos.toByteArray();
235         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(b);
236         is = new ObjectInputStream JavaDoc(bais);
237         Handle JavaDoc deserializedHandle = (Handle JavaDoc) is.readObject();
238         EJBObject JavaDoc ejbObject = deserializedHandle.getEJBObject();
239         Target beanRef2 = (Target) PortableRemoteObject.narrow(ejbObject, Target.class);
240     }
241
242     /**
243      * test that EJBHome.removeHandle()
244      * test we can remove a session via its handle
245      * it verifies the bean cannot be accessed after remove and
246      * java.rmi.NoSuchObjectException is raised
247      */

248     public void testRemoveHandle() throws Exception JavaDoc {
249         TargetSLHome tgh = getHome();
250         Target tr = tgh.create();
251         Handle JavaDoc h = tr.getHandle();
252         tgh.remove(h);
253         try {
254             tr.method2("Bye");
255             fail("Cannot remove a session bean via its handle");
256         } catch ( NoSuchObjectException JavaDoc e) {
257
258         }
259     }
260
261 }
262
Popular Tags