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