001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
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 of the License, or (at your option) 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 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: */package org.objectweb.speedo.runtime.detach;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: import javax.jdo.JDOException;
031: import javax.jdo.PersistenceManager;
032: import javax.jdo.Query;
033:
034: import junit.framework.Assert;
035:
036: import org.objectweb.speedo.SpeedoTestHelper;
037: import org.objectweb.speedo.api.ExceptionHelper;
038: import org.objectweb.speedo.pobjects.detach.Coach;
039: import org.objectweb.speedo.pobjects.detach.Player;
040: import org.objectweb.speedo.pobjects.detach.Team;
041: import org.objectweb.util.monolog.api.BasicLevel;
042:
043: /**
044: * @author Y.Bersihand
045: */
046: public class TestVersion extends SpeedoTestHelper {
047:
048: public TestVersion(String s) {
049: super (s);
050: }
051:
052: protected String getLoggerName() {
053: return LOG_NAME + ".rt.detach.TestVersion";
054: }
055:
056: /**
057: * Test the attach method with versioning:
058: * make an object persistent
059: * tx.begin
060: * detach it
061: * tx.commit
062: * attach => OK
063: */
064: public void testVersion1() {
065: logger.log(BasicLevel.DEBUG,
066: "***************testVersion1*****************");
067: Team t = new Team("T1", null, null);
068: Collection players = new ArrayList();
069: Player p1 = new Player("p1", t, 20);
070: players.add(p1);
071: Player p2 = new Player("p2", t, 30);
072: players.add(p2);
073: t.setPlayers(players);
074: Coach c = new Coach("c1", 10, t);
075: t.setCoach(c);
076: PersistenceManager pm = pmf.getPersistenceManager();
077: pm.currentTransaction().begin();
078: pm.makePersistent(t);
079: pm.currentTransaction().commit();
080: pm.currentTransaction().begin();
081: //detach the team t
082: Team copyOfT = (Team) pm.detachCopy(t);
083: //commit the tx
084: pm.currentTransaction().commit();
085: pm.currentTransaction().begin();
086: logger.log(BasicLevel.DEBUG, "ATTACH BEGIN");
087: try {
088: //attach the "invalid" team
089: Team attachedTeam = (Team) pm.makePersistent(copyOfT);
090: logger.log(BasicLevel.DEBUG, "ATTACH END");
091: pm.currentTransaction().commit();
092: logger.log(BasicLevel.DEBUG,
093: "The attached version of the team is as follows:\n "
094: + attachedTeam.toString());
095: } catch (Exception e) {
096: fail("The attached is supposed to be ok");
097: } finally {
098: if (pm.currentTransaction().isActive()) {
099: pm.currentTransaction().rollback();
100: }
101: pm.close();
102: }
103: }
104:
105: /**
106: * Test the attach method with versioning:
107: * make an object persistent
108: * tx.begin
109: * write
110: * detach
111: * tx.commit
112: * attach => OK
113: */
114: public void testVersion2() {
115: logger.log(BasicLevel.DEBUG,
116: "***************testVersion2*****************");
117: Team t = new Team("T2", null, null);
118: Collection players = new ArrayList();
119: Player p1 = new Player("p3", t, 20);
120: players.add(p1);
121: Player p2 = new Player("p4", t, 30);
122: players.add(p2);
123: t.setPlayers(players);
124: Coach c = new Coach("c2", 10, t);
125: t.setCoach(c);
126: PersistenceManager pm = pmf.getPersistenceManager();
127: pm.currentTransaction().begin();
128: pm.makePersistent(t);
129: pm.currentTransaction().commit();
130: //begin
131: pm.currentTransaction().begin();
132: //modify the coach
133: t.getCoach().setExperience(99);
134: Iterator it = t.getPlayers().iterator();
135: //modify the players
136: while (it.hasNext()) {
137: Player p = (Player) it.next();
138: p.setAge(99);
139: }
140: //detach the team t
141: Team copyOfT = (Team) pm.detachCopy(t);
142: //commit
143: pm.currentTransaction().commit();
144: pm.currentTransaction().begin();
145: try {
146: //attach the team
147: Team attachedTeam = (Team) pm.makePersistent(copyOfT);
148: pm.currentTransaction().commit();
149: } catch (Exception e) {
150: fail("The attached is supposed to be ok");
151: } finally {
152: if (pm.currentTransaction().isActive()) {
153: pm.currentTransaction().rollback();
154: }
155: pm.close();
156: }
157: }
158:
159: /**
160: * Test the attach method with versioning:
161: * make an object persistent
162: * tx.begin
163: * detach
164: * write
165: * tx.commit
166: * attach => FAIL
167: */
168: public void testVersion3() {
169: logger.log(BasicLevel.DEBUG,
170: "***************testVersion3*****************");
171: Team t = new Team("T3", null, null);
172: Collection players = new ArrayList();
173: Player p1 = new Player("p5", t, 20);
174: players.add(p1);
175: Player p2 = new Player("p6", t, 30);
176: players.add(p2);
177: t.setPlayers(players);
178: Coach c = new Coach("c3", 10, t);
179: t.setCoach(c);
180: PersistenceManager pm = pmf.getPersistenceManager();
181: pm.currentTransaction().begin();
182: pm.makePersistent(t);
183: pm.currentTransaction().commit();
184: //begin
185: pm.currentTransaction().begin();
186: //detach the team t
187: Team copyOfT = null;
188: try {
189: copyOfT = (Team) pm.detachCopy(t);
190: // modify the coach
191: t.setCoach(new Coach("c31", 10, t));
192: Iterator it = t.getPlayers().iterator();
193: //modify the players
194: while (it.hasNext()) {
195: Player p = (Player) it.next();
196: p.setAge(99);
197: }
198: //commit
199: pm.currentTransaction().commit();
200: } catch (Exception e) {
201: fail("Detach error: " + e.getMessage());
202: pm.currentTransaction().rollback();
203: pm.close();
204: return;
205: }
206: pm.currentTransaction().begin();
207: try {
208: //attach the team
209: Team attachedTeam = (Team) pm.makePersistent(copyOfT);
210: pm.currentTransaction().commit();
211: fail("The attached is supposed to fail");
212: } catch (Exception e) {
213: assertEquals("Wrong exception thrown: " + e.getClass(),
214: JDOException.class, e.getClass());
215: } finally {
216: if (pm.currentTransaction().isActive()) {
217: pm.currentTransaction().rollback();
218: }
219: pm.close();
220: }
221: }
222:
223: /**
224: * Test the attach method with versioning:
225: * make an object persistent
226: * tx.begin
227: * detach it
228: * tx.rollback
229: * attach => OK
230: */
231: public void testVersion4() {
232: logger.log(BasicLevel.DEBUG,
233: "***************testVersion4*****************");
234: Team t = new Team("T4", null, null);
235: Collection players = new ArrayList();
236: Player p1 = new Player("p7", t, 20);
237: players.add(p1);
238: Player p2 = new Player("p8", t, 30);
239: players.add(p2);
240: t.setPlayers(players);
241: Coach c = new Coach("c4", 10, t);
242: t.setCoach(c);
243: PersistenceManager pm = pmf.getPersistenceManager();
244: pm.currentTransaction().begin();
245: pm.makePersistent(t);
246: pm.currentTransaction().commit();
247: pm.currentTransaction().begin();
248: //detach the team t
249: Team copyOfT = (Team) pm.detachCopy(t);
250: //rollback the tx
251: pm.currentTransaction().rollback();
252: pm.currentTransaction().begin();
253: try {
254: //attach the "invalid" team
255: Team attachedTeam = (Team) pm.makePersistent(copyOfT);
256: pm.currentTransaction().commit();
257: } catch (Exception e) {
258: fail("The attached is supposed to be ok");
259: } finally {
260: if (pm.currentTransaction().isActive()) {
261: pm.currentTransaction().rollback();
262: }
263: pm.close();
264: }
265: }
266:
267: /**
268: * Test the attach method with versioning:
269: * make an object persistent
270: * tx.begin
271: * write
272: * detach
273: * tx.rollback
274: * attach => FAIL
275: */
276: public void testVersion5() {
277: logger.log(BasicLevel.DEBUG,
278: "***************testVersion5*****************");
279: Team t = new Team("T5", null, null);
280: Collection players = new ArrayList();
281: Player p1 = new Player("p9", t, 20);
282: players.add(p1);
283: Player p2 = new Player("p10", t, 30);
284: players.add(p2);
285: t.setPlayers(players);
286: Coach c = new Coach("c5", 10, t);
287: t.setCoach(c);
288: PersistenceManager pm = pmf.getPersistenceManager();
289: pm.currentTransaction().begin();
290: pm.makePersistent(t);
291: pm.currentTransaction().commit();
292: //begin
293: pm.currentTransaction().begin();
294: //modify the coach
295: t.setCoach(new Coach("c51", 10, t));
296: Iterator it = t.getPlayers().iterator();
297: //modify the players
298: while (it.hasNext()) {
299: Player p = (Player) it.next();
300: p.setAge(99);
301: }
302: //detach the team t
303: Team copyOfT = (Team) pm.detachCopy(t);
304: //rollback
305: pm.currentTransaction().rollback();
306: pm.currentTransaction().begin();
307: try {
308: //attach the team
309: Team attachedTeam = (Team) pm.makePersistent(copyOfT);
310: pm.currentTransaction().commit();
311: fail("The attached is supposed to fail.");
312: } catch (Exception e) {
313: assertEquals("Wrong exception thrown: " + e.getClass(),
314: JDOException.class, e.getClass());
315: } finally {
316: if (pm.currentTransaction().isActive()) {
317: pm.currentTransaction().rollback();
318: }
319: pm.close();
320: }
321: }
322:
323: /**
324: * Test the attach method with versioning:
325: * make an object persistent
326: * tx.begin
327: * detach
328: * write
329: * tx.rollback
330: * attach => OK
331: */
332: public void testVersion6() {
333: logger.log(BasicLevel.DEBUG,
334: "***************testVersion6*****************");
335: Team t = new Team("T6", null, null);
336: Collection players = new ArrayList();
337: Player p1 = new Player("p11", t, 20);
338: players.add(p1);
339: Player p2 = new Player("p12", t, 30);
340: players.add(p2);
341: t.setPlayers(players);
342: Coach c = new Coach("c6", 10, t);
343: t.setCoach(c);
344: PersistenceManager pm = pmf.getPersistenceManager();
345: pm.currentTransaction().begin();
346: pm.makePersistent(t);
347: pm.currentTransaction().commit();
348: //begin
349: pm.currentTransaction().begin();
350: //detach the team t
351: Team copyOfT = (Team) pm.detachCopy(t);
352: //modify the coach
353: t.getCoach().setExperience(99);
354: Iterator it = t.getPlayers().iterator();
355: //modify the players
356: while (it.hasNext()) {
357: Player p = (Player) it.next();
358: p.setAge(99);
359: }
360: //rollback
361: pm.currentTransaction().rollback();
362: pm.currentTransaction().begin();
363: try {
364: //attach the team
365: Team attachedTeam = (Team) pm.makePersistent(copyOfT);
366: pm.currentTransaction().commit();
367: } catch (Exception e) {
368: fail("The attached is supposed to be ok.");
369: } finally {
370: if (pm.currentTransaction().isActive()) {
371: pm.currentTransaction().rollback();
372: }
373: pm.close();
374: }
375: }
376:
377: /**
378: * Test the attach method with versioning:
379: * make an object persistent
380: * tx.begin
381: * detach(d1)
382: * detach(d2)
383: * tx.commit
384: * write d1
385: * write d2
386: * tx.begin
387: * attach(d1) => OK
388: * tx.commit
389: * tx.begin
390: * attach(d2) => FAIL
391: * tx.commit
392: */
393: public void testVersion7() {
394: logger.log(BasicLevel.DEBUG,
395: "***************testVersion7*****************");
396: Team t = new Team("T7", null, null);
397: Collection players = new ArrayList();
398: Player p1 = new Player("p13", t, 20);
399: players.add(p1);
400: Player p2 = new Player("p14", t, 30);
401: players.add(p2);
402: t.setPlayers(players);
403: Coach c = new Coach("c7", 10, t);
404: t.setCoach(c);
405: PersistenceManager pm = pmf.getPersistenceManager();
406: pm.currentTransaction().begin();
407: pm.makePersistent(t);
408: pm.currentTransaction().commit();
409: //begin
410: pm.currentTransaction().begin();
411: //detach the team t
412: Team copyOfT1 = (Team) pm.detachCopy(t);
413: //detach the team t
414: Team copyOfT2 = (Team) pm.detachCopy(t);
415: //commit
416: pm.currentTransaction().commit();
417:
418: //modify the copy1
419: copyOfT1.getCoach().setExperience(99);
420: Iterator it = copyOfT1.getPlayers().iterator();
421: //modify the players
422: while (it.hasNext()) {
423: Player p = (Player) it.next();
424: p.setAge(99);
425: }
426: //modify the copy2
427: copyOfT2.getCoach().setExperience(99);
428: it = copyOfT2.getPlayers().iterator();
429: //modify the players
430: while (it.hasNext()) {
431: Player p = (Player) it.next();
432: p.setAge(99);
433: }
434:
435: boolean attach1OK = false;
436: try {
437: pm.currentTransaction().begin();
438: //attach the copy1
439: Team attachedTeam1 = (Team) pm.makePersistent(copyOfT1);
440: pm.currentTransaction().commit();
441: attach1OK = true;
442: pm.currentTransaction().begin();
443: //attach the copy2
444: Team attachedTeam2 = (Team) pm.makePersistent(copyOfT2);
445: pm.currentTransaction().commit();
446: fail("The second attach is supposed to fail");
447: } catch (Exception e) {
448: assertTrue("The first attach is supposed to be ok.",
449: attach1OK);
450: assertEquals("Wrong exception thrown: " + e.getClass(),
451: JDOException.class, e.getClass());
452: } finally {
453: if (pm.currentTransaction().isActive()) {
454: pm.currentTransaction().rollback();
455: }
456: pm.close();
457: }
458: }
459:
460: /**
461: * Test the attach method with versioning:
462: * make an object persistent
463: * tx.begin
464: * detach(d1)
465: * detach(d2)
466: * tx.commit
467: * write d1
468: * write d2
469: * tx.begin
470: * attach(d2) => OK
471: * tx.commit
472: * tx.begin
473: * attach(d1) => FAIL
474: * tx.commit
475: */
476: public void testVersion8() {
477: logger.log(BasicLevel.DEBUG,
478: "***************testVersion8*****************");
479: Team t = new Team("T8", null, null);
480: Collection players = new ArrayList();
481: Player p1 = new Player("p15", t, 20);
482: players.add(p1);
483: Player p2 = new Player("p16", t, 30);
484: players.add(p2);
485: t.setPlayers(players);
486: Coach c = new Coach("c8", 10, t);
487: t.setCoach(c);
488: PersistenceManager pm = pmf.getPersistenceManager();
489: pm.currentTransaction().begin();
490: pm.makePersistent(t);
491: pm.currentTransaction().commit();
492: //begin
493: pm.currentTransaction().begin();
494: //detach the team t
495: Team copyOfT1 = (Team) pm.detachCopy(t);
496: //detach the team t
497: Team copyOfT2 = (Team) pm.detachCopy(t);
498: //commit
499: pm.currentTransaction().commit();
500:
501: //modify the copy1
502: copyOfT1.getCoach().setExperience(99);
503: Iterator it = copyOfT1.getPlayers().iterator();
504: //modify the players
505: while (it.hasNext()) {
506: Player p = (Player) it.next();
507: p.setAge(99);
508: }
509: //modify the copy2
510: copyOfT2.getCoach().setExperience(99);
511: it = copyOfT2.getPlayers().iterator();
512: //modify the players
513: while (it.hasNext()) {
514: Player p = (Player) it.next();
515: p.setAge(99);
516: }
517:
518: boolean attach2OK = false;
519: try {
520: pm.currentTransaction().begin();
521: //attach the copy2
522: Team attachedTeam2 = (Team) pm.makePersistent(copyOfT2);
523: pm.currentTransaction().commit();
524: attach2OK = true;
525: pm.currentTransaction().begin();
526: //attach the copy1
527: Team attachedTeam1 = (Team) pm.makePersistent(copyOfT1);
528: pm.currentTransaction().commit();
529: fail("The attach of copy1 is supposed to fail");
530: } catch (Exception e) {
531: assertTrue("The attach of copy2 is supposed to be ok.",
532: attach2OK);
533: assertEquals("Wrong exception thrown: " + e.getClass(),
534: JDOException.class, e.getClass());
535: } finally {
536: if (pm.currentTransaction().isActive()) {
537: pm.currentTransaction().rollback();
538: }
539: pm.close();
540: }
541: }
542:
543: /**
544: * Test the attach method with versioning:
545: * make an object persistent
546: * detach
547: * tx.begin
548: * write
549: * attach => FAIL
550: * tx.commit
551: */
552: public void testVersion9() {
553: logger.log(BasicLevel.DEBUG,
554: "***************testVersion9*****************");
555: Team t = new Team("T9", null, null);
556: Collection players = new ArrayList();
557: Player p1 = new Player("p17", t, 20);
558: players.add(p1);
559: Player p2 = new Player("p18", t, 30);
560: players.add(p2);
561: t.setPlayers(players);
562: Coach c = new Coach("c9", 10, t);
563: t.setCoach(c);
564: PersistenceManager pm = pmf.getPersistenceManager();
565: pm.currentTransaction().begin();
566: pm.makePersistent(t);
567: pm.currentTransaction().commit();
568: //detach the team t
569: Team copyOfT = (Team) pm.detachCopy(t);
570: //begin
571: pm.currentTransaction().begin();
572: //modify the coach
573: t.getCoach().setExperience(99);
574: Iterator it = t.getPlayers().iterator();
575: //modify the players
576: while (it.hasNext()) {
577: Player p = (Player) it.next();
578: p.setAge(99);
579: }
580: try {
581: //attach the team
582: Team attachedTeam = (Team) pm.makePersistent(copyOfT);
583: pm.currentTransaction().commit();
584: fail("The attached is supposed to fail.");
585: } catch (Exception e) {
586: assertEquals("Wrong exception thrown: " + e.getClass(),
587: JDOException.class, e.getClass());
588: } finally {
589: if (pm.currentTransaction().isActive()) {
590: pm.currentTransaction().rollback();
591: }
592: pm.close();
593: }
594: }
595:
596: /**
597: * Test the attach method with versioning:
598: * make an object persistent
599: * detach
600: * tx.begin
601: * write
602: * tx.commit
603: * tx.begin
604: * attach => FAIL
605: * tx.commit
606: */
607: public void testVersion10() {
608: logger.log(BasicLevel.DEBUG,
609: "***************testVersion10*****************");
610: Team t = new Team("T10", null, null);
611: Collection players = new ArrayList();
612: Player p1 = new Player("p19", t, 20);
613: players.add(p1);
614: Player p2 = new Player("p20", t, 30);
615: players.add(p2);
616: t.setPlayers(players);
617: Coach c = new Coach("c10", 10, t);
618: t.setCoach(c);
619: PersistenceManager pm = pmf.getPersistenceManager();
620: pm.currentTransaction().begin();
621: pm.makePersistent(t);
622: pm.currentTransaction().commit();
623: //detach the team t
624: Team copyOfT = (Team) pm.detachCopy(t);
625: //begin
626: pm.currentTransaction().begin();
627: //modify the coach
628: t.getCoach().setExperience(99);
629: Iterator it = t.getPlayers().iterator();
630: //modify the players
631: while (it.hasNext()) {
632: Player p = (Player) it.next();
633: p.setAge(99);
634: }
635: //commit
636: pm.currentTransaction().commit();
637: //begin
638: pm.currentTransaction().begin();
639: try {
640: //attach the team
641: Team attachedTeam = (Team) pm.makePersistent(copyOfT);
642: pm.currentTransaction().commit();
643: fail("The attached is supposed to fail.");
644: } catch (Exception e) {
645: assertEquals("Wrong exception thrown: " + e.getClass(),
646: JDOException.class, e.getClass());
647: } finally {
648: if (pm.currentTransaction().isActive()) {
649: pm.currentTransaction().rollback();
650: }
651: pm.close();
652: }
653: }
654:
655: /**
656: * Test the attach method with versioning:
657: * make an object persistent
658: * detach
659: * tx.begin
660: * write
661: * tx.rollback
662: * tx.begin
663: * attach => OK
664: * tx.commit
665: */
666: public void testVersion11() {
667: logger.log(BasicLevel.DEBUG,
668: "***************testVersion11*****************");
669: Team t = new Team("T11", null, null);
670: Collection players = new ArrayList();
671: Player p1 = new Player("p21", t, 20);
672: players.add(p1);
673: Player p2 = new Player("p22", t, 30);
674: players.add(p2);
675: t.setPlayers(players);
676: Coach c = new Coach("c11", 10, t);
677: t.setCoach(c);
678: PersistenceManager pm = pmf.getPersistenceManager();
679: pm.currentTransaction().begin();
680: pm.makePersistent(t);
681: pm.currentTransaction().commit();
682: //detach the team t
683: Team copyOfT = (Team) pm.detachCopy(t);
684: //begin
685: pm.currentTransaction().begin();
686: //modify the coach
687: t.getCoach().setExperience(99);
688: Iterator it = t.getPlayers().iterator();
689: //modify the players
690: while (it.hasNext()) {
691: Player p = (Player) it.next();
692: p.setAge(99);
693: }
694: //rollback
695: pm.currentTransaction().rollback();
696: //begin
697: pm.currentTransaction().begin();
698: try {
699: //attach the team
700: Team attachedTeam = (Team) pm.makePersistent(copyOfT);
701: pm.currentTransaction().commit();
702: } catch (Exception e) {
703: fail("The attached is supposed to be ok.");
704: } finally {
705: if (pm.currentTransaction().isActive()) {
706: pm.currentTransaction().rollback();
707: }
708: pm.close();
709: }
710: }
711:
712: public void testRemovingOfPersistentObject() {
713: PersistenceManager pm = pmf.getPersistenceManager();
714: try {
715: Class[] cs = new Class[] { Coach.class, Player.class,
716: Team.class };
717: pm.currentTransaction().begin();
718: for (int i = 0; i < cs.length; i++) {
719: Query query = pm.newQuery(cs[i]);
720: Collection col = (Collection) query.execute();
721: Iterator it = col.iterator();
722: while (it.hasNext()) {
723: Object o = it.next();
724: Assert.assertNotNull(
725: "null object in the query result"
726: + cs[i].getName(), o);
727: pm.deletePersistent(o);
728:
729: }
730: query.close(col);
731: }
732: pm.currentTransaction().commit();
733: } catch (JDOException e) {
734: Exception ie = ExceptionHelper.getNested(e);
735: logger.log(BasicLevel.ERROR, "", ie);
736: fail(ie.getMessage());
737: } finally {
738: pm.close();
739: }
740: }
741: }
|