KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.List JavaDoc;
33
34 import javax.jdo.Extent;
35 import javax.jdo.JDOException;
36 import javax.jdo.JDOUserException;
37 import javax.jdo.PersistenceManager;
38 import javax.jdo.Query;
39
40 import junit.framework.Assert;
41
42 import org.objectweb.speedo.SpeedoTestHelper;
43 import org.objectweb.speedo.api.ExceptionHelper;
44 import org.objectweb.speedo.mim.api.SpeedoProxy;
45 import org.objectweb.speedo.pobjects.detach.Car;
46 import org.objectweb.speedo.pobjects.detach.Coach;
47 import org.objectweb.speedo.pobjects.detach.FormulaOne;
48 import org.objectweb.speedo.pobjects.detach.Player;
49 import org.objectweb.speedo.pobjects.detach.Team;
50 import org.objectweb.speedo.pobjects.detach.Vehicle;
51 import org.objectweb.util.monolog.api.BasicLevel;
52
53 /**
54  * @author Y.Bersihand
55  */

56 public class TestDetach extends SpeedoTestHelper {
57
58     public TestDetach(String JavaDoc s) {
59         super(s);
60     }
61     
62     protected String JavaDoc getLoggerName() {
63         return LOG_NAME + ".rt.detach.TestDetach";
64     }
65     
66     /**
67      * Test the detach method: make an object persistent, and detach it out of an active transaction.
68      */

69     public void testDetachCopy() {
70         logger.log(BasicLevel.DEBUG, "***************testDetachCopy*****************");
71         Team t = new Team("Bordeaux",null,null);
72         Collection JavaDoc players = new ArrayList JavaDoc();
73         Player p1 = new Player("p1", t, 25);
74         players.add(p1);
75         Player p2 = new Player("p2", t, 32);
76         players.add(p2);
77         t.setPlayers(players);
78         Coach c = new Coach("c1", 5, t);
79         t.setCoach(c);
80         
81         PersistenceManager pm = pmf.getPersistenceManager();
82         pm.currentTransaction().begin();
83         logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
84         pm.makePersistent(t);
85         pm.currentTransaction().commit();
86         
87         t = null;
88         p1 = null;
89         p2 = null;
90         c = null;
91         
92         pm.evictAll();
93         pm.close();
94         
95         
96         pm = pmf.getPersistenceManager();
97         Query query = pm.newQuery(Team.class, "players.contains(player) & player.age<12");
98         query.declareVariables("Player player");
99         Collection JavaDoc results = (Collection JavaDoc)query.execute();
100         assertTrue("The result of the query should be empty.", results.isEmpty());
101         query.closeAll();
102         
103         query = pm.newQuery(Team.class, "players.contains(player) & player.age>12");
104         query.declareVariables("Player player");
105         results = (Collection JavaDoc)query.execute();
106         assertTrue("The result of the query shouldn't be empty.", !results.isEmpty());
107         Team res = (Team) results.iterator().next();
108         Collection JavaDoc cpl = res.getPlayers();
109         Iterator JavaDoc itPlayer = cpl.iterator();
110         while(itPlayer.hasNext()) {
111             Player pl = (Player) itPlayer.next();
112             assertTrue("The age of the player should be over 12. It is " + pl.getAge() + ".", pl.getAge()>12);
113         }
114         query.closeAll();
115         
116         query = pm.newQuery(Team.class, "town==\"Bordeaux\"");
117         results = (Collection JavaDoc)query.execute();
118         assertEquals("The result of the query shouldn't be empty.", false, results.isEmpty());
119         Team toDetach = (Team) results.iterator().next();
120         query.closeAll();
121         
122         //detach the team t
123
Team copyOfT = (Team) pm.detachCopy(toDetach);
124         try {
125             assertNotNull(copyOfT);
126             assertEquals("Town of team and detached team are not the same", toDetach.getTown(), copyOfT.getTown());
127             assertEquals("Coach experience of team and detached team are not the same",toDetach.getCoach().getExperience(), copyOfT.getCoach().getExperience());
128             assertEquals("Coach name of team and detached team are not the same",toDetach.getCoach().getName(), copyOfT.getCoach().getName());
129             //print the team out
130
logger.log(BasicLevel.DEBUG, copyOfT.toString());
131         } catch (Exception JavaDoc e) {
132             fail(e.getMessage());
133         } finally {
134             if (pm.currentTransaction().isActive())
135                 pm.currentTransaction().rollback();
136             pm.close();
137         }
138     }
139     
140     
141     /**
142       * Test the detach method with a non persistent object: the object is made persistent before being detached.
143       */

144     public void testDetachNonPersistentCopy() {
145         logger.log(BasicLevel.DEBUG, "*************testDetachNonPersistentCopy*****************");
146         Team t = new Team("Marseille",null,null);
147         Coach c = new Coach("c2", 5, t);
148         t.setCoach(c);
149         Player p = new Player("p3", t, 25);
150         t.addPlayer(p);
151         PersistenceManager pm = pmf.getPersistenceManager();
152         pm.currentTransaction().begin();
153         //detach the team t while it is not persistent
154
Team copyOfT = (Team) pm.detachCopy(t);
155         pm.currentTransaction().commit();
156         try {
157             assertNotNull(copyOfT);
158             assertTrue(((SpeedoProxy )t).jdoIsPersistent());
159             // print the team out
160
logger.log(BasicLevel.DEBUG, copyOfT.toString());
161         } catch(Exception JavaDoc e) {
162             fail(e.getMessage());
163         } finally {
164             if (pm.currentTransaction().isActive())
165                 pm.currentTransaction().rollback();
166             pm.close();
167         }
168     }
169     
170     /**
171       * Test the detach method with a persistent-new object: the object is flushed before being detached.
172       */

173     public void testDetachPersistentNew() {
174         logger.log(BasicLevel.DEBUG, "******************testDetachPersistentNew****************");
175         Team t = new Team("Monaco",null,null);
176         Coach c = new Coach("c3", 5, t);
177         t.setCoach(c);
178         Player p = new Player("p4", t, 25);
179         t.addPlayer(p);
180         
181         PersistenceManager pm = pmf.getPersistenceManager();
182         pm.currentTransaction().begin();
183         pm.makePersistent(c);
184         //detach the team while it is persistent-new: the object must be flushed before being detached
185
Team copyOfT = (Team) pm.detachCopy(t);
186         try {
187             assertNotNull(copyOfT);
188             assertEquals("Town of team and detached team are not the same", t.getTown(), copyOfT.getTown());
189             assertEquals("Coach experience of team and detached team are not the same",t.getCoach().getExperience(), copyOfT.getCoach().getExperience());
190             assertEquals("Coach name of team and detached team are not the same",t.getCoach().getName(), copyOfT.getCoach().getName());
191             pm.currentTransaction().commit();
192             // print the team out
193
logger.log(BasicLevel.DEBUG, copyOfT.toString());
194         } catch(Exception JavaDoc e) {
195             fail(e.getMessage());
196         } finally {
197             if (pm.currentTransaction().isActive())
198                 pm.currentTransaction().rollback();
199             pm.close();
200         }
201     }
202     
203     /**
204       * Test the detach method with a persistent-dirty object: the object is flushed before being detached.
205       */

206     public void testDetachPersistentDirty() {
207         logger.log(BasicLevel.DEBUG, "******************testDetachPersistentDirty****************");
208         Team t = new Team("Nantes",null,null);
209         Coach c = new Coach("c4", 5, t);
210         t.setCoach(c);
211         Player p = new Player("p5", t, 28);
212         t.addPlayer(p);
213                 
214         PersistenceManager pm = pmf.getPersistenceManager();
215         //make persistent
216
pm.currentTransaction().begin();
217         pm.makePersistent(c);
218         pm.currentTransaction().commit();
219         //update and detach
220
pm.currentTransaction().begin();
221         c.setExperience(10);
222         //detach the team while it is persistent-dirty: the object must be flushed before being detached
223
Team copyOfT = (Team) pm.detachCopy(t);
224         pm.currentTransaction().commit();
225         try {
226             assertEquals(10, copyOfT.getCoach().getExperience());
227             logger.log(BasicLevel.DEBUG, copyOfT.toString());
228         } catch(Exception JavaDoc e) {
229             fail(e.getMessage());
230         } finally {
231             if (pm.currentTransaction().isActive())
232                 pm.currentTransaction().rollback();
233             pm.close();
234         }
235     }
236
237     /**
238       * Test the detach method with a persistent-deleted object: an exception must be thrown.
239       */

240     public void testDetachPersistentDeleted() {
241         logger.log(BasicLevel.DEBUG, "******************testDetachPersistentDeleted****************");
242         Team t = new Team("Paris",null, null);
243         Coach c = new Coach("c5", 5, t);
244         t.setCoach(c);
245         Player p = new Player("p6", t, 28);
246         t.addPlayer(p);
247         
248         PersistenceManager pm = pmf.getPersistenceManager();
249         //make persistent
250
pm.currentTransaction().begin();
251         pm.makePersistent(c);
252         pm.currentTransaction().commit();
253         //delete
254
pm.currentTransaction().begin();
255         pm.deletePersistent(c);
256         try {
257             //detach the coach c while it is persistent-deleted: an exception must be thrown
258
pm.detachCopy(c);
259             pm.currentTransaction().commit();
260         } catch (Exception JavaDoc e) {
261
262             assertEquals("Wrong exception thrown: " + e.getMessage(),
263                     JDOUserException.class, e.getClass());
264         } finally {
265             if (pm.currentTransaction().isActive()) {
266                 pm.currentTransaction().commit();
267             }
268             pm.close();
269         }
270     }
271     
272     /**
273       * Test the detach method with a persistent-deleted object within a collection: an exception must be thrown.
274       */

275     public void testDetachPersistentDeletedCollection() {
276         logger.log(BasicLevel.DEBUG, "******************testDetachPersistentDeletedCollection****************");
277         Team t = new Team("Auxerre",null, null);
278         Coach c = new Coach("c6", 10, t);
279         t.setCoach(c);
280         Player p = new Player("p7", t, 28);
281         t.addPlayer(p);
282         
283         PersistenceManager pm = pmf.getPersistenceManager();
284         //make persistent
285
pm.currentTransaction().begin();
286         pm.makePersistent(c);
287         pm.currentTransaction().commit();
288         //delete
289
pm.currentTransaction().begin();
290         pm.deletePersistent(p);
291         try{
292             // detach the coach c while it is persistent-deleted:
293
// an exception must be thrown
294
pm.detachCopy(c);
295             pm.currentTransaction().commit();
296         } catch (Exception JavaDoc e) {
297             assertEquals("Wrong exception thrown: " + e.getMessage(),
298                     JDOUserException.class, e.getClass());
299         } finally {
300             if (pm.currentTransaction().isActive()) {
301                 pm.currentTransaction().commit();
302             }
303             pm.close();
304         }
305     }
306
307     /**
308      * Test the detach method: make an object persistent with a null refrence, and detach it out of an active transaction.
309      */

310     public void testDetachNullReference() {
311         logger.log(BasicLevel.DEBUG, "***************testDetachNullReference*****************");
312         Team t = new Team("Niort",null,null);
313         Collection JavaDoc players = new ArrayList JavaDoc();
314         Player p1 = new Player("p8", t, 25);
315         players.add(p1);
316         Player p2 = new Player("p9", t, 32);
317         players.add(p2);
318         t.setPlayers(players);
319         
320         PersistenceManager pm = pmf.getPersistenceManager();
321         pm.currentTransaction().begin();
322         logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString());
323         pm.makePersistent(t);
324         pm.currentTransaction().commit();
325         
326         try {
327             List JavaDoc result = new ArrayList JavaDoc();
328             Extent extent = pm.getExtent(Team.class, false);
329             Query query = pm.newQuery(extent, "town == townName");
330             query.declareParameters("String townName");
331             Collection JavaDoc collection = (Collection JavaDoc) query.execute(t.getTown());
332             assertTrue("The result should not be empty", !collection.isEmpty());
333             Iterator JavaDoc itr = collection.iterator();
334             Team copyOfT = null;
335             while (itr.hasNext()) {
336                 Team team = (Team) itr.next();
337                 assertEquals("The town should be " + t.getTown(), t.getTown(), team.getTown());
338                 //detach the team t
339
copyOfT = (Team) pm.detachCopy(team);
340             }
341             query.close(collection);
342             extent.closeAll();
343             assertNotNull(copyOfT);
344             assertEquals("Town of team and detached team are not the same", t.getTown(), copyOfT.getTown());
345             assertNull("The coach reference is supposed to be null", copyOfT.getCoach());
346         } catch (Exception JavaDoc e) {
347             fail(e.getMessage());
348         } finally {
349             if (pm.currentTransaction().isActive())
350                 pm.currentTransaction().rollback();
351             pm.close();
352         }
353     }
354     
355     /**
356      * Test the detach method with inheritance.
357      */

358     public void testDetachInherited() {
359         logger.log(BasicLevel.DEBUG, "***************testDetachInherited*****************");
360         Car c = new Car("r5", 4, "red");
361         FormulaOne f = new FormulaOne("williams", 4, "green", 262);
362     
363         PersistenceManager pm = pmf.getPersistenceManager();
364         pm.currentTransaction().begin();
365         logger.log(BasicLevel.DEBUG, "make persistent the car " + c.toString() +
366             " and the formula one " + f.toString());
367         pm.makePersistent(c);
368         pm.makePersistent(f);
369         pm.currentTransaction().commit();
370         try {
371             //detach the car c
372
Car copyOfC = (Car) pm.detachCopy(c);
373             assertNotNull(copyOfC);
374             assertEquals(c.getName(), copyOfC.getName());
375             assertEquals(c.getColor(), copyOfC.getColor());
376             assertEquals(c.getNbOfWheels(), copyOfC.getNbOfWheels());
377             assertEquals(c.getType(), copyOfC.getType());
378             // print the car out
379
logger.log(BasicLevel.DEBUG, copyOfC.toString());
380             //detach the formula one f
381
FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
382             assertNotNull(copyOfF);
383             assertEquals(f.getName(), copyOfF.getName());
384             assertEquals(f.getColor(), copyOfF.getColor());
385             assertEquals(f.getNbOfWheels(), copyOfF.getNbOfWheels());
386             assertEquals(f.getType(), copyOfF.getType());
387             assertEquals(f.getSpeedMax(), copyOfF.getSpeedMax());
388             //print the formula one out
389
logger.log(BasicLevel.DEBUG, copyOfF.toString());
390         } catch(Exception JavaDoc e) {
391             fail(e.getMessage());
392         } finally {
393             if (pm.currentTransaction().isActive())
394                 pm.currentTransaction().rollback();
395             pm.close();
396         }
397     }
398
399     /**
400       * Test the detach method with a non persistent object which is inherited from a super class: the object is made persistent before being detached.
401       */

402     public void testDetachNonPersistentInherited() {
403         logger.log(BasicLevel.DEBUG, "*************testDetachNonPersistentInherited*****************");
404         FormulaOne f = new FormulaOne("sauber", 4, "blue", 274);
405         PersistenceManager pm = pmf.getPersistenceManager();
406         pm.currentTransaction().begin();
407         // detach the formula one f while it is not persistent
408
FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
409         assertTrue(((SpeedoProxy) f).jdoIsPersistent());
410         pm.currentTransaction().commit();
411         try {
412             assertNotNull(copyOfF);
413             assertEquals(f.getName(), copyOfF.getName());
414             assertEquals(f.getColor(), copyOfF.getColor());
415             assertEquals(f.getNbOfWheels(), copyOfF.getNbOfWheels());
416             assertEquals(f.getType(), copyOfF.getType());
417             assertEquals(f.getSpeedMax(), copyOfF.getSpeedMax());
418             //print the team out
419
logger.log(BasicLevel.DEBUG, copyOfF.toString());
420         } catch(Exception JavaDoc e) {
421             fail(e.getMessage());
422         } finally {
423             if (pm.currentTransaction().isActive())
424                 pm.currentTransaction().rollback();
425             pm.close();
426         }
427     }
428
429     /**
430       * Test the detach method with a persistent-deleted inherited object: an exception must be thrown.
431       */

432     public void testDetachPersistentDeletedInherited() {
433         logger.log(BasicLevel.DEBUG, "******************testDetachPersistentDeletedInherited****************");
434         FormulaOne f = new FormulaOne("honda", 4, "yellow", 240);
435         
436         PersistenceManager pm = pmf.getPersistenceManager();
437         //make persistent
438
pm.currentTransaction().begin();
439         pm.makePersistent(f);
440         pm.currentTransaction().commit();
441         //delete
442
pm.currentTransaction().begin();
443         pm.deletePersistent(f);
444         try{
445             //detach the coach c while it is persistent-deleted: an exception must be thrown
446
pm.detachCopy(f);
447         } catch(Exception JavaDoc e){
448             assertEquals("Wrong exception thrown: ",JDOUserException.class, e.getClass());
449         } finally {
450             if (pm.currentTransaction().isActive())
451                 pm.currentTransaction().rollback();
452             pm.close();
453         }
454     }
455     
456     
457     /**
458       * Test the detach method with a persistent-dirty inherited object: the object is flushed before being detached.
459       */

460     public void testDetachPersistentDirtyInherited() {
461         logger.log(BasicLevel.DEBUG, "******************testDetachPersistentDirtyInherited****************");
462         FormulaOne f = new FormulaOne("ferrari", 4, "yellow", 240);
463         PersistenceManager pm = pmf.getPersistenceManager();
464         //make persistent
465
pm.currentTransaction().begin();
466         pm.makePersistent(f);
467         pm.currentTransaction().commit();
468         //update and detach
469
pm.currentTransaction().begin();
470         f.setNbOfWheels(3);
471         f.setSpeedMax(300);
472         pm.currentTransaction().commit();
473         try {
474             //detach the formula one while it is persistent-dirty: the object must be flushed before being detached
475
FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
476             assertEquals(f.getName(), copyOfF.getName());
477             assertEquals(f.getColor(), copyOfF.getColor());
478             assertEquals(3, copyOfF.getNbOfWheels());
479             assertEquals(f.getType(), copyOfF.getType());
480             assertEquals(300, copyOfF.getSpeedMax());
481             //print the formula one out
482
logger.log(BasicLevel.DEBUG, copyOfF.toString());
483         } catch(Exception JavaDoc e) {
484             fail(e.getMessage());
485         } finally {
486             if (pm.currentTransaction().isActive())
487                 pm.currentTransaction().rollback();
488             pm.close();
489         }
490     }
491     
492     /**
493       * Test the detach method with a persistent-new inherited object: the object is flushed before being detached.
494       */

495     public void testDetachPersistentNewInherited() {
496         logger.log(BasicLevel.DEBUG, "******************testDetachPersistentNewInherited****************");
497         FormulaOne f = new FormulaOne("renault", 4, "yellow", 260);
498         
499         PersistenceManager pm = pmf.getPersistenceManager();
500         pm.currentTransaction().begin();
501         pm.makePersistent(f);
502         pm.currentTransaction().commit();
503         try {
504             //detach the formula one while it is persistent-new: the object must be flushed before being detached
505
FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
506             assertEquals(f.getName(), copyOfF.getName());
507             assertEquals(f.getColor(), copyOfF.getColor());
508             assertEquals(f.getNbOfWheels(), copyOfF.getNbOfWheels());
509             assertEquals(f.getType(), copyOfF.getType());
510             assertEquals(f.getSpeedMax(), copyOfF.getSpeedMax());
511             // print the formula one out
512
logger.log(BasicLevel.DEBUG, copyOfF.toString());
513         } catch(Exception JavaDoc e) {
514             fail(e.getMessage());
515         } finally {
516             if (pm.currentTransaction().isActive())
517                 pm.currentTransaction().rollback();
518             pm.close();
519         }
520     }
521     
522     public void testRemovingOfPersistentObject() {
523         PersistenceManager pm = pmf.getPersistenceManager();
524         try {
525             Class JavaDoc[] cs = new Class JavaDoc[]{Coach.class, Player.class, Team.class, Vehicle.class};
526             pm.currentTransaction().begin();
527             for(int i=0; i<cs.length; i++) {
528                 Query query = pm.newQuery(cs[i]);
529                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
530                 Iterator JavaDoc it = col.iterator();
531                 while(it.hasNext()) {
532                     Object JavaDoc o = it.next();
533                     Assert.assertNotNull("null object in the query result"
534                         + cs[i].getName(), o);
535                     pm.deletePersistent(o);
536
537                 }
538                 query.close(col);
539             }
540             pm.currentTransaction().commit();
541         } catch (JDOException e) {
542             Exception JavaDoc ie = ExceptionHelper.getNested(e);
543             logger.log(BasicLevel.ERROR, "", ie);
544             fail(ie.getMessage());
545         } finally {
546             if (pm.currentTransaction().isActive())
547                 pm.currentTransaction().rollback();
548             pm.close();
549         }
550     }
551 }
552
Popular Tags