001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.ejb.runtime.basic;
018:
019: import org.objectweb.speedo.ejb.SpeedoTestHelper;
020:
021: import junit.framework.Assert;
022:
023: /**
024: *
025: * @author S.Chassande-Barrioz
026: */
027: public class TestBasicA extends SpeedoTestHelper {
028:
029: public TestBasicA(String s) {
030: super (s, "");
031: }
032:
033: protected String getLoggerName() {
034: return LOG_NAME + ".ejb.rt.basic.TestBasicA";
035: }
036: /*
037: public void testgetPMF() {
038: assertTrue("Not the same PMF",
039: pmf == JDOHelper.getPersistenceManagerFactory(getPMFProperties()));
040: }
041: public void testCreateInTxAndRemoveInTx2PM() {
042: logger.log(BasicLevel.DEBUG, "testCreateInTxAndRemoveInTx2PM");
043: BasicA ba = new BasicA();
044: ba.writeF1("testCreateInTxAndRemoveInTx2PM");
045: ba.writeF2(2);
046: Locale l = new Locale("en"); //Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC"
047: ba.setLocale(l);
048: PersistenceManager pm = pmf.getPersistenceManager();
049: pm.currentTransaction().begin();
050: pm.makePersistent(ba);
051: Object id = pm.getObjectId(ba);
052: Assert.assertNotNull("Null object identifier", id);
053: assertEquals(BasicA.CALLBACK_INIT, ba.getCheckJdoStore());
054: assertEquals(BasicA.CALLBACK_INIT, ba.getCheckJdoLoad());
055: assertEquals(BasicA.CALLBACK_INIT, ba.getCheckJdoClear());
056: assertEquals(BasicA.CALLBACK_INIT, ba.getCheckJdoDelete());
057: assertEquals("Bad locale language value", l.getLanguage(), ba.getLocale().getLanguage());
058: assertEquals("Bad locale country value", l.getCountry(), ba.getLocale().getCountry());
059: assertEquals("Bad locale variant value", l.getVariant(), ba.getLocale().getVariant());
060: pm.currentTransaction().commit();
061: assertEquals(2, ba.getCheckJdoStore());
062: pm.close();
063:
064: ba = null;
065:
066: pm = pmf.getPersistenceManager();
067: pmf.getDataStoreCache().evictAll();
068: pm.currentTransaction().begin();
069: ba = (BasicA) pm.getObjectById(id, true);
070: Assert.assertNotNull("Object not found", ba);
071: Assert.assertEquals("Bad f1 value", "testCreateInTxAndRemoveInTx2PM", ba.readF1());
072: Assert.assertEquals("Bad f1 value", 2, ba.readF2());
073: assertEquals("Bad locale language value", l.getLanguage(), ba.getLocale().getLanguage());
074: assertEquals("Bad locale country value", l.getCountry(), ba.getLocale().getCountry());
075: assertEquals("Bad locale variant value", l.getVariant(), ba.getLocale().getVariant());
076: //assertEquals(BasicA.CALLBACK_INIT + 1, ba.getCheckJdoClear());
077: assertEquals(BasicA.CALLBACK_INIT, ba.getCheckJdoDelete());
078: pm.deletePersistent(ba);
079: assertEquals(BasicA.CALLBACK_INIT + 1, ba.getCheckJdoDelete());
080: pm.currentTransaction().commit();
081:
082: try {
083: Object o = pm.getObjectById(id, true);
084: if (o != null)
085: fail("Removed object always availlable: " + o);
086: } catch (PersistenceException e) {
087: //OK
088: } catch (Exception e) {
089: e.printStackTrace();
090: fail("Bad exception thrown when an object does not exist: " + e);
091: }
092:
093: pm.close();
094: }
095:
096: public void testCreateAndRemoveInTx2PM() {
097: logger.log(BasicLevel.DEBUG, "testCreateAndRemoveInTx2PM");
098: BasicA ba = new BasicA();
099: ba.writeF1("testCreateAndRemoveInTx2PM");
100: ba.writeF2(2);
101: Locale l = new Locale("de", "de"); //Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC"
102: ba.setLocale(l);
103: PersistenceManager pm = pmf.getPersistenceManager();
104: pm.makePersistent(ba);
105: Object id = pm.getObjectId(ba);
106: Assert.assertNotNull("Null object identifier", id);
107: pm.close();
108:
109: ba = null;
110:
111: pm = pmf.getPersistenceManager();
112: pm.currentTransaction().begin();
113: ba = (BasicA) pm.getObjectById(id, true);
114: Assert.assertNotNull("Object not found", ba);
115: Assert.assertEquals("Bad f1 value", "testCreateAndRemoveInTx2PM", ba.readF1());
116: Assert.assertEquals("Bad f1 value", 2, ba.readF2());
117: assertEquals("Bad locale language value", l.getLanguage(), ba.getLocale().getLanguage());
118: assertEquals("Bad locale country value", l.getCountry(), ba.getLocale().getCountry());
119: assertEquals("Bad locale variant value", l.getVariant(), ba.getLocale().getVariant());
120:
121: pm.deletePersistent(ba);
122: pm.currentTransaction().commit();
123: pm.close();
124: }
125:
126:
127: public void testCreateAndRemoveInTx1PM() {
128: logger.log(BasicLevel.DEBUG, "testCreateAndRemoveInTx1PM");
129: BasicA ba = new BasicA();
130: ba.writeF1("testCreateAndRemoveInTx1PM");
131: ba.writeF2(2);
132: PersistenceManager pm = pmf.getPersistenceManager();
133: pm.makePersistent(ba);
134: pm.currentTransaction().begin();
135: pm.deletePersistent(ba);
136: pm.currentTransaction().commit();
137: pm.close();
138: }
139:
140: public void testCreateAndRemoveInSameTx1PM() {
141: logger.log(BasicLevel.DEBUG, "testCreateAndRemoveInSameTx1PM");
142: BasicA ba = new BasicA();
143: ba.writeF1("testCreateAndRemoveInSameTx1PM");
144: ba.writeF2(2);
145: PersistenceManager pm = pmf.getPersistenceManager();
146: pm.currentTransaction().begin();
147: pm.makePersistent(ba);
148: ba.writeF2(3);
149: pm.deletePersistent(ba);
150: try {
151: pm.currentTransaction().commit();
152: } catch (PersistenceException e) {
153: logger.log(BasicLevel.ERROR, "testCreateAndRemoveInSameTx1PM", ExceptionHelper.getNested(e));
154: fail("Jorm does not support export following by an unexport");
155: }
156: pm.close();
157: }
158:
159: public void testCreateInTxAndRemoveInTx1PM() {
160: logger.log(BasicLevel.DEBUG, "testCreateInTxAndRemove1PM");
161: BasicA ba = new BasicA();
162: ba.writeF1("testCreateInTxAndRemove1PM");
163: ba.writeF2(2);
164: PersistenceManager pm = pmf.getPersistenceManager();
165: pm.currentTransaction().begin();
166: pm.makePersistent(ba);
167: pm.currentTransaction().commit();
168: pm.currentTransaction().begin();
169: pm.deletePersistent(ba);
170: pm.currentTransaction().commit();
171: try {
172: pm.close();
173: } catch (PersistenceException e) {
174: logger.log(BasicLevel.ERROR, "testCreateInTxAndRemove1PM", ExceptionHelper.getNested(e));
175: fail("Speedo does not support the use of a PM after a transaction commit");
176: }
177: }
178:
179: public void testTransientAfterTx() {
180: logger.log(BasicLevel.DEBUG, "testTransientAfterTx");
181: BasicA ba = new BasicA();
182: ba.writeF1("testTransient");
183: ba.writeF2(2);
184: Locale l = new Locale("GB"); //Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC"
185: ba.setLocale(l);
186: PersistenceManager pm = pmf.getPersistenceManager();
187: pm.currentTransaction().begin();
188: pm.makePersistent(ba);
189: pm.currentTransaction().commit();
190: Object oid = pm.getObjectId(ba);
191: try {
192: pm.makeTransient(ba);
193: pm.close();
194: Assert.assertEquals("Bad f1 value", "testTransient", ba.readF1());
195: assertEquals("Bad locale language value", l.getLanguage(), ba.getLocale().getLanguage());
196: assertEquals("Bad locale country value", l.getCountry(), ba.getLocale().getCountry());
197: assertEquals("Bad locale variant value", l.getVariant(), ba.getLocale().getVariant());
198: ba = null;
199: } catch (PersistenceException e) {
200: logger.log(BasicLevel.ERROR, "testTransient", ExceptionHelper.getNested(e));
201: fail("Speedo does not support the makeTransient operation after a Tx");
202: } finally {
203: pm = pmf.getPersistenceManager();
204: ba = (BasicA) pm.getObjectById(oid, false);
205: pm.currentTransaction().begin();
206: pm.deletePersistent(ba);
207: pm.currentTransaction().commit();
208: pm.close();
209: }
210: }
211:
212: public void testTransientNoTx() {
213: logger.log(BasicLevel.DEBUG, "testTransientNoTx");
214: BasicA ba = new BasicA();
215: ba.writeF1("testTransient");
216: ba.writeF2(2);
217: PersistenceManager pm = pmf.getPersistenceManager();
218: pm.currentTransaction().begin();
219: pm.makePersistent(ba);
220: pm.currentTransaction().commit();
221: Object oid = pm.getObjectId(ba);
222: pm.close();
223: ba = null;
224: pm = pmf.getPersistenceManager();
225: try {
226: ba = (BasicA) pm.getObjectById(oid, false);
227: pm.makeTransient(ba);
228: pm.close();
229: Assert.assertEquals("Bad f1 value", "testTransient", ba.readF1());
230: ba = null;
231: } catch (PersistenceException e) {
232: logger.log(BasicLevel.ERROR, "testTransientNoTx", ExceptionHelper.getNested(e));
233: fail("Speedo does not support the makeTransient operation (notx)");
234: } finally {
235: pm = pmf.getPersistenceManager();
236: ba = (BasicA) pm.getObjectById(oid, false);
237: pm.currentTransaction().begin();
238: pm.deletePersistent(ba);
239: pm.currentTransaction().commit();
240: pm.close();
241: }
242: }
243:
244: public void testImbricatedPM() {
245: logger.log(BasicLevel.DEBUG, "testImbricatedPM");
246: BasicA ba = new BasicA();
247: ba.writeF1("testImbricatedPM");
248: ba.writeF2(2);
249: PersistenceManager pm1 = pmf.getPersistenceManager();
250: PersistenceManager pm2 = pmf.getPersistenceManager();
251: Assert.assertTrue("Same persistence manager", pm1!=pm2);
252: pm2.makePersistent(ba);
253: pm2.close();
254: //touch the pm
255: pm1.getUserObject();
256: ba.readF1();
257: ba.readF2();
258: pm1.currentTransaction().begin();
259: pm1.deletePersistent(ba);
260: pm1.currentTransaction().commit();
261: try {
262: pm1.close();
263: } catch (PersistenceException e) {
264: logger.log(BasicLevel.ERROR, "testImbricatedPM", ExceptionHelper.getNested(e));
265: fail("Speedo does not support the use of a PM after a transaction commit");
266: }
267: }
268:
269: public void testRollBackTxSamePM() {
270: logger.log(BasicLevel.DEBUG, "testRollBackTxSamePM");
271: BasicA ba = new BasicA();
272: ba.writeF1("testRollBack1");
273: ba.writeF2(2);
274: PersistenceManager pm = pmf.getPersistenceManager();
275:
276: pm.currentTransaction().begin();
277: pm.makePersistent(ba);
278: pm.currentTransaction().commit();
279:
280: pm.currentTransaction().begin();
281: ba.writeF2(3);
282: Assert.assertEquals("Bad f2 value before roolback", 3, ba.readF2());
283: pm.currentTransaction().rollback();
284: Assert.assertEquals("Bad f2 value after roolback", 2, ba.readF2());
285:
286: pm.currentTransaction().begin();
287: pm.deletePersistent(ba);
288: pm.currentTransaction().commit();
289: pm.close();
290: }
291:
292: public void testRollBack2() {
293: logger.log(BasicLevel.DEBUG, "testRollBack2");
294: BasicA ba = new BasicA();
295: ba.writeF1("testRollBack1");
296: ba.writeF2(2);
297: PersistenceManager pm = pmf.getPersistenceManager();
298:
299: pm.currentTransaction().begin();
300: pm.makePersistent(ba);
301: pm.currentTransaction().commit();
302:
303: pm.currentTransaction().begin();
304: ba.writeF2(3);
305: Assert.assertEquals("Bad f2 value before roolback", 3, ba.readF2());
306: pm.currentTransaction().rollback();
307: Assert.assertEquals("Bad f2 value after roolback", 2, ba.readF2());
308: ba.writeF2(4);
309: Assert.assertEquals("Bad f2 value after roolback", 4, ba.readF2());
310: pm.close();
311:
312: pm = pmf.getPersistenceManager();
313: Assert.assertEquals("Bad f2 value after roolback", 4, ba.readF2());
314:
315: pm.currentTransaction().begin();
316: pm.deletePersistent(ba);
317: pm.currentTransaction().commit();
318: pm.close();
319: }
320:
321: public void testBasicType(long longval) {
322: logger.log(BasicLevel.DEBUG, "testBasicType" + longval);
323: BasicType bt = new BasicType();
324: bt.assign(longval);
325: PersistenceManager pm = pmf.getPersistenceManager();
326:
327: pm.currentTransaction().begin();
328: pm.makePersistent(bt);
329: Object oid = pm.getObjectId(bt);
330: pm.currentTransaction().commit();
331:
332: bt = null;
333: pmf.getDataStoreCache().evictAll();
334:
335: pm.currentTransaction().begin();
336: bt = (BasicType) pm.getObjectById(oid, false);
337: Assert.assertNotNull("bt is null", bt);
338: bt.check(longval);
339: pm.deletePersistent(bt);
340: pm.currentTransaction().commit();
341: pm.close();
342: }
343: public void testBasicType35() {
344: testBasicType(35);
345: }
346: public void _testBasicType0() {
347: testBasicType(0);
348: }
349:
350: public void testSerialization() {
351: logger.log(BasicLevel.DEBUG, "testSerialization");
352: BasicA ba = new BasicA();
353: ba.writeF1("testSerialization");
354: ba.writeF2(5);
355: Locale l = new Locale("en"); //Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC"
356: ba.setLocale(l);
357: PersistenceManager pm = pmf.getPersistenceManager();
358:
359: pm.currentTransaction().begin();
360: pm.makePersistent(ba);
361: Object oid = pm.getObjectId(ba);
362: pm.currentTransaction().commit();
363: pm.close();
364: File f = new File(new File(new File("output"),"test"), "testSerialization");
365: if (f.exists()) {
366: f.delete();
367: }
368: try {
369: ObjectOutputStream oos = new ObjectOutputStream(
370: new FileOutputStream(f));
371: oos.writeObject(ba);
372: oos.close();
373:
374: ba = null;
375: ObjectInputStream ois = new ObjectInputStream(
376: new FileInputStream(f));
377: ba = (BasicA) ois.readObject();
378: ois.close();
379: ba.readF1_F2();
380: assertEquals("Bad locale language value", l.getLanguage(), ba.getLocale().getLanguage());
381: assertEquals("Bad locale country value", l.getCountry(), ba.getLocale().getCountry());
382: assertEquals("Bad locale variant value", l.getVariant(), ba.getLocale().getVariant());
383:
384: } catch (Exception e) {
385: logger.log(BasicLevel.ERROR, "testSerialization fails: ", e);
386: fail(e.getMessage());
387: } finally {
388: pm = pmf.getPersistenceManager();
389: pm.currentTransaction().begin();
390: pm.deletePersistent(pm.getObjectById(oid, false));
391: pm.currentTransaction().commit();
392: pm.close();
393: }
394: }
395:
396: public void testStaticFinder() {
397: logger.log(BasicLevel.DEBUG, "testStaticFinder");
398: ArrayList oids = new ArrayList();
399: PersistenceManager pm = pmf.getPersistenceManager();
400: for(int i =0; i<50; i++) {
401: BasicA ba = new BasicA();
402: ba.writeF1("testStaticFinder" + i);
403: ba.writeF2(i);
404: pm.makePersistent(ba);
405: oids.add(pm.getObjectId(ba));
406: }
407: pm.close();
408: try {
409: List l = BasicA.getAllBasicAId(pmf);
410: assertSameCollection("bad query result", oids, l);
411: } finally {
412: pm = pmf.getPersistenceManager();
413: pm.currentTransaction().begin();
414: for (Iterator it = oids.iterator(); it.hasNext();) {
415: pm.deletePersistent(pm.getObjectById(it.next(), false));
416: }
417: pm.currentTransaction().commit();
418: pm.close();
419: }
420: }
421:
422: public void testNonNullField() {
423: logger.log(BasicLevel.DEBUG, "testNonNullField");
424: BasicA ba = new BasicA();
425: ba.writeF1("testNonNullField");
426: PersistenceManager pm = pmf.getPersistenceManager();
427: pm.currentTransaction().begin();
428: pm.makePersistent(ba);
429: pm.currentTransaction().commit();
430: pm.currentTransaction().begin();
431: ba.setNonullField(null);
432: try {
433: pm.currentTransaction().commit();
434: fail("no exception thrown when a null value try to be store for a persistent field marked as non null");
435: } catch (PsersistenceException e) {
436: } catch (Exception e) {
437: logger.log(BasicLevel.ERROR, "Bad expected exception", e);
438: fail("Bad exception thrown when a null value try to be store for a persistent field marked as non null: " + e.getClass());
439: } finally {
440: pm.currentTransaction().begin();
441: pm.deletePersistent(ba);
442: pm.currentTransaction().commit();
443: pm.close();
444: }
445: }
446: public void testRefresh() {
447: logger.log(BasicLevel.DEBUG, "testRefresh");
448: BasicA ba = new BasicA();
449: ba.writeF1("testRefresh");
450: ba.writeF2(2);
451: PersistenceManager pm = pmf.getPersistenceManager();
452: pm.currentTransaction().begin();
453: pm.makePersistent(ba);
454: pm.currentTransaction().commit();
455:
456: pm.currentTransaction().begin();
457: pm.refresh(ba);
458: int f2 = ba.readF2();
459: assertTrue("bad F2 value:" + f2, f2 < 4);
460: pm.currentTransaction().commit();
461:
462: pm.currentTransaction().begin();
463: pm.deletePersistent(ba);
464: pm.currentTransaction().commit();
465: pm.close();
466: }
467:
468: public void testRefreshDirty() {
469: logger.log(BasicLevel.DEBUG, "testRefreshDirty");
470: BasicA ba = new BasicA();
471: ba.writeF1("testRefreshDirty");
472: ba.writeF2(2);
473: PersistenceManager pm = pmf.getPersistenceManager();
474: pm.currentTransaction().begin();
475: pm.makePersistent(ba);
476: pm.currentTransaction().commit();
477:
478: pm.currentTransaction().begin();
479: ba.writeF2(4);
480: pm.refresh(ba);
481: int f2 = ba.readF2();
482: assertTrue("bad F2 value:" + f2, f2 < 4);
483: pm.currentTransaction().commit();
484:
485: pm.currentTransaction().begin();
486: pm.deletePersistent(ba);
487: pm.currentTransaction().commit();
488: pm.close();
489: }
490:
491:
492: public void testPONEICompatible() {
493: logger.log(BasicLevel.DEBUG, "testPONEICompatible");
494: final String[] FIELD_NAMES = new String[]{
495: "locale",
496: "d",
497: "f2",
498: "privateField",
499: "protectedField",
500: "f1",
501: "myArrayOfchar",
502: "noModifierField",
503: "undeclaredField",
504: "l",
505: "d2",
506: "nonullField"
507: };
508: final Class[] FIELD_TYPES = new Class[]{
509: java.util.Locale.class,
510: java.util.Date.class,
511: Integer.TYPE,
512: Integer.TYPE,
513: Integer.TYPE,
514: java.lang.String.class,
515: char[].class,
516: Integer.TYPE,
517: java.lang.String.class,
518: Long.TYPE,
519: java.util.Date.class,
520: java.lang.String.class
521: };
522: assertTrue("Class not registered: ",
523: JDOImplHelper.getInstance().getRegisteredClasses().contains(BasicA.class));
524: final String[] fns = JDOImplHelper.getInstance().getFieldNames(BasicA.class);
525: assertNotNull("Null array of field names", fns);
526: assertEquals("Bad length of the array of field names", FIELD_NAMES.length, fns.length);
527: final Class[] fts = JDOImplHelper.getInstance().getFieldTypes(BasicA.class);
528: assertNotNull("Null array of field types", fts);
529: assertEquals("Bad length of the array of field types", FIELD_TYPES.length, fts.length);
530: final HashMap expected = new HashMap();
531: final HashMap found = new HashMap();
532: for(int i=0; i<FIELD_NAMES.length; i++) {
533: expected.put(FIELD_NAMES[i], FIELD_TYPES[i]);
534: found.put(fns[i], fts[i]);
535: }
536: assertEquals("Bad content", expected, found);
537: final Class superClass = JDOImplHelper.getInstance().getPersistenceCapableSuperclass(BasicA.class);
538: assertNull("Bad super class name", superClass);
539:
540: BasicA ba = new BasicA();
541: ba.writeF1("testPONEICompatible");
542: ba.writeF2(23242);
543: PersistenceManager pm = pmf.getPersistenceManager();
544: pm.currentTransaction().begin();
545: pm.makePersistent(ba);
546: pm.currentTransaction().commit();
547:
548: //Field access inside a transaction
549: pm.currentTransaction().begin();
550: assertEquals("Bad value on getter", new Integer(23242),
551: invokeStaticMethod(BasicA.class, "jdoGetf2",
552: new Class[]{BasicA.class},
553: new Object[]{ba}));
554: assertNull("Bad value on getter",
555: invokeStaticMethod(BasicA.class, "jdoSetf2",
556: new Class[]{BasicA.class, Integer.TYPE},
557: new Object[]{ba, new Integer(1)}));
558: assertEquals("Bad value on getter", new Integer(1),
559: invokeStaticMethod(BasicA.class, "jdoGetf2",
560: new Class[]{BasicA.class},
561: new Object[]{ba}));
562: pm.currentTransaction().commit();
563:
564: //Field access outside a transaction
565: assertEquals("Bad value on getter", new Integer(1),
566: invokeStaticMethod(BasicA.class, "jdoGetf2",
567: new Class[]{BasicA.class},
568: new Object[]{ba}));
569: assertNull("Bad value on getter",
570: invokeStaticMethod(BasicA.class, "jdoSetf2",
571: new Class[]{BasicA.class, Integer.TYPE},
572: new Object[]{ba, new Integer(2)}));
573: assertEquals("Bad value on getter", new Integer(2),
574: invokeStaticMethod(BasicA.class, "jdoGetf2",
575: new Class[]{BasicA.class},
576: new Object[]{ba}));
577:
578: pm.close();
579: pm = pmf.getPersistenceManager();
580: pm.currentTransaction().begin();
581: pm.deletePersistent(ba);
582: pm.currentTransaction().commit();
583: pm.close();
584:
585: }
586:
587: // test delete and make an object persistent in the same transaction
588: public void testDeleteMakePersistent() {
589: long pIdentifier = 1;
590: PersistenceManager pm = pmf.getPersistenceManager();
591: try {
592: Product p = new Product(pIdentifier);
593: p.setCost("cost1");
594: p.setProductName("product1");
595: //make a product persistent
596: pm.currentTransaction().begin();
597: pm.makePersistent(p);
598: Object pId = pm.getObjectId(p);
599: Assert.assertNotNull("null object identifier", pId);
600: pm.currentTransaction().commit();
601:
602: p = null;
603: p = (Product) pm.getObjectById(pId, true);
604: Assert.assertNotNull("null instance returned by getObjectById", p);
605: Assert.assertEquals("Bad product id", pIdentifier, p.getId());
606: //delete it
607: //and recreate a new one with the same id
608: pm.currentTransaction().begin();
609: pm.deletePersistent(p);
610: Product newP = new Product(pIdentifier);
611: newP.setCost("cost2");
612: newP.setProductName("product2");
613: pm.makePersistent(newP);
614: pm.currentTransaction().commit();
615: } catch (Exception e) {
616: e.printStackTrace();
617: fail(e.getMessage());
618: } finally {
619: if (pm.currentTransaction().isActive())
620: pm.currentTransaction().rollback();
621: pm.close();
622: }
623: pm = pmf.getPersistenceManager();
624: pm.currentTransaction().begin();
625: pm.deletePersistent(pm.getObjectById(
626: pm.newObjectIdInstance(Product.class, "" + pIdentifier), false));
627: pm.currentTransaction().commit();
628: pm.close();
629: }
630:
631:
632: //test delete and make an object persistent in the same transaction
633: public void testDeleteRollback() {
634: logger.log(BasicLevel.DEBUG, "testDeleteRollBack");
635: BasicA ba = new BasicA();
636: ba.writeF1("testDeleteRollBack");
637: ba.writeF2(2);
638: PersistenceManager pm = pmf.getPersistenceManager();
639:
640: pm.currentTransaction().begin();
641: pm.makePersistent(ba);
642: pm.currentTransaction().commit();
643:
644: pm.currentTransaction().begin();
645: pm.deletePersistent(ba);
646: pm.currentTransaction().rollback();
647:
648: pm.currentTransaction().begin();
649: assertTrue("The object should be persistent", JDOHelper.isPersistent(ba));
650: assertEquals("F2 should be 2", 2, ba.readF2());
651: pm.deletePersistent(ba);
652: pm.currentTransaction().commit();
653:
654: pm.close();
655: }
656: public void testDeleteAndUse() {
657: logger.log(BasicLevel.DEBUG, "testRefreshDirty");
658: BasicA ba = new BasicA();
659: ba.writeF1("testRefreshDirty");
660: ba.writeF2(2);
661: PersistenceManager pm = pmf.getPersistenceManager();
662: pm.currentTransaction().begin();
663: pm.makePersistent(ba);
664: pm.currentTransaction().commit();
665:
666: pm.currentTransaction().begin();
667: pm.deletePersistent(ba);
668: assertEquals("bad F2 value after delete:", 2, ba.readF2());
669: ba.writeF2(3);
670: assertEquals("bad F2 value after delete and set:", 3, ba.readF2());
671: pm.currentTransaction().commit();
672: assertEquals("bad F2 value after commit:", 3, ba.readF2());
673: ba.writeF2(4);
674: assertEquals("bad F2 value after commit and set:", 4, ba.readF2());
675: pm.close();
676: assertEquals("bad F2 value after close:", 4, ba.readF2());
677: ba.writeF2(5);
678: assertEquals("bad F2 value after close and set:", 5, ba.readF2());
679: }
680:
681: public void testInstanceLifecycleListener() {
682: final int PRE = 0;
683: final int POST = 1;
684: final InstanceLifecycleEvent[][] marks = new InstanceLifecycleEvent[8][2];
685: final int[] nbEvent = new int[1];
686: nbEvent[0] = 0;
687: InstanceLifecycleListener[] listeners = new InstanceLifecycleListener[8];
688: listeners[InstanceLifecycleEvent.ATTACH] = new AttachLifecycleListener() {
689: public void preAttach(InstanceLifecycleEvent ev) {
690: marks[InstanceLifecycleEvent.ATTACH][PRE] = ev;
691: nbEvent[0] ++;
692: }
693: public void postAttach(InstanceLifecycleEvent ev) {
694: marks[InstanceLifecycleEvent.ATTACH][POST] = ev;
695: nbEvent[0] ++;
696: }
697: };
698: listeners[InstanceLifecycleEvent.DETACH] = new DetachLifecycleListener() {
699: public void preDetach(InstanceLifecycleEvent ev) {
700: marks[InstanceLifecycleEvent.DETACH][PRE] = ev;
701: nbEvent[0] ++;
702: }
703: public void postDetach(InstanceLifecycleEvent ev) {
704: marks[InstanceLifecycleEvent.DETACH][POST] = ev;
705: nbEvent[0] ++;
706: }
707: };
708: listeners[InstanceLifecycleEvent.CREATE] = new CreateLifecycleListener() {
709: public void postCreate(InstanceLifecycleEvent ev) {
710: marks[InstanceLifecycleEvent.CREATE][POST] = ev;
711: nbEvent[0] ++;
712: }
713: };
714: listeners[InstanceLifecycleEvent.DELETE] = new DeleteLifecycleListener() {
715: public void preDelete(InstanceLifecycleEvent ev) {
716: marks[InstanceLifecycleEvent.DELETE][PRE] = ev;
717: nbEvent[0] ++;
718: }
719: public void postDelete(InstanceLifecycleEvent ev) {
720: marks[InstanceLifecycleEvent.DELETE][POST] = ev;
721: nbEvent[0] ++;
722: }
723: };
724: listeners[InstanceLifecycleEvent.LOAD] = new LoadLifecycleListener() {
725: public void preLoad(InstanceLifecycleEvent ev) {
726: marks[InstanceLifecycleEvent.LOAD][PRE] = ev;
727: nbEvent[0] ++;
728: }
729: public void postLoad(InstanceLifecycleEvent ev) {
730: marks[InstanceLifecycleEvent.LOAD][POST] = ev;
731: nbEvent[0] ++;
732: }
733: };
734: listeners[InstanceLifecycleEvent.STORE] = new StoreLifecycleListener() {
735: public void preStore(InstanceLifecycleEvent ev) {
736: marks[InstanceLifecycleEvent.STORE][PRE] = ev;
737: nbEvent[0] ++;
738: }
739: public void postStore(InstanceLifecycleEvent ev) {
740: marks[InstanceLifecycleEvent.STORE][POST] = ev;
741: nbEvent[0] ++;
742: }
743: };
744: listeners[InstanceLifecycleEvent.DIRTY] = new DirtyLifecycleListener() {
745: public void preDirty(InstanceLifecycleEvent ev) {
746: marks[InstanceLifecycleEvent.DIRTY][PRE] = ev;
747: nbEvent[0] ++;
748: }
749: public void postDirty(InstanceLifecycleEvent ev) {
750: marks[InstanceLifecycleEvent.DIRTY][POST] = ev;
751: nbEvent[0] ++;
752: }
753: };
754: listeners[InstanceLifecycleEvent.CLEAR] = new ClearLifecycleListener() {
755: public void preClear(InstanceLifecycleEvent ev) {
756: marks[InstanceLifecycleEvent.CLEAR][PRE] = ev;
757: nbEvent[0] ++;
758: }
759: public void postClear(InstanceLifecycleEvent ev) {
760: marks[InstanceLifecycleEvent.CLEAR][POST] = ev;
761: nbEvent[0] ++;
762: }
763: };
764: PersistenceManager pm = pmf.getPersistenceManager();
765: for (int i = 0; i < listeners.length; i++) {
766: pmf.addInstanceLifecycleListener(listeners[i], new Class[]{BasicA.class});
767: }
768: BasicA ba = new BasicA();
769: assertEquals("unexpect event on create ", 0, nbEvent[0]);
770: ba.writeF1("testInstanceLifecycleListener_1");
771: assertEquals("unexpect event on write ", 0, nbEvent[0]);
772: pm.currentTransaction().begin();
773: pm.makePersistent(ba);
774: assertEquals("Bad event number on make persistent: ", 1, nbEvent[0]);
775: ba.writeF1("testInstanceLifecycleListener_1");
776: assertEquals("Bad event number on make persistent after write: ", 1, nbEvent[0]);
777: assertNotNull("Not postCreate event", marks[InstanceLifecycleEvent.CREATE][POST]);
778: assertTrue("Bad source ", marks[InstanceLifecycleEvent.CREATE][POST].getSource() == ba);
779:
780: clear(nbEvent, marks);
781: pm.currentTransaction().commit();
782: pm.currentTransaction().begin();
783: assertTrue("Bad event number on commit: " + nbEvent[0], 2 == nbEvent[0] | 3 ==nbEvent[0]);
784: assertNotNull("Not preStore event", marks[InstanceLifecycleEvent.STORE][PRE]);
785: assertTrue("Bad source on preStore", marks[InstanceLifecycleEvent.STORE][PRE].getSource() == ba);
786: assertNotNull("Not postStore event", marks[InstanceLifecycleEvent.STORE][POST]);
787: assertTrue("Bad source on postStore", marks[InstanceLifecycleEvent.STORE][POST].getSource() == ba);
788: pm.currentTransaction().commit();
789:
790: pm.evict(ba);
791:
792: pm.currentTransaction().begin();
793: clear(nbEvent, marks);
794: assertEquals("Bad f1 value" , "testInstanceLifecycleListener_1", ba.readF1());
795: assertTrue("Bad event number on load: ", 2 == nbEvent[0] || 1 == nbEvent[0]);
796: assertNotNull("Not postStore event", marks[InstanceLifecycleEvent.LOAD][POST]);
797: assertTrue("Bad source on postStore", marks[InstanceLifecycleEvent.LOAD][POST].getSource() == ba);
798: */
799: /**
800: nbEvent[0] = 0;
801: ba.writeF1("testInstanceLifecycleListener_2");
802: assertEquals("Bad event number on write: ", 2, nbEvent[0]);
803: assertNotNull("Not preStore event", marks[InstanceLifecycleEvent.DIRTY][PRE]);
804: assertTrue("Bad source on preStore", marks[InstanceLifecycleEvent.DIRTY][PRE].getSource() == ba);
805: assertNotNull("Not postStore event", marks[InstanceLifecycleEvent.DIRTY][POST]);
806: assertTrue("Bad source on postStore", marks[InstanceLifecycleEvent.DIRTY][POST].getSource() == ba);
807: */
808: /**
809: pm.currentTransaction().rollback();
810:
811: pm.currentTransaction().begin();
812: clear(nbEvent, marks);
813: pm.deletePersistent(ba);
814: assertTrue("Bad event number on delete persistent: " + nbEvent[0], 2 == nbEvent[0] || 3 == nbEvent[0]);
815: assertNotNull("Not preStore event", marks[InstanceLifecycleEvent.DELETE][PRE]);
816: assertTrue("Bad source on preStore", marks[InstanceLifecycleEvent.DELETE][PRE].getSource() == ba);
817: assertNotNull("Not postStore event", marks[InstanceLifecycleEvent.DELETE][POST]);
818: assertTrue("Bad source on postStore", marks[InstanceLifecycleEvent.DELETE][POST].getSource() == ba);
819:
820: pm.currentTransaction().commit();
821: pm.close();
822: for (int i = 0; i < listeners.length; i++) {
823: pmf.removeInstanceLifecycleListener(listeners[i]);
824: }
825: }
826:
827: private void clear(int[] nbEvent, Object[][] marks) {
828: nbEvent[0] = 0;
829: for(int i=0; i<8; i++) {
830: marks[i][0] = null;
831: marks[i][1] = null;
832: }
833: }
834:
835: private Object invokeStaticMethod(Class clazz,
836: String methodName,
837: Class[] paramTypes,
838: Object[] paramValues) {
839: try {
840: Method m = clazz.getMethod(methodName, paramTypes);
841: return m.invoke(null, paramValues);
842: } catch (Exception e) {
843: logger.log(BasicLevel.ERROR, "Exception during invocation:", e);
844: fail(e.getMessage());
845: return null;
846: }
847: }
848: */
849: }
|