KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > entity > A_BasicHomeInterface


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_BasicHomeInterface.java,v 1.11 2005/04/15 14:39:59 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import java.util.Collection JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.ejb.FinderException JavaDoc;
33
34
35 import org.objectweb.jonas.jtests.beans.ebasic.Simple;
36 import org.objectweb.jonas.jtests.beans.ebasic.SimpleHome;
37 import org.objectweb.jonas.jtests.util.JTestCase;
38
39 /**
40  * test cases common to both suites CMP and BMP.
41  */

42 public abstract class A_BasicHomeInterface extends JTestCase {
43
44     protected static SimpleHome home = null;
45
46     public A_BasicHomeInterface(String JavaDoc name) {
47         super(name);
48     }
49
50     /**
51      * init environment:
52      * - load beans
53      * - create/init database for entities.
54      */

55     protected void setUp() {
56         super.setUp();
57         useBeans("ebasic", true);
58     }
59
60     /**
61      * return SimpleHome, that can be either BMP or CMP bean.
62      */

63     abstract public SimpleHome getHome();
64
65     /**
66      * testFindByPK verify that findByPrimaryKey find an existing entityBean
67      * pre condition: an element with "pk1" as primary key must exist in the database
68      *
69      * findByPrimaryKey("testFindByPK") must pass.
70      */

71     public void testFindByPK() throws Exception JavaDoc {
72         getHome().findByPrimaryKey("pk1");
73     }
74
75     /**
76      * testFindUnexistingPK verify that findByPrimaryKey throw Finder Exception
77      * when the specified entity beab doesn't exist.
78      * pre condition there is no element with "pk999" in the table
79      *
80      * findByPrimaryKey("testFindUnexistingPK") must throw Finder Exception
81      */

82     public void testFindUnexistingPK() throws Exception JavaDoc {
83         try {
84             getHome().findByPrimaryKey("pk999");
85             fail("findByPrimaryKey must throw ObjectNotFound Exception");
86         } catch(FinderException JavaDoc e) {
87         }
88     }
89
90     /**
91      * testOtherFinder verify that we can use finder method other than findByPrimaryKey
92      * pre condition an element with "pk2" as primary key must exist in the database
93      * findByTestName("testOtherFinder") must pass
94      *
95      */

96     public void testOtherFinder() throws Exception JavaDoc {
97         getHome().findByTestName("pk2");
98     }
99
100     /**
101      * testFinderEnumObjNotFound verify that a finder method that can return a Enumeration
102      * return an empty enumeration where there is no matching bean
103      * pre condition there is no elements with NumTest = 999
104      *
105      */

106     public void testFinderEnumObjNotFound() throws Exception JavaDoc {
107         Simple entity = null;
108         Enumeration JavaDoc listOfEntity = null;
109
110         listOfEntity = getHome().findInfoForNum(999);
111         if (listOfEntity.hasMoreElements())
112             fail("findInfoForNum must return an empty enumeration");
113     
114     }
115     
116     /**
117      * testCreateNewEntity verify that we can create a new entityBean
118      * We create a new entity testCreateNewEntity, 20, 6
119      * the findByTestName("testCreateNewEntity") must pass and the resulting must be equals to 20
120      * pre condition the testCreateNewEntity element must not exist
121      *
122      */

123     public void testCreateNewEntity() throws Exception JavaDoc {
124         getHome().create("pk100", 20, 6);
125         Simple entity2 = getHome().findByTestName("pk100");
126         assertEquals(20, entity2.getInfo());
127         // cleaning
128
entity2.remove();
129     }
130
131     /*
132      * Simpler tests, for debugging only :
133      * - cannot be passed twice
134      * - are not independant each others
135      * Use Jadmin to check instance counts are OK !
136      */

137     public void essaiC1() throws Exception JavaDoc {
138         getHome().create("pke1", 20, 6);
139     }
140     public void essaiA1() throws Exception JavaDoc {
141         getHome().findByPrimaryKey("pke1").getInfo();
142     }
143     public void essaiA1C() throws Exception JavaDoc {
144         utx.begin();
145         getHome().findByPrimaryKey("pke1").getInfo();
146         utx.commit();
147     }
148     public void essaiF1() throws Exception JavaDoc {
149         getHome().findByPrimaryKey("pke1");
150     }
151     public void essaiR1() throws Exception JavaDoc {
152         getHome().remove("pke1");
153     }
154     public void essaiR1C() throws Exception JavaDoc {
155         utx.begin();
156         getHome().remove("pke1");
157         utx.commit();
158     }
159     public void essaiC2() throws Exception JavaDoc {
160         getHome().create("pke2", 20, 6);
161     }
162     public void essaiA2() throws Exception JavaDoc {
163         getHome().findByPrimaryKey("pke2").getInfo();
164     }
165     public void essaiA2C() throws Exception JavaDoc {
166         utx.begin();
167         getHome().findByPrimaryKey("pke2").getInfo();
168         utx.commit();
169     }
170     public void essaiF2() throws Exception JavaDoc {
171         getHome().findByTestName("pke2");
172     }
173     public void essaiR2() throws Exception JavaDoc {
174         getHome().findByTestName("pke2").remove();
175     }
176     public void essaiR2C() throws Exception JavaDoc {
177         utx.begin();
178         getHome().findByTestName("pke2").remove();
179         utx.commit();
180     }
181
182     /*
183      * testCreateRolledBack verify that we cannot access to a bean whose
184      * creation has been rolledback by a finder method.
185      * pre condition the pk110 element must not exist
186      */

187     public void testCreateRolledBack() throws Exception JavaDoc {
188         utx.begin();
189         try {
190             Simple entity1 = getHome().create("pk110", 30, 7);
191         }
192         catch (Exception JavaDoc e) {
193             fail(e.getMessage());
194         }
195         finally {
196             utx.rollback();
197         }
198
199         try {
200             getHome().findByTestName("pk110");
201             fail("element should not be found");
202         } catch(FinderException JavaDoc e) {
203         }
204     }
205
206     /*
207      * testCreateRolledBack verify that we cannot access to a bean whose
208      * creation has been rolledback by findByPrimaryKey.
209      * pre condition the pk110 element must not exist
210      */

211     public void testCreateRolledBackPK() throws Exception JavaDoc {
212         utx.begin();
213         try {
214             Simple entity1 = getHome().create("pk110", 30, 7);
215         }
216         catch (Exception JavaDoc e) {
217             fail(e.getMessage());
218         }
219         finally {
220             utx.rollback();
221         }
222
223         try {
224             getHome().findByPrimaryKey("pk110");
225             fail("element should not be found");
226         } catch(FinderException JavaDoc e) {
227         }
228     }
229
230     /*
231      * testRemoveViaEJBHome verify it is possible to remove an entity bean via the home object
232      * pre condition the testRemoveViaEJBHome element must exist
233      *
234      */

235     public void testRemoveViaEJBHome() throws Exception JavaDoc {
236         getHome().remove("pk4");
237         try {
238             getHome().findByTestName("pk4");
239             fail("not removed");
240         } catch (FinderException JavaDoc e) {
241         }
242         // cleaning
243
getHome().create("pk4", 40, 8);
244     }
245
246     /**
247      * test remove by PK twice.
248      * test that a removeByPrimaryKey can be followed by a create of the same entity.
249      */

250     public void testRemoveByPKTwice() throws Exception JavaDoc {
251         getHome().create("pkn4", 40, 8);
252         getHome().remove("pkn4");
253         getHome().create("pkn4", 40, 8);
254         getHome().remove("pkn4");
255     }
256
257     /**
258      * test remove by EJBObject twice.
259      * test that a remove can be followed by a create of the same entity.
260      */

261     public void testRemoveTwice() throws Exception JavaDoc {
262         Simple entity = getHome().create("pkn5", 50, 8);
263         entity.remove();
264         entity = getHome().create("pkn5", 50, 8);
265         entity.remove();
266     }
267
268     /*
269      * testRemoveViaEJBObject verify it is possible to remove an entity bean via the EJBObject
270      * pre condition the testRemoveViaEJBObject element must exist
271      */

272     public void testRemoveViaEJBObject() throws Exception JavaDoc {
273         Simple entity = getHome().findByPrimaryKey("pk5");
274         entity.remove();
275         try {
276             getHome().findByPrimaryKey("pk5");
277             fail("not removed");
278         } catch (FinderException JavaDoc e) {
279         }
280         // cleaning
281
getHome().create("pk5", 50, 8);
282     }
283
284     /**
285      * testRemoveInsideTransaction Verify that after a remove inside a transaction,
286      * the bean cannot be found anymore
287      * pre condition the pk4 instance must exist.
288      */

289     public void testRemoveInsideTransaction() throws Exception JavaDoc {
290         Simple entity1 = getHome().findByPrimaryKey("pk4");
291         utx.begin();
292         try {
293             entity1.remove();
294         }
295         catch (Exception JavaDoc e) {
296             fail(e.getMessage());
297         }
298         finally {
299             utx.commit();
300         }
301         utx.begin();
302         try {
303             getHome().findByPrimaryKey("pk4");
304             fail("should not exist anymore");
305         } catch(FinderException JavaDoc e) {
306         } finally {
307             utx.rollback();
308         }
309         // cleaning
310
getHome().create("pk4", 40, 8);
311     }
312
313     /**
314      * same test without second tx.
315      */

316     public void testRemoveInTransaction() throws Exception JavaDoc {
317         Simple entity1 = getHome().findByPrimaryKey("pk4");
318         utx.begin();
319         try {
320             entity1.remove();
321         }
322         catch (Exception JavaDoc e) {
323             fail(e.getMessage());
324         }
325         finally {
326             utx.commit();
327         }
328         try {
329             getHome().findByPrimaryKey("pk4");
330             fail("should not exist anymore");
331         } catch(FinderException JavaDoc e) {
332         }
333         // cleaning
334
getHome().create("pk4", 40, 8);
335     }
336
337     /**
338      * test remove by PK in transaction
339      */

340     public void testHomeRemoveCommitted() throws Exception JavaDoc {
341         utx.begin();
342         try {
343             getHome().remove("pk4");
344         }
345         catch (Exception JavaDoc e) {
346             fail(e.getMessage());
347         }
348         finally {
349             utx.commit();
350         }
351         try {
352             getHome().findByPrimaryKey("pk4");
353             fail("should not exist anymore");
354         } catch(FinderException JavaDoc e) {
355         }
356         // cleaning
357
getHome().create("pk4", 40, 8);
358     }
359     
360     /**
361      * testRemoveRolledBack verify that we can access to a bean after remove has been rolledback
362      * pre condition the testRemoveRolledBack must exist
363      */

364     public void testRemoveRolledBack() throws Exception JavaDoc {
365         Simple entity1 = getHome().findByPrimaryKey("pk6");
366         utx.begin();
367         try {
368             entity1.remove();
369         }
370         catch (Exception JavaDoc e) {
371             utx.rollback();
372             fail(e.getMessage());
373         }
374         try {
375             // Here we verify we cannot acces to the removed bean
376
// (the transaction is not yet rolled back")
377
getHome().findByPrimaryKey("pk6");
378             fail("should not exist anymore at this point");
379         } catch(FinderException JavaDoc e) {
380         } finally {
381             utx.rollback();
382         }
383         Simple entity3 = getHome().findByTestName("pk6");
384         assertEquals(60, entity3.getInfo());
385     }
386
387     /**
388      * testHomeRemoveRolledBack verify that we can access to a bean after remove has been rolledback
389      * it is the same that testRemoveRolledBack but with home.remove(pk);
390      * pre condition the testHomeRemoveRolledBack must exist
391      */

392     public void testHomeRemoveRolledBack() throws Exception JavaDoc {
393         Simple entity1 = getHome().findByPrimaryKey("pk7");
394         utx.begin();
395         try {
396             getHome().remove("pk7");
397         }
398         catch (Exception JavaDoc e) {
399             utx.rollback();
400             fail(e.getMessage());
401         }
402         try {
403             // Here we verify we cannot acces to the removed bean
404
// (the transaction is not yet rolled back")
405
getHome().findByPrimaryKey("pk7");
406             fail("should not exist anymore at this point");
407         } catch(FinderException JavaDoc e) {
408         } finally {
409             utx.rollback();
410         }
411         Simple entity3 = getHome().findByTestName("pk7");
412         assertEquals(70, entity3.getInfo());
413     }
414
415     /**
416      * testFinderEnum verify a finder method that return a Enumeration
417      * pre condition there are 3 elements with num =4
418      * all of them have a field info equals to 10
419      */

420     public void testFinderEnum() throws Exception JavaDoc {
421         Simple entity = null;
422         Enumeration JavaDoc listOfEntity = getHome().findInfoForNum(4);
423         int nb = 0;
424         while (listOfEntity.hasMoreElements()) {
425             entity = (Simple)javax.rmi.PortableRemoteObject.narrow(listOfEntity.nextElement(), Simple.class);
426             assertEquals(10, entity.getInfo());
427             nb++;
428         }
429         assertEquals(3, nb);
430     }
431
432     /**
433      * Verify that we can create an entity and retrieve it by
434      * a finder method inside the same transaction
435      */

436     public void testCreateFindUserTx() throws Exception JavaDoc {
437         utx.begin();
438         try {
439             Simple e1 = getHome().create("pk120", 32, 7);
440             Simple e2 = getHome().findByTestName("pk120");
441             assertTrue(e2.isIdentical(e1));
442         }
443         catch (Exception JavaDoc e) {
444             fail(e.getMessage());
445         } finally {
446             utx.rollback();
447         }
448     }
449
450    
451     /**
452      * testFinderCollection verify a finder method that return a Collection
453      * pre condition there are 4 elements with c_numtest =4 (pk4,...,pk7)
454      * findInCollection returns all the beans where c_numtest = 4
455      * this test is equivalent to testcase2 in SimpleTest in finder_col
456      */

457     public void testFinderCollection() throws Exception JavaDoc {
458         Simple entity = null;
459         Collection JavaDoc cListEntity = getHome().findInCollection();
460         int nb = 0;
461         Iterator JavaDoc icListEntity = cListEntity.iterator();
462         while(icListEntity.hasNext()) {
463             entity = (Simple) javax.rmi.PortableRemoteObject.narrow(icListEntity.next(), Simple.class);
464             nb++;
465         }
466         assertEquals(4, nb);
467     }
468
469     /**
470      * Test that the removed is no more seen after the rollback
471      */

472     public void testFindAfterRBR() throws Exception JavaDoc {
473         getHome().findByPrimaryKey("pk8");
474         utx.begin();
475         getHome().remove("pk8");
476         utx.rollback();
477         getHome().findByTestName("pk8");
478     }
479
480     /**
481      * test loop on finder method (findByPrimaryKey)
482      */

483     public void testLoopFindByPK() throws Exception JavaDoc {
484         for (int i = 0; i < 20; i++) {
485             getHome().findByPrimaryKey("pk9");
486         }
487     }
488
489     /**
490      * test loop on other finder method
491      */

492     public void testLoopFinder() throws Exception JavaDoc {
493         for (int i = 0; i < 20; i++) {
494             getHome().findByTestName("pk10");
495         }
496     }
497
498     /**
499      * test loopback on non reentrant bean in same tx
500      */

501     public void testLoopBackTx() throws Exception JavaDoc {
502         Simple s = getHome().findByPrimaryKey("pk9");
503         assertTrue(s.loopBackTx());
504     }
505     
506     /**
507      * test that we can access the instance twice in the same transaction,
508      * even if the bean is non reentrant.
509      */

510     public void testAccessTwiceTx() throws Exception JavaDoc {
511         Simple s = getHome().findByPrimaryKey("pk9");
512         utx.begin();
513         try {
514             s.getNumTest();
515             s.getNumTest();
516         } finally {
517             utx.commit();
518         }
519     }
520
521     /**
522      * test that we can access the instance twice in the same transaction,
523      * even if the bean is non reentrant.
524      */

525     public void testFindAccessTx() throws Exception JavaDoc {
526         utx.begin();
527         try {
528             Simple s = getHome().findByPrimaryKey("pk9");
529             s.getNumTest();
530         } finally {
531             utx.commit();
532         }
533     }
534     
535     /**
536      * test loopback on non reentrant bean outside tx
537      */

538     public void testLoopBack() throws Exception JavaDoc {
539         Simple s = getHome().findByPrimaryKey("pk10");
540         assertTrue(s.loopBack());
541     }
542 }
543
Popular Tags