Source Code Cross Referenced for TestDetach.java in  » Database-ORM » Speedo_1.4.5 » org » objectweb » speedo » runtime » detach » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » Speedo_1.4.5 » org.objectweb.speedo.runtime.detach 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        import java.util.List;
030:        import java.util.Map;
031:
032:        import javax.jdo.Extent;
033:        import javax.jdo.JDOException;
034:        import javax.jdo.JDOUserException;
035:        import javax.jdo.PersistenceManager;
036:        import javax.jdo.Query;
037:
038:        import junit.framework.Assert;
039:
040:        import org.objectweb.speedo.SpeedoTestHelper;
041:        import org.objectweb.speedo.api.ExceptionHelper;
042:        import org.objectweb.speedo.mim.jdo.api.JDOPersistentObjectItf;
043:        import org.objectweb.speedo.pobjects.detach.Car;
044:        import org.objectweb.speedo.pobjects.detach.Coach;
045:        import org.objectweb.speedo.pobjects.detach.FormulaOne;
046:        import org.objectweb.speedo.pobjects.detach.Player;
047:        import org.objectweb.speedo.pobjects.detach.Team;
048:        import org.objectweb.speedo.pobjects.detach.Vehicle;
049:        import org.objectweb.speedo.pobjects.map.C;
050:        import org.objectweb.speedo.pobjects.map.D;
051:        import org.objectweb.util.monolog.api.BasicLevel;
052:
053:        /**
054:         * @author Y.Bersihand
055:         */
056:        public class TestDetach extends SpeedoTestHelper {
057:
058:            public TestDetach(String s) {
059:                super (s);
060:            }
061:
062:            protected String getLoggerName() {
063:                return LOG_NAME + ".rt.detach.TestDetach";
064:            }
065:
066:            /**
067:             * Test the deatch on an object which references a map.
068:             */
069:            public void testDetachMap() {
070:                logger.log(BasicLevel.DEBUG,
071:                        "***************testDetachMap*****************");
072:                int MAP_SIZE = 10;
073:                String F1_PREFIX = "f1_";
074:                PersistenceManager pm = pmf.getPersistenceManager();
075:
076:                //Create  instances
077:                pm.currentTransaction().begin();
078:                C c = new C(1);
079:                ArrayList ds = new ArrayList(MAP_SIZE * 2);
080:                for (int i = 0; i < MAP_SIZE; i++) {
081:                    String f1 = F1_PREFIX + i;
082:                    ds.add(new D(i, f1));
083:                }
084:                pm.makePersistent(c);
085:                pm.makePersistentAll(ds);
086:                pm.currentTransaction().commit();
087:
088:                //Add into the map
089:                pm.currentTransaction().begin();
090:                Map m = c.getDkey2d();
091:                for (int i = 0; i < MAP_SIZE; i++) {
092:                    D d = (D) ds.get(i);
093:                    m.put(d.getF1(), d);
094:                }
095:                pm.currentTransaction().commit();
096:
097:                pm.getFetchPlan().addGroup("all").removeGroup("default");
098:                C copyOfC = (C) pm.detachCopy(c);
099:                assertEquals("Not the same id for c and its detached copy", c
100:                        .getMyid(), copyOfC.getMyid());
101:                Map mC = c.getDkey2d();
102:                Map mCopyOfC = copyOfC.getDkey2d();
103:                assertEquals("The maps' size should be the same.", mC.size(),
104:                        mCopyOfC.size());
105:                Iterator itC = mC.values().iterator();
106:                Collection col = mCopyOfC.values();
107:                while (itC.hasNext()) {
108:                    D dC = (D) itC.next();
109:                    boolean contained = false;
110:                    Iterator it = col.iterator();
111:                    while (it.hasNext() && !contained) {
112:                        D dCopy = (D) it.next();
113:                        if (dC.getMyid() == dCopy.getMyid()
114:                                && dC.getF1().equals(dCopy.getF1()))
115:                            contained = true;
116:                    }
117:                    assertTrue("The element " + dC.getMyid()
118:                            + " is not contained", contained);
119:                }
120:            }
121:
122:            /**
123:             * Test the detach method: make an object persistent, and detach it out of an active transaction.
124:             */
125:            public void testDetachCopy() {
126:                logger.log(BasicLevel.DEBUG,
127:                        "***************testDetachCopy*****************");
128:                Team t = new Team("Bordeaux", null, null);
129:                Collection players = new ArrayList();
130:                Player p1 = new Player("p1", t, 25);
131:                players.add(p1);
132:                Player p2 = new Player("p2", t, 32);
133:                players.add(p2);
134:                t.setPlayers(players);
135:                Coach c = new Coach("c1", 5, t);
136:                t.setCoach(c);
137:
138:                PersistenceManager pm = pmf.getPersistenceManager();
139:                pm.currentTransaction().begin();
140:                logger.log(BasicLevel.DEBUG, "make persistent the team "
141:                        + t.toString());
142:                pm.makePersistent(t);
143:                pm.currentTransaction().commit();
144:
145:                t = null;
146:                p1 = null;
147:                p2 = null;
148:                c = null;
149:
150:                pm.evictAll();
151:                pm.close();
152:
153:                pm = pmf.getPersistenceManager();
154:                Query query = pm.newQuery(Team.class,
155:                        "players.contains(player) & player.age<12");
156:                query.declareVariables("Player player");
157:                Collection results = (Collection) query.execute();
158:                assertTrue("The result of the query should be empty.", results
159:                        .isEmpty());
160:                query.closeAll();
161:
162:                query = pm.newQuery(Team.class,
163:                        "players.contains(player) & player.age>12");
164:                query.declareVariables("Player player");
165:                results = (Collection) query.execute();
166:                assertTrue("The result of the query shouldn't be empty.",
167:                        !results.isEmpty());
168:                Team res = (Team) results.iterator().next();
169:                Collection cpl = res.getPlayers();
170:                Iterator itPlayer = cpl.iterator();
171:                while (itPlayer.hasNext()) {
172:                    Player pl = (Player) itPlayer.next();
173:                    assertTrue(
174:                            "The age of the player should be over 12. It is "
175:                                    + pl.getAge() + ".", pl.getAge() > 12);
176:                }
177:                query.closeAll();
178:
179:                query = pm.newQuery(Team.class, "town==\"Bordeaux\"");
180:                results = (Collection) query.execute();
181:                assertEquals("The result of the query shouldn't be empty.",
182:                        false, results.isEmpty());
183:                Team toDetach = (Team) results.iterator().next();
184:                query.closeAll();
185:
186:                //detach the team t
187:                Team copyOfT = (Team) pm.detachCopy(toDetach);
188:                try {
189:                    assertNotNull(copyOfT);
190:                    assertEquals(
191:                            "Town of team and detached team are not the same",
192:                            toDetach.getTown(), copyOfT.getTown());
193:                    assertEquals(
194:                            "Coach experience of team and detached team are not the same",
195:                            toDetach.getCoach().getExperience(), copyOfT
196:                                    .getCoach().getExperience());
197:                    assertEquals(
198:                            "Coach name of team and detached team are not the same",
199:                            toDetach.getCoach().getName(), copyOfT.getCoach()
200:                                    .getName());
201:                    //print the team out
202:                    logger.log(BasicLevel.DEBUG, copyOfT.toString());
203:                } catch (Exception e) {
204:                    fail(e.getMessage());
205:                } finally {
206:                    if (pm.currentTransaction().isActive())
207:                        pm.currentTransaction().rollback();
208:                    pm.close();
209:                }
210:            }
211:
212:            /**
213:             * Test the detach method with a non persistent object: the object is made persistent before being detached. 
214:             */
215:            public void testDetachNonPersistentCopy() {
216:                logger
217:                        .log(BasicLevel.DEBUG,
218:                                "*************testDetachNonPersistentCopy*****************");
219:                Team t = new Team("Marseille", null, null);
220:                Coach c = new Coach("c2", 5, t);
221:                t.setCoach(c);
222:                Player p = new Player("p3", t, 25);
223:                t.addPlayer(p);
224:                PersistenceManager pm = pmf.getPersistenceManager();
225:                pm.currentTransaction().begin();
226:                //detach the team t while it is not persistent
227:                Team copyOfT = (Team) pm.detachCopy(t);
228:                pm.currentTransaction().commit();
229:                try {
230:                    assertNotNull(copyOfT);
231:                    assertTrue(((JDOPersistentObjectItf) t).jdoIsPersistent());
232:                    //	print the team out
233:                    logger.log(BasicLevel.DEBUG, copyOfT.toString());
234:                } catch (Exception e) {
235:                    fail(e.getMessage());
236:                } finally {
237:                    if (pm.currentTransaction().isActive())
238:                        pm.currentTransaction().rollback();
239:                    pm.close();
240:                }
241:            }
242:
243:            /**
244:             * Test the detach method with a persistent-new object: the object is flushed before being detached. 
245:             */
246:            public void testDetachPersistentNew() {
247:                logger
248:                        .log(BasicLevel.DEBUG,
249:                                "******************testDetachPersistentNew****************");
250:                Team t = new Team("Monaco", null, null);
251:                Coach c = new Coach("c3", 5, t);
252:                t.setCoach(c);
253:                Player p = new Player("p4", t, 25);
254:                t.addPlayer(p);
255:
256:                PersistenceManager pm = pmf.getPersistenceManager();
257:                pm.currentTransaction().begin();
258:                pm.makePersistent(c);
259:                //detach the team while it is persistent-new: the object must be flushed before being detached
260:                Team copyOfT = (Team) pm.detachCopy(t);
261:                try {
262:                    assertNotNull(copyOfT);
263:                    assertEquals(
264:                            "Town of team and detached team are not the same",
265:                            t.getTown(), copyOfT.getTown());
266:                    assertEquals(
267:                            "Coach experience of team and detached team are not the same",
268:                            t.getCoach().getExperience(), copyOfT.getCoach()
269:                                    .getExperience());
270:                    assertEquals(
271:                            "Coach name of team and detached team are not the same",
272:                            t.getCoach().getName(), copyOfT.getCoach()
273:                                    .getName());
274:                    pm.currentTransaction().commit();
275:                    //	print the team out
276:                    logger.log(BasicLevel.DEBUG, copyOfT.toString());
277:                } catch (Exception e) {
278:                    fail(e.getMessage());
279:                } finally {
280:                    if (pm.currentTransaction().isActive())
281:                        pm.currentTransaction().rollback();
282:                    pm.close();
283:                }
284:            }
285:
286:            /**
287:             * Test the detach method with a persistent-dirty object: the object is flushed before being detached. 
288:             */
289:            public void testDetachPersistentDirty() {
290:                logger
291:                        .log(BasicLevel.DEBUG,
292:                                "******************testDetachPersistentDirty****************");
293:                Team t = new Team("Nantes", null, null);
294:                Coach c = new Coach("c4", 5, t);
295:                t.setCoach(c);
296:                Player p = new Player("p5", t, 28);
297:                t.addPlayer(p);
298:
299:                PersistenceManager pm = pmf.getPersistenceManager();
300:                //make persistent
301:                pm.currentTransaction().begin();
302:                pm.makePersistent(c);
303:                pm.currentTransaction().commit();
304:                //update and detach
305:                pm.currentTransaction().begin();
306:                c.setExperience(10);
307:                //detach the team while it is persistent-dirty: the object must be flushed before being detached
308:                Team copyOfT = (Team) pm.detachCopy(t);
309:                pm.currentTransaction().commit();
310:                try {
311:                    assertEquals(10, copyOfT.getCoach().getExperience());
312:                    logger.log(BasicLevel.DEBUG, copyOfT.toString());
313:                } catch (Exception e) {
314:                    fail(e.getMessage());
315:                } finally {
316:                    if (pm.currentTransaction().isActive())
317:                        pm.currentTransaction().rollback();
318:                    pm.close();
319:                }
320:            }
321:
322:            /**
323:             * Test the detach method with a persistent-deleted object: an exception must be thrown. 
324:             */
325:            public void testDetachPersistentDeleted() {
326:                logger
327:                        .log(BasicLevel.DEBUG,
328:                                "******************testDetachPersistentDeleted****************");
329:                Team t = new Team("Paris", null, null);
330:                Coach c = new Coach("c5", 5, t);
331:                t.setCoach(c);
332:                Player p = new Player("p6", t, 28);
333:                t.addPlayer(p);
334:
335:                PersistenceManager pm = pmf.getPersistenceManager();
336:                //make persistent
337:                pm.currentTransaction().begin();
338:                pm.makePersistent(c);
339:                pm.currentTransaction().commit();
340:                //delete
341:                pm.currentTransaction().begin();
342:                pm.deletePersistent(c);
343:                try {
344:                    //detach the coach c while it is persistent-deleted: an exception must be thrown
345:                    pm.detachCopy(c);
346:                    pm.currentTransaction().commit();
347:                } catch (Exception e) {
348:
349:                    assertEquals("Wrong exception thrown: " + e.getMessage(),
350:                            JDOUserException.class, e.getClass());
351:                } finally {
352:                    if (pm.currentTransaction().isActive()) {
353:                        pm.currentTransaction().commit();
354:                    }
355:                    pm.close();
356:                }
357:            }
358:
359:            /**
360:             * Test the detach method with a persistent-deleted object within a collection: an exception must be thrown. 
361:             */
362:            public void testDetachPersistentDeletedCollection() {
363:                logger
364:                        .log(BasicLevel.DEBUG,
365:                                "******************testDetachPersistentDeletedCollection****************");
366:                Team t = new Team("Auxerre", null, null);
367:                Coach c = new Coach("c6", 10, t);
368:                t.setCoach(c);
369:                Player p = new Player("p7", t, 28);
370:                t.addPlayer(p);
371:
372:                PersistenceManager pm = pmf.getPersistenceManager();
373:                //make persistent
374:                pm.currentTransaction().begin();
375:                pm.makePersistent(c);
376:                pm.currentTransaction().commit();
377:                //delete
378:                pm.currentTransaction().begin();
379:                pm.deletePersistent(p);
380:                try {
381:                    // detach the coach c while it is persistent-deleted: 
382:                    // an exception must be thrown
383:                    pm.detachCopy(c);
384:                    pm.currentTransaction().commit();
385:                } catch (Exception e) {
386:                    assertEquals("Wrong exception thrown: " + e.getMessage(),
387:                            JDOUserException.class, e.getClass());
388:                } finally {
389:                    if (pm.currentTransaction().isActive()) {
390:                        pm.currentTransaction().commit();
391:                    }
392:                    pm.close();
393:                }
394:            }
395:
396:            /**
397:             * Test the detach method: make an object persistent with a null refrence, and detach it out of an active transaction.
398:             */
399:            public void testDetachNullReference() {
400:                logger
401:                        .log(BasicLevel.DEBUG,
402:                                "***************testDetachNullReference*****************");
403:                Team t = new Team("Niort", null, null);
404:                Collection players = new ArrayList();
405:                Player p1 = new Player("p8", t, 25);
406:                players.add(p1);
407:                Player p2 = new Player("p9", t, 32);
408:                players.add(p2);
409:                t.setPlayers(players);
410:
411:                PersistenceManager pm = pmf.getPersistenceManager();
412:                pm.currentTransaction().begin();
413:                logger.log(BasicLevel.DEBUG, "make persistent the team "
414:                        + t.toString());
415:                pm.makePersistent(t);
416:                pm.currentTransaction().commit();
417:
418:                try {
419:                    Extent extent = pm.getExtent(Team.class, false);
420:                    Query query = pm.newQuery(extent, "town == townName");
421:                    query.declareParameters("String townName");
422:                    Collection collection = (Collection) query.execute(t
423:                            .getTown());
424:                    assertTrue("The result should not be empty", !collection
425:                            .isEmpty());
426:                    Iterator itr = collection.iterator();
427:                    Team copyOfT = null;
428:                    while (itr.hasNext()) {
429:                        Team team = (Team) itr.next();
430:                        assertEquals("The town should be " + t.getTown(), t
431:                                .getTown(), team.getTown());
432:                        //detach the team t
433:                        copyOfT = (Team) pm.detachCopy(team);
434:                    }
435:                    query.close(collection);
436:                    extent.closeAll();
437:                    assertNotNull(copyOfT);
438:                    assertEquals(
439:                            "Town of team and detached team are not the same",
440:                            t.getTown(), copyOfT.getTown());
441:                    assertNull("The coach reference is supposed to be null",
442:                            copyOfT.getCoach());
443:                } catch (Exception e) {
444:                    fail(e.getMessage());
445:                } finally {
446:                    if (pm.currentTransaction().isActive())
447:                        pm.currentTransaction().rollback();
448:                    pm.close();
449:                }
450:            }
451:
452:            /**
453:             * Test the makePersistent on an object referencing a detached one 
454:             */
455:            public void testNewReferencingDetached() {
456:                PersistenceManager pm = pmf.getPersistenceManager();
457:                try {
458:                    logger
459:                            .log(BasicLevel.DEBUG,
460:                                    "******************testNewReferencingDetached****************");
461:                    Coach c = new Coach("cXX", 10, null);
462:                    //make persistent the coach
463:                    pm.currentTransaction().begin();
464:                    pm.makePersistent(c);
465:                    pm.currentTransaction().commit();
466:                    //detach a copy
467:                    Coach copyOfC = (Coach) pm.detachCopy(c);
468:                    //create a team referencing the detached coach
469:                    Team t = new Team("LeMans", null, null);
470:                    t.setCoach(copyOfC);
471:                    //make persistent the team
472:                    pm.currentTransaction().begin();
473:                    pm.makePersistent(t);
474:                    pm.currentTransaction().commit();
475:                    assertNotNull(t.getCoach());
476:                    assertEquals(t.getCoach().getName(), c.getName());
477:                } catch (Exception e) {
478:                    fail(e.getMessage());
479:                } finally {
480:                    if (pm.currentTransaction().isActive())
481:                        pm.currentTransaction().rollback();
482:                    pm.close();
483:                }
484:            }
485:
486:            /**
487:             * Test the detach method with inheritance.
488:             */
489:            public void testDetachInherited() {
490:                logger.log(BasicLevel.DEBUG,
491:                        "***************testDetachInherited*****************");
492:                Car c = new Car("r5", 4, "red");
493:                FormulaOne f = new FormulaOne("williams", 4, "green", 262);
494:
495:                PersistenceManager pm = pmf.getPersistenceManager();
496:                pm.currentTransaction().begin();
497:                logger
498:                        .log(BasicLevel.DEBUG, "make persistent the car "
499:                                + c.toString() + " and the formula one "
500:                                + f.toString());
501:                pm.makePersistent(c);
502:                pm.makePersistent(f);
503:                pm.currentTransaction().commit();
504:                try {
505:                    //detach the car c
506:                    Car copyOfC = (Car) pm.detachCopy(c);
507:                    assertNotNull(copyOfC);
508:                    assertEquals(c.getName(), copyOfC.getName());
509:                    assertEquals(c.getColor(), copyOfC.getColor());
510:                    assertEquals(c.getNbOfWheels(), copyOfC.getNbOfWheels());
511:                    assertEquals(c.getType(), copyOfC.getType());
512:                    //	print the car out
513:                    logger.log(BasicLevel.DEBUG, copyOfC.toString());
514:                    //detach the formula one f
515:                    FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
516:                    assertNotNull(copyOfF);
517:                    assertEquals(f.getName(), copyOfF.getName());
518:                    assertEquals(f.getColor(), copyOfF.getColor());
519:                    assertEquals(f.getNbOfWheels(), copyOfF.getNbOfWheels());
520:                    assertEquals(f.getType(), copyOfF.getType());
521:                    assertEquals(f.getSpeedMax(), copyOfF.getSpeedMax());
522:                    //print the formula one out
523:                    logger.log(BasicLevel.DEBUG, copyOfF.toString());
524:                } catch (Exception e) {
525:                    fail(e.getMessage());
526:                } finally {
527:                    if (pm.currentTransaction().isActive())
528:                        pm.currentTransaction().rollback();
529:                    pm.close();
530:                }
531:            }
532:
533:            /**
534:             * Test the detach method with a non persistent object which is inherited from a super class: the object is made persistent before being detached. 
535:             */
536:            public void testDetachNonPersistentInherited() {
537:                logger
538:                        .log(BasicLevel.DEBUG,
539:                                "*************testDetachNonPersistentInherited*****************");
540:                FormulaOne f = new FormulaOne("sauber", 4, "blue", 274);
541:                PersistenceManager pm = pmf.getPersistenceManager();
542:                pm.currentTransaction().begin();
543:                //		detach the formula one f while it is not persistent
544:                FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
545:                assertTrue(((JDOPersistentObjectItf) f).jdoIsPersistent());
546:                pm.currentTransaction().commit();
547:                try {
548:                    assertNotNull(copyOfF);
549:                    assertEquals(f.getName(), copyOfF.getName());
550:                    assertEquals(f.getColor(), copyOfF.getColor());
551:                    assertEquals(f.getNbOfWheels(), copyOfF.getNbOfWheels());
552:                    assertEquals(f.getType(), copyOfF.getType());
553:                    assertEquals(f.getSpeedMax(), copyOfF.getSpeedMax());
554:                    //print the team out
555:                    logger.log(BasicLevel.DEBUG, copyOfF.toString());
556:                } catch (Exception e) {
557:                    fail(e.getMessage());
558:                } finally {
559:                    if (pm.currentTransaction().isActive())
560:                        pm.currentTransaction().rollback();
561:                    pm.close();
562:                }
563:            }
564:
565:            /**
566:             * Test the detach method with a persistent-deleted inherited object: an exception must be thrown. 
567:             */
568:            public void testDetachPersistentDeletedInherited() {
569:                logger
570:                        .log(BasicLevel.DEBUG,
571:                                "******************testDetachPersistentDeletedInherited****************");
572:                FormulaOne f = new FormulaOne("honda", 4, "yellow", 240);
573:
574:                PersistenceManager pm = pmf.getPersistenceManager();
575:                //make persistent
576:                pm.currentTransaction().begin();
577:                pm.makePersistent(f);
578:                pm.currentTransaction().commit();
579:                //delete
580:                pm.currentTransaction().begin();
581:                pm.deletePersistent(f);
582:                try {
583:                    //detach the coach c while it is persistent-deleted: an exception must be thrown
584:                    pm.detachCopy(f);
585:                } catch (Exception e) {
586:                    assertEquals("Wrong exception thrown: ",
587:                            JDOUserException.class, e.getClass());
588:                } finally {
589:                    if (pm.currentTransaction().isActive())
590:                        pm.currentTransaction().rollback();
591:                    pm.close();
592:                }
593:            }
594:
595:            /**
596:             * Test the detach method with a persistent-dirty inherited object: the object is flushed before being detached. 
597:             */
598:            public void testDetachPersistentDirtyInherited() {
599:                logger
600:                        .log(BasicLevel.DEBUG,
601:                                "******************testDetachPersistentDirtyInherited****************");
602:                FormulaOne f = new FormulaOne("ferrari", 4, "yellow", 240);
603:                PersistenceManager pm = pmf.getPersistenceManager();
604:                //make persistent
605:                pm.currentTransaction().begin();
606:                pm.makePersistent(f);
607:                pm.currentTransaction().commit();
608:                //update and detach
609:                pm.currentTransaction().begin();
610:                f.setNbOfWheels(3);
611:                f.setSpeedMax(300);
612:                pm.currentTransaction().commit();
613:                try {
614:                    //detach the formula one while it is persistent-dirty: the object must be flushed before being detached
615:                    FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
616:                    assertEquals(f.getName(), copyOfF.getName());
617:                    assertEquals(f.getColor(), copyOfF.getColor());
618:                    assertEquals(3, copyOfF.getNbOfWheels());
619:                    assertEquals(f.getType(), copyOfF.getType());
620:                    assertEquals(300, copyOfF.getSpeedMax());
621:                    //print the formula one out
622:                    logger.log(BasicLevel.DEBUG, copyOfF.toString());
623:                } catch (Exception e) {
624:                    fail(e.getMessage());
625:                } finally {
626:                    if (pm.currentTransaction().isActive())
627:                        pm.currentTransaction().rollback();
628:                    pm.close();
629:                }
630:            }
631:
632:            /**
633:             * Test the detach method with a persistent-new inherited object: the object is flushed before being detached. 
634:             */
635:            public void testDetachPersistentNewInherited() {
636:                logger
637:                        .log(BasicLevel.DEBUG,
638:                                "******************testDetachPersistentNewInherited****************");
639:                FormulaOne f = new FormulaOne("renault", 4, "yellow", 260);
640:
641:                PersistenceManager pm = pmf.getPersistenceManager();
642:                pm.currentTransaction().begin();
643:                pm.makePersistent(f);
644:                pm.currentTransaction().commit();
645:                try {
646:                    //detach the formula one while it is persistent-new: the object must be flushed before being detached
647:                    FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
648:                    assertEquals(f.getName(), copyOfF.getName());
649:                    assertEquals(f.getColor(), copyOfF.getColor());
650:                    assertEquals(f.getNbOfWheels(), copyOfF.getNbOfWheels());
651:                    assertEquals(f.getType(), copyOfF.getType());
652:                    assertEquals(f.getSpeedMax(), copyOfF.getSpeedMax());
653:                    //	print the formula one out
654:                    logger.log(BasicLevel.DEBUG, copyOfF.toString());
655:                } catch (Exception e) {
656:                    fail(e.getMessage());
657:                } finally {
658:                    if (pm.currentTransaction().isActive())
659:                        pm.currentTransaction().rollback();
660:                    pm.close();
661:                }
662:            }
663:
664:            public void testRemovingOfPersistentObject() {
665:                PersistenceManager pm = pmf.getPersistenceManager();
666:                try {
667:                    Class[] cs = new Class[] { Coach.class, Player.class,
668:                            Team.class, Vehicle.class };
669:                    pm.currentTransaction().begin();
670:                    for (int i = 0; i < cs.length; i++) {
671:                        Query query = pm.newQuery(cs[i]);
672:                        Collection col = (Collection) query.execute();
673:                        Iterator it = col.iterator();
674:                        while (it.hasNext()) {
675:                            Object o = it.next();
676:                            Assert.assertNotNull(
677:                                    "null object in the query result"
678:                                            + cs[i].getName(), o);
679:                            pm.deletePersistent(o);
680:
681:                        }
682:                        query.close(col);
683:                    }
684:                    pm.currentTransaction().commit();
685:                } catch (JDOException e) {
686:                    Exception ie = ExceptionHelper.getNested(e);
687:                    logger.log(BasicLevel.ERROR, "", ie);
688:                    fail(ie.getMessage());
689:                } finally {
690:                    if (pm.currentTransaction().isActive())
691:                        pm.currentTransaction().rollback();
692:                    pm.close();
693:                }
694:            }
695:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.