KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > test > entity > cmp > EncCmpBean


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: EncCmpBean.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

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

69 public class EncCmpBean implements javax.ejb.EntityBean JavaDoc{
70     
71     public static int key = 20;
72     
73     public int primaryKey;
74     public String JavaDoc firstName;
75     public String JavaDoc lastName;
76     public EntityContext JavaDoc ejbContext;
77     
78     //=============================
79
// Home interface methods
80
//
81

82     /**
83      * Maps to EncCmpHome.create
84      *
85      * @param name
86      * @return
87      * @exception javax.ejb.CreateException
88      * @see EncCmpHome.create
89      */

90     public Integer JavaDoc ejbCreate(String JavaDoc name)
91     throws javax.ejb.CreateException JavaDoc{
92         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(name, " ");
93         firstName = st.nextToken();
94         lastName = st.nextToken();
95         this.primaryKey = key++;
96         return null;
97     }
98     
99     public void ejbPostCreate(String JavaDoc name)
100     throws javax.ejb.CreateException JavaDoc{
101     }
102     
103     
104     //
105
// Home interface methods
106
//=============================
107

108
109     //=============================
110
// Remote interface methods
111
//
112

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

347
348     //================================
349
// EntityBean interface methods
350
//
351

352     /**
353      * A container invokes this method to instruct the
354      * instance to synchronize its state by loading it state from the
355      * underlying database.
356      */

357     public void ejbLoad() throws EJBException JavaDoc,RemoteException JavaDoc {
358     }
359     
360     /**
361      * Set the associated entity context. The container invokes this method
362      * on an instance after the instance has been created.
363      */

364     public void setEntityContext(EntityContext JavaDoc ctx) throws EJBException JavaDoc,RemoteException JavaDoc {
365         ejbContext = ctx;
366     }
367     
368     /**
369      * Unset the associated entity context. The container calls this method
370      * before removing the instance.
371      */

372     public void unsetEntityContext() throws EJBException JavaDoc,RemoteException JavaDoc {
373     }
374     
375     /**
376      * A container invokes this method to instruct the
377      * instance to synchronize its state by storing it to the underlying
378      * database.
379      */

380     public void ejbStore() throws EJBException JavaDoc,RemoteException JavaDoc {
381     }
382     
383     /**
384      * A container invokes this method before it removes the EJB object
385      * that is currently associated with the instance. This method
386      * is invoked when a client invokes a remove operation on the
387      * enterprise Bean's home interface or the EJB object's remote interface.
388      * This method transitions the instance from the ready state to the pool
389      * of available instances.
390      */

391     public void ejbRemove() throws RemoveException JavaDoc,EJBException JavaDoc,RemoteException JavaDoc {
392     }
393     
394     /**
395      * A container invokes this method when the instance
396      * is taken out of the pool of available instances to become associated
397      * with a specific EJB object. This method transitions the instance to
398      * the ready state.
399      */

400     public void ejbActivate() throws EJBException JavaDoc,RemoteException JavaDoc {
401     }
402     
403     /**
404      * A container invokes this method on an instance before the instance
405      * becomes disassociated with a specific EJB object. After this method
406      * completes, the container will place the instance into the pool of
407      * available instances.
408      */

409     public void ejbPassivate() throws EJBException JavaDoc,RemoteException JavaDoc {
410     }
411     //
412
// EntityBean interface methods
413
//================================
414
}
415
Popular Tags