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.fetchgroup;
025:
026: import java.util.Collection;
027: import java.util.HashSet;
028: import java.util.Iterator;
029: import java.util.Set;
030:
031: import javax.jdo.FetchPlan;
032: import javax.jdo.JDODetachedFieldAccessException;
033: import javax.jdo.JDOException;
034: import javax.jdo.PersistenceManager;
035: import javax.jdo.Query;
036:
037: import junit.framework.Assert;
038:
039: import org.objectweb.speedo.SpeedoTestHelper;
040: import org.objectweb.speedo.api.ExceptionHelper;
041: import org.objectweb.speedo.pobjects.fetchgroup.Address;
042: import org.objectweb.speedo.pobjects.fetchgroup.Country;
043: import org.objectweb.speedo.pobjects.fetchgroup.EdgeWeight;
044: import org.objectweb.speedo.pobjects.fetchgroup.Node;
045: import org.objectweb.speedo.pobjects.fetchgroup.Person;
046: import org.objectweb.util.monolog.api.BasicLevel;
047:
048: /**
049: *
050: * @author Y.Bersihand
051: */
052: public class TestDefinedFetchGroup extends SpeedoTestHelper {
053:
054: public TestDefinedFetchGroup(String s) {
055: super (s);
056: }
057:
058: protected String getLoggerName() {
059: return LOG_NAME + ".rt.fetchgroup.TestDefinedFetchGroup";
060: }
061:
062: /**
063: * Test the loading with the detail fetch group :
064: * test the definition of a fetch-group in the jdo file with a a.b.c field
065: * <field name="a.b.c">
066: */
067: public void testLoadingReference() {
068: logger.log(BasicLevel.DEBUG,
069: "***************testLoadingReference*****************");
070: Country country = new Country("it", "Italie");
071: Address address = new Address("Rue Spiaggi", "Milan", country);
072: Person parent = new Person();
073: parent.setName("Del Piero Joel");
074: parent.setAge(32);
075: parent.setAddress(address);
076: Person child1 = new Person("Del Piero Sophie", address, null,
077: 14);
078: Person child2 = new Person("Del Piero Mikael", address, null,
079: 11);
080: Set children = new HashSet();
081: children.add(child1);
082: children.add(child2);
083: parent.setChildren(children);
084:
085: PersistenceManager pm = pmf.getPersistenceManager();
086: FetchPlan fp = pm.getFetchPlan();
087: fp.clearGroups();
088: fp.addGroup("detail").removeGroup("default");
089: pm.currentTransaction().begin();
090: logger.log(BasicLevel.DEBUG, "make persistent the person "
091: + parent.toString());
092: pm.makePersistent(parent);
093: pm.currentTransaction().commit();
094: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
095: try {
096: pm.currentTransaction().begin();
097: Person detachedParent = (Person) pm.detachCopy(parent);
098: logger.log(BasicLevel.DEBUG, "Name can be accessed: "
099: + detachedParent.getName());
100: logger.log(BasicLevel.DEBUG, "Age can be accessed: "
101: + detachedParent.getAge());
102: logger.log(BasicLevel.DEBUG, "Address can be accessed: "
103: + detachedParent.getAddress().toString());
104: assertEquals(parent.getName(), detachedParent.getName());
105: assertEquals(parent.getAge(), detachedParent.getAge());
106: assertEquals(parent.getAddress().getCity(), detachedParent
107: .getAddress().getCity());
108: assertEquals(parent.getAddress().getCountry().getCode(),
109: detachedParent.getAddress().getCountry().getCode());
110: assertEquals(parent.getAddress().getCountry().getName(),
111: detachedParent.getAddress().getCountry().getName());
112: assertEquals(parent.getAddress().getStreet(),
113: detachedParent.getAddress().getStreet());
114: logger.log(BasicLevel.DEBUG,
115: "Children should not be accessed: "
116: + detachedParent.getChildren().toString());
117: } catch (Exception e) {
118: if (!JDODetachedFieldAccessException.class.equals(e
119: .getClass())) {
120: logger.log(BasicLevel.ERROR, "Exception found: ", e);
121: assertEquals(e.getClass(),
122: JDODetachedFieldAccessException.class);
123: }
124: if (e instanceof JDODetachedFieldAccessException
125: && e.getMessage().indexOf("children") != -1) {
126: logger.log(BasicLevel.DEBUG,
127: "correct type exception caught: " + e);
128: } else {
129: logger.log(BasicLevel.DEBUG, "Error: " + e);
130: fail(e.getMessage());
131: }
132: } finally {
133: if (pm.currentTransaction().isActive())
134: pm.currentTransaction().rollback();
135: pm.close();
136: }
137: }
138:
139: /**
140: * Test the loading with the detail+children-names fetch group:
141: * test the definition of a fetch-group in the jdo file with a a#element.b field
142: * <field name="a#element.b">
143: */
144: public void testLoadingArrayElement() {
145: logger.log(BasicLevel.DEBUG,
146: "************testLoadingArrayElement**************");
147: Country country = new Country("be", "Belgique");
148: Address address = new Address("Rue Anvers", "Bruges", country);
149: Person parent = new Person();
150: parent.setName("Dermuck Joel");
151: parent.setAge(32);
152: parent.setAddress(address);
153: Person child1 = new Person("Dermuck Sophie", address, null, 14);
154: Person child2 = new Person("Dermuck Mikael", address, null, 11);
155: Set children = new HashSet();
156: children.add(child1);
157: children.add(child2);
158: parent.setChildren(children);
159:
160: PersistenceManager pm = pmf.getPersistenceManager();
161: FetchPlan fp = pm.getFetchPlan();
162: fp.clearGroups();
163: fp.addGroup("detail+children-names").removeGroup("default");
164: pm.currentTransaction().begin();
165: logger.log(BasicLevel.DEBUG, "make persistent the person "
166: + parent.toString());
167: pm.makePersistent(parent);
168: pm.currentTransaction().commit();
169:
170: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
171: try {
172: pm.currentTransaction().begin();
173: Person detachedParent = (Person) pm.detachCopy(parent);
174: logger.log(BasicLevel.DEBUG, "Name can be accessed: "
175: + detachedParent.getName());
176: logger.log(BasicLevel.DEBUG, "Age can be accessed: "
177: + detachedParent.getAge());
178: logger.log(BasicLevel.DEBUG, "Address can be accessed: "
179: + detachedParent.getAddress().getStreet()
180: + ", "
181: + detachedParent.getAddress().getCity()
182: + ", "
183: + detachedParent.getAddress().getCountry()
184: .getCode());
185: assertEquals(parent.getName(), detachedParent.getName());
186: assertEquals(parent.getAge(), detachedParent.getAge());
187: assertEquals(parent.getAddress().getCity(), detachedParent
188: .getAddress().getCity());
189: assertEquals(parent.getAddress().getCountry().getCode(),
190: detachedParent.getAddress().getCountry().getCode());
191: assertEquals(parent.getAddress().getCountry().getName(),
192: detachedParent.getAddress().getCountry().getName());
193: assertEquals(parent.getAddress().getStreet(),
194: detachedParent.getAddress().getStreet());
195: assertEquals(parent.getChildren().size(), detachedParent
196: .getChildren().size());
197: Collection childrenTmp = detachedParent.getChildren();
198: logger.log(BasicLevel.DEBUG,
199: "Children names can be accessed: ");
200: Iterator it = childrenTmp.iterator();
201: while (it.hasNext()) {
202: Person p = (Person) it.next();
203: logger.log(BasicLevel.DEBUG, "Child: " + p.getName()
204: + ", " + p.getAge());
205: }
206: it = childrenTmp.iterator();
207: while (it.hasNext()) {
208: Person p = (Person) it.next();
209: logger.log(BasicLevel.DEBUG,
210: "Children address should not be accessed: "
211: + p.getAddress().toString());
212: }
213: } catch (Exception e) {
214: assertEquals(e.getClass(),
215: JDODetachedFieldAccessException.class);
216: assertTrue(e.getMessage().indexOf("address") != -1);
217: if (e instanceof JDODetachedFieldAccessException
218: && e.getMessage().indexOf("address") != -1) {
219: logger.log(BasicLevel.DEBUG,
220: "correct type exception caught: " + e);
221: } else {
222: logger.log(BasicLevel.DEBUG, "Error: " + e);
223: fail(e.getMessage());
224: }
225: } finally {
226: if (pm.currentTransaction().isActive())
227: pm.currentTransaction().rollback();
228: pm.close();
229: }
230: }
231:
232: /**
233: * Test the loading with the detail+children-list fetch group:
234: * test the definition of a fetch-group in the jdo file with a fetch-group attribute in a field
235: * <field name="a" fetch-group="fg"/>
236: */
237: public void testLoadingFetchGroupField() {
238: logger.log(BasicLevel.DEBUG,
239: "************testLoadingFetchGroupField**************");
240: Country country = new Country("us", "Etats-Unis");
241: Address address = new Address("Rue Enclif", "San Diego",
242: country);
243: Person parent = new Person();
244: parent.setName("Smith Joel");
245: parent.setAge(32);
246: parent.setAddress(address);
247: Person child1 = new Person("Smith Sofia", address, null, 14);
248: Person child2 = new Person("Smith Michael", address, null, 11);
249: Set children = new HashSet();
250: children.add(child1);
251: children.add(child2);
252: parent.setChildren(children);
253:
254: PersistenceManager pm = pmf.getPersistenceManager();
255: FetchPlan fp = pm.getFetchPlan();
256: fp.clearGroups();
257: fp.addGroup("detail+children-list").removeGroup("default");
258: pm.currentTransaction().begin();
259: logger.log(BasicLevel.DEBUG, "make persistent the person "
260: + parent.toString());
261: pm.makePersistent(parent);
262: pm.currentTransaction().commit();
263:
264: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
265: try {
266: pm.currentTransaction().begin();
267: Person detachedParent = (Person) pm.detachCopy(parent);
268: logger.log(BasicLevel.DEBUG, "Name can be accessed: "
269: + detachedParent.getName());
270: logger.log(BasicLevel.DEBUG, "Age can be accessed: "
271: + detachedParent.getAge());
272: logger.log(BasicLevel.DEBUG, "Address can be accessed: "
273: + detachedParent.getAddress().getStreet()
274: + ", "
275: + detachedParent.getAddress().getCity()
276: + ", "
277: + detachedParent.getAddress().getCountry()
278: .getCode());
279: Collection childrenTmp = detachedParent.getChildren();
280: logger.log(BasicLevel.DEBUG,
281: "Children names can be accessed: ");
282: assertEquals(parent.getName(), detachedParent.getName());
283: assertEquals(parent.getAge(), detachedParent.getAge());
284: assertEquals(parent.getAddress().getCity(), detachedParent
285: .getAddress().getCity());
286: assertEquals(parent.getAddress().getCountry().getCode(),
287: detachedParent.getAddress().getCountry().getCode());
288: assertEquals(parent.getAddress().getCountry().getName(),
289: detachedParent.getAddress().getCountry().getName());
290: assertEquals(parent.getAddress().getStreet(),
291: detachedParent.getAddress().getStreet());
292: assertEquals(parent.getChildren().size(), detachedParent
293: .getChildren().size());
294: Iterator it = childrenTmp.iterator();
295: while (it.hasNext()) {
296: Person p = (Person) it.next();
297: logger.log(BasicLevel.DEBUG, "Child: " + p.getName()
298: + ", " + p.getAge());
299: }
300: it = childrenTmp.iterator();
301: while (it.hasNext()) {
302: Person p = (Person) it.next();
303: logger.log(BasicLevel.DEBUG,
304: "Child address should not be accessed: "
305: + p.getAddress().toString());
306: }
307: } catch (Exception e) {
308: assertEquals(e.getClass(),
309: JDODetachedFieldAccessException.class);
310: assertTrue(e.getMessage().indexOf("address") != -1);
311: if (e instanceof JDODetachedFieldAccessException
312: && e.getMessage().indexOf("address") != -1) {
313: logger.log(BasicLevel.DEBUG,
314: "correct type exception caught: " + e);
315: } else {
316: logger.log(BasicLevel.DEBUG, "Error: " + e);
317: fail(e.getMessage());
318: }
319: } finally {
320: if (pm.currentTransaction().isActive())
321: pm.currentTransaction().rollback();
322: pm.close();
323: }
324:
325: }
326:
327: /**
328: * Test the loading with the detailChildren fetch group:
329: * test the definition of a fetch-group in the jdo file with a depth attribute in a field
330: * for recursive reference
331: * <field name="a" depth="X"/>
332: */
333: public void testLoadingRecursiveDepth() {
334: logger.log(BasicLevel.DEBUG,
335: "************testLoadingRecursiveDepth**************");
336: Country country = new Country("sp", "Espagne");
337: Address address = new Address("Rue Rio", "Santander", country);
338: Person parent = new Person();
339: parent.setName("Casillas Joel");
340: parent.setAge(63);
341: parent.setAddress(address);
342: Person child1 = new Person("Casillas Sofia", address, null, 30);
343: Person child2 = new Person("Casillas Michael", address, null,
344: 40);
345: Set children = new HashSet();
346: children.add(child1);
347: children.add(child2);
348: parent.setChildren(children);
349: Person child11 = new Person("Casillas Maria", address, null, 14);
350: Person child21 = new Person("Casillas Juan", address, null, 11);
351: Set children1 = new HashSet();
352: children1.add(child11);
353: Set children2 = new HashSet();
354: children2.add(child21);
355: child1.setChildren(children1);
356: child2.setChildren(children2);
357:
358: PersistenceManager pm = pmf.getPersistenceManager();
359: FetchPlan fp = pm.getFetchPlan();
360: fp.clearGroups();
361: fp.addGroup("detailChildren").removeGroup("default");
362: pm.currentTransaction().begin();
363: logger.log(BasicLevel.DEBUG, "make persistent the person "
364: + parent.toString());
365: pm.makePersistent(parent);
366: pm.currentTransaction().commit();
367: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
368:
369: try {
370: pm.currentTransaction().begin();
371: Person detachedParent = (Person) pm.detachCopy(parent);
372: assertEquals(parent.getName(), detachedParent.getName());
373: assertEquals(parent.getAge(), detachedParent.getAge());
374: assertEquals(parent.getAddress().getCity(), detachedParent
375: .getAddress().getCity());
376: assertEquals(parent.getAddress().getCountry().getCode(),
377: detachedParent.getAddress().getCountry().getCode());
378: assertEquals(parent.getAddress().getCountry().getName(),
379: detachedParent.getAddress().getCountry().getName());
380: assertEquals(parent.getAddress().getStreet(),
381: detachedParent.getAddress().getStreet());
382: assertEquals(parent.getChildren().size(), detachedParent
383: .getChildren().size());
384: logger.log(BasicLevel.DEBUG, "Name can be accessed: "
385: + detachedParent.getName());
386: logger.log(BasicLevel.DEBUG, "Age can be accessed: "
387: + detachedParent.getAge());
388: logger.log(BasicLevel.DEBUG, "Address can be accessed: "
389: + detachedParent.getAddress().getStreet()
390: + ", "
391: + detachedParent.getAddress().getCity()
392: + ", "
393: + detachedParent.getAddress().getCountry()
394: .getCode());
395: Collection childrenTmp = detachedParent.getChildren();
396: logger
397: .log(BasicLevel.DEBUG,
398: "Children names and address.country.code can be accessed: ");
399: Iterator it = childrenTmp.iterator();
400: while (it.hasNext()) {
401: Person p = (Person) it.next();
402: logger.log(BasicLevel.DEBUG, "Child: " + p.getName()
403: + ", " + p.getAge() + ", "
404: + p.getAddress().getStreet() + ", "
405: + p.getAddress().getCity() + ", "
406: + p.getAddress().getCountry().getCode());
407: }
408: it = childrenTmp.iterator();
409: while (it.hasNext()) {
410: Person p = (Person) it.next();
411: Iterator it2 = p.getChildren().iterator();
412: while (it2.hasNext()) {
413: Person person = (Person) it2.next();
414: logger.log(BasicLevel.DEBUG,
415: "\tChild of children should not be accessed: "
416: + person.toString());
417: }
418: }
419: } catch (Exception e) {
420: assertEquals(e.getClass(),
421: JDODetachedFieldAccessException.class);
422: assertTrue(e.getMessage().indexOf("children") != -1);
423: if (e instanceof JDODetachedFieldAccessException
424: && e.getMessage().indexOf("children") != -1) {
425: logger.log(BasicLevel.DEBUG,
426: "correct type exception caught: " + e);
427: } else {
428: logger.log(BasicLevel.DEBUG, "Error: " + e);
429: fail(e.getMessage());
430: }
431: } finally {
432: if (pm.currentTransaction().isActive())
433: pm.currentTransaction().rollback();
434: pm.close();
435: }
436: }
437:
438: /**
439: * Test the loading with the detailChildren fetch group:
440: * test the definition of a fetch-group in the jdo file with a depth attribute defined twice for a field
441: * <field name="a" depth="X"/>
442: */
443: public void testLoadingDoubleDepth() {
444: logger.log(BasicLevel.DEBUG,
445: "************testLoadingDoubleDepth**************");
446: Country country = new Country("bl", "Belarus");
447: Address address = new Address("Rue Kaloc", "Minsk", country);
448: Person parent = new Person();
449: parent.setName("Castuk Joel");
450: parent.setAge(63);
451: parent.setAddress(address);
452: Person child1 = new Person("Castuk Sofia", address, null, 30);
453: Person child2 = new Person("Castuk Michael", address, null, 40);
454: Set children = new HashSet();
455: children.add(child1);
456: children.add(child2);
457: parent.setChildren(children);
458: Person child11 = new Person("Castuk Maria", address, null, 14);
459: Person child21 = new Person("Castuk Juan", address, null, 11);
460: Set children1 = new HashSet();
461: children1.add(child11);
462: Set children2 = new HashSet();
463: children2.add(child21);
464: child1.setChildren(children1);
465: child2.setChildren(children2);
466:
467: PersistenceManager pm = pmf.getPersistenceManager();
468: FetchPlan fp = pm.getFetchPlan();
469: fp.clearGroups();
470: fp.addGroup("detailChildren2").removeGroup("default");
471: pm.currentTransaction().begin();
472: logger.log(BasicLevel.DEBUG, "make persistent the person "
473: + parent.toString());
474: pm.makePersistent(parent);
475: pm.currentTransaction().commit();
476: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
477:
478: try {
479: pm.currentTransaction().begin();
480: Person detachedParent = (Person) pm.detachCopy(parent);
481: assertEquals(parent.getName(), detachedParent.getName());
482: assertEquals(parent.getAge(), detachedParent.getAge());
483: assertEquals(parent.getAddress().getCity(), detachedParent
484: .getAddress().getCity());
485: assertEquals(parent.getAddress().getCountry().getCode(),
486: detachedParent.getAddress().getCountry().getCode());
487: assertEquals(parent.getAddress().getCountry().getName(),
488: detachedParent.getAddress().getCountry().getName());
489: assertEquals(parent.getAddress().getStreet(),
490: detachedParent.getAddress().getStreet());
491: assertEquals(parent.getChildren().size(), detachedParent
492: .getChildren().size());
493: logger.log(BasicLevel.DEBUG, "Name can be accessed: "
494: + detachedParent.getName());
495: logger.log(BasicLevel.DEBUG, "Age can be accessed: "
496: + detachedParent.getAge());
497: logger.log(BasicLevel.DEBUG, "Address can be accessed: "
498: + detachedParent.getAddress().getStreet()
499: + ", "
500: + detachedParent.getAddress().getCity()
501: + ", "
502: + detachedParent.getAddress().getCountry()
503: .getCode());
504: Collection childrenTmp = detachedParent.getChildren();
505: logger
506: .log(BasicLevel.DEBUG,
507: "Children names and address.country.code can be accessed: ");
508: Iterator it = childrenTmp.iterator();
509: while (it.hasNext()) {
510: Person p = (Person) it.next();
511: logger.log(BasicLevel.DEBUG, "Child: " + p.getName()
512: + ", " + p.getAge() + ", "
513: + p.getAddress().getStreet() + ", "
514: + p.getAddress().getCity() + ", "
515: + p.getAddress().getCountry().getCode());
516: }
517: it = childrenTmp.iterator();
518: while (it.hasNext()) {
519: Person p = (Person) it.next();
520: Iterator it2 = p.getChildren().iterator();
521: while (it2.hasNext()) {
522: Person person = (Person) it2.next();
523: logger.log(BasicLevel.DEBUG, "\tChild of children "
524: + person.getName()
525: + ", "
526: + person.getAge()
527: + ", "
528: + person.getAddress().getStreet()
529: + ", "
530: + person.getAddress().getCity()
531: + ", "
532: + person.getAddress().getCountry()
533: .getCode());
534: }
535: }
536: it = childrenTmp.iterator();
537: while (it.hasNext()) {
538: Person p = (Person) it.next();
539: Iterator it2 = p.getChildren().iterator();
540: while (it2.hasNext()) {
541: Person person = (Person) it2.next();
542: Iterator it3 = person.getChildren().iterator();
543: while (it3.hasNext()) {
544: Person pf = (Person) it3.next();
545: logger.log(BasicLevel.DEBUG, "\t\tChild: "
546: + pf.toString());
547: }
548: }
549: }
550: } catch (Exception e) {
551: assertEquals("Supposed to be a "
552: + JDODetachedFieldAccessException.class.getName(),
553: JDODetachedFieldAccessException.class, e.getClass());
554: assertTrue(e.getMessage().indexOf("children") != -1);
555: if (e instanceof JDODetachedFieldAccessException
556: && e.getMessage().indexOf("children") != -1) {
557: logger.log(BasicLevel.DEBUG,
558: "correct type exception caught: " + e);
559: } else {
560: logger.log(BasicLevel.DEBUG, "Error: " + e);
561: fail(e.getMessage());
562: }
563: } finally {
564: if (pm.currentTransaction().isActive())
565: pm.currentTransaction().rollback();
566: pm.close();
567: }
568: }
569:
570: /**
571: * Test the loading with the detailChildren fetch group:
572: * test the definition of a fetch-group in the jdo file with a depth attribute defined twice for a field
573: * one of them is unlimited
574: * <field name="a" depth="X"/>
575: */
576: public void testLoadingDoubleDepthUnlimited() {
577: logger
578: .log(BasicLevel.DEBUG,
579: "************testLoadingDoubleDepthUnlimited**************");
580: Country country = new Country("p", "Portugal");
581: Address address = new Address("Rue Christiano", "Lisbonne",
582: country);
583: Person parent = new Person();
584: parent.setName("Simoes Joel");
585: parent.setAge(63);
586: parent.setAddress(address);
587: Person child1 = new Person("Simoes Sofia", address, null, 30);
588: Person child2 = new Person("Simoes Michael", address, null, 40);
589: Set children = new HashSet();
590: children.add(child1);
591: children.add(child2);
592: parent.setChildren(children);
593: Person child11 = new Person("Simoes Maria", address, null, 14);
594: Person child21 = new Person("Simoes Juan", address, null, 11);
595: Set children1 = new HashSet();
596: children1.add(child11);
597: Set children2 = new HashSet();
598: children2.add(child21);
599: child1.setChildren(children1);
600: child2.setChildren(children2);
601:
602: PersistenceManager pm = pmf.getPersistenceManager();
603: FetchPlan fp = pm.getFetchPlan();
604: fp.clearGroups();
605: fp.addGroup("detailChildren3").removeGroup("default");
606: pm.currentTransaction().begin();
607: logger.log(BasicLevel.DEBUG, "make persistent the person "
608: + parent.toString());
609: pm.makePersistent(parent);
610: pm.currentTransaction().commit();
611: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
612:
613: try {
614: pm.currentTransaction().begin();
615: Person detachedParent = (Person) pm.detachCopy(parent);
616: assertEquals(parent.getName(), detachedParent.getName());
617: assertEquals(parent.getAge(), detachedParent.getAge());
618: assertEquals(parent.getAddress().getCity(), detachedParent
619: .getAddress().getCity());
620: assertEquals(parent.getAddress().getCountry().getCode(),
621: detachedParent.getAddress().getCountry().getCode());
622: assertEquals(parent.getAddress().getCountry().getName(),
623: detachedParent.getAddress().getCountry().getName());
624: assertEquals(parent.getAddress().getStreet(),
625: detachedParent.getAddress().getStreet());
626: assertEquals(parent.getChildren().size(), detachedParent
627: .getChildren().size());
628: logger.log(BasicLevel.DEBUG, "Name can be accessed: "
629: + detachedParent.getName());
630: logger.log(BasicLevel.DEBUG, "Age can be accessed: "
631: + detachedParent.getAge());
632: logger.log(BasicLevel.DEBUG, "Address can be accessed: "
633: + detachedParent.getAddress().getStreet()
634: + ", "
635: + detachedParent.getAddress().getCity()
636: + ", "
637: + detachedParent.getAddress().getCountry()
638: .getCode());
639: Collection childrenTmp = detachedParent.getChildren();
640: logger
641: .log(BasicLevel.DEBUG,
642: "Children names and address.country.code can be accessed: ");
643: Iterator it = childrenTmp.iterator();
644: while (it.hasNext()) {
645: Person p = (Person) it.next();
646: logger.log(BasicLevel.DEBUG, "Child: " + p.getName()
647: + ", " + p.getAge() + ", "
648: + p.getAddress().getStreet() + ", "
649: + p.getAddress().getCity() + ", "
650: + p.getAddress().getCountry().getCode());
651: }
652: it = childrenTmp.iterator();
653: while (it.hasNext()) {
654: Person p = (Person) it.next();
655: Iterator it2 = p.getChildren().iterator();
656: while (it2.hasNext()) {
657: Person person = (Person) it2.next();
658: logger.log(BasicLevel.DEBUG, "\tChild of children "
659: + person.toString());
660: }
661: }
662: pm.close();
663: } catch (Exception e) {
664: logger.log(BasicLevel.DEBUG, "Error: " + e);
665: fail(e.getMessage());
666: } finally {
667: if (pm.currentTransaction().isActive())
668: pm.currentTransaction().rollback();
669: pm.close();
670: }
671: }
672:
673: /**
674: * Test the loading with the keyValue fetchgroup:
675: * test the definition of a fetch-group with a map, loading both keys and values
676: * <field name="map#key">
677: * <field name="map#value">
678: */
679: public void testLoadingMapKeyValue() {
680: logger.log(BasicLevel.DEBUG,
681: "************testLoadingMapKeyValue**************");
682: Node n1 = new Node("n1");
683: Node n2 = new Node("n2");
684: Node n3 = new Node("n3");
685: Node n4 = new Node("n4");
686: Node n5 = new Node("n5");
687:
688: n1.addEdge(n2.getName(), new EdgeWeight(1));
689: n1.addEdge(n3.getName(), new EdgeWeight(2));
690:
691: n2.addEdge(n4.getName(), new EdgeWeight(7));
692: n2.addEdge(n5.getName(), new EdgeWeight(4));
693:
694: PersistenceManager pm = pmf.getPersistenceManager();
695: FetchPlan fp = pm.getFetchPlan();
696: fp.clearGroups();
697: fp.addGroup("keyValue").removeGroup("default");
698: pm.currentTransaction().begin();
699: logger.log(BasicLevel.DEBUG, "make persistent the nodes "
700: + n1.toString() + ", " + n2.toString() + ", "
701: + n3.toString() + ", " + n4.toString() + ", "
702: + n5.toString());
703: pm.makePersistent(n1);
704: pm.makePersistent(n2);
705: pm.makePersistent(n3);
706: pm.makePersistent(n4);
707: pm.makePersistent(n5);
708: pm.currentTransaction().commit();
709: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
710:
711: try {
712: Node detachedN1 = (Node) pm.detachCopy(n1);
713: assertEquals(n1.getName(), detachedN1.getName());
714: assertEquals(n1.getEdges().size(), detachedN1.getEdges()
715: .size());
716: logger.log(BasicLevel.DEBUG, "Name can be accessed: "
717: + detachedN1.getName());
718: logger.log(BasicLevel.DEBUG, "ToString(): "
719: + detachedN1.toString());
720: } catch (Exception e) {
721: logger.log(BasicLevel.DEBUG, "Error: " + e);
722: fail(e.getMessage());
723: } finally {
724: if (pm.currentTransaction().isActive())
725: pm.currentTransaction().rollback();
726: pm.close();
727: }
728: }
729:
730: /**
731: * Test the loading with the keyOnly fetchgroup:
732: * test the definition of a fetch-group with a map, loading only keys
733: * <field name="map#key">
734: */
735: public void testLoadingMapKeyOnly() {
736: logger.log(BasicLevel.DEBUG,
737: "************testLoadingMapKeyOnly**************");
738:
739: Node n1 = new Node("n11");
740: Node n2 = new Node("n21");
741: Node n3 = new Node("n31");
742: Node n4 = new Node("n41");
743: Node n5 = new Node("n51");
744:
745: n1.addEdge(n2.getName(), new EdgeWeight(1));
746: n1.addEdge(n3.getName(), new EdgeWeight(2));
747:
748: n2.addEdge(n4.getName(), new EdgeWeight(7));
749: n2.addEdge(n5.getName(), new EdgeWeight(4));
750:
751: PersistenceManager pm = pmf.getPersistenceManager();
752: FetchPlan fp = pm.getFetchPlan();
753: fp.clearGroups();
754: fp.addGroup("keyOnly").removeGroup("default");
755: pm.currentTransaction().begin();
756: logger.log(BasicLevel.DEBUG, "make persistent the nodes "
757: + n1.toString() + ", " + n2.toString() + ", "
758: + n3.toString() + ", " + n4.toString() + ", "
759: + n5.toString());
760: pm.makePersistent(n1);
761: pm.makePersistent(n2);
762: pm.makePersistent(n3);
763: pm.makePersistent(n4);
764: pm.makePersistent(n5);
765: pm.currentTransaction().commit();
766: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
767:
768: try {
769: Node detachedN1 = (Node) pm.detachCopy(n1);
770: assertEquals(n1.getName(), detachedN1.getName());
771: assertEquals(n1.getEdges().size(), detachedN1.getEdges()
772: .size());
773: logger.log(BasicLevel.DEBUG, "Name can be accessed: "
774: + detachedN1.getName());
775: logger.log(BasicLevel.DEBUG, "ToString(): "
776: + detachedN1.toString());
777: } catch (Exception e) {
778: logger.log(BasicLevel.DEBUG, "Error: " + e);
779: fail(e.getMessage());
780: } finally {
781: if (pm.currentTransaction().isActive())
782: pm.currentTransaction().rollback();
783: pm.close();
784: }
785: }
786:
787: public void testRemovingOfPersistentObject() {
788: PersistenceManager pm = pmf.getPersistenceManager();
789: try {
790: Class[] cs = new Class[] { Person.class, Address.class,
791: Country.class, Node.class, EdgeWeight.class };
792: pm.currentTransaction().begin();
793: for (int i = 0; i < cs.length; i++) {
794: Query query = pm.newQuery(cs[i]);
795: Collection col = (Collection) query.execute();
796: Iterator it = col.iterator();
797: while (it.hasNext()) {
798: Object o = it.next();
799: Assert.assertNotNull(
800: "null object in the query result"
801: + cs[i].getName(), o);
802: pm.deletePersistent(o);
803:
804: }
805: query.close(col);
806: }
807: pm.currentTransaction().commit();
808: } catch (JDOException e) {
809: Exception ie = ExceptionHelper.getNested(e);
810: logger.log(BasicLevel.ERROR, "", ie);
811: fail(ie.getMessage());
812: } finally {
813: pm.close();
814: }
815: }
816: }
|