001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.kernel;
020:
021: import org.apache.openjpa.persistence.kernel.common.apps.ModRuntimeTest1;
022: import org.apache.openjpa.persistence.kernel.common.apps.ModRuntimeTest2;
023: import org.apache.openjpa.persistence.kernel.common.apps.PersistenceAware;
024:
025: import org.apache.openjpa.enhance.PersistenceCapable;
026: import org.apache.openjpa.persistence.OpenJPAEntityManager;
027: import org.apache.openjpa.persistence.OpenJPAPersistence;
028: import org.apache.openjpa.persistence.RestoreStateType;
029:
030: public class TestTransition2 extends BaseKernelTest {
031:
032: private boolean supportsPessimistic = true;
033:
034: private int _id = 0;
035:
036: /**
037: * Creates a new instance of TestTransitions
038: */
039: public TestTransition2() {
040: }
041:
042: public TestTransition2(String name) {
043: super (name);
044: }
045:
046: public void setUp() throws Exception {
047: try {
048: OpenJPAEntityManager pm = getPM(false, false);
049: supportsPessimistic = !pm.getOptimistic();
050: pm.close();
051: } catch (Exception e) {
052: supportsPessimistic = false;
053: }
054:
055: OpenJPAEntityManager pm = getPM();
056: startTx(pm);
057:
058: ModRuntimeTest2 b = createTest2();
059: pm.persist(b);
060: _id = b.getId();
061:
062: endTx(pm);
063: endEm(pm);
064: }
065:
066: /**
067: * Tests basic jdo flag transitions from transient to
068: * persistent-transactional and back to transient after rollback.
069: */
070: public void testFlagTransitions1() throws Exception {
071: if (!supportsPessimistic)
072: return;
073:
074: ModRuntimeTest2 b = createTest2();
075: assertTransient(b);
076: assertTransient(b.getSelfOneOne());
077:
078: OpenJPAEntityManager pm = getPM(false, false);
079: startTx(pm);
080: pm.persist(b);
081:
082: assertPersistent(b, true, true, false, true);
083:
084: rollbackTx(pm);
085:
086: assertTransient(b);
087: assertTransient(b.getSelfOneOne());
088: endEm(pm);
089: }
090:
091: /**
092: * Tests basic jdo flag transitions from transient to
093: * persistent-transactional to persistent-nontransactional after commit.
094: */
095: public void testFlagTransitions2() throws Exception {
096: if (!supportsPessimistic)
097: return;
098:
099: ModRuntimeTest2 b = createTest2();
100: assertTransient(b);
101: assertTransient(b.getSelfOneOne());
102:
103: OpenJPAEntityManager pm = getPM(false, false);
104: startTx(pm);
105: pm.persist(b);
106:
107: assertPersistent(b, true, true, false, true);
108:
109: endTx(pm);
110:
111: assertPersistent(b, false, false, false, false);
112: assertPersistent(b.getSelfOneOne(), false, false, false, false);
113: endEm(pm);
114: }
115:
116: /**
117: * Tests basic jdo flag transitions when finding a transactional instance by
118: * id, then committing.
119: */
120: public void testFlagTransitions3() throws Exception {
121: if (!supportsPessimistic)
122: return;
123:
124: OpenJPAEntityManager pm = getPM(false, false);
125: startTx(pm);
126:
127: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
128: ModRuntimeTest2.class, _id);
129: assertPersistent(b, true, false, false, false);
130: assertPersistent(b.getSelfOneOne(), true, false, false, false);
131:
132: endTx(pm);
133:
134: assertPersistent(b, false, false, false, false);
135: assertPersistent(b.getSelfOneOne(), false, false, false, false);
136: endEm(pm);
137: }
138:
139: /**
140: * Tests basic jdo flag transitions when finding a transactional
141: * instance by id, then rolling back.
142: */
143: public void testFlagTransitions4() throws Exception {
144: if (!supportsPessimistic)
145: return;
146:
147: OpenJPAEntityManager pm = getPM(false, false);
148: startTx(pm);
149:
150: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
151: ModRuntimeTest2.class, _id);
152: assertPersistent(b, true, false, false, false);
153: assertPersistent(b.getSelfOneOne(), true, false, false, false);
154:
155: rollbackTx(pm);
156: //pm.getTransaction().rollback();
157:
158: assertPersistent(b, false, false, false, false);
159: assertPersistent(b.getSelfOneOne(), false, false, false, false);
160: endEm(pm);
161: }
162:
163: /**
164: * Tests basic jdo flag transitions when finding a non-transactional
165: * instance by id.
166: */
167: public void testFlagTransitions5() throws Exception {
168: if (!supportsPessimistic)
169: return;
170:
171: OpenJPAEntityManager pm = getPM(false, false);
172: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
173: ModRuntimeTest2.class, _id);
174:
175: assertPersistent(b, false, false, false, false);
176: assertPersistent(b.getSelfOneOne(), false, false, false, false);
177:
178: endEm(pm);
179: }
180:
181: /**
182: * Tests basic jdo flag transitions from persistent-transactional
183: * to transient after delete and commit.
184: */
185: public void testFlagTransitions6() throws Exception {
186: if (!supportsPessimistic)
187: return;
188:
189: OpenJPAEntityManager pm = getPM(false, false);
190: startTx(pm);
191:
192: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
193: ModRuntimeTest2.class, _id);
194: ModRuntimeTest1 parent = b.getSelfOneOne();
195: pm.remove(b);
196: assertPersistent(b, true, false, true, true);
197: assertPersistent(parent, true, false, false, false);
198:
199: endTx(pm);
200:
201: assertTransient(b);
202: assertPersistent(parent, false, false, false, false);
203: endEm(pm);
204: }
205:
206: /**
207: * Tests basic jdo flag transitions from persistent-transactional
208: * to persistent-nontransactional after delete and rollback.
209: */
210: public void testFlagTransitions7() throws Exception {
211: if (!supportsPessimistic)
212: return;
213:
214: OpenJPAEntityManager pm = getPM(false, false);
215: startTx(pm);
216:
217: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
218: ModRuntimeTest2.class, _id);
219: ModRuntimeTest1 parent = b.getSelfOneOne();
220: pm.remove(b);
221: assertPersistent(b, true, false, true, true);
222: assertPersistent(parent, true, false, false, false);
223:
224: rollbackTx(pm);
225:
226: assertPersistent(b, false, false, false, false);
227: assertPersistent(parent, false, false, false, false);
228: endEm(pm);
229: }
230:
231: /**
232: * Tests basic state transitions from transient to
233: * persistent-transactional and back to transient after rollback.
234: */
235: public void testStateTransitions1() throws Exception {
236: if (!supportsPessimistic)
237: return;
238:
239: ModRuntimeTest2 b = createTest2();
240:
241: OpenJPAEntityManager pm = getPM(false, false);
242: startTx(pm);
243:
244: pm.persist(b);
245: pm.persist(b.getSelfOneOne());
246: assertTest2Orig(b);
247: changeTest2(b);
248:
249: rollbackTx(pm);
250:
251: assertTest2Orig(b);
252: endEm(pm);
253: }
254:
255: /**
256: * Tests basic state transitions from transient to
257: * persistent-transactional to persistent-nontransactional after commit.
258: */
259: public void testStateTransitions2() throws Exception {
260: if (!supportsPessimistic)
261: return;
262:
263: ModRuntimeTest2 b = createTest2();
264:
265: OpenJPAEntityManager pm = getPM(false, false);
266: startTx(pm);
267:
268: pm.persist(b);
269: assertTest2Orig(b);
270: changeTest2(b);
271:
272: endTx(pm);
273:
274: assertTest2Changed(b, false);
275: endEm(pm);
276: }
277:
278: /**
279: * Tests basic state transitions when finding a transactional
280: * instance by id, then committing.
281: */
282: public void testStateTransitions3() throws Exception {
283: if (!supportsPessimistic)
284: return;
285:
286: OpenJPAEntityManager pm = getPM(false, false);
287: startTx(pm);
288:
289: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
290: ModRuntimeTest2.class, _id);
291: changeTest2(b);
292:
293: endTx(pm);
294:
295: assertTest2Changed(b, false);
296: endEm(pm);
297: }
298:
299: /**
300: * Tests basic state transitions when finding a transactional
301: * instance by id, then committing.
302: */
303: public void testStateTransitions3a() throws Exception {
304: if (!supportsPessimistic)
305: return;
306:
307: OpenJPAEntityManager pm = getPM(false, false);
308: startTx(pm);
309:
310: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
311: ModRuntimeTest2.class, _id);
312: changeTest2(b);
313:
314: endTx(pm);
315:
316: assertTest2Changed(b, false);
317: endEm(pm);
318: }
319:
320: /**
321: * Tests basic state transitions when finding a transactional
322: * instance by id, then rolling back.
323: */
324: public void testStateTransitions4() throws Exception {
325: if (!supportsPessimistic)
326: return;
327:
328: OpenJPAEntityManager pm = getPM(false, false);
329: startTx(pm);
330:
331: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
332: ModRuntimeTest2.class, _id);
333: assertTest2Orig(b);
334: changeTest2(b);
335:
336: rollbackTx(pm);
337:
338: assertTest2Orig(b);
339: endEm(pm);
340: }
341:
342: /**
343: * Tests basic state transitions when finding a transactional
344: * instance by id, then rolling back.
345: */
346: public void testStateTransitions4a() throws Exception {
347: if (!supportsPessimistic)
348: return;
349:
350: OpenJPAEntityManager pm = getPM(false, false);
351: startTx(pm);
352:
353: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
354: ModRuntimeTest2.class, _id);
355: assertTest2Orig(b);
356: changeTest2(b);
357:
358: rollbackTx(pm);
359:
360: assertTest2Orig(b);
361: endEm(pm);
362: }
363:
364: /**
365: * Tests basic state transitions from persistent-transactional
366: * to transient after delete and commit.
367: */
368: public void testStateTransitions5() throws Exception {
369: if (!supportsPessimistic)
370: return;
371:
372: OpenJPAEntityManager pm = getPM(false, false);
373: startTx(pm);
374:
375: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
376: ModRuntimeTest2.class, _id);
377: ModRuntimeTest1 parent = b.getSelfOneOne();
378: assertNotNull("parent is null", parent);
379: pm.remove(b);
380:
381: endTx(pm);
382:
383: // parent should be valid
384: assertEquals("PARENT", parent.getStringField());
385:
386: // 'b' should be cleared
387: assertNull(b.getStringField());
388: assertEquals(0, b.getIntField());
389: assertNull(b.getSelfOneOne());
390: }
391:
392: /**
393: * Tests basic state transitions from persistent-transactional
394: * to persistent-nontransactional after delete and rollback.
395: */
396: public void testStateTransitions6() throws Exception {
397: if (!supportsPessimistic)
398: return;
399:
400: OpenJPAEntityManager pm = getPM(false, false);
401: startTx(pm);
402:
403: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
404: ModRuntimeTest2.class, _id);
405: assertTest2Orig(b);
406: pm.remove(b);
407:
408: rollbackTx(pm);
409:
410: assertTest2Orig(b);
411: endEm(pm);
412: }
413:
414: /**
415: * Tests basic state transitions from transient to
416: * persistent-transactional and back to transient after rollback.
417: */
418: public void testOptStateTransitions1() throws Exception {
419: ModRuntimeTest2 b = createTest2();
420:
421: OpenJPAEntityManager pm = getPM(true, false);
422: startTx(pm);
423:
424: pm.persist(b);
425: pm.persist(b.getSelfOneOne());
426: assertTest2Orig(b);
427: changeTest2(b);
428:
429: rollbackTx(pm);
430:
431: assertTest2Orig(b);
432: endEm(pm);
433: }
434:
435: /**
436: * Tests basic state transitions from transient to
437: * persistent-transactional to persistent-nontransactional after commit.
438: */
439: public void testOptStateTransitions2() throws Exception {
440: ModRuntimeTest2 b = createTest2();
441:
442: OpenJPAEntityManager pm = getPM(true, false);
443: startTx(pm);
444:
445: pm.persist(b);
446: assertTest2Orig(b);
447: changeTest2(b);
448:
449: endTx(pm);
450:
451: assertTest2Changed(b, false);
452: endEm(pm);
453: }
454:
455: /**
456: * Tests basic state transitions when finding a transactional
457: * instance by id, then committing.
458: */
459: public void testOptStateTransitions3() throws Exception {
460: OpenJPAEntityManager pm = getPM(true, false);
461: startTx(pm);
462:
463: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
464: ModRuntimeTest2.class, _id);
465: assertTest2Orig(b);
466: changeTest2(b);
467:
468: endTx(pm);
469:
470: assertTest2Changed(b, false);
471: endEm(pm);
472: }
473:
474: /**
475: * Tests basic state transitions when finding a transactional
476: * instance by id, then rolling back.
477: */
478: public void testOptStateTransitions4() throws Exception {
479: OpenJPAEntityManager pm = getPM(true, false);
480: startTx(pm);
481:
482: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
483: ModRuntimeTest2.class, _id);
484: assertTest2Orig(b);
485: changeTest2(b);
486:
487: rollbackTx(pm);
488:
489: assertTest2Orig(b);
490: endEm(pm);
491: }
492:
493: /**
494: * Tests basic state transitions from persistent-transactional
495: * to transient after delete and commit.
496: */
497: public void testOptStateTransitions5() throws Exception {
498: OpenJPAEntityManager pm = getPM(true, false);
499: startTx(pm);
500:
501: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
502: ModRuntimeTest2.class, _id);
503: ModRuntimeTest1 parent = b.getSelfOneOne();
504: assertNotNull("parent is null", parent);
505: pm.remove(b);
506:
507: endTx(pm);
508:
509: // parent should be valid
510: assertEquals("PARENT", parent.getStringField());
511:
512: // 'b' should be cleared
513: assertNull(b.getStringField());
514: assertEquals(0, b.getIntField());
515: assertNull(b.getSelfOneOne());
516: }
517:
518: /**
519: * Tests basic state transitions from persistent-transactional
520: * to persistent-nontransactional after delete and rollback.
521: */
522: public void testOptStateTransitions6() throws Exception {
523: OpenJPAEntityManager pm = getPM(true, false);
524: startTx(pm);
525:
526: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
527: ModRuntimeTest2.class, _id);
528: pm.remove(b);
529:
530: rollbackTx(pm);
531:
532: assertTest2Orig(b);
533: endEm(pm);
534: }
535:
536: /**
537: * Tests basic state transitions from transient to
538: * persistent-transactional and back to transient after rollback.
539: */
540: public void testOptRetainStateTransitions1() throws Exception {
541: ModRuntimeTest2 b = createTest2();
542:
543: OpenJPAEntityManager pm = getPM(true, true);
544: startTx(pm);
545:
546: pm.persist(b);
547: pm.persist(b.getSelfOneOne());
548: assertTest2Orig(b);
549: changeTest2(b);
550:
551: rollbackTx(pm);
552:
553: assertTest2Orig(b);
554: endEm(pm);
555: }
556:
557: /**
558: * Tests basic state transitions from transient to
559: * persistent-transactional to persistent-nontransactional after commit.
560: */
561: public void testOptRetainStateTransitions2() throws Exception {
562: ModRuntimeTest2 b = createTest2();
563:
564: OpenJPAEntityManager pm = getPM(true, true);
565: startTx(pm);
566:
567: pm.persist(b);
568: assertTest2Orig(b);
569: changeTest2(b);
570:
571: endTx(pm);
572:
573: assertTest2Changed(b, true);
574: endEm(pm);
575: }
576:
577: /**
578: * Tests basic state transitions when finding a transactional
579: * instance by id, then committing.
580: */
581: public void testOptRetainStateTransitions3() throws Exception {
582: OpenJPAEntityManager pm = getPM(true, true);
583: startTx(pm);
584:
585: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
586: ModRuntimeTest2.class, _id);
587: assertTest2Orig(b);
588: changeTest2(b);
589:
590: endTx(pm);
591:
592: assertTest2Changed(b, true);
593: endEm(pm);
594: }
595:
596: /**
597: * Tests basic state transitions when finding a transactional
598: * instance by id, then rolling back.
599: */
600: public void testOptRetainStateTransitions4() throws Exception {
601: OpenJPAEntityManager pm = getPM(true, true);
602: startTx(pm);
603:
604: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
605: ModRuntimeTest2.class, _id);
606: assertTest2Orig(b);
607: changeTest2(b);
608:
609: rollbackTx(pm);
610:
611: assertTest2Orig(b);
612: endEm(pm);
613: }
614:
615: /**
616: * Tests basic state transitions from persistent-transactional
617: * to transient after delete and commit.
618: */
619: public void testOptRetainStateTransitions5() throws Exception {
620: OpenJPAEntityManager pm = getPM(true, true);
621: startTx(pm);
622:
623: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
624: ModRuntimeTest2.class, _id);
625: ModRuntimeTest1 parent = b.getSelfOneOne();
626:
627: assertNotNull("parent is null", parent);
628: pm.remove(b);
629:
630: endTx(pm);
631:
632: // parent should be valid
633: assertEquals("PARENT", parent.getStringField());
634:
635: // 'b' should be cleared
636: assertNull(b.getStringField());
637: assertEquals(0, b.getIntField());
638: assertNull(b.getSelfOneOne());
639: }
640:
641: /**
642: * Tests basic state transitions from persistent-transactional
643: * to persistent-nontransactional after delete and rollback.
644: */
645: public void testOptRetainStateTransitions6() throws Exception {
646: OpenJPAEntityManager pm = getPM(true, true);
647: startTx(pm);
648:
649: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
650: ModRuntimeTest2.class, _id);
651: pm.remove(b);
652:
653: rollbackTx(pm);
654:
655: if (pm.getRestoreState() != RestoreStateType.NONE)
656: assertTest2Orig(b);
657: else
658: assertNull(b.getStringField());
659: endEm(pm);
660: }
661:
662: /**
663: * Tests basic state transitions from transient to
664: * persistent-transactional and back to transient after rollback.
665: */
666: public void testRetainStateTransitions1() throws Exception {
667: if (!supportsPessimistic)
668: return;
669:
670: ModRuntimeTest2 b = createTest2();
671:
672: OpenJPAEntityManager pm = getPM(false, true);
673: startTx(pm);
674:
675: pm.persist(b);
676: pm.persist(b.getSelfOneOne());
677: assertTest2Orig(b);
678: changeTest2(b);
679:
680: rollbackTx(pm);
681:
682: assertTest2Orig(b);
683: endEm(pm);
684: }
685:
686: /**
687: * Tests basic state transitions from transient to
688: * persistent-transactional to persistent-nontransactional after commit.
689: */
690: public void testRetainStateTransitions2() throws Exception {
691: if (!supportsPessimistic)
692: return;
693:
694: ModRuntimeTest2 b = createTest2();
695:
696: OpenJPAEntityManager pm = getPM(false, true);
697: startTx(pm);
698:
699: pm.persist(b);
700: assertTest2Orig(b);
701: changeTest2(b);
702:
703: endTx(pm);
704:
705: assertTest2Changed(b, true);
706: endEm(pm);
707: }
708:
709: /**
710: * Tests basic state transitions when finding a transactional
711: * instance by id, then committing.
712: */
713: public void testRetainStateTransitions3() throws Exception {
714: if (!supportsPessimistic)
715: return;
716:
717: OpenJPAEntityManager pm = getPM(false, true);
718: startTx(pm);
719:
720: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
721: ModRuntimeTest2.class, _id);
722: assertTest2Orig(b);
723: changeTest2(b);
724:
725: endTx(pm);
726:
727: assertTest2Changed(b, true);
728: endEm(pm);
729: }
730:
731: /**
732: * Tests basic state transitions when finding a transactional
733: * instance by id, then rolling back.
734: */
735: public void testRetainStateTransitions4() throws Exception {
736: if (!supportsPessimistic)
737: return;
738:
739: OpenJPAEntityManager pm = getPM(false, true);
740: startTx(pm);
741:
742: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
743: ModRuntimeTest2.class, _id);
744: assertTest2Orig(b);
745: changeTest2(b);
746:
747: rollbackTx(pm);
748:
749: assertTest2Orig(b);
750: endEm(pm);
751: }
752:
753: /**
754: * Tests basic state transitions from persistent-transactional
755: * to transient after delete and commit.
756: */
757: public void testRetainStateTransitions5() throws Exception {
758: if (!supportsPessimistic)
759: return;
760:
761: OpenJPAEntityManager pm = getPM(false, true);
762: startTx(pm);
763:
764: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
765: ModRuntimeTest2.class, _id);
766: ModRuntimeTest1 parent = b.getSelfOneOne();
767: assertNotNull("parent is null", parent);
768: pm.remove(b);
769:
770: endTx(pm);
771:
772: // parent should be valid
773: assertEquals("PARENT", parent.getStringField());
774:
775: // 'b' should be cleared
776: assertNull(b.getStringField());
777: assertEquals(0, b.getIntField());
778: assertNull(b.getSelfOneOne());
779: }
780:
781: /**
782: * Tests basic state transitions from persistent-transactional
783: * to persistent-nontransactional after delete and rollback.
784: */
785: public void testRetainStateTransitions6() throws Exception {
786: if (!supportsPessimistic)
787: return;
788:
789: OpenJPAEntityManager pm = getPM(false, true);
790: startTx(pm);
791:
792: ModRuntimeTest2 b = (ModRuntimeTest2) pm.find(
793: ModRuntimeTest2.class, _id);
794: pm.remove(b);
795:
796: rollbackTx(pm);
797:
798: assertTest2Orig(b);
799: endEm(pm);
800: }
801:
802: /**
803: * Tests basic state transitions from transient to
804: * transient-dirty and back to transient after rollback.
805: */
806: public void testTransientStateTransitions1() throws Exception {
807: if (!supportsPessimistic)
808: return;
809:
810: ModRuntimeTest2 b = createTest2();
811:
812: OpenJPAEntityManager pm = getPM(false, false);
813: startTx(pm);
814:
815: pm.transactional(b, true);
816: pm.transactional(b.getSelfOneOne(), true);
817: changeTest2(b);
818:
819: rollbackTx(pm);
820:
821: assertTest2Orig(b);
822: endEm(pm);
823: }
824:
825: /**
826: * Tests basic state transitions from transient to
827: * transient-transactional and stick on commit.
828: */
829: public void testTransientStateTransitions2() throws Exception {
830: if (!supportsPessimistic)
831: return;
832:
833: ModRuntimeTest2 b = createTest2();
834:
835: OpenJPAEntityManager pm = getPM(false, false);
836: startTx(pm);
837:
838: pm.transactional(b, true);
839: pm.transactional(b.getSelfOneOne(), true);
840: changeTest2(b);
841:
842: endTx(pm);
843:
844: assertTest2Changed(b, false);
845: endEm(pm);
846: }
847:
848: /**
849: * Tests state transitions from PClean to transient.
850: */
851: public void testTransientStateTransitions3() throws Exception {
852: if (!supportsPessimistic)
853: return;
854:
855: ModRuntimeTest2 b = createTest2();
856:
857: OpenJPAEntityManager pm = getPM(false, false);
858: startTx(pm);
859: pm.persist(b);
860: int oid = b.getId();
861: endTx(pm);
862: endEm(pm);
863:
864: pm = getPM(false, false);
865: //FIXME jthomas
866: b = (ModRuntimeTest2) pm.find(ModRuntimeTest2.class, oid);
867: pm.retrieve(b);
868: //FIXME jthomas
869: endEm(pm);
870:
871: // note that at this point, parent is not transient, just retrieved.
872: assertNotNull("b is null", b.getSelfOneOne());
873: }
874:
875: private ModRuntimeTest2 createTest2() {
876: return createTest2("NAME", 50);
877: }
878:
879: private ModRuntimeTest2 createTest2(String str, int i) {
880: return createTest2(new ModRuntimeTest2(str, i));
881: }
882:
883: private ModRuntimeTest2 createTest2(ModRuntimeTest2 b) {
884: ModRuntimeTest1 parent = new ModRuntimeTest1("PARENT", 70);
885: b.setSelfOneOne(parent);
886: return b;
887: }
888:
889: private void changeTest2(ModRuntimeTest2 b) {
890: PersistenceAware.setModTransString(b, "999");
891: b.setStringField("CHANGED");
892: b.setIntField(1000);
893: b.getSelfOneOne().setStringField("PCHANGED");
894: b.setSelfOneOne(null);
895: }
896:
897: private void assertTest2Changed(ModRuntimeTest2 b,
898: boolean retainValues) {
899: if (retainValues)
900: assertEquals("999", PersistenceAware.getModTransString(b));
901: assertEquals("CHANGED", b.getStringField());
902: assertEquals(1000, b.getIntField());
903: assertNull(b.getSelfOneOne());
904: }
905:
906: private void assertTest2Orig(ModRuntimeTest2 b) {
907: assertEquals("NAME", b.getStringField());
908: assertEquals(50, b.getIntField());
909: assertNotNull(b.getSelfOneOne());
910: assertEquals("PARENT", b.getSelfOneOne().getStringField());
911:
912: assertEquals(
913: "transactional field 'transString' was not the "
914: + "same as it was originally. Ensure that "
915: + "openjpa.kernel.PersistenceAware is enhanced. "
916: + "It is persistence-aware, so will not be enhanced by commands "
917: + "like jdoc $(find test -name '*.jdo').",
918: null, PersistenceAware.getModTransString(b));
919: }
920:
921: /**
922: * Assert that the given object is persistent.
923: */
924: public void assertTransient(Object a) {
925: OpenJPAEntityManager pm = currentEntityManager();
926: assertTrue(!pm.isPersistent(a));
927: assertTrue(!pm.isTransactional(a));
928: assertTrue(!pm.isNewlyPersistent(a));
929: assertTrue(!pm.isDirty(a));
930: assertTrue(!pm.isRemoved(a));
931: assertNull(pm.getObjectId(a));
932: assertNull(OpenJPAPersistence.getEntityManager(a));
933: endEm(pm);
934: }
935:
936: /**
937: * Assert that the given object is persistent and is in the given state.
938: */
939: public void assertPersistent(Object a, boolean isTrans,
940: boolean isNew, boolean isDeleted, boolean isDirty) {
941: OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
942: assertTrue(pm.isPersistent(a));
943:
944: PersistenceCapable xman = (PersistenceCapable) a;
945:
946: assertEquals(isTrans, pm.isTransactional(a));
947: assertEquals(isNew, pm.isNewlyPersistent(a));
948: assertEquals(isDeleted, pm.isRemoved(a));
949: assertEquals(isDirty || isNew || isDeleted, pm.isDirty(a));
950: assertNotNull(pm.getObjectId(a));
951: assertNotNull(OpenJPAPersistence.getEntityManager(a));
952:
953: endEm(pm);
954: }
955:
956: }
|