KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > detach > TestVersion


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  */

25
26 package org.objectweb.speedo.runtime.detach;
27
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javax.jdo.JDOException;
34 import javax.jdo.PersistenceManager;
35 import javax.jdo.Query;
36
37 import junit.framework.Assert;
38
39 import org.objectweb.speedo.SpeedoTestHelper;
40 import org.objectweb.speedo.api.ExceptionHelper;
41 import org.objectweb.speedo.pobjects.detach.Coach;
42 import org.objectweb.speedo.pobjects.detach.Player;
43 import org.objectweb.speedo.pobjects.detach.Team;
44 import org.objectweb.util.monolog.api.BasicLevel;
45
46 /**
47  * @author Y.Bersihand
48  */

49 public class TestVersion extends SpeedoTestHelper {
50
51     public TestVersion(String JavaDoc s) {
52         super(s);
53     }
54     
55     protected String JavaDoc getLoggerName() {
56         return LOG_NAME + ".rt.detach.TestVersion";
57     }
58     
59     /**
60      * Test the attach method with versioning:
61      * make an object persistent
62      * tx.begin
63      * detach it
64      * tx.commit
65      * attach => OK
66      */

67     public void testVersion1() {
68         logger.log(BasicLevel.DEBUG, "***************testVersion1*****************");
69         Team t = new Team("T1",null,null);
70         Collection JavaDoc players = new ArrayList JavaDoc();
71         Player p1 = new Player("p1", t, 20);
72         players.add(p1);
73         Player p2 = new Player("p2", t, 30);
74         players.add(p2);
75         t.setPlayers(players);
76         Coach c = new Coach("c1", 10, t);
77         t.setCoach(c);
78         PersistenceManager pm = pmf.getPersistenceManager();
79         pm.currentTransaction().begin();
80         pm.makePersistent(t);
81         pm.currentTransaction().commit();
82         pm.currentTransaction().begin();
83         //detach the team t
84
Team copyOfT = (Team) pm.detachCopy(t);
85         //commit the tx
86
pm.currentTransaction().commit();
87         pm.currentTransaction().begin();
88         logger.log(BasicLevel.DEBUG, "ATTACH BEGIN");
89         try{
90             //attach the "invalid" team
91
Team attachedTeam = (Team) pm.attachCopy(copyOfT,false);
92             logger.log(BasicLevel.DEBUG, "ATTACH END");
93             pm.currentTransaction().commit();
94             logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
95         }catch(Exception JavaDoc e){
96             fail("The attached is supposed to be ok");
97         } finally {
98             if (pm.currentTransaction().isActive()) {
99                 pm.currentTransaction().rollback();
100             }
101             pm.close();
102         }
103     }
104     
105     
106     /**
107      * Test the attach method with versioning:
108      * make an object persistent
109      * tx.begin
110      * write
111      * detach
112      * tx.commit
113      * attach => OK
114      */

115     public void testVersion2() {
116         logger.log(BasicLevel.DEBUG, "***************testVersion2*****************");
117         Team t = new Team("T2",null,null);
118         Collection JavaDoc players = new ArrayList JavaDoc();
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 JavaDoc 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.attachCopy(copyOfT,false);
148             pm.currentTransaction().commit();
149         }catch(Exception JavaDoc 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, "***************testVersion3*****************");
170         Team t = new Team("T3",null,null);
171         Collection JavaDoc players = new ArrayList JavaDoc();
172         Player p1 = new Player("p5", t, 20);
173         players.add(p1);
174         Player p2 = new Player("p6", t, 30);
175         players.add(p2);
176         t.setPlayers(players);
177         Coach c = new Coach("c3", 10, t);
178         t.setCoach(c);
179         PersistenceManager pm = pmf.getPersistenceManager();
180         pm.currentTransaction().begin();
181         pm.makePersistent(t);
182         pm.currentTransaction().commit();
183         //begin
184
pm.currentTransaction().begin();
185         //detach the team t
186
Team copyOfT = (Team) pm.detachCopy(t);
187         //modify the coach
188
t.setCoach(new Coach("c31", 10, t));
189         Iterator JavaDoc it = t.getPlayers().iterator();
190         //modify the players
191
while(it.hasNext()){
192             Player p = (Player) it.next();
193             p.setAge(99);
194         }
195         //commit
196
pm.currentTransaction().commit();
197         pm.currentTransaction().begin();
198         try{
199             //attach the team
200
Team attachedTeam = (Team) pm.attachCopy(copyOfT,false);
201             pm.currentTransaction().commit();
202             fail("The attached is supposed to fail");
203         }catch(Exception JavaDoc e){
204             assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass());
205         } finally {
206             if (pm.currentTransaction().isActive()) {
207                 pm.currentTransaction().rollback();
208             }
209             pm.close();
210         }
211     }
212     
213     
214     /**
215      * Test the attach method with versioning:
216      * make an object persistent
217      * tx.begin
218      * detach it
219      * tx.rollback
220      * attach => OK
221      */

222     public void testVersion4() {
223         logger.log(BasicLevel.DEBUG, "***************testVersion4*****************");
224         Team t = new Team("T4",null,null);
225         Collection JavaDoc players = new ArrayList JavaDoc();
226         Player p1 = new Player("p7", t, 20);
227         players.add(p1);
228         Player p2 = new Player("p8", t, 30);
229         players.add(p2);
230         t.setPlayers(players);
231         Coach c = new Coach("c4", 10, t);
232         t.setCoach(c);
233         PersistenceManager pm = pmf.getPersistenceManager();
234         pm.currentTransaction().begin();
235         pm.makePersistent(t);
236         pm.currentTransaction().commit();
237         pm.currentTransaction().begin();
238         //detach the team t
239
Team copyOfT = (Team) pm.detachCopy(t);
240         //rollback the tx
241
pm.currentTransaction().rollback();
242         pm.currentTransaction().begin();
243         try{
244             //attach the "invalid" team
245
Team attachedTeam = (Team) pm.attachCopy(copyOfT,false);
246             pm.currentTransaction().commit();
247         }catch(Exception JavaDoc e){
248             fail("The attached is supposed to be ok");
249         } finally {
250             if (pm.currentTransaction().isActive()) {
251                 pm.currentTransaction().rollback();
252             }
253             pm.close();
254         }
255     }
256     
257     
258     /**
259      * Test the attach method with versioning:
260      * make an object persistent
261      * tx.begin
262      * write
263      * detach
264      * tx.rollback
265      * attach => FAIL
266      */

267     public void testVersion5() {
268         logger.log(BasicLevel.DEBUG, "***************testVersion5*****************");
269         Team t = new Team("T5",null,null);
270         Collection JavaDoc players = new ArrayList JavaDoc();
271         Player p1 = new Player("p9", t, 20);
272         players.add(p1);
273         Player p2 = new Player("p10", t, 30);
274         players.add(p2);
275         t.setPlayers(players);
276         Coach c = new Coach("c5", 10, t);
277         t.setCoach(c);
278         PersistenceManager pm = pmf.getPersistenceManager();
279         pm.currentTransaction().begin();
280         pm.makePersistent(t);
281         pm.currentTransaction().commit();
282         //begin
283
pm.currentTransaction().begin();
284         //modify the coach
285
t.setCoach(new Coach("c51", 10, t));
286         Iterator JavaDoc it = t.getPlayers().iterator();
287         //modify the players
288
while(it.hasNext()){
289             Player p = (Player) it.next();
290             p.setAge(99);
291         }
292         //detach the team t
293
Team copyOfT = (Team) pm.detachCopy(t);
294         //rollback
295
pm.currentTransaction().rollback();
296         pm.currentTransaction().begin();
297         try{
298             //attach the team
299
Team attachedTeam = (Team) pm.attachCopy(copyOfT,false);
300             pm.currentTransaction().commit();
301             fail("The attached is supposed to fail.");
302         }catch(Exception JavaDoc e){
303             assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass());
304         } finally {
305             if (pm.currentTransaction().isActive()) {
306                 pm.currentTransaction().rollback();
307             }
308             pm.close();
309         }
310     }
311     
312     /**
313      * Test the attach method with versioning:
314      * make an object persistent
315      * tx.begin
316      * detach
317      * write
318      * tx.rollback
319      * attach => OK
320      */

321     public void testVersion6() {
322         logger.log(BasicLevel.DEBUG, "***************testVersion6*****************");
323         Team t = new Team("T6",null,null);
324         Collection JavaDoc players = new ArrayList JavaDoc();
325         Player p1 = new Player("p11", t, 20);
326         players.add(p1);
327         Player p2 = new Player("p12", t, 30);
328         players.add(p2);
329         t.setPlayers(players);
330         Coach c = new Coach("c6", 10, t);
331         t.setCoach(c);
332         PersistenceManager pm = pmf.getPersistenceManager();
333         pm.currentTransaction().begin();
334         pm.makePersistent(t);
335         pm.currentTransaction().commit();
336         //begin
337
pm.currentTransaction().begin();
338         //detach the team t
339
Team copyOfT = (Team) pm.detachCopy(t);
340         //modify the coach
341
t.getCoach().setExperience(99);
342         Iterator JavaDoc it = t.getPlayers().iterator();
343         //modify the players
344
while(it.hasNext()){
345             Player p = (Player) it.next();
346             p.setAge(99);
347         }
348         //rollback
349
pm.currentTransaction().rollback();
350         pm.currentTransaction().begin();
351         try{
352             //attach the team
353
Team attachedTeam = (Team) pm.attachCopy(copyOfT,false);
354             pm.currentTransaction().commit();
355         }catch(Exception JavaDoc e){
356             fail("The attached is supposed to be ok.");
357         } finally {
358             if (pm.currentTransaction().isActive()) {
359                 pm.currentTransaction().rollback();
360             }
361             pm.close();
362         }
363     }
364     
365     /**
366      * Test the attach method with versioning:
367      * make an object persistent
368      * tx.begin
369      * detach(d1)
370      * detach(d2)
371      * tx.commit
372      * write d1
373      * write d2
374      * tx.begin
375      * attach(d1) => OK
376      * tx.commit
377      * tx.begin
378      * attach(d2) => FAIL
379      * tx.commit
380      */

381     public void testVersion7() {
382         logger.log(BasicLevel.DEBUG, "***************testVersion7*****************");
383         Team t = new Team("T7",null,null);
384         Collection JavaDoc players = new ArrayList JavaDoc();
385         Player p1 = new Player("p13", t, 20);
386         players.add(p1);
387         Player p2 = new Player("p14", t, 30);
388         players.add(p2);
389         t.setPlayers(players);
390         Coach c = new Coach("c7", 10, t);
391         t.setCoach(c);
392         PersistenceManager pm = pmf.getPersistenceManager();
393         pm.currentTransaction().begin();
394         pm.makePersistent(t);
395         pm.currentTransaction().commit();
396         //begin
397
pm.currentTransaction().begin();
398         //detach the team t
399
Team copyOfT1 = (Team) pm.detachCopy(t);
400         //detach the team t
401
Team copyOfT2 = (Team) pm.detachCopy(t);
402         //commit
403
pm.currentTransaction().commit();
404         
405         //modify the copy1
406
copyOfT1.getCoach().setExperience(99);
407         Iterator JavaDoc it = copyOfT1.getPlayers().iterator();
408         //modify the players
409
while(it.hasNext()){
410             Player p = (Player) it.next();
411             p.setAge(99);
412         }
413         //modify the copy2
414
copyOfT2.getCoach().setExperience(99);
415         it = copyOfT2.getPlayers().iterator();
416         //modify the players
417
while(it.hasNext()){
418             Player p = (Player) it.next();
419             p.setAge(99);
420         }
421         
422         boolean attach1OK = false;
423         try{
424             pm.currentTransaction().begin();
425             //attach the copy1
426
Team attachedTeam1 = (Team) pm.attachCopy(copyOfT1,false);
427             pm.currentTransaction().commit();
428             attach1OK = true;
429             pm.currentTransaction().begin();
430             //attach the copy2
431
Team attachedTeam2 = (Team) pm.attachCopy(copyOfT2,false);
432             pm.currentTransaction().commit();
433             fail("The second attach is supposed to fail");
434         }catch(Exception JavaDoc e){
435             assertTrue("The first attach is supposed to be ok.", attach1OK);
436             assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass());
437         } finally {
438             if (pm.currentTransaction().isActive()) {
439                 pm.currentTransaction().rollback();
440             }
441             pm.close();
442         }
443     }
444     
445     /**
446      * Test the attach method with versioning:
447      * make an object persistent
448      * tx.begin
449      * detach(d1)
450      * detach(d2)
451      * tx.commit
452      * write d1
453      * write d2
454      * tx.begin
455      * attach(d2) => OK
456      * tx.commit
457      * tx.begin
458      * attach(d1) => FAIL
459      * tx.commit
460      */

461     public void testVersion8() {
462         logger.log(BasicLevel.DEBUG, "***************testVersion8*****************");
463         Team t = new Team("T8",null,null);
464         Collection JavaDoc players = new ArrayList JavaDoc();
465         Player p1 = new Player("p15", t, 20);
466         players.add(p1);
467         Player p2 = new Player("p16", t, 30);
468         players.add(p2);
469         t.setPlayers(players);
470         Coach c = new Coach("c8", 10, t);
471         t.setCoach(c);
472         PersistenceManager pm = pmf.getPersistenceManager();
473         pm.currentTransaction().begin();
474         pm.makePersistent(t);
475         pm.currentTransaction().commit();
476         //begin
477
pm.currentTransaction().begin();
478         //detach the team t
479
Team copyOfT1 = (Team) pm.detachCopy(t);
480         //detach the team t
481
Team copyOfT2 = (Team) pm.detachCopy(t);
482         //commit
483
pm.currentTransaction().commit();
484         
485         //modify the copy1
486
copyOfT1.getCoach().setExperience(99);
487         Iterator JavaDoc it = copyOfT1.getPlayers().iterator();
488         //modify the players
489
while(it.hasNext()){
490             Player p = (Player) it.next();
491             p.setAge(99);
492         }
493         //modify the copy2
494
copyOfT2.getCoach().setExperience(99);
495         it = copyOfT2.getPlayers().iterator();
496         //modify the players
497
while(it.hasNext()){
498             Player p = (Player) it.next();
499             p.setAge(99);
500         }
501         
502         boolean attach2OK = false;
503         try{
504             pm.currentTransaction().begin();
505             //attach the copy2
506
Team attachedTeam2 = (Team) pm.attachCopy(copyOfT2,false);
507             pm.currentTransaction().commit();
508             attach2OK = true;
509             pm.currentTransaction().begin();
510             //attach the copy1
511
Team attachedTeam1 = (Team) pm.attachCopy(copyOfT1,false);
512             pm.currentTransaction().commit();
513             fail("The attach of copy1 is supposed to fail");
514         }catch(Exception JavaDoc e){
515             assertTrue("The attach of copy2 is supposed to be ok.", attach2OK);
516             assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass());
517         } finally {
518             if (pm.currentTransaction().isActive()) {
519                 pm.currentTransaction().rollback();
520             }
521             pm.close();
522         }
523     }
524     
525     /**
526      * Test the attach method with versioning:
527      * make an object persistent
528      * detach
529      * tx.begin
530      * write
531      * attach => FAIL
532      * tx.commit
533      */

534     public void testVersion9() {
535         logger.log(BasicLevel.DEBUG, "***************testVersion9*****************");
536         Team t = new Team("T9",null,null);
537         Collection JavaDoc players = new ArrayList JavaDoc();
538         Player p1 = new Player("p17", t, 20);
539         players.add(p1);
540         Player p2 = new Player("p18", t, 30);
541         players.add(p2);
542         t.setPlayers(players);
543         Coach c = new Coach("c9", 10, t);
544         t.setCoach(c);
545         PersistenceManager pm = pmf.getPersistenceManager();
546         pm.currentTransaction().begin();
547         pm.makePersistent(t);
548         pm.currentTransaction().commit();
549         //detach the team t
550
Team copyOfT = (Team) pm.detachCopy(t);
551         //begin
552
pm.currentTransaction().begin();
553         //modify the coach
554
t.getCoach().setExperience(99);
555         Iterator JavaDoc it = t.getPlayers().iterator();
556         //modify the players
557
while(it.hasNext()){
558             Player p = (Player) it.next();
559             p.setAge(99);
560         }
561         try{
562             //attach the team
563
Team attachedTeam = (Team) pm.attachCopy(copyOfT,false);
564             pm.currentTransaction().commit();
565             fail("The attached is supposed to fail.");
566         }catch(Exception JavaDoc e){
567             assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass());
568         } finally {
569             if (pm.currentTransaction().isActive()) {
570                 pm.currentTransaction().rollback();
571             }
572             pm.close();
573         }
574     }
575     
576     /**
577      * Test the attach method with versioning:
578      * make an object persistent
579      * detach
580      * tx.begin
581      * write
582      * tx.commit
583      * tx.begin
584      * attach => FAIL
585      * tx.commit
586      */

587     public void testVersion10() {
588         logger.log(BasicLevel.DEBUG, "***************testVersion10*****************");
589         Team t = new Team("T10",null,null);
590         Collection JavaDoc players = new ArrayList JavaDoc();
591         Player p1 = new Player("p19", t, 20);
592         players.add(p1);
593         Player p2 = new Player("p20", t, 30);
594         players.add(p2);
595         t.setPlayers(players);
596         Coach c = new Coach("c10", 10, t);
597         t.setCoach(c);
598         PersistenceManager pm = pmf.getPersistenceManager();
599         pm.currentTransaction().begin();
600         pm.makePersistent(t);
601         pm.currentTransaction().commit();
602         //detach the team t
603
Team copyOfT = (Team) pm.detachCopy(t);
604         //begin
605
pm.currentTransaction().begin();
606         //modify the coach
607
t.getCoach().setExperience(99);
608         Iterator JavaDoc it = t.getPlayers().iterator();
609         //modify the players
610
while(it.hasNext()){
611             Player p = (Player) it.next();
612             p.setAge(99);
613         }
614         //commit
615
pm.currentTransaction().commit();
616         //begin
617
pm.currentTransaction().begin();
618         try{
619             //attach the team
620
Team attachedTeam = (Team) pm.attachCopy(copyOfT,false);
621             pm.currentTransaction().commit();
622             fail("The attached is supposed to fail.");
623         }catch(Exception JavaDoc e){
624             assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass());
625         } finally {
626             if (pm.currentTransaction().isActive()) {
627                 pm.currentTransaction().rollback();
628             }
629             pm.close();
630         }
631     }
632     
633     /**
634      * Test the attach method with versioning:
635      * make an object persistent
636      * detach
637      * tx.begin
638      * write
639      * tx.rollback
640      * tx.begin
641      * attach => OK
642      * tx.commit
643      */

644     public void testVersion11() {
645         logger.log(BasicLevel.DEBUG, "***************testVersion11*****************");
646         Team t = new Team("T11",null,null);
647         Collection JavaDoc players = new ArrayList JavaDoc();
648         Player p1 = new Player("p21", t, 20);
649         players.add(p1);
650         Player p2 = new Player("p22", t, 30);
651         players.add(p2);
652         t.setPlayers(players);
653         Coach c = new Coach("c11", 10, t);
654         t.setCoach(c);
655         PersistenceManager pm = pmf.getPersistenceManager();
656         pm.currentTransaction().begin();
657         pm.makePersistent(t);
658         pm.currentTransaction().commit();
659         //detach the team t
660
Team copyOfT = (Team) pm.detachCopy(t);
661         //begin
662
pm.currentTransaction().begin();
663         //modify the coach
664
t.getCoach().setExperience(99);
665         Iterator JavaDoc it = t.getPlayers().iterator();
666         //modify the players
667
while(it.hasNext()){
668             Player p = (Player) it.next();
669             p.setAge(99);
670         }
671         //rollback
672
pm.currentTransaction().rollback();
673         //begin
674
pm.currentTransaction().begin();
675         try{
676             //attach the team
677
Team attachedTeam = (Team) pm.attachCopy(copyOfT,false);
678             pm.currentTransaction().commit();
679         }catch(Exception JavaDoc e){
680             fail("The attached is supposed to be ok.");
681         } finally {
682             if (pm.currentTransaction().isActive()) {
683                 pm.currentTransaction().rollback();
684             }
685             pm.close();
686         }
687     }
688     
689     public void testRemovingOfPersistentObject() {
690         PersistenceManager pm = pmf.getPersistenceManager();
691         try {
692             Class JavaDoc[] cs = new Class JavaDoc[]{Coach.class, Player.class, Team.class};
693             pm.currentTransaction().begin();
694             for(int i=0; i<cs.length; i++) {
695                 Query query = pm.newQuery(cs[i]);
696                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
697                 Iterator JavaDoc it = col.iterator();
698                 while(it.hasNext()) {
699                     Object JavaDoc o = it.next();
700                     Assert.assertNotNull("null object in the query result"
701                         + cs[i].getName(), o);
702                     pm.deletePersistent(o);
703
704                 }
705                 query.close(col);
706             }
707             pm.currentTransaction().commit();
708         } catch (JDOException e) {
709             Exception JavaDoc ie = ExceptionHelper.getNested(e);
710             logger.log(BasicLevel.ERROR, "", ie);
711             fail(ie.getMessage());
712         } finally {
713             pm.close();
714         }
715     }
716 }
717
Popular Tags