Source Code Cross Referenced for TestAttach.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:
030:        import javax.jdo.JDOException;
031:        import javax.jdo.PersistenceManager;
032:        import javax.jdo.Query;
033:
034:        import junit.framework.Assert;
035:
036:        import org.objectweb.speedo.SpeedoTestHelper;
037:        import org.objectweb.speedo.api.ExceptionHelper;
038:        import org.objectweb.speedo.mim.jdo.api.JDOPersistentObjectItf;
039:        import org.objectweb.speedo.pobjects.detach.Car;
040:        import org.objectweb.speedo.pobjects.detach.Coach;
041:        import org.objectweb.speedo.pobjects.detach.FormulaOne;
042:        import org.objectweb.speedo.pobjects.detach.Player;
043:        import org.objectweb.speedo.pobjects.detach.Team;
044:        import org.objectweb.speedo.pobjects.detach.Vehicle;
045:        import org.objectweb.util.monolog.api.BasicLevel;
046:
047:        /**
048:         * @author Y.Bersihand
049:         */
050:        public class TestAttach extends SpeedoTestHelper {
051:
052:            public TestAttach(String s) {
053:                super (s);
054:            }
055:
056:            protected String getLoggerName() {
057:                return LOG_NAME + ".rt.detach.TestAttach";
058:            }
059:
060:            /**
061:             * Test the attach method: make an object persistent, detach it then attach it.
062:             */
063:            public void testAttachCopy() {
064:                logger.log(BasicLevel.DEBUG,
065:                        "***************testAttachCopy*****************");
066:                Team t = new Team("Bordeaux", null, null);
067:                Collection players = new ArrayList();
068:                Player p1 = new Player("p1", t, 25);
069:                players.add(p1);
070:                Player p2 = new Player("p2", t, 32);
071:                players.add(p2);
072:                t.setPlayers(players);
073:                Coach c = new Coach("c1", 5, t);
074:                t.setCoach(c);
075:
076:                PersistenceManager pm = pmf.getPersistenceManager();
077:                pm.currentTransaction().begin();
078:                logger.log(BasicLevel.DEBUG, "make persistent the team "
079:                        + t.toString());
080:                pm.makePersistent(t);
081:                pm.currentTransaction().commit();
082:                try {
083:                    //detach the team t
084:                    Team copyOfT = (Team) pm.detachCopy(t);
085:                    assertNotNull(copyOfT);
086:                    //print the team out
087:                    logger.log(BasicLevel.DEBUG, copyOfT.toString());
088:                    pm.currentTransaction().begin();
089:                    //attach the team t
090:                    Team attachedTeam = (Team) pm.makePersistent(copyOfT);
091:                    assertEquals(copyOfT.getTown(), attachedTeam.getTown());
092:                    assertEquals(copyOfT.getCoach().getExperience(),
093:                            attachedTeam.getCoach().getExperience());
094:                    assertEquals(copyOfT.getCoach().getName(), attachedTeam
095:                            .getCoach().getName());
096:                    pm.currentTransaction().commit();
097:                    logger.log(BasicLevel.DEBUG,
098:                            "The attached version of the team is as follows:\n "
099:                                    + attachedTeam.toString());
100:                } catch (Exception e) {
101:                    fail(e.getMessage());
102:                } finally {
103:                    if (pm.currentTransaction().isActive())
104:                        pm.currentTransaction().rollback();
105:                    pm.close();
106:                }
107:            }
108:
109:            /**
110:             * Test the attach method with update: make an object persistent, detach it , modify it and then attach it.
111:             */
112:            public void testAttachModifiedCopy() {
113:                logger
114:                        .log(BasicLevel.DEBUG,
115:                                "***************testAttachModifiedCopy*****************");
116:                Team t = new Team("Paris", null, null);
117:                Collection players = new ArrayList();
118:                Player p1 = new Player("p3", t, 20);
119:                players.add(p1);
120:                Player p2 = new Player("p4", t, 30);
121:                players.add(p2);
122:                t.setPlayers(players);
123:                Coach c = new Coach("c2", 10, t);
124:                t.setCoach(c);
125:
126:                PersistenceManager pm = pmf.getPersistenceManager();
127:                pm.currentTransaction().begin();
128:                logger.log(BasicLevel.DEBUG, "make persistent the team "
129:                        + t.toString());
130:                pm.makePersistent(t);
131:                pm.currentTransaction().commit();
132:                //detach the team t
133:                Team copyOfT = (Team) pm.detachCopy(t);
134:                //modify the team
135:                copyOfT.getCoach().setExperience(99);
136:                //print the team out
137:                logger.log(BasicLevel.DEBUG, "Copy of team "
138:                        + copyOfT.toString());
139:                pm.currentTransaction().begin();
140:                //attach the team t
141:                Team attachedTeam = (Team) pm.makePersistent(copyOfT);
142:                try {
143:                    assertNotNull(attachedTeam);
144:                    logger.log(BasicLevel.DEBUG,
145:                            "The attached version of the team is as follows:\n "
146:                                    + attachedTeam.toString());
147:                    assertEquals(99, attachedTeam.getCoach().getExperience());
148:                    pm.currentTransaction().commit();
149:                } catch (Exception e) {
150:                    fail(e.getMessage());
151:                } finally {
152:                    if (pm.currentTransaction().isActive())
153:                        pm.currentTransaction().rollback();
154:                    pm.close();
155:                }
156:            }
157:
158:            /**
159:             * Test the attach method with an update on a reference: make an object persistent, detach it , modify it and then attach it.
160:             */
161:            public void testAttachModifiedReference() {
162:                logger
163:                        .log(BasicLevel.DEBUG,
164:                                "***************testAttachModifiedReference*****************");
165:                Team t = new Team("Lens", null, null);
166:                Collection players = new ArrayList();
167:                Player p1 = new Player("p21", t, 20);
168:                players.add(p1);
169:                Player p2 = new Player("p22", t, 30);
170:                players.add(p2);
171:                t.setPlayers(players);
172:                Coach c = new Coach("c23", 10, t);
173:                t.setCoach(c);
174:
175:                PersistenceManager pm = pmf.getPersistenceManager();
176:                pm.currentTransaction().begin();
177:                logger.log(BasicLevel.DEBUG, "make persistent the team "
178:                        + t.toString());
179:                pm.makePersistent(t);
180:                pm.currentTransaction().commit();
181:                try {
182:                    //detach the team t
183:                    Team copyOfT = (Team) pm.detachCopy(t);
184:                    Coach newCoach = new Coach("c33", 15, new Team("DummyTeam",
185:                            null, null));
186:                    //	update the reference while detached
187:                    copyOfT.setCoach(newCoach);
188:                    //print the team out
189:                    logger.log(BasicLevel.DEBUG, "Copy of team "
190:                            + copyOfT.toString());
191:                    pm.currentTransaction().begin();
192:                    //	attach the team t
193:                    Team attachedTeam = (Team) pm.makePersistent(copyOfT);
194:                    assertNotNull("attachedTeam should not be null",
195:                            attachedTeam);
196:                    logger.log(BasicLevel.DEBUG,
197:                            "The attached version of the team is as follows:\n "
198:                                    + attachedTeam.toString());
199:                    assertEquals(
200:                            "Coach of the attached team is not the good one",
201:                            newCoach, attachedTeam.getCoach());
202:                    pm.currentTransaction().commit();
203:                } catch (Exception e) {
204:                    e.printStackTrace();
205:                    fail(e.getMessage());
206:                } finally {
207:                    if (pm.currentTransaction().isActive())
208:                        pm.currentTransaction().rollback();
209:                    pm.close();
210:                }
211:            }
212:
213:            /**
214:             * Test the attach method with update on a collection: make an object persistent, detach it , modify it and then attach it.
215:             */
216:            public void testAttachModifiedCollectionCopy() {
217:                logger
218:                        .log(BasicLevel.DEBUG,
219:                                "***************testAttachModifiedCollectionCopy*****************");
220:                Team t = new Team("Nantes", null, null);
221:                Collection players = new ArrayList();
222:                Player p1 = new Player("p5", t, 20);
223:                players.add(p1);
224:                Player p2 = new Player("p6", t, 30);
225:                players.add(p2);
226:                t.setPlayers(players);
227:                Coach c = new Coach("c3", 10, t);
228:                t.setCoach(c);
229:
230:                PersistenceManager pm = pmf.getPersistenceManager();
231:                pm.currentTransaction().begin();
232:                logger.log(BasicLevel.DEBUG, "make persistent the team "
233:                        + t.toString());
234:                pm.makePersistent(t);
235:                pm.currentTransaction().commit();
236:                //detach the team t
237:                Team copyOfT = (Team) pm.detachCopy(t);
238:                pm.close();
239:                //modify the coach
240:                copyOfT.getCoach().setExperience(99);
241:                //and modify the collection
242:                Iterator it = copyOfT.getPlayers().iterator();
243:                while (it.hasNext()) {
244:                    Player p = (Player) it.next();
245:                    p.setAge(99);
246:                }
247:                //		print the team out
248:                logger.log(BasicLevel.DEBUG, "Copy of team "
249:                        + copyOfT.toString());
250:                PersistenceManager pm2 = pmf.getPersistenceManager();
251:                try {
252:                    pm2.currentTransaction().begin();
253:                    //attach the team t
254:                    Team attachedTeam = (Team) pm2.makePersistent(copyOfT);
255:
256:                    Iterator itP = attachedTeam.getPlayers().iterator();
257:                    while (itP.hasNext()) {
258:                        Player p = (Player) itP.next();
259:                        assertEquals(99, p.getAge());
260:                    }
261:                    pm2.currentTransaction().commit();
262:                    logger.log(BasicLevel.DEBUG,
263:                            "The attached version of the team is as follows:\n "
264:                                    + attachedTeam.toString());
265:                } catch (Exception e) {
266:                    fail(e.getMessage());
267:                } finally {
268:                    if (pm.currentTransaction().isActive())
269:                        pm.currentTransaction().rollback();
270:                    pm.close();
271:                    if (pm2.currentTransaction().isActive())
272:                        pm2.currentTransaction().rollback();
273:                    pm2.close();
274:                }
275:            }
276:
277:            /**
278:             * Test the attach method with a new object: the object is made persistent.
279:             */
280:            public void testAttachNewObject() {
281:                logger.log(BasicLevel.DEBUG,
282:                        "***************testAttachNewObject*****************");
283:                Team t = new Team("Bastia", null, null);
284:                Collection players = new ArrayList();
285:                Player p1 = new Player("p7", t, 20);
286:                players.add(p1);
287:                Player p2 = new Player("p8", t, 30);
288:                players.add(p2);
289:                t.setPlayers(players);
290:                Coach c = new Coach("c4", 10, t);
291:                t.setCoach(c);
292:
293:                PersistenceManager pm = pmf.getPersistenceManager();
294:                pm.currentTransaction().begin();
295:                logger.log(BasicLevel.DEBUG, "attach the team " + t.toString());
296:                Team attachedTeam = (Team) pm.makePersistent(t);
297:                pm.currentTransaction().commit();
298:                try {
299:                    assertTrue(((JDOPersistentObjectItf) attachedTeam)
300:                            .jdoIsPersistent());
301:                    logger.log(BasicLevel.DEBUG,
302:                            "The attached version of the team is as follows:\n "
303:                                    + attachedTeam.toString());
304:                } catch (Exception e) {
305:                    fail(e.getMessage());
306:                } finally {
307:                    if (pm.currentTransaction().isActive())
308:                        pm.currentTransaction().rollback();
309:                    pm.close();
310:                }
311:            }
312:
313:            /**
314:             * Test the attach method with an object having a null reference.
315:             */
316:            public void testAttachNullRef() {
317:                logger.log(BasicLevel.DEBUG,
318:                        "***************testAttachNullRef*****************");
319:                Team t = new Team("Istres", null, null);
320:                Collection players = new ArrayList();
321:                Player p1 = new Player("p9", t, 20);
322:                players.add(p1);
323:                Player p2 = new Player("p10", t, 30);
324:                players.add(p2);
325:                t.setPlayers(players);
326:                Coach c = new Coach("c5", 10, t);
327:                t.setCoach(c);
328:
329:                PersistenceManager pm = pmf.getPersistenceManager();
330:                pm.currentTransaction().begin();
331:                logger.log(BasicLevel.DEBUG, "make persistent the team "
332:                        + t.toString());
333:                pm.makePersistent(t);
334:                pm.currentTransaction().commit();
335:                try {
336:                    //detach the team t
337:                    Team copyOfT = (Team) pm.detachCopy(t);
338:                    assertNotNull(copyOfT);
339:                    assertNotNull("Coach of detached team should not be null.",
340:                            copyOfT.getCoach());
341:                    //set null for the coach
342:                    copyOfT.setCoach(null);
343:                    assertNull(copyOfT.getCoach());
344:                    //print the team out
345:                    logger.log(BasicLevel.DEBUG, copyOfT.toString());
346:                    pm.currentTransaction().begin();
347:                    //attach the team t
348:                    Team attachedTeam = (Team) pm.makePersistent(copyOfT);
349:                    assertEquals(copyOfT.getTown(), attachedTeam.getTown());
350:                    assertNull("Coach of attached team should be null",
351:                            attachedTeam.getCoach());
352:                    pm.currentTransaction().commit();
353:                    logger.log(BasicLevel.DEBUG,
354:                            "The attached version of the team is as follows:\n "
355:                                    + attachedTeam.toString());
356:                } catch (Exception e) {
357:                    fail(e.getMessage());
358:                } finally {
359:                    if (pm.currentTransaction().isActive())
360:                        pm.currentTransaction().rollback();
361:                    pm.close();
362:                }
363:            }
364:
365:            /**
366:             * Test the attach method with a new object in a Collection : the object is made persistent.
367:             */
368:            public void testAttachNewObjectInCollection() {
369:                logger
370:                        .log(BasicLevel.DEBUG,
371:                                "***************testAttachNewObjectInCollection*****************");
372:                Team t = new Team("Le Mans", null, null);
373:                Collection players = new ArrayList();
374:                Player p1 = new Player("p11", t, 20);
375:                players.add(p1);
376:                Player p2 = new Player("p12", t, 30);
377:                players.add(p2);
378:                t.setPlayers(players);
379:                Coach c = new Coach("c6", 10, t);
380:                t.setCoach(c);
381:
382:                PersistenceManager pm = pmf.getPersistenceManager();
383:                pm.currentTransaction().begin();
384:                logger.log(BasicLevel.DEBUG, "Make the team persistent "
385:                        + t.toString());
386:                pm.makePersistent(t);
387:                pm.currentTransaction().commit();
388:
389:                try {
390:                    //detach the team
391:                    Team copyOfT = (Team) pm.detachCopy(t);
392:                    t = null;
393:                    //create a new player
394:                    String newPlayerName = "pXX";
395:                    Player newPlayer = new Player(newPlayerName, copyOfT, 35);
396:                    copyOfT.addPlayer(newPlayer);
397:                    //attach the team
398:                    pm.currentTransaction().begin();
399:                    Team attachedTeam = (Team) pm.makePersistent(copyOfT);
400:                    pm.currentTransaction().commit();
401:                    Iterator it = attachedTeam.getPlayers().iterator();
402:                    boolean newPlayerFound = false;
403:                    while (!newPlayerFound && it.hasNext()) {
404:                        Player p = (Player) it.next();
405:                        if (p.getName().equals(newPlayerName)) {
406:                            assertTrue(
407:                                    "The new player is not a persistent object",
408:                                    ((JDOPersistentObjectItf) p)
409:                                            .jdoIsPersistent());
410:                            newPlayerFound = true;
411:                        }
412:                    }
413:                    assertTrue(
414:                            "The new player should have been made persistent",
415:                            newPlayerFound);
416:                } catch (Exception e) {
417:                    fail(e.getMessage());
418:                } finally {
419:                    if (pm.currentTransaction().isActive())
420:                        pm.currentTransaction().rollback();
421:                    pm.close();
422:                }
423:            }
424:
425:            /**
426:             * Test the attach method: make an inherited object persistent, detach it then attach it.
427:             */
428:            public void testAttachInherited() {
429:                logger.log(BasicLevel.DEBUG,
430:                        "***************testAttachInherited*****************");
431:                Car c = new Car("r5", 4, "red");
432:                FormulaOne f = new FormulaOne("williams", 4, "green", 262);
433:
434:                PersistenceManager pm = pmf.getPersistenceManager();
435:                pm.currentTransaction().begin();
436:                logger.log(BasicLevel.DEBUG, "make persistent the car "
437:                        + c.toString());
438:                logger.log(BasicLevel.DEBUG, "make persistent the formula one "
439:                        + f.toString());
440:                pm.makePersistent(c);
441:                pm.makePersistent(f);
442:                pm.currentTransaction().commit();
443:                //detach the car c
444:                Car copyOfC = (Car) pm.detachCopy(c);
445:                //print the car out
446:                logger.log(BasicLevel.DEBUG, copyOfC.toString());
447:                //detach the formula one f
448:                FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
449:                //print the formula one out
450:                logger.log(BasicLevel.DEBUG, copyOfF.toString());
451:                pm.currentTransaction().begin();
452:                //attach the copied car 
453:                Car attachedCar = (Car) pm.makePersistent(copyOfC);
454:                //attach the copied formula one
455:                FormulaOne attachedF = (FormulaOne) pm.makePersistent(copyOfF);
456:                pm.currentTransaction().commit();
457:                try {
458:                    assertNotNull(attachedCar);
459:                    assertEquals(copyOfC.getColor(), attachedCar.getColor());
460:                    assertEquals(copyOfC.getName(), attachedCar.getName());
461:                    assertEquals(copyOfC.getNbOfWheels(), attachedCar
462:                            .getNbOfWheels());
463:                    assertEquals(copyOfC.getType(), attachedCar.getType());
464:
465:                    assertNotNull(attachedF);
466:                    assertEquals(copyOfF.getColor(), attachedF.getColor());
467:                    assertEquals(copyOfF.getName(), attachedF.getName());
468:                    assertEquals(copyOfF.getNbOfWheels(), attachedF
469:                            .getNbOfWheels());
470:                    assertEquals(copyOfF.getType(), attachedF.getType());
471:                    assertEquals(copyOfF.getSpeedMax(), attachedF.getSpeedMax());
472:
473:                    logger.log(BasicLevel.DEBUG,
474:                            "The attached version of the car is as follows:\n "
475:                                    + attachedCar.toString());
476:                    logger.log(BasicLevel.DEBUG,
477:                            "The attached version of the formula one is as follows:\n "
478:                                    + attachedF.toString());
479:                } catch (Exception e) {
480:                    fail(e.getMessage());
481:                } finally {
482:                    if (pm.currentTransaction().isActive())
483:                        pm.currentTransaction().rollback();
484:                    pm.close();
485:                }
486:            }
487:
488:            /**
489:             * Test the attach method with update: make an inherited object persistent, detach it , modify it and then attach it.
490:             */
491:            public void testAttachModifiedInherited() {
492:                logger
493:                        .log(BasicLevel.DEBUG,
494:                                "***************testAttachModifiedInherited*****************");
495:                FormulaOne f = new FormulaOne("renault", 4, "yellow", 262);
496:
497:                PersistenceManager pm = pmf.getPersistenceManager();
498:                pm.currentTransaction().begin();
499:                logger.log(BasicLevel.DEBUG, "make persistent the formula one "
500:                        + f.toString());
501:                pm.makePersistent(f);
502:                pm.currentTransaction().commit();
503:                //detach
504:                FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
505:                //modify 
506:                copyOfF.setNbOfWheels(2);
507:                copyOfF.setSpeedMax(320);
508:                //print out
509:                logger.log(BasicLevel.DEBUG, "Copy of formula one "
510:                        + copyOfF.toString());
511:                pm.currentTransaction().begin();
512:                //attach 
513:                FormulaOne attachedF = (FormulaOne) pm.makePersistent(copyOfF);
514:                try {
515:                    assertEquals(2, attachedF.getNbOfWheels());
516:                    assertEquals(320, attachedF.getSpeedMax());
517:                    pm.currentTransaction().commit();
518:                    logger.log(BasicLevel.DEBUG,
519:                            "The attached version of the formula one is as follows:\n "
520:                                    + attachedF.toString());
521:                } catch (Exception e) {
522:                    fail(e.getMessage());
523:                } finally {
524:                    if (pm.currentTransaction().isActive())
525:                        pm.currentTransaction().rollback();
526:                    pm.close();
527:                }
528:            }
529:
530:            /**
531:             * Test the attach method with a new inherited object: the object is made persistent.
532:             */
533:            public void testAttachNewInherited() {
534:                logger
535:                        .log(BasicLevel.DEBUG,
536:                                "***************testAttachNewInherited*****************");
537:                FormulaOne f = new FormulaOne("sauber", 4, "red", 262);
538:                PersistenceManager pm = pmf.getPersistenceManager();
539:                pm.currentTransaction().begin();
540:                logger.log(BasicLevel.DEBUG, "attach the f1 " + f.toString());
541:                FormulaOne attachedF = (FormulaOne) pm.makePersistent(f);
542:                try {
543:                    assertTrue(((JDOPersistentObjectItf) attachedF)
544:                            .jdoIsPersistent());
545:                    pm.currentTransaction().commit();
546:                    logger.log(BasicLevel.DEBUG,
547:                            "The attached version of the team is as follows:\n "
548:                                    + attachedF.toString());
549:                } catch (Exception e) {
550:                    fail(e.getMessage());
551:                } finally {
552:                    if (pm.currentTransaction().isActive())
553:                        pm.currentTransaction().rollback();
554:                    pm.close();
555:                }
556:            }
557:
558:            /**
559:             * Test the attach method with an invalid copy: make an inherited object persistent, modify it, detach it , rollback the transaction and then attach it.
560:             */
561:            public void testAttachInvalidInherited() {
562:                logger
563:                        .log(BasicLevel.DEBUG,
564:                                "***************testAttachInvalidInherited*****************");
565:                FormulaOne f = new FormulaOne("ferrari", 4, "red", 262);
566:                PersistenceManager pm = pmf.getPersistenceManager();
567:                pm.currentTransaction().begin();
568:                logger.log(BasicLevel.DEBUG, "make persistent the f1 "
569:                        + f.toString());
570:                pm.makePersistent(f);
571:                pm.currentTransaction().commit();
572:                pm.currentTransaction().begin();
573:                //modify
574:                f.setNbOfWheels(1);
575:                //detach the team t
576:                FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f);
577:                //rollback the tx : it invalids the copy
578:                pm.currentTransaction().rollback();
579:                //	print out
580:                logger
581:                        .log(BasicLevel.DEBUG, "Copy of f1 "
582:                                + copyOfF.toString());
583:                logger.log(BasicLevel.DEBUG,
584:                        "rollback of the tx: the attach shouldn't work.");
585:                try {
586:                    //attach the "invalid" f1 
587:                    pm.currentTransaction().begin();
588:                    FormulaOne attachedF = (FormulaOne) pm
589:                            .makePersistent(copyOfF);
590:                    pm.currentTransaction().commit();
591:                    logger.log(BasicLevel.DEBUG,
592:                            "The attached version of the team is as follows:\n "
593:                                    + attachedF.toString());
594:                } catch (Exception e) {
595:                    assertEquals("Wrong exception thrown: " + e.getMessage(),
596:                            JDOException.class, e.getClass());
597:                } finally {
598:                    if (pm.currentTransaction().isActive()) {
599:                        pm.currentTransaction().rollback();
600:                    }
601:                    pm.close();
602:                }
603:            }
604:
605:            public void testRemovingOfPersistentObject() {
606:                PersistenceManager pm = pmf.getPersistenceManager();
607:                try {
608:                    Class[] cs = new Class[] { Car.class, Coach.class,
609:                            FormulaOne.class, Player.class, Team.class,
610:                            Vehicle.class };
611:                    pm.currentTransaction().begin();
612:                    for (int i = 0; i < cs.length; i++) {
613:                        Query query = pm.newQuery(cs[i]);
614:                        Collection col = (Collection) query.execute();
615:                        Iterator it = col.iterator();
616:                        while (it.hasNext()) {
617:                            Object o = it.next();
618:                            Assert.assertNotNull(
619:                                    "null object in the query result"
620:                                            + cs[i].getName(), o);
621:                            pm.deletePersistent(o);
622:
623:                        }
624:                        query.close(col);
625:                    }
626:                    pm.currentTransaction().commit();
627:                } catch (JDOException e) {
628:                    Exception ie = ExceptionHelper.getNested(e);
629:                    logger.log(BasicLevel.ERROR, "", ie);
630:                    fail(ie.getMessage());
631:                } finally {
632:                    pm.close();
633:                }
634:            }
635:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.