KickJava   Java API By Example, From Geeks To Geeks.

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


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.mim.api.SpeedoProxy;
42 import org.objectweb.speedo.pm.api.ProxyManager;
43 import org.objectweb.speedo.pobjects.detach.Car;
44 import org.objectweb.speedo.pobjects.detach.Coach;
45 import org.objectweb.speedo.pobjects.detach.FormulaOne;
46 import org.objectweb.speedo.pobjects.detach.Player;
47 import org.objectweb.speedo.pobjects.detach.Team;
48 import org.objectweb.speedo.pobjects.detach.Vehicle;
49 import org.objectweb.util.monolog.api.BasicLevel;
50
51 /**
52  * @author Y.Bersihand
53  */

54 public class TestAttach extends SpeedoTestHelper {
55
56     public TestAttach(String JavaDoc s) {
57         super(s);
58     }
59     
60     protected String JavaDoc getLoggerName() {
61         return LOG_NAME + ".rt.detach.TestAttach";
62     }
63     
64     /**
65      * Test the attach method: make an object persistent, detach it then attach it.
66      */

67     public void testAttachCopy() {
68         logger.log(BasicLevel.DEBUG, "***************testAttachCopy*****************");
69         Team t = new Team("Bordeaux",null,null);
70         Collection JavaDoc players = new ArrayList JavaDoc();
71         Player p1 = new Player("p1", t, 25);
72         players.add(p1);
73         Player p2 = new Player("p2", t, 32);
74         players.add(p2);
75         t.setPlayers(players);
76         Coach c = new Coach("c1", 5, t);
77         t.setCoach(c);
78         
79         PersistenceManager pm = pmf.getPersistenceManager();
80         pm.currentTransaction().begin();
81         logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
82         pm.makePersistent(t);
83         pm.currentTransaction().commit();
84         try {
85             //detach the team t
86
Team copyOfT = (Team) pm.detachCopy(t);
87             assertNotNull(copyOfT);
88             //print the team out
89
logger.log(BasicLevel.DEBUG, copyOfT.toString());
90             pm.currentTransaction().begin();
91             //attach the team t
92
Team attachedTeam = (Team) pm.attachCopy(copyOfT,false);
93             assertEquals(copyOfT.getTown(), attachedTeam.getTown());
94             assertEquals(copyOfT.getCoach().getExperience(), attachedTeam.getCoach().getExperience());
95             assertEquals(copyOfT.getCoach().getName(), attachedTeam.getCoach().getName());
96             pm.currentTransaction().commit();
97             logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
98         } catch (Exception JavaDoc e) {
99             fail(e.getMessage());
100         } finally {
101             if (pm.currentTransaction().isActive())
102                 pm.currentTransaction().rollback();
103             pm.close();
104         }
105     }
106         
107     /**
108      * Test the attach method with update: make an object persistent, detach it , modify it and then attach it.
109      */

110     public void testAttachModifiedCopy() {
111         logger.log(BasicLevel.DEBUG, "***************testAttachModifiedCopy*****************");
112         Team t = new Team("Paris",null,null);
113         Collection JavaDoc players = new ArrayList JavaDoc();
114         Player p1 = new Player("p3", t, 20);
115         players.add(p1);
116         Player p2 = new Player("p4", t, 30);
117         players.add(p2);
118         t.setPlayers(players);
119         Coach c = new Coach("c2", 10, t);
120         t.setCoach(c);
121         
122         PersistenceManager pm = pmf.getPersistenceManager();
123         pm.currentTransaction().begin();
124         logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
125         pm.makePersistent(t);
126         pm.currentTransaction().commit();
127         //detach the team t
128
Team copyOfT = (Team) pm.detachCopy(t);
129         //modify the team
130
copyOfT.getCoach().setExperience(99);
131         //print the team out
132
logger.log(BasicLevel.DEBUG, "Copy of team " + copyOfT.toString());
133         pm.currentTransaction().begin();
134         //attach the team t
135
Team attachedTeam = (Team) pm.attachCopy(copyOfT,false);
136         try {
137             assertNotNull(attachedTeam);
138             logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
139             assertEquals(99, attachedTeam.getCoach().getExperience());
140             pm.currentTransaction().commit();
141         } catch (Exception JavaDoc e) {
142             fail(e.getMessage());
143         } finally {
144             if (pm.currentTransaction().isActive())
145                 pm.currentTransaction().rollback();
146             pm.close();
147         }
148     }
149
150     /**
151      * Test the attach method with an update on a reference: make an object persistent, detach it , modify it and then attach it.
152      */

153     public void testAttachModifiedReference() {
154         logger.log(BasicLevel.DEBUG, "***************testAttachModifiedReference*****************");
155         Team t = new Team("Lens",null,null);
156         Collection JavaDoc players = new ArrayList JavaDoc();
157         Player p1 = new Player("p21", t, 20);
158         players.add(p1);
159         Player p2 = new Player("p22", t, 30);
160         players.add(p2);
161         t.setPlayers(players);
162         Coach c = new Coach("c23", 10, t);
163         t.setCoach(c);
164         
165         PersistenceManager pm = pmf.getPersistenceManager();
166         pm.currentTransaction().begin();
167         logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
168         pm.makePersistent(t);
169         pm.currentTransaction().commit();
170         try {
171             //detach the team t
172
Team copyOfT = (Team) pm.detachCopy(t);
173             Coach newCoach = new Coach("c33", 15, new Team("DummyTeam",null,null));
174             // update the reference while detached
175
copyOfT.setCoach(newCoach);
176             //print the team out
177
logger.log(BasicLevel.DEBUG, "Copy of team " + copyOfT.toString());
178             pm.currentTransaction().begin();
179             // attach the team t
180
Team attachedTeam = (Team) pm.attachCopy(copyOfT,false);
181             assertNotNull("attachedTeam should not be null", attachedTeam);
182             logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
183             assertEquals("Coach of the attached team is not the good one", newCoach, attachedTeam.getCoach());
184             pm.currentTransaction().commit();
185         } catch (Exception JavaDoc e) {
186             e.printStackTrace();
187             fail(e.getMessage());
188         } finally {
189             if (pm.currentTransaction().isActive())
190                 pm.currentTransaction().rollback();
191             pm.close();
192         }
193     }
194     
195     /**
196      * Test the attach method with update on a collection: make an object persistent, detach it , modify it and then attach it.
197      */

198     public void testAttachModifiedCollectionCopy() {
199         logger.log(BasicLevel.DEBUG, "***************testAttachModifiedCollectionCopy*****************");
200         Team t = new Team("Nantes",null,null);
201         Collection JavaDoc players = new ArrayList JavaDoc();
202         Player p1 = new Player("p5", t, 20);
203         players.add(p1);
204         Player p2 = new Player("p6", t, 30);
205         players.add(p2);
206         t.setPlayers(players);
207         Coach c = new Coach("c3", 10, t);
208         t.setCoach(c);
209         
210         PersistenceManager pm = pmf.getPersistenceManager();
211         pm.currentTransaction().begin();
212         logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
213         pm.makePersistent(t);
214         pm.currentTransaction().commit();
215         //detach the team t
216
Team copyOfT = (Team) pm.detachCopy(t);
217         pm.close();
218         //modify the coach
219
copyOfT.getCoach().setExperience(99);
220         //and modify the collection
221
Iterator JavaDoc it = copyOfT.getPlayers().iterator();
222         while(it.hasNext()){
223             Player p = (Player) it.next();
224             p.setAge(99);
225         }
226         // print the team out
227
logger.log(BasicLevel.DEBUG, "Copy of team " + copyOfT.toString());
228         PersistenceManager pm2 = pmf.getPersistenceManager();
229         try {
230             pm2.currentTransaction().begin();
231             //attach the team t
232
Team attachedTeam = (Team) ((ProxyManager)pm2).attachCopy(copyOfT,false);
233         
234             Iterator JavaDoc itP = attachedTeam.getPlayers().iterator();
235             while(itP.hasNext()){
236                 Player p = (Player) itP.next();
237                 assertEquals(99, p.getAge());
238             }
239             pm2.currentTransaction().commit();
240             logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
241         } catch (Exception JavaDoc e) {
242             fail(e.getMessage());
243         } finally {
244             if (pm.currentTransaction().isActive())
245                 pm.currentTransaction().rollback();
246             pm.close();
247             if (pm2.currentTransaction().isActive())
248                 pm2.currentTransaction().rollback();
249             pm2.close();
250         }
251     }
252
253     /**
254      * Test the attach method with a new object: the object is made persistent.
255      */

256     public void testAttachNewObject() {
257         logger.log(BasicLevel.DEBUG, "***************testAttachNewObject*****************");
258         Team t = new Team("Bastia",null,null);
259         Collection JavaDoc players = new ArrayList JavaDoc();
260         Player p1 = new Player("p7", t, 20);
261         players.add(p1);
262         Player p2 = new Player("p8", t, 30);
263         players.add(p2);
264         t.setPlayers(players);
265         Coach c = new Coach("c4", 10, t);
266         t.setCoach(c);
267         
268         PersistenceManager pm = pmf.getPersistenceManager();
269         pm.currentTransaction().begin();
270         logger.log(BasicLevel.DEBUG, "attach the team " + t.toString());
271         Team attachedTeam = (Team) pm.attachCopy(t, false);
272         pm.currentTransaction().commit();
273         try {
274             assertTrue( ((SpeedoProxy) attachedTeam).jdoIsPersistent());
275             logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
276         } catch (Exception JavaDoc e) {
277             fail(e.getMessage());
278         } finally {
279             if (pm.currentTransaction().isActive())
280                 pm.currentTransaction().rollback();
281             pm.close();
282         }
283     }
284
285     /**
286      * Test the attach method with an object having a null reference.
287      */

288     public void testAttachNullRef() {
289         logger.log(BasicLevel.DEBUG, "***************testAttachNullRef*****************");
290         Team t = new Team("Istres",null,null);
291         Collection JavaDoc players = new ArrayList JavaDoc();
292         Player p1 = new Player("p9", t, 20);
293         players.add(p1);
294         Player p2 = new Player("p10", t, 30);
295         players.add(p2);
296         t.setPlayers(players);
297         Coach c = new Coach("c5", 10, t);
298         t.setCoach(c);
299         
300         PersistenceManager pm = pmf.getPersistenceManager();
301         pm.currentTransaction().begin();
302         logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
303         pm.makePersistent(t);
304         pm.currentTransaction().commit();
305         try {
306             //detach the team t
307
Team copyOfT = (Team) pm.detachCopy(t);
308             assertNotNull(copyOfT);
309             assertNotNull("Coach of detached team should not be null.", copyOfT.getCoach());
310             //set null for the coach
311
copyOfT.setCoach(null);
312             assertNull(copyOfT.getCoach());
313             //print the team out
314
logger.log(BasicLevel.DEBUG, copyOfT.toString());
315             pm.currentTransaction().begin();
316             //attach the team t
317
Team attachedTeam = (Team) pm.attachCopy(copyOfT,false);
318             assertEquals(copyOfT.getTown(), attachedTeam.getTown());
319             assertNull("Coach of attached team should be null", attachedTeam.getCoach());
320             pm.currentTransaction().commit();
321             logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString());
322         } catch (Exception JavaDoc e) {
323             fail(e.getMessage());
324         } finally {
325             if (pm.currentTransaction().isActive())
326                 pm.currentTransaction().rollback();
327             pm.close();
328         }
329     }
330     
331     /**
332      * Test the attach method: make an inherited object persistent, detach it then attach it.
333      */

334     public void testAttachInherited() {
335         logger.log(BasicLevel.DEBUG, "***************testAttachInherited*****************");
336         Car c = new Car("r5", 4, "red");
337         FormulaOne f = new FormulaOne("williams", 4, "green", 262);
338         
339         PersistenceManager pm = pmf.getPersistenceManager();
340         pm.currentTransaction().begin();
341         logger.log(BasicLevel.DEBUG, "make persistent the car " + c.toString() );
342         logger.log(BasicLevel.DEBUG, "make persistent the formula one " + f.toString());
343         pm.makePersistent(c);
344         pm.makePersistent(f);
345         pm.currentTransaction().commit();
346         //detach the car c
347
Car copyOfC = (Car) pm.detachCopy(c);
348         //print the car out
349
logger.log(BasicLevel.DEBUG, copyOfC.toString());
350         //detach the formula one f
351
FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
352         //print the formula one out
353
logger.log(BasicLevel.DEBUG, copyOfF.toString());
354         pm.currentTransaction().begin();
355         //attach the copied car
356
Car attachedCar = (Car) pm.attachCopy(copyOfC,false);
357         //attach the copied formula one
358
FormulaOne attachedF = (FormulaOne) pm.attachCopy(copyOfF,false);
359         pm.currentTransaction().commit();
360         try {
361             assertNotNull(attachedCar);
362             assertEquals(copyOfC.getColor(), attachedCar.getColor());
363             assertEquals(copyOfC.getName(), attachedCar.getName());
364             assertEquals(copyOfC.getNbOfWheels(), attachedCar.getNbOfWheels());
365             assertEquals(copyOfC.getType(), attachedCar.getType());
366         
367             assertNotNull(attachedF);
368             assertEquals(copyOfF.getColor(), attachedF.getColor());
369             assertEquals(copyOfF.getName(), attachedF.getName());
370             assertEquals(copyOfF.getNbOfWheels(), attachedF.getNbOfWheels());
371             assertEquals(copyOfF.getType(), attachedF.getType());
372             assertEquals(copyOfF.getSpeedMax(), attachedF.getSpeedMax());
373         
374             logger.log(BasicLevel.DEBUG,"The attached version of the car is as follows:\n " + attachedCar.toString());
375             logger.log(BasicLevel.DEBUG,"The attached version of the formula one is as follows:\n " + attachedF.toString());
376         } catch (Exception JavaDoc e) {
377             fail(e.getMessage());
378         } finally {
379             if (pm.currentTransaction().isActive())
380                 pm.currentTransaction().rollback();
381             pm.close();
382         }
383     }
384     
385     /**
386      * Test the attach method with update: make an inherited object persistent, detach it , modify it and then attach it.
387      */

388     public void testAttachModifiedInherited() {
389         logger.log(BasicLevel.DEBUG, "***************testAttachModifiedInherited*****************");
390         FormulaOne f = new FormulaOne("renault", 4, "yellow", 262);
391         
392         PersistenceManager pm = pmf.getPersistenceManager();
393         pm.currentTransaction().begin();
394         logger.log(BasicLevel.DEBUG, "make persistent the formula one " + f.toString());
395         pm.makePersistent(f);
396         pm.currentTransaction().commit();
397         //detach
398
FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
399         //modify
400
copyOfF.setNbOfWheels(2);
401         copyOfF.setSpeedMax(320);
402         //print out
403
logger.log(BasicLevel.DEBUG, "Copy of formula one " + copyOfF.toString());
404         pm.currentTransaction().begin();
405         //attach
406
FormulaOne attachedF = (FormulaOne) pm.attachCopy(copyOfF,false);
407         try {
408             assertEquals(2, attachedF.getNbOfWheels());
409             assertEquals(320, attachedF.getSpeedMax());
410             pm.currentTransaction().commit();
411             logger.log(BasicLevel.DEBUG,"The attached version of the formula one is as follows:\n " + attachedF.toString());
412         } catch (Exception JavaDoc e) {
413             fail(e.getMessage());
414         } finally {
415             if (pm.currentTransaction().isActive())
416                 pm.currentTransaction().rollback();
417             pm.close();
418         }
419     }
420     
421     
422     /**
423      * Test the attach method with a new inherited object: the object is made persistent.
424      */

425     public void testAttachNewInherited() {
426         logger.log(BasicLevel.DEBUG, "***************testAttachNewInherited*****************");
427         FormulaOne f = new FormulaOne("sauber", 4, "red", 262);
428         PersistenceManager pm = pmf.getPersistenceManager();
429         pm.currentTransaction().begin();
430         logger.log(BasicLevel.DEBUG, "attach the f1 " + f.toString());
431         FormulaOne attachedF = (FormulaOne) pm.attachCopy(f,false);
432         try {
433             assertTrue(((SpeedoProxy)attachedF).jdoIsPersistent());
434             pm.currentTransaction().commit();
435             logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedF.toString());
436         } catch (Exception JavaDoc e) {
437             fail(e.getMessage());
438         } finally {
439             if (pm.currentTransaction().isActive())
440                 pm.currentTransaction().rollback();
441             pm.close();
442         }
443     }
444     
445     
446     /**
447      * Test the attach method with an invalid copy: make an inherited object persistent, modify it, detach it , rollback the transaction and then attach it.
448      */

449     public void testAttachInvalidInherited() {
450         logger.log(BasicLevel.DEBUG, "***************testAttachInvalidInherited*****************");
451         FormulaOne f = new FormulaOne("ferrari", 4, "red", 262);
452         PersistenceManager pm = pmf.getPersistenceManager();
453         pm.currentTransaction().begin();
454         logger.log(BasicLevel.DEBUG, "make persistent the f1 " + f.toString());
455         pm.makePersistent(f);
456         pm.currentTransaction().commit();
457         pm.currentTransaction().begin();
458         //modify
459
f.setNbOfWheels(1);
460          //detach the team t
461
FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
462         //rollback the tx : it invalids the copy
463
pm.currentTransaction().rollback();
464         // print out
465
logger.log(BasicLevel.DEBUG, "Copy of f1 " + copyOfF.toString());
466         logger.log(BasicLevel.DEBUG, "rollback of the tx: the attach shouldn't work.");
467         try{
468             //attach the "invalid" f1
469
pm.currentTransaction().begin();
470             FormulaOne attachedF = (FormulaOne) pm.attachCopy(copyOfF,false);
471             pm.currentTransaction().commit();
472             logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedF.toString());
473         } catch(Exception JavaDoc e){
474             assertEquals("Wrong exception thrown: " + e.getMessage(), JDOException.class, e.getClass());
475         } finally {
476             if (pm.currentTransaction().isActive()) {
477                 pm.currentTransaction().rollback();
478             }
479             pm.close();
480         }
481     }
482     
483     public void testRemovingOfPersistentObject() {
484         PersistenceManager pm = pmf.getPersistenceManager();
485         try {
486             Class JavaDoc[] cs = new Class JavaDoc[]{Car.class, Coach.class, FormulaOne.class,
487                     Player.class, Team.class, Vehicle.class};
488             pm.currentTransaction().begin();
489             for(int i=0; i<cs.length; i++) {
490                 Query query = pm.newQuery(cs[i]);
491                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
492                 Iterator JavaDoc it = col.iterator();
493                 while(it.hasNext()) {
494                     Object JavaDoc o = it.next();
495                     Assert.assertNotNull("null object in the query result"
496                         + cs[i].getName(), o);
497                     pm.deletePersistent(o);
498
499                 }
500                 query.close(col);
501             }
502             pm.currentTransaction().commit();
503         } catch (JDOException e) {
504             Exception JavaDoc ie = ExceptionHelper.getNested(e);
505             logger.log(BasicLevel.ERROR, "", ie);
506             fail(ie.getMessage());
507         } finally {
508             pm.close();
509         }
510     }
511 }
512
Popular Tags