KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > fetchgroup > TestDefinedFetchGroup


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.fetchgroup;
27
28
29 import java.util.Collection JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import javax.jdo.FetchPlan;
35 import javax.jdo.JDODetachedFieldAccessException;
36 import javax.jdo.JDOException;
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.pobjects.fetchgroup.Address;
45 import org.objectweb.speedo.pobjects.fetchgroup.Country;
46 import org.objectweb.speedo.pobjects.fetchgroup.EdgeWeight;
47 import org.objectweb.speedo.pobjects.fetchgroup.Node;
48 import org.objectweb.speedo.pobjects.fetchgroup.Person;
49 import org.objectweb.util.monolog.api.BasicLevel;
50
51 /**
52  *
53  * @author Y.Bersihand
54  */

55 public class TestDefinedFetchGroup extends SpeedoTestHelper {
56
57     public TestDefinedFetchGroup(String JavaDoc s) {
58         super(s);
59     }
60
61     protected String JavaDoc getLoggerName() {
62         return LOG_NAME + ".rt.fetchgroup.TestDefinedFetchGroup";
63     }
64     
65     /**
66      * Test the loading with the detail fetch group :
67      * test the definition of a fetch-group in the jdo file with a a.b.c field
68      * <field name="a.b.c">
69      */

70     public void testLoadingReference() {
71         logger.log(BasicLevel.DEBUG, "***************testLoadingReference*****************");
72         Country country = new Country("it","Italie");
73         Address address = new Address("Rue Spiaggi", "Milan", country);
74         Person parent = new Person();
75         parent.setName("Del Piero Joel");
76         parent.setAge(32);
77         parent.setAddress(address);
78         Person child1 = new Person("Del Piero Sophie", address, null, 14);
79         Person child2 = new Person("Del Piero Mikael", address, null, 11);
80         Set JavaDoc children = new HashSet JavaDoc();
81         children.add(child1);
82         children.add(child2);
83         parent.setChildren(children);
84         
85         PersistenceManager pm = pmf.getPersistenceManager();
86         FetchPlan fp = pm.getFetchPlan();
87         fp.clearGroups();
88         fp.addGroup("detail").removeGroup("default");
89         pm.currentTransaction().begin();
90         logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
91         pm.makePersistent(parent);
92         pm.currentTransaction().commit();
93         logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
94         try {
95             pm.currentTransaction().begin();
96             Person detachedParent = (Person) pm.detachCopy(parent);
97             logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName());
98             logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge());
99             logger.log(BasicLevel.DEBUG, "Address can be accessed: " + detachedParent.getAddress().toString());
100             assertEquals(parent.getName(), detachedParent.getName());
101             assertEquals(parent.getAge(), detachedParent.getAge());
102             assertEquals(parent.getAddress().getCity(), detachedParent.getAddress().getCity());
103             assertEquals(parent.getAddress().getCountry().getCode(), detachedParent.getAddress().getCountry().getCode());
104             assertEquals(parent.getAddress().getCountry().getName(), detachedParent.getAddress().getCountry().getName());
105             assertEquals(parent.getAddress().getStreet(), detachedParent.getAddress().getStreet());
106             logger.log(BasicLevel.DEBUG, "Children should not be accessed: " + detachedParent.getChildren().toString());
107         } catch(Exception JavaDoc e) {
108             assertEquals(e.getClass(), JDODetachedFieldAccessException.class);
109             if (e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("children") != -1) {
110                 logger.log(BasicLevel.DEBUG, "correct type exception caught: " + e);
111             } else {
112                 logger.log(BasicLevel.DEBUG, "Error: " + e);
113                 fail(e.getMessage());
114             }
115         } finally {
116             if (pm.currentTransaction().isActive())
117                 pm.currentTransaction().rollback();
118             pm.close();
119         }
120     }
121     
122     /**
123      * Test the loading with the detail+children-names fetch group:
124      * test the definition of a fetch-group in the jdo file with a a#element.b field
125      * <field name="a#element.b">
126      */

127     public void testLoadingArrayElement() {
128         logger.log(BasicLevel.DEBUG, "************testLoadingArrayElement**************");
129         Country country = new Country("be","Belgique");
130         Address address = new Address("Rue Anvers", "Bruges", country);
131         Person parent = new Person();
132         parent.setName("Dermuck Joel");
133         parent.setAge(32);
134         parent.setAddress(address);
135         Person child1 = new Person("Dermuck Sophie", address, null, 14);
136         Person child2 = new Person("Dermuck Mikael", address, null, 11);
137         Set JavaDoc children = new HashSet JavaDoc();
138         children.add(child1);
139         children.add(child2);
140         parent.setChildren(children);
141         
142         PersistenceManager pm = pmf.getPersistenceManager();
143         FetchPlan fp = pm.getFetchPlan();
144         fp.clearGroups();
145         fp.addGroup("detail+children-names").removeGroup("default");
146         pm.currentTransaction().begin();
147         logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
148         pm.makePersistent(parent);
149         pm.currentTransaction().commit();
150         
151         logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
152         try {
153             pm.currentTransaction().begin();
154             Person detachedParent = (Person) pm.detachCopy(parent);
155             logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName());
156             logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge());
157             logger.log(BasicLevel.DEBUG, "Address can be accessed: " +
158                     detachedParent.getAddress().getStreet() + ", " + detachedParent.getAddress().getCity()
159                     + ", " + detachedParent.getAddress().getCountry().getCode());
160             assertEquals(parent.getName(), detachedParent.getName());
161             assertEquals(parent.getAge(), detachedParent.getAge());
162             assertEquals(parent.getAddress().getCity(), detachedParent.getAddress().getCity());
163             assertEquals(parent.getAddress().getCountry().getCode(), detachedParent.getAddress().getCountry().getCode());
164             assertEquals(parent.getAddress().getCountry().getName(), detachedParent.getAddress().getCountry().getName());
165             assertEquals(parent.getAddress().getStreet(), detachedParent.getAddress().getStreet());
166             assertEquals(parent.getChildren().size(), detachedParent.getChildren().size());
167             Collection JavaDoc childrenTmp = detachedParent.getChildren();
168             logger.log(BasicLevel.DEBUG, "Children names can be accessed: ");
169             Iterator JavaDoc it = childrenTmp.iterator();
170             while(it.hasNext()){
171                 Person p = (Person) it.next();
172                 logger.log(BasicLevel.DEBUG, "Child: " + p.getName() + ", " + p.getAge());
173             }
174             it = childrenTmp.iterator();
175             while(it.hasNext()){
176                 Person p = (Person) it.next();
177                 logger.log(BasicLevel.DEBUG, "Children address should not be accessed: " + p.getAddress().toString());
178             }
179         } catch(Exception JavaDoc e) {
180             assertEquals(e.getClass(), JDODetachedFieldAccessException.class);
181             assertTrue(e.getMessage().indexOf("address") != -1);
182             if (e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("address") != -1) {
183                 logger.log(BasicLevel.DEBUG, "correct type exception caught: " + e);
184             } else {
185                 logger.log(BasicLevel.DEBUG, "Error: " + e);
186                 fail(e.getMessage());
187             }
188         } finally {
189             if (pm.currentTransaction().isActive())
190                 pm.currentTransaction().rollback();
191             pm.close();
192         }
193     }
194     
195     
196     /**
197      * Test the loading with the detail+children-list fetch group:
198      * test the definition of a fetch-group in the jdo file with a fetch-group attribute in a field
199      * <field name="a" fetch-group="fg"/>
200      */

201     public void testLoadingFetchGroupField() {
202         logger.log(BasicLevel.DEBUG, "************testLoadingFetchGroupField**************");
203         Country country = new Country("us","Etats-Unis");
204         Address address = new Address("Rue Enclif", "San Diego", country);
205         Person parent = new Person();
206         parent.setName("Smith Joel");
207         parent.setAge(32);
208         parent.setAddress(address);
209         Person child1 = new Person("Smith Sofia", address, null, 14);
210         Person child2 = new Person("Smith Michael", address, null, 11);
211         Set JavaDoc children = new HashSet JavaDoc();
212         children.add(child1);
213         children.add(child2);
214         parent.setChildren(children);
215         
216         PersistenceManager pm = pmf.getPersistenceManager();
217         FetchPlan fp = pm.getFetchPlan();
218         fp.clearGroups();
219         fp.addGroup("detail+children-list").removeGroup("default");
220         pm.currentTransaction().begin();
221         logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
222         pm.makePersistent(parent);
223         pm.currentTransaction().commit();
224         
225         logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
226         try {
227             pm.currentTransaction().begin();
228             Person detachedParent = (Person) pm.detachCopy(parent);
229             logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName());
230             logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge());
231             logger.log(BasicLevel.DEBUG, "Address can be accessed: " +
232                     detachedParent.getAddress().getStreet() + ", " + detachedParent.getAddress().getCity()
233                     + ", " + detachedParent.getAddress().getCountry().getCode());
234             Collection JavaDoc childrenTmp = detachedParent.getChildren();
235             logger.log(BasicLevel.DEBUG, "Children names can be accessed: ");
236             assertEquals(parent.getName(), detachedParent.getName());
237             assertEquals(parent.getAge(), detachedParent.getAge());
238             assertEquals(parent.getAddress().getCity(), detachedParent.getAddress().getCity());
239             assertEquals(parent.getAddress().getCountry().getCode(), detachedParent.getAddress().getCountry().getCode());
240             assertEquals(parent.getAddress().getCountry().getName(), detachedParent.getAddress().getCountry().getName());
241             assertEquals(parent.getAddress().getStreet(), detachedParent.getAddress().getStreet());
242             assertEquals(parent.getChildren().size(), detachedParent.getChildren().size());
243             Iterator JavaDoc it = childrenTmp.iterator();
244             while(it.hasNext()){
245                 Person p = (Person) it.next();
246                 logger.log(BasicLevel.DEBUG, "Child: " + p.getName() + ", " + p.getAge());
247             }
248             it = childrenTmp.iterator();
249             while(it.hasNext()){
250                 Person p = (Person) it.next();
251                 logger.log(BasicLevel.DEBUG, "Child address should not be accessed: " + p.getAddress().toString());
252             }
253         } catch(Exception JavaDoc e){
254             assertEquals(e.getClass(), JDODetachedFieldAccessException.class);
255             assertTrue(e.getMessage().indexOf("address") != -1);
256             if (e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("address") != -1) {
257                 logger.log(BasicLevel.DEBUG, "correct type exception caught: " + e);
258             } else {
259                 logger.log(BasicLevel.DEBUG, "Error: " + e);
260                 fail(e.getMessage());
261             }
262         } finally {
263             if (pm.currentTransaction().isActive())
264                 pm.currentTransaction().rollback();
265             pm.close();
266         }
267         
268     }
269     
270     /**
271      * Test the loading with the detailChildren fetch group:
272      * test the definition of a fetch-group in the jdo file with a depth attribute in a field
273      * for recursive reference
274      * <field name="a" depth="X"/>
275      */

276     public void testLoadingRecursiveDepth() {
277         logger.log(BasicLevel.DEBUG, "************testLoadingRecursiveDepth**************");
278         Country country = new Country("sp","Espagne");
279         Address address = new Address("Rue Rio", "Santander", country);
280         Person parent = new Person();
281         parent.setName("Casillas Joel");
282         parent.setAge(63);
283         parent.setAddress(address);
284         Person child1 = new Person("Casillas Sofia", address, null, 30);
285         Person child2 = new Person("Casillas Michael", address, null, 40);
286         Set JavaDoc children = new HashSet JavaDoc();
287         children.add(child1);
288         children.add(child2);
289         parent.setChildren(children);
290         Person child11 = new Person("Casillas Maria", address, null, 14);
291         Person child21 = new Person("Casillas Juan", address, null, 11);
292         Set JavaDoc children1 = new HashSet JavaDoc();
293         children1.add(child11);
294         Set JavaDoc children2 = new HashSet JavaDoc();
295         children2.add(child21);
296         child1.setChildren(children1);
297         child2.setChildren(children2);
298         
299         PersistenceManager pm = pmf.getPersistenceManager();
300         FetchPlan fp = pm.getFetchPlan();
301         fp.clearGroups();
302         fp.addGroup("detailChildren").removeGroup("default");
303         pm.currentTransaction().begin();
304         logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
305         pm.makePersistent(parent);
306         pm.currentTransaction().commit();
307         logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
308         
309         try {
310             pm.currentTransaction().begin();
311             Person detachedParent = (Person) pm.detachCopy(parent);
312             assertEquals(parent.getName(), detachedParent.getName());
313             assertEquals(parent.getAge(), detachedParent.getAge());
314             assertEquals(parent.getAddress().getCity(), detachedParent.getAddress().getCity());
315             assertEquals(parent.getAddress().getCountry().getCode(), detachedParent.getAddress().getCountry().getCode());
316             assertEquals(parent.getAddress().getCountry().getName(), detachedParent.getAddress().getCountry().getName());
317             assertEquals(parent.getAddress().getStreet(), detachedParent.getAddress().getStreet());
318             assertEquals(parent.getChildren().size(), detachedParent.getChildren().size());
319             logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName());
320             logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge());
321             logger.log(BasicLevel.DEBUG, "Address can be accessed: " +
322                     detachedParent.getAddress().getStreet() + ", " + detachedParent.getAddress().getCity()
323                     + ", " + detachedParent.getAddress().getCountry().getCode());
324             Collection JavaDoc childrenTmp = detachedParent.getChildren();
325             logger.log(BasicLevel.DEBUG, "Children names and address.country.code can be accessed: ");
326             Iterator JavaDoc it = childrenTmp.iterator();
327             while(it.hasNext()){
328                 Person p = (Person) it.next();
329                 logger.log(BasicLevel.DEBUG, "Child: " + p.getName() + ", " + p.getAge() + ", " + p.getAddress().getStreet() + ", " + p.getAddress().getCity()
330                         + ", " + p.getAddress().getCountry().getCode());
331             }
332             it = childrenTmp.iterator();
333             while(it.hasNext()){
334                 Person p = (Person) it.next();
335                 Iterator JavaDoc it2 = p.getChildren().iterator();
336                 while(it2.hasNext()){
337                     Person person = (Person) it2.next();
338                     logger.log(BasicLevel.DEBUG, "\tChild of children should not be accessed: " + person.toString());
339                 }
340             }
341         } catch(Exception JavaDoc e) {
342             assertEquals(e.getClass(), JDODetachedFieldAccessException.class);
343             assertTrue(e.getMessage().indexOf("children") != -1);
344             if (e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("children") != -1) {
345                 logger.log(BasicLevel.DEBUG, "correct type exception caught: " + e);
346             } else {
347                 logger.log(BasicLevel.DEBUG, "Error: " + e);
348                 fail(e.getMessage());
349             }
350         } finally {
351             if (pm.currentTransaction().isActive())
352                 pm.currentTransaction().rollback();
353             pm.close();
354         }
355     }
356     
357     /**
358      * Test the loading with the detailChildren fetch group:
359      * test the definition of a fetch-group in the jdo file with a depth attribute defined twice for a field
360      * <field name="a" depth="X"/>
361      */

362     public void testLoadingDoubleDepth() {
363         logger.log(BasicLevel.DEBUG, "************testLoadingDoubleDepth**************");
364         Country country = new Country("bl","Belarus");
365         Address address = new Address("Rue Kaloc", "Minsk", country);
366         Person parent = new Person();
367         parent.setName("Castuk Joel");
368         parent.setAge(63);
369         parent.setAddress(address);
370         Person child1 = new Person("Castuk Sofia", address, null, 30);
371         Person child2 = new Person("Castuk Michael", address, null, 40);
372         Set JavaDoc children = new HashSet JavaDoc();
373         children.add(child1);
374         children.add(child2);
375         parent.setChildren(children);
376         Person child11 = new Person("Castuk Maria", address, null, 14);
377         Person child21 = new Person("Castuk Juan", address, null, 11);
378         Set JavaDoc children1 = new HashSet JavaDoc();
379         children1.add(child11);
380         Set JavaDoc children2 = new HashSet JavaDoc();
381         children2.add(child21);
382         child1.setChildren(children1);
383         child2.setChildren(children2);
384         
385         PersistenceManager pm = pmf.getPersistenceManager();
386         FetchPlan fp = pm.getFetchPlan();
387         fp.clearGroups();
388         fp.addGroup("detailChildren2").removeGroup("default");
389         pm.currentTransaction().begin();
390         logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
391         pm.makePersistent(parent);
392         pm.currentTransaction().commit();
393         logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
394         
395         try{
396             pm.currentTransaction().begin();
397             Person detachedParent = (Person) pm.detachCopy(parent);
398             assertEquals(parent.getName(), detachedParent.getName());
399             assertEquals(parent.getAge(), detachedParent.getAge());
400             assertEquals(parent.getAddress().getCity(), detachedParent.getAddress().getCity());
401             assertEquals(parent.getAddress().getCountry().getCode(), detachedParent.getAddress().getCountry().getCode());
402             assertEquals(parent.getAddress().getCountry().getName(), detachedParent.getAddress().getCountry().getName());
403             assertEquals(parent.getAddress().getStreet(), detachedParent.getAddress().getStreet());
404             assertEquals(parent.getChildren().size(), detachedParent.getChildren().size());
405             logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName());
406             logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge());
407             logger.log(BasicLevel.DEBUG, "Address can be accessed: " +
408                     detachedParent.getAddress().getStreet() + ", " + detachedParent.getAddress().getCity()
409                     + ", " + detachedParent.getAddress().getCountry().getCode());
410             Collection JavaDoc childrenTmp = detachedParent.getChildren();
411             logger.log(BasicLevel.DEBUG, "Children names and address.country.code can be accessed: ");
412             Iterator JavaDoc it = childrenTmp.iterator();
413             while(it.hasNext()){
414                 Person p = (Person) it.next();
415                 logger.log(BasicLevel.DEBUG, "Child: " + p.getName() + ", " + p.getAge() + ", " + p.getAddress().getStreet() + ", " + p.getAddress().getCity()
416                         + ", " + p.getAddress().getCountry().getCode());
417             }
418             it = childrenTmp.iterator();
419             while(it.hasNext()){
420                 Person p = (Person) it.next();
421                 Iterator JavaDoc it2 = p.getChildren().iterator();
422                 while(it2.hasNext()){
423                     Person person = (Person) it2.next();
424                     logger.log(BasicLevel.DEBUG, "\tChild of children " + person.getName() + ", " + person.getAge() + ", " + person.getAddress().getStreet() + ", " + person.getAddress().getCity()
425                             + ", " + person.getAddress().getCountry().getCode());
426                 }
427             }
428             it = childrenTmp.iterator();
429             while(it.hasNext()){
430                 Person p = (Person) it.next();
431                 Iterator JavaDoc it2 = p.getChildren().iterator();
432                 while(it2.hasNext()){
433                     Person person = (Person) it2.next();
434                     Iterator JavaDoc it3 = person.getChildren().iterator();
435                     while(it3.hasNext()){
436                         Person pf = (Person) it3.next();
437                         logger.log(BasicLevel.DEBUG, "\t\tChild: " + pf.toString());
438                     }
439                 }
440             }
441         } catch(Exception JavaDoc e){
442             assertEquals(e.getClass(), JDODetachedFieldAccessException.class);
443             assertTrue(e.getMessage().indexOf("children") != -1);
444             if (e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("children") != -1) {
445                 logger.log(BasicLevel.DEBUG, "correct type exception caught: " + e);
446             } else {
447                 logger.log(BasicLevel.DEBUG, "Error: " + e);
448                 fail(e.getMessage());
449             }
450         } finally {
451             if (pm.currentTransaction().isActive())
452                 pm.currentTransaction().rollback();
453             pm.close();
454         }
455     }
456     
457     /**
458      * Test the loading with the detailChildren fetch group:
459      * test the definition of a fetch-group in the jdo file with a depth attribute defined twice for a field
460      * one of them is unlimited
461      * <field name="a" depth="X"/>
462      */

463     public void testLoadingDoubleDepthUnlimited() {
464         logger.log(BasicLevel.DEBUG, "************testLoadingDoubleDepthUnlimited**************");
465         Country country = new Country("p","Portugal");
466         Address address = new Address("Rue Christiano", "Lisbonne", country);
467         Person parent = new Person();
468         parent.setName("Simoes Joel");
469         parent.setAge(63);
470         parent.setAddress(address);
471         Person child1 = new Person("Simoes Sofia", address, null, 30);
472         Person child2 = new Person("Simoes Michael", address, null, 40);
473         Set JavaDoc children = new HashSet JavaDoc();
474         children.add(child1);
475         children.add(child2);
476         parent.setChildren(children);
477         Person child11 = new Person("Simoes Maria", address, null, 14);
478         Person child21 = new Person("Simoes Juan", address, null, 11);
479         Set JavaDoc children1 = new HashSet JavaDoc();
480         children1.add(child11);
481         Set JavaDoc children2 = new HashSet JavaDoc();
482         children2.add(child21);
483         child1.setChildren(children1);
484         child2.setChildren(children2);
485         
486         PersistenceManager pm = pmf.getPersistenceManager();
487         FetchPlan fp = pm.getFetchPlan();
488         fp.clearGroups();
489         fp.addGroup("detailChildren3").removeGroup("default");
490         pm.currentTransaction().begin();
491         logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
492         pm.makePersistent(parent);
493         pm.currentTransaction().commit();
494         logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
495         
496         try {
497             pm.currentTransaction().begin();
498             Person detachedParent = (Person) pm.detachCopy(parent);
499             assertEquals(parent.getName(), detachedParent.getName());
500             assertEquals(parent.getAge(), detachedParent.getAge());
501             assertEquals(parent.getAddress().getCity(), detachedParent.getAddress().getCity());
502             assertEquals(parent.getAddress().getCountry().getCode(), detachedParent.getAddress().getCountry().getCode());
503             assertEquals(parent.getAddress().getCountry().getName(), detachedParent.getAddress().getCountry().getName());
504             assertEquals(parent.getAddress().getStreet(), detachedParent.getAddress().getStreet());
505             assertEquals(parent.getChildren().size(), detachedParent.getChildren().size());
506             logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName());
507             logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge());
508             logger.log(BasicLevel.DEBUG, "Address can be accessed: " +
509                     detachedParent.getAddress().getStreet() + ", " + detachedParent.getAddress().getCity()
510                     + ", " + detachedParent.getAddress().getCountry().getCode());
511             Collection JavaDoc childrenTmp = detachedParent.getChildren();
512             logger.log(BasicLevel.DEBUG, "Children names and address.country.code can be accessed: ");
513             Iterator JavaDoc it = childrenTmp.iterator();
514             while(it.hasNext()){
515                 Person p = (Person) it.next();
516                 logger.log(BasicLevel.DEBUG, "Child: " + p.getName() + ", " + p.getAge() + ", " + p.getAddress().getStreet() + ", " + p.getAddress().getCity()
517                         + ", " + p.getAddress().getCountry().getCode());
518             }
519             it = childrenTmp.iterator();
520             while(it.hasNext()){
521                 Person p = (Person) it.next();
522                 Iterator JavaDoc it2 = p.getChildren().iterator();
523                 while(it2.hasNext()){
524                     Person person = (Person) it2.next();
525                     logger.log(BasicLevel.DEBUG, "\tChild of children " + person.toString());
526                 }
527             }
528             pm.close();
529         } catch(Exception JavaDoc e) {
530             logger.log(BasicLevel.DEBUG, "Error: " + e);
531             fail(e.getMessage());
532         } finally {
533             if (pm.currentTransaction().isActive())
534                 pm.currentTransaction().rollback();
535             pm.close();
536         }
537     }
538     
539     /**
540      * Test the loading with the keyValue fetchgroup:
541      * test the definition of a fetch-group with a map, loading both keys and values
542      * <field name="map#key">
543      * <field name="map#value">
544      */

545     public void testLoadingMapKeyValue() {
546         logger.log(BasicLevel.DEBUG, "************testLoadingMapKeyValue**************");
547         Node n1 = new Node("n1");
548         Node n2 = new Node("n2");
549         Node n3 = new Node("n3");
550         Node n4 = new Node("n4");
551         Node n5 = new Node("n5");
552         
553         n1.addEdge(n2.getName(), new EdgeWeight(1));
554         n1.addEdge(n3.getName(), new EdgeWeight(2));
555         
556         n2.addEdge(n4.getName(), new EdgeWeight(7));
557         n2.addEdge(n5.getName(), new EdgeWeight(4));
558         
559         PersistenceManager pm = pmf.getPersistenceManager();
560         FetchPlan fp = pm.getFetchPlan();
561         fp.clearGroups();
562         fp.addGroup("keyValue").removeGroup("default");
563         pm.currentTransaction().begin();
564         logger.log(BasicLevel.DEBUG, "make persistent the nodes " + n1.toString() + ", " + n2.toString()
565                 + ", " + n3.toString() + ", " + n4.toString() + ", " + n5.toString());
566         pm.makePersistent(n1);
567         pm.makePersistent(n2);
568         pm.makePersistent(n3);
569         pm.makePersistent(n4);
570         pm.makePersistent(n5);
571         pm.currentTransaction().commit();
572         logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
573         
574         try {
575             Node detachedN1 = (Node) pm.detachCopy(n1);
576             assertEquals(n1.getName(), detachedN1.getName());
577             assertEquals(n1.getEdges().size(), detachedN1.getEdges().size());
578             logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedN1.getName());
579             logger.log(BasicLevel.DEBUG, "ToString(): " + detachedN1.toString());
580         } catch(Exception JavaDoc e){
581             logger.log(BasicLevel.DEBUG, "Error: " + e);
582             fail(e.getMessage());
583         } finally {
584             if (pm.currentTransaction().isActive())
585                 pm.currentTransaction().rollback();
586             pm.close();
587         }
588     }
589     
590     
591     /**
592      * Test the loading with the keyOnly fetchgroup:
593      * test the definition of a fetch-group with a map, loading only keys
594      * <field name="map#key">
595      */

596     public void testLoadingMapKeyOnly() {
597         logger.log(BasicLevel.DEBUG, "************testLoadingMapKeyOnly**************");
598     
599         Node n1 = new Node("n11");
600         Node n2 = new Node("n21");
601         Node n3 = new Node("n31");
602         Node n4 = new Node("n41");
603         Node n5 = new Node("n51");
604         
605         n1.addEdge(n2.getName(), new EdgeWeight(1));
606         n1.addEdge(n3.getName(), new EdgeWeight(2));
607         
608         n2.addEdge(n4.getName(), new EdgeWeight(7));
609         n2.addEdge(n5.getName(), new EdgeWeight(4));
610         
611         PersistenceManager pm = pmf.getPersistenceManager();
612         FetchPlan fp = pm.getFetchPlan();
613         fp.clearGroups();
614         fp.addGroup("keyOnly").removeGroup("default");
615         pm.currentTransaction().begin();
616         logger.log(BasicLevel.DEBUG, "make persistent the nodes " + n1.toString() + ", " + n2.toString()
617                 + ", " + n3.toString() + ", " + n4.toString() + ", " + n5.toString());
618         pm.makePersistent(n1);
619         pm.makePersistent(n2);
620         pm.makePersistent(n3);
621         pm.makePersistent(n4);
622         pm.makePersistent(n5);
623         pm.currentTransaction().commit();
624         logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
625         
626         try {
627             Node detachedN1 = (Node) pm.detachCopy(n1);
628             assertEquals(n1.getName(), detachedN1.getName());
629             assertEquals(n1.getEdges().size(), detachedN1.getEdges().size());
630             logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedN1.getName());
631             logger.log(BasicLevel.DEBUG, "ToString(): " + detachedN1.toString());
632         } catch(Exception JavaDoc e){
633             logger.log(BasicLevel.DEBUG, "Error: " + e);
634             fail(e.getMessage());
635         } finally {
636             if (pm.currentTransaction().isActive())
637                 pm.currentTransaction().rollback();
638             pm.close();
639         }
640     }
641
642     public void testRemovingOfPersistentObject() {
643         PersistenceManager pm = pmf.getPersistenceManager();
644         try {
645             Class JavaDoc[] cs = new Class JavaDoc[]{Person.class, Address.class, Country.class,
646                     Node.class, EdgeWeight.class};
647             pm.currentTransaction().begin();
648             for(int i=0; i<cs.length; i++) {
649                 Query query = pm.newQuery(cs[i]);
650                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
651                 Iterator JavaDoc it = col.iterator();
652                 while(it.hasNext()) {
653                     Object JavaDoc o = it.next();
654                     Assert.assertNotNull("null object in the query result"
655                         + cs[i].getName(), o);
656                     pm.deletePersistent(o);
657
658                 }
659                 query.close(col);
660             }
661             pm.currentTransaction().commit();
662         } catch (JDOException e) {
663             Exception JavaDoc ie = ExceptionHelper.getNested(e);
664             logger.log(BasicLevel.ERROR, "", ie);
665             fail(ie.getMessage());
666         } finally {
667             pm.close();
668         }
669     }
670 }
671
Popular Tags