KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > stateful > EncStatefulBean


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: EncStatefulBean.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.test.stateful;
46
47 import java.rmi.RemoteException JavaDoc;
48
49 import javax.ejb.EJBException JavaDoc;
50 import javax.ejb.SessionContext JavaDoc;
51 import javax.ejb.SessionSynchronization JavaDoc;
52 import javax.naming.InitialContext JavaDoc;
53
54 import junit.framework.Assert;
55 import junit.framework.AssertionFailedError;
56
57 import org.openejb.test.TestFailureException;
58 import org.openejb.test.entity.bmp.BasicBmpHome;
59 import org.openejb.test.entity.bmp.BasicBmpObject;
60 import org.openejb.test.stateless.BasicStatelessHome;
61 import org.openejb.test.stateless.BasicStatelessObject;
62
63 /**
64  *
65  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
66  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
67  */

68 public class EncStatefulBean implements javax.ejb.SessionBean JavaDoc, SessionSynchronization JavaDoc {
69
70     
71     private String JavaDoc name;
72     private SessionContext JavaDoc ejbContext;
73     
74     
75     //=============================
76
// Home interface methods
77
//
78
/**
79      * Maps to EncStatefulHome.create
80      *
81      * @param name
82      * @exception javax.ejb.CreateException
83      * @see EncStatefulHome.create
84      */

85     public void ejbCreate(String JavaDoc name)
86     throws javax.ejb.CreateException JavaDoc{
87         this.name = name;
88     }
89     //
90
// Home interface methods
91
//=============================
92

93
94     //=============================
95
// Remote interface methods
96
//
97

98     public void lookupEntityBean() throws TestFailureException{
99         try{
100             try{
101             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
102             Assert.assertNotNull("The InitialContext is null", ctx );
103             
104             BasicBmpHome home = (BasicBmpHome) javax.rmi.PortableRemoteObject.narrow( ctx.lookup("java:comp/env/stateful/beanReferences/bmp_entity"), BasicBmpHome.class );
105             Assert.assertNotNull("The EJBHome looked up is null",home);
106
107             BasicBmpObject object = home.create("Enc Bean");
108             Assert.assertNotNull("The EJBObject is null", object );
109             } catch (Exception JavaDoc e){
110                 Assert.fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
111             }
112         } catch (AssertionFailedError afe){
113             throw new TestFailureException(afe);
114         }
115     }
116     
117     public void lookupStatefulBean() throws TestFailureException{
118         try{
119             try{
120             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
121             Assert.assertNotNull("The InitialContext is null", ctx );
122             
123             BasicStatefulHome home = (BasicStatefulHome) javax.rmi.PortableRemoteObject.narrow( ctx.lookup("java:comp/env/stateful/beanReferences/stateful"), BasicStatefulHome.class );
124             Assert.assertNotNull("The EJBHome looked up is null",home);
125
126             BasicStatefulObject object = home.create("Enc Bean");
127             Assert.assertNotNull("The EJBObject is null", object );
128             } catch (Exception JavaDoc e){
129                 Assert.fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
130             }
131         } catch (AssertionFailedError afe){
132             throw new TestFailureException(afe);
133         }
134     }
135     
136     public void lookupStatelessBean() throws TestFailureException{
137         try{
138             try{
139             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
140             Assert.assertNotNull("The InitialContext is null", ctx );
141             
142             BasicStatelessHome home = (BasicStatelessHome) javax.rmi.PortableRemoteObject.narrow( ctx.lookup("java:comp/env/stateful/beanReferences/stateless"), BasicStatelessHome.class );
143             Assert.assertNotNull("The EJBHome looked up is null",home);
144
145             BasicStatelessObject object = home.create();
146             Assert.assertNotNull("The EJBObject is null", object );
147             } catch (Exception JavaDoc e){
148                 Assert.fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
149             }
150         } catch (AssertionFailedError afe){
151             throw new TestFailureException(afe);
152         }
153     }
154
155     public void lookupStringEntry() throws TestFailureException{
156         try{
157             try{
158             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
159             Assert.assertNotNull("The InitialContext is null", ctx );
160             
161             String JavaDoc expected = new String JavaDoc("1");
162             String JavaDoc actual = (String JavaDoc)ctx.lookup("java:comp/env/stateful/references/String");
163             
164             Assert.assertNotNull("The String looked up is null", actual );
165             Assert.assertEquals(expected, actual );
166
167             } catch (Exception JavaDoc e){
168                 Assert.fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
169             }
170         } catch (AssertionFailedError afe){
171             throw new TestFailureException(afe);
172         }
173     }
174     
175     public void lookupDoubleEntry() throws TestFailureException{
176         try{
177             try{
178             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
179             Assert.assertNotNull("The InitialContext is null", ctx );
180             
181             Double JavaDoc expected = new Double JavaDoc(1.0D);
182             Double JavaDoc actual = (Double JavaDoc)ctx.lookup("java:comp/env/stateful/references/Double");
183             
184             Assert.assertNotNull("The Double looked up is null", actual );
185             Assert.assertEquals(expected, actual );
186
187             } catch (Exception JavaDoc e){
188                 Assert.fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
189             }
190         } catch (AssertionFailedError afe){
191             throw new TestFailureException(afe);
192         }
193     }
194     
195     public void lookupLongEntry() throws TestFailureException{
196         try{
197             try{
198             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
199             Assert.assertNotNull("The InitialContext is null", ctx );
200             
201             Long JavaDoc expected = new Long JavaDoc(1L);
202             Long JavaDoc actual = (Long JavaDoc)ctx.lookup("java:comp/env/stateful/references/Long");
203             
204             Assert.assertNotNull("The Long looked up is null", actual );
205             Assert.assertEquals(expected, actual );
206
207             } catch (Exception JavaDoc e){
208                 Assert.fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
209             }
210         } catch (AssertionFailedError afe){
211             throw new TestFailureException(afe);
212         }
213     }
214     
215     public void lookupFloatEntry() throws TestFailureException{
216         try{
217             try{
218             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
219             Assert.assertNotNull("The InitialContext is null", ctx );
220             
221             Float JavaDoc expected = new Float JavaDoc(1.0F);
222             Float JavaDoc actual = (Float JavaDoc)ctx.lookup("java:comp/env/stateful/references/Float");
223             
224             Assert.assertNotNull("The Float looked up is null", actual );
225             Assert.assertEquals(expected, actual );
226
227             } catch (Exception JavaDoc e){
228                 Assert.fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
229             }
230         } catch (AssertionFailedError afe){
231             throw new TestFailureException(afe);
232         }
233     }
234     
235     public void lookupIntegerEntry() throws TestFailureException{
236         try{
237             try{
238             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
239             Assert.assertNotNull("The InitialContext is null", ctx );
240             
241             Integer JavaDoc expected = new Integer JavaDoc(1);
242             Integer JavaDoc actual = (Integer JavaDoc)ctx.lookup("java:comp/env/stateful/references/Integer");
243             
244             Assert.assertNotNull("The Integer looked up is null", actual );
245             Assert.assertEquals(expected, actual );
246
247             } catch (Exception JavaDoc e){
248                 Assert.fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
249             }
250         } catch (AssertionFailedError afe){
251             throw new TestFailureException(afe);
252         }
253     }
254     
255     public void lookupShortEntry() throws TestFailureException{
256         try{
257             try{
258             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
259             Assert.assertNotNull("The InitialContext is null", ctx );
260             
261             Short JavaDoc expected = new Short JavaDoc((short)1);
262             Short JavaDoc actual = (Short JavaDoc)ctx.lookup("java:comp/env/stateful/references/Short");
263             
264             Assert.assertNotNull("The Short looked up is null", actual );
265             Assert.assertEquals(expected, actual );
266
267             } catch (Exception JavaDoc e){
268                 Assert.fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
269             }
270         } catch (AssertionFailedError afe){
271             throw new TestFailureException(afe);
272         }
273     }
274     
275     public void lookupBooleanEntry() throws TestFailureException{
276         try{
277             try{
278             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
279             Assert.assertNotNull("The InitialContext is null", ctx );
280             
281             Boolean JavaDoc expected = new Boolean JavaDoc(true);
282             Boolean JavaDoc actual = (Boolean JavaDoc)ctx.lookup("java:comp/env/stateful/references/Boolean");
283             
284             Assert.assertNotNull("The Boolean looked up is null", actual );
285             Assert.assertEquals(expected, actual );
286
287             } catch (Exception JavaDoc e){
288                 Assert.fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
289             }
290         } catch (AssertionFailedError afe){
291             throw new TestFailureException(afe);
292         }
293     }
294     
295     public void lookupByteEntry() throws TestFailureException{
296         try{
297             try{
298             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
299             Assert.assertNotNull("The InitialContext is null", ctx );
300             
301             Byte JavaDoc expected = new Byte JavaDoc((byte)1);
302             Byte JavaDoc actual = (Byte JavaDoc)ctx.lookup("java:comp/env/stateful/references/Byte");
303             
304             Assert.assertNotNull("The Byte looked up is null", actual );
305             Assert.assertEquals(expected, actual );
306
307             } catch (Exception JavaDoc e){
308                 Assert.fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
309             }
310         } catch (AssertionFailedError afe){
311             throw new TestFailureException(afe);
312         }
313     }
314
315     public void lookupResource() throws TestFailureException{
316         try{
317             try{
318             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
319             Assert.assertNotNull("The InitialContext is null", ctx );
320             } catch (Exception JavaDoc e){
321                 Assert.fail("Received Exception "+e.getClass()+ " : "+e.getMessage());
322             }
323         } catch (AssertionFailedError afe){
324             throw new TestFailureException(afe);
325         }
326     }
327     //
328
// Remote interface methods
329
//=============================
330

331
332     //=================================
333
// SessionBean interface methods
334
//
335
/**
336      * Set the associated session context. The container calls this method
337      * after the instance creation.
338      */

339     public void setSessionContext(SessionContext JavaDoc ctx) throws EJBException JavaDoc,RemoteException JavaDoc {
340         ejbContext = ctx;
341     }
342     /**
343      * A container invokes this method before it ends the life of the session
344      * object. This happens as a result of a client's invoking a remove
345      * operation, or when a container decides to terminate the session object
346      * after a timeout.
347      */

348     public void ejbRemove() throws EJBException JavaDoc,RemoteException JavaDoc {
349     }
350     /**
351      * The activate method is called when the instance is activated
352      * from its "passive" state. The instance should acquire any resource
353      * that it has released earlier in the ejbPassivate() method.
354      */

355     public void ejbActivate() throws EJBException JavaDoc,RemoteException JavaDoc {
356     }
357     /**
358      * The passivate method is called before the instance enters
359      * the "passive" state. The instance should release any resources that
360      * it can re-acquire later in the ejbActivate() method.
361      */

362     public void ejbPassivate() throws EJBException JavaDoc,RemoteException JavaDoc {
363     }
364     //
365
// SessionBean interface methods
366
//==================================
367

368     
369     //============================================
370
// SessionSynchronization interface methods
371
//
372
/**
373      * The afterBegin method notifies a session Bean instance that a new
374      * transaction has started, and that the subsequent business methods on the
375      * instance will be invoked in the context of the transaction.
376      */

377     public void afterBegin() throws EJBException JavaDoc,RemoteException JavaDoc {
378     }
379     /**
380      * The beforeCompletion method notifies a session Bean instance that
381      * a transaction is about to be committed. The instance can use this
382      * method, for example, to write any cached data to a database.
383      */

384     public void beforeCompletion() throws EJBException JavaDoc,RemoteException JavaDoc {
385     }
386     /**
387      * The afterCompletion method notifies a session Bean instance that a
388      * transaction commit protocol has completed, and tells the instance
389      * whether the transaction has been committed or rolled back.
390      */

391     public void afterCompletion(boolean committed) throws EJBException JavaDoc,RemoteException JavaDoc {
392     }
393     //
394
// SessionSynchronization interface methods
395
//============================================
396

397 }
398
Popular Tags