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_BasicHomeInterface.java 7398 2005-09-21 11:18:31Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.entity;
027:
028: import java.util.Collection;
029: import java.util.Enumeration;
030: import java.util.Iterator;
031:
032: import javax.ejb.FinderException;
033:
034: import org.objectweb.jonas.jtests.beans.ebasic.Simple;
035: import org.objectweb.jonas.jtests.beans.ebasic.SimpleHome;
036: import org.objectweb.jonas.jtests.util.JTestCase;
037:
038: /**
039: * test cases common to both suites CMP and BMP.
040: */
041: public abstract class A_BasicHomeInterface extends JTestCase {
042:
043: protected static SimpleHome home = null;
044:
045: public A_BasicHomeInterface(String name) {
046: super (name);
047: }
048:
049: /**
050: * init environment:
051: * - load beans
052: * - create/init database for entities.
053: */
054: protected void setUp() {
055: super .setUp();
056: useBeans("ebasic", true);
057: }
058:
059: /**
060: * return SimpleHome, that can be either BMP or CMP bean.
061: */
062: abstract public SimpleHome getHome();
063:
064: /**
065: * testFindByPK verify that findByPrimaryKey find an existing entityBean
066: * pre condition: an element with "pk1" as primary key must exist in the database
067: *
068: * findByPrimaryKey("testFindByPK") must pass.
069: */
070: public void testFindByPK() throws Exception {
071: getHome().findByPrimaryKey("pk1");
072: }
073:
074: /**
075: * testFindUnexistingPK verify that findByPrimaryKey throw Finder Exception
076: * when the specified entity beab doesn't exist.
077: * pre condition there is no element with "pk999" in the table
078: *
079: * findByPrimaryKey("testFindUnexistingPK") must throw Finder Exception
080: */
081: public void testFindUnexistingPK() throws Exception {
082: try {
083: getHome().findByPrimaryKey("pk999");
084: fail("findByPrimaryKey must throw ObjectNotFound Exception");
085: } catch (FinderException e) {
086: }
087: }
088:
089: /**
090: * testOtherFinder verify that we can use finder method other than findByPrimaryKey
091: * pre condition an element with "pk2" as primary key must exist in the database
092: * findByTestName("testOtherFinder") must pass
093: *
094: */
095: public void testOtherFinder() throws Exception {
096: getHome().findByTestName("pk2");
097: }
098:
099: /**
100: * testFinderEnumObjNotFound verify that a finder method that can return a Enumeration
101: * return an empty enumeration where there is no matching bean
102: * pre condition there is no elements with NumTest = 999
103: *
104: */
105: public void testFinderEnumObjNotFound() throws Exception {
106: Simple entity = null;
107: Enumeration listOfEntity = null;
108:
109: listOfEntity = getHome().findInfoForNum(999);
110: if (listOfEntity.hasMoreElements())
111: fail("findInfoForNum must return an empty enumeration");
112:
113: }
114:
115: /**
116: * testCreateNewEntity verify that we can create a new entityBean
117: * We create a new entity testCreateNewEntity, 20, 6
118: * the findByTestName("testCreateNewEntity") must pass and the resulting must be equals to 20
119: * pre condition the testCreateNewEntity element must not exist
120: *
121: */
122: public void testCreateNewEntity() throws Exception {
123: getHome().create("pk100", 20, 6);
124: Simple entity2 = getHome().findByTestName("pk100");
125: assertEquals(20, entity2.getInfo());
126: // cleaning
127: entity2.remove();
128: }
129:
130: /*
131: * Simpler tests, for debugging only :
132: * - cannot be passed twice
133: * - are not independant each others
134: * Use Jadmin to check instance counts are OK !
135: */
136: public void essaiC1() throws Exception {
137: getHome().create("pke1", 20, 6);
138: }
139:
140: public void essaiA1() throws Exception {
141: getHome().findByPrimaryKey("pke1").getInfo();
142: }
143:
144: public void essaiA1C() throws Exception {
145: utx.begin();
146: getHome().findByPrimaryKey("pke1").getInfo();
147: utx.commit();
148: }
149:
150: public void essaiF1() throws Exception {
151: getHome().findByPrimaryKey("pke1");
152: }
153:
154: public void essaiR1() throws Exception {
155: getHome().remove("pke1");
156: }
157:
158: public void essaiR1C() throws Exception {
159: utx.begin();
160: getHome().remove("pke1");
161: utx.commit();
162: }
163:
164: public void essaiC2() throws Exception {
165: getHome().create("pke2", 20, 6);
166: }
167:
168: public void essaiA2() throws Exception {
169: getHome().findByPrimaryKey("pke2").getInfo();
170: }
171:
172: public void essaiA2C() throws Exception {
173: utx.begin();
174: getHome().findByPrimaryKey("pke2").getInfo();
175: utx.commit();
176: }
177:
178: public void essaiF2() throws Exception {
179: getHome().findByTestName("pke2");
180: }
181:
182: public void essaiR2() throws Exception {
183: getHome().findByTestName("pke2").remove();
184: }
185:
186: public void essaiR2C() throws Exception {
187: utx.begin();
188: getHome().findByTestName("pke2").remove();
189: utx.commit();
190: }
191:
192: /*
193: * testCreateRolledBack verify that we cannot access to a bean whose
194: * creation has been rolledback by a finder method.
195: * pre condition the pk110 element must not exist
196: */
197: public void testCreateRolledBack() throws Exception {
198: utx.begin();
199: try {
200: Simple entity1 = getHome().create("pk110", 30, 7);
201: } catch (Exception e) {
202: fail(e.getMessage());
203: } finally {
204: utx.rollback();
205: }
206:
207: try {
208: getHome().findByTestName("pk110");
209: fail("element should not be found");
210: } catch (FinderException e) {
211: }
212: }
213:
214: /*
215: * testCreateRolledBack verify that we cannot access to a bean whose
216: * creation has been rolledback by findByPrimaryKey.
217: * pre condition the pk110 element must not exist
218: */
219: public void testCreateRolledBackPK() throws Exception {
220: utx.begin();
221: try {
222: Simple entity1 = getHome().create("pk110", 30, 7);
223: } catch (Exception e) {
224: fail(e.getMessage());
225: } finally {
226: utx.rollback();
227: }
228:
229: try {
230: getHome().findByPrimaryKey("pk110");
231: fail("element should not be found");
232: } catch (FinderException e) {
233: }
234: }
235:
236: /*
237: * testRemoveViaEJBHome verify it is possible to remove an entity bean via the home object
238: * pre condition the testRemoveViaEJBHome element must exist
239: *
240: */
241: public void testRemoveViaEJBHome() throws Exception {
242: getHome().remove("pk4");
243: try {
244: getHome().findByTestName("pk4");
245: fail("not removed");
246: } catch (FinderException e) {
247: }
248: // cleaning
249: getHome().create("pk4", 40, 8);
250: }
251:
252: /**
253: * test remove by PK twice.
254: * test that a removeByPrimaryKey can be followed by a create of the same entity.
255: */
256: public void testRemoveByPKTwice() throws Exception {
257: getHome().create("pkn4", 40, 8);
258: getHome().remove("pkn4");
259: getHome().create("pkn4", 40, 8);
260: getHome().remove("pkn4");
261: }
262:
263: /**
264: * test remove by EJBObject twice.
265: * test that a remove can be followed by a create of the same entity.
266: */
267: public void testRemoveTwice() throws Exception {
268: Simple entity = getHome().create("pkn5", 50, 8);
269: entity.remove();
270: entity = getHome().create("pkn5", 50, 8);
271: entity.remove();
272: }
273:
274: /*
275: * testRemoveViaEJBObject verify it is possible to remove an entity bean via the EJBObject
276: * pre condition the testRemoveViaEJBObject element must exist
277: */
278: public void testRemoveViaEJBObject() throws Exception {
279: Simple entity = getHome().findByPrimaryKey("pk5");
280: entity.remove();
281: try {
282: getHome().findByPrimaryKey("pk5");
283: fail("not removed");
284: } catch (FinderException e) {
285: }
286: // cleaning
287: getHome().create("pk5", 50, 8);
288: }
289:
290: /**
291: * testRemoveInsideTransaction Verify that after a remove inside a transaction,
292: * the bean cannot be found anymore
293: * pre condition the pk4 instance must exist.
294: */
295: public void testRemoveInsideTransaction() throws Exception {
296: Simple entity1 = getHome().findByPrimaryKey("pk4");
297: utx.begin();
298: try {
299: entity1.remove();
300: } catch (Exception e) {
301: fail(e.getMessage());
302: } finally {
303: utx.commit();
304: }
305: utx.begin();
306: try {
307: getHome().findByPrimaryKey("pk4");
308: fail("should not exist anymore");
309: } catch (FinderException e) {
310: } finally {
311: utx.rollback();
312: }
313: // cleaning
314: getHome().create("pk4", 40, 8);
315: }
316:
317: /**
318: * same test without second tx.
319: */
320: public void testRemoveInTransaction() throws Exception {
321: Simple entity1 = getHome().findByPrimaryKey("pk4");
322: utx.begin();
323: try {
324: entity1.remove();
325: } catch (Exception e) {
326: fail(e.getMessage());
327: } finally {
328: utx.commit();
329: }
330: try {
331: getHome().findByPrimaryKey("pk4");
332: fail("should not exist anymore");
333: } catch (FinderException e) {
334: }
335: // cleaning
336: getHome().create("pk4", 40, 8);
337: }
338:
339: /**
340: * test remove by PK in transaction
341: */
342: public void testHomeRemoveCommitted() throws Exception {
343: utx.begin();
344: try {
345: getHome().remove("pk4");
346: } catch (Exception e) {
347: fail(e.getMessage());
348: } finally {
349: utx.commit();
350: }
351: try {
352: getHome().findByPrimaryKey("pk4");
353: fail("should not exist anymore");
354: } catch (FinderException 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 {
365: Simple entity1 = getHome().findByPrimaryKey("pk6");
366: utx.begin();
367: try {
368: entity1.remove();
369: } catch (Exception e) {
370: utx.rollback();
371: fail(e.getMessage());
372: }
373: try {
374: // Here we verify we cannot acces to the removed bean
375: // (the transaction is not yet rolled back")
376: getHome().findByPrimaryKey("pk6");
377: fail("should not exist anymore at this point");
378: } catch (FinderException e) {
379: } finally {
380: utx.rollback();
381: }
382: Simple entity3 = getHome().findByTestName("pk6");
383: assertEquals(60, entity3.getInfo());
384: }
385:
386: /**
387: * testHomeRemoveRolledBack verify that we can access to a bean after remove has been rolledback
388: * it is the same that testRemoveRolledBack but with home.remove(pk);
389: * pre condition the testHomeRemoveRolledBack must exist
390: */
391: public void testHomeRemoveRolledBack() throws Exception {
392: Simple entity1 = getHome().findByPrimaryKey("pk7");
393: utx.begin();
394: try {
395: getHome().remove("pk7");
396: } catch (Exception e) {
397: utx.rollback();
398: fail(e.getMessage());
399: }
400: try {
401: // Here we verify we cannot acces to the removed bean
402: // (the transaction is not yet rolled back")
403: getHome().findByPrimaryKey("pk7");
404: fail("should not exist anymore at this point");
405: } catch (FinderException e) {
406: } finally {
407: utx.rollback();
408: }
409: Simple entity3 = getHome().findByTestName("pk7");
410: assertEquals(70, entity3.getInfo());
411: }
412:
413: /**
414: * testFinderEnum verify a finder method that return a Enumeration
415: * pre condition there are 3 elements with num =4
416: * all of them have a field info equals to 10
417: */
418: public void testFinderEnum() throws Exception {
419: Simple entity = null;
420: Enumeration listOfEntity = getHome().findInfoForNum(4);
421: int nb = 0;
422: while (listOfEntity.hasMoreElements()) {
423: entity = (Simple) javax.rmi.PortableRemoteObject.narrow(
424: listOfEntity.nextElement(), Simple.class);
425: assertEquals(10, entity.getInfo());
426: nb++;
427: }
428: assertEquals(3, nb);
429: }
430:
431: /**
432: * Verify that we can create an entity and retrieve it by
433: * a finder method inside the same transaction
434: */
435: public void testCreateFindUserTx() throws Exception {
436: utx.begin();
437: try {
438: Simple e1 = getHome().create("pk120", 32, 7);
439: Simple e2 = getHome().findByTestName("pk120");
440: assertTrue(e2.isIdentical(e1));
441: } catch (Exception e) {
442: fail(e.getMessage());
443: } finally {
444: utx.rollback();
445: }
446: }
447:
448: /**
449: * testFinderCollection verify a finder method that return a Collection
450: * pre condition there are 4 elements with c_numtest =4 (pk4,...,pk7)
451: * findInCollection returns all the beans where c_numtest = 4
452: * this test is equivalent to testcase2 in SimpleTest in finder_col
453: */
454: public void testFinderCollection() throws Exception {
455: Simple entity = null;
456: Collection cListEntity = getHome().findInCollection();
457: int nb = 0;
458: Iterator icListEntity = cListEntity.iterator();
459: while (icListEntity.hasNext()) {
460: entity = (Simple) javax.rmi.PortableRemoteObject.narrow(
461: icListEntity.next(), Simple.class);
462: nb++;
463: }
464: assertEquals(4, nb);
465: }
466:
467: /**
468: * Test that the removed is no more seen after the rollback
469: */
470: public void testFindAfterRBR() throws Exception {
471: getHome().findByPrimaryKey("pk8");
472: utx.begin();
473: getHome().remove("pk8");
474: utx.rollback();
475: getHome().findByTestName("pk8");
476: }
477:
478: /**
479: * test loop on finder method (findByPrimaryKey)
480: */
481: public void testLoopFindByPK() throws Exception {
482: for (int i = 0; i < 20; i++) {
483: getHome().findByPrimaryKey("pk9");
484: }
485: }
486:
487: /**
488: * test loop on other finder method
489: */
490: public void testLoopFinder() throws Exception {
491: for (int i = 0; i < 20; i++) {
492: getHome().findByTestName("pk10");
493: }
494: }
495:
496: /**
497: * test loopback on non reentrant bean in same tx
498: */
499: public void testLoopBackTx() throws Exception {
500: Simple s = getHome().findByPrimaryKey("pk9");
501: assertTrue(s.loopBackTx());
502: }
503:
504: /**
505: * test that we can access the instance twice in the same transaction,
506: * even if the bean is non reentrant.
507: */
508: public void testAccessTwiceTx() throws Exception {
509: Simple s = getHome().findByPrimaryKey("pk9");
510: utx.begin();
511: try {
512: s.getNumTest();
513: s.getNumTest();
514: } finally {
515: utx.commit();
516: }
517: }
518:
519: /**
520: * test that we can access the instance twice in the same transaction,
521: * even if the bean is non reentrant.
522: */
523: public void testFindAccessTx() throws Exception {
524: utx.begin();
525: try {
526: Simple s = getHome().findByPrimaryKey("pk9");
527: s.getNumTest();
528: } finally {
529: utx.commit();
530: }
531: }
532:
533: /**
534: * test loopback on non reentrant bean outside tx
535: * Invalid this test because loopback with no transaction context
536: * is not clearly specified in EJB spec.
537: */
538: public void _testLoopBack() throws Exception {
539: Simple s = getHome().findByPrimaryKey("pk10");
540: assertTrue(s.loopBack());
541: }
542: }
|