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.JDOException;
033: import javax.jdo.PersistenceManager;
034: import javax.jdo.Query;
035:
036: import junit.framework.Assert;
037:
038: import org.objectweb.speedo.SpeedoTestHelper;
039: import org.objectweb.speedo.api.ExceptionHelper;
040: import org.objectweb.speedo.pobjects.fetchgroup.Address;
041: import org.objectweb.speedo.pobjects.fetchgroup.Country;
042: import org.objectweb.speedo.pobjects.fetchgroup.EdgeWeight;
043: import org.objectweb.speedo.pobjects.fetchgroup.Node;
044: import org.objectweb.speedo.pobjects.fetchgroup.Person;
045: import org.objectweb.util.monolog.api.BasicLevel;
046:
047: /**
048: *
049: * @author Y.Bersihand
050: */
051: public class TestRefreshFetchGroup extends SpeedoTestHelper {
052:
053: public TestRefreshFetchGroup(String s) {
054: super (s);
055: }
056:
057: protected String getLoggerName() {
058: return LOG_NAME + ".rt.fetchgroup.TestRefreshFetchGroup";
059: }
060:
061: /**
062: * Test the refresh with the detail fetch group :
063: * test the definition of a fetch-group in the jdo file with a a.b.c field
064: * <field name="a.b.c">
065: */
066: public void testRefreshReference() {
067: logger.log(BasicLevel.DEBUG,
068: "***************testRefreshReference*****************");
069: Country country = new Country("it", "Italie");
070: Address address = new Address("Rue Spiaggi", "Milan", country);
071: Person parent = new Person();
072: parent.setName("Del Piero Joel");
073: parent.setAge(32);
074: parent.setAddress(address);
075: Person child1 = new Person("Del Piero Sophie", address, null,
076: 14);
077: Person child2 = new Person("Del Piero Mikael", address, null,
078: 11);
079: Set children = new HashSet();
080: children.add(child1);
081: children.add(child2);
082: parent.setChildren(children);
083: PersistenceManager pm = pmf.getPersistenceManager();
084: try {
085: FetchPlan fp = pm.getFetchPlan();
086: fp.clearGroups();
087: fp.addGroup("detail").removeGroup("default");
088: pm.currentTransaction().begin();
089: logger.log(BasicLevel.DEBUG, "make persistent the person "
090: + parent.toString());
091: pm.makePersistent(parent);
092: Object id = pm.getObjectId(parent);
093: pm.currentTransaction().commit();
094: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
095:
096: pm.currentTransaction().begin();
097: //update the age and the address
098: parent.setAge(99);
099: Address newAddress = new Address("rue Ampere", "Rennes",
100: new Country("bz", "Bretagne"));
101: parent.setAddress(newAddress);
102: pm.refresh(parent);
103: pm.currentTransaction().commit();
104: assertEquals(32, parent.getAge());
105: assertEquals(address.getCity(), parent.getAddress()
106: .getCity());
107: assertEquals(address.getStreet(), parent.getAddress()
108: .getStreet());
109: } catch (Exception e) {
110: if (pm.currentTransaction().isActive())
111: pm.currentTransaction().rollback();
112: fail(e.getMessage());
113: } finally {
114: pm.close();
115: }
116: }
117:
118: /**
119: * Test the refresh with the detail+children-names fetch group:
120: * test the definition of a fetch-group in the jdo file with a a#element.b field
121: * <field name="a#element.b">
122: */
123: public void testRefreshArrayElement() {
124: logger.log(BasicLevel.DEBUG,
125: "************testRefreshArrayElement**************");
126: Country country = new Country("be", "Belgique");
127: Address address = new Address("Rue Anvers", "Bruges", country);
128: Person parent = new Person();
129: parent.setName("Dermuck Joel");
130: parent.setAge(32);
131: parent.setAddress(address);
132: int ageChild1 = 14;
133: int ageChil2 = 11;
134: Person child1 = new Person("Dermuck Sophie", address, null,
135: ageChild1);
136: Person child2 = new Person("Dermuck Mikael", address, null,
137: ageChil2);
138: int totalAgeChildren = 14 + 11;
139: Set children = new HashSet();
140: children.add(child1);
141: children.add(child2);
142: parent.setChildren(children);
143:
144: PersistenceManager pm = pmf.getPersistenceManager();
145: FetchPlan fp = pm.getFetchPlan();
146: fp.clearGroups();
147: fp.addGroup("detail+children-names").removeGroup("default");
148: pm.currentTransaction().begin();
149: logger.log(BasicLevel.DEBUG, "make persistent the person "
150: + parent.toString());
151: pm.makePersistent(parent);
152: pm.currentTransaction().commit();
153: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
154: try {
155: pm.currentTransaction().begin();
156: //update the first child age and address
157: Person p = (Person) parent.getChildren().iterator().next();
158: p.setAge(2000);
159: Address newAddress = new Address("Rue Refresh", "Moon",
160: new Country("m", "mars"));
161: p.setAddress(newAddress);
162: pm.refresh(parent);
163:
164: int totalAgeRefreshed = 0;
165: Iterator it = parent.getChildren().iterator();
166: Person ch = null;
167: while (it.hasNext()) {
168: ch = (Person) it.next();
169: totalAgeRefreshed += ch.getAge();
170: assertEquals(address.getCity(), ch.getAddress()
171: .getCity());
172: assertEquals(address.getStreet(), ch.getAddress()
173: .getStreet());
174: }
175: pm.currentTransaction().commit();
176: assertEquals(totalAgeChildren, totalAgeRefreshed);
177: } catch (Exception e) {
178: if (pm.currentTransaction().isActive())
179: pm.currentTransaction().rollback();
180: fail(e.getMessage());
181: } finally {
182: pm.close();
183: }
184: }
185:
186: /**
187: * Test the refresh with the detail+children-list fetch group:
188: * test the definition of a fetch-group in the jdo file with a fetch-group attribute in a field
189: * <field name="a" fetch-group="fg"/>
190: */
191: public void testRefreshFetchGroupField() {
192: logger.log(BasicLevel.DEBUG,
193: "************testRefreshFetchGroupField**************");
194: Country country = new Country("us", "Etats-Unis");
195: Address address = new Address("Rue Enclif", "San Diego",
196: country);
197: Person parent = new Person();
198: parent.setName("Smith Joel");
199: parent.setAge(32);
200: parent.setAddress(address);
201: Person child1 = new Person("Smith Sofia", address, null, 14);
202: Person child2 = new Person("Smith Michael", address, null, 11);
203: Set children = new HashSet();
204: children.add(child1);
205: children.add(child2);
206: parent.setChildren(children);
207:
208: PersistenceManager pm = pmf.getPersistenceManager();
209: FetchPlan fp = pm.getFetchPlan();
210: fp.clearGroups();
211: fp.addGroup("detail+children-list").removeGroup("default");
212: pm.currentTransaction().begin();
213: logger.log(BasicLevel.DEBUG, "make persistent the person "
214: + parent.toString());
215: pm.makePersistent(parent);
216: pm.currentTransaction().commit();
217: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
218: try {
219: pm.currentTransaction().begin();
220: // update the first child age and address
221: Person p = (Person) parent.getChildren().iterator().next();
222: p.setAge(2000);
223: Address newAddress = new Address("Rue Far", "Moon",
224: new Country("n", "neptune"));
225: p.setAddress(newAddress);
226: pm.refresh(parent);
227:
228: pm.currentTransaction().commit();
229: Person ch = (Person) parent.getChildren().iterator().next();
230: int expectedAge = 0;
231: if (ch.getName().equals(child1.getName())) {
232: expectedAge = child1.getAge();
233: } else {
234: expectedAge = child2.getAge();
235: }
236: assertEquals(expectedAge, ch.getAge());
237: assertEquals(address.getCity(), ch.getAddress().getCity());
238: assertEquals(address.getStreet(), ch.getAddress()
239: .getStreet());
240: } catch (Exception e) {
241: if (pm.currentTransaction().isActive())
242: pm.currentTransaction().rollback();
243: fail(e.getMessage());
244: } finally {
245: pm.close();
246: }
247:
248: }
249:
250: /**
251: * Test the refresh with the detailChildren fetch group:
252: * test the definition of a fetch-group in the jdo file with a depth attribute in a field
253: * for recursive reference
254: * <field name="a" depth="X"/>
255: */
256: public void testRefreshRecursiveDepth() {
257: logger.log(BasicLevel.DEBUG,
258: "************testRefreshRecursiveDepth**************");
259: Country country = new Country("sp", "Espagne");
260: Address address = new Address("Rue Rio", "Santander", country);
261: Person parent = new Person();
262: parent.setName("Casillas Joel");
263: parent.setAge(63);
264: parent.setAddress(address);
265: Person child1 = new Person("Casillas Sofia", address, null, 30);
266: Person child2 = new Person("Casillas Michael", address, null,
267: 40);
268: Set children = new HashSet();
269: children.add(child1);
270: children.add(child2);
271: parent.setChildren(children);
272: Person child11 = new Person("Casillas Maria", address, null, 14);
273: Person child21 = new Person("Casillas Juan", address, null, 11);
274: Set children1 = new HashSet();
275: children1.add(child11);
276: Set children2 = new HashSet();
277: children2.add(child21);
278: child1.setChildren(children1);
279: child2.setChildren(children2);
280:
281: PersistenceManager pm = pmf.getPersistenceManager();
282: FetchPlan fp = pm.getFetchPlan();
283: fp.clearGroups();
284: fp.addGroup("detailChildren").removeGroup("default");
285: pm.currentTransaction().begin();
286: logger.log(BasicLevel.DEBUG, "make persistent the person "
287: + parent.toString());
288: pm.makePersistent(parent);
289: pm.currentTransaction().commit();
290: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
291: try {
292: Address a = new Address("Rue So", "Moon", new Country("k",
293: "Krypton"));
294:
295: pm.currentTransaction().begin();
296: Person p = (Person) parent.getChildren().iterator().next();
297: p.setAge(2000);
298: p.setAddress(a);
299: Person pc = (Person) p.getChildren().iterator().next();
300: pc.setAge(3000);
301: pc.setAddress(a);
302: pm.refresh(parent);
303: logger
304: .log(BasicLevel.DEBUG,
305: "Warning: the child of child will not be refreshed.");
306: pm.currentTransaction().commit();
307:
308: Person ch = (Person) parent.getChildren().iterator().next();
309: int expectedAge = 0;
310: if (ch.getName().equals(child1.getName())) {
311: expectedAge = child1.getAge();
312: } else {
313: expectedAge = child2.getAge();
314: }
315: assertEquals(expectedAge, ch.getAge());
316: assertEquals(address.getCity(), ch.getAddress().getCity());
317: ch = (Person) ch.getChildren().iterator().next();
318: assertEquals(3000, ch.getAge());
319: assertEquals("Moon", ch.getAddress().getCity());
320: } catch (Exception e) {
321: if (pm.currentTransaction().isActive())
322: pm.currentTransaction().rollback();
323: fail(e.getMessage());
324: } finally {
325: pm.close();
326: }
327: }
328:
329: /**
330: * Test the refresh with the detailChildren fetch group:
331: * test the definition of a fetch-group in the jdo file with a depth attribute defined twice for a field
332: * <field name="a" depth="X"/>
333: */
334: public void testRefreshDoubleDepth() {
335: logger.log(BasicLevel.DEBUG,
336: "************testRefreshDoubleDepth**************");
337: Country country = new Country("bl", "Belarus");
338: Address address = new Address("Rue Kaloc", "Minsk", country);
339: Person parent = new Person();
340: parent.setName("Castuk Joel");
341: parent.setAge(63);
342: parent.setAddress(address);
343: Person child1 = new Person("Castuk Sofia", address, null, 40);
344: Person child2 = new Person("Castuk Michael", address, null, 40);
345: Set children = new HashSet();
346: children.add(child1);
347: children.add(child2);
348: parent.setChildren(children);
349: Person child11 = new Person("Castuk Maria", address, null, 11);
350: Person child21 = new Person("Castuk 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("detailChildren2").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: try {
369: pm.currentTransaction().begin();
370: Address a = new Address("Rue Away", "Moon", new Country(
371: "pl", "Pluton"));
372: Person p = (Person) parent.getChildren().iterator().next();
373: p.setAge(2000);
374: p.setAddress(a);
375: Person pc = (Person) p.getChildren().iterator().next();
376: pc.setAge(3000);
377: pc.setAddress(a);
378: pm.refresh(parent);
379: pm.currentTransaction().commit();
380:
381: Person ch = (Person) parent.getChildren().iterator().next();
382: assertEquals(40, ch.getAge());
383: assertEquals(address.getCity(), ch.getAddress().getCity());
384: ch = (Person) ch.getChildren().iterator().next();
385: assertEquals(11, ch.getAge());
386: assertEquals(address.getCity(), ch.getAddress().getCity());
387: } catch (Exception e) {
388: if (pm.currentTransaction().isActive())
389: pm.currentTransaction().rollback();
390: fail(e.getMessage());
391: } finally {
392: pm.close();
393: }
394: }
395:
396: /**
397: * Test the refresh with the detailChildren fetch group:
398: * test the definition of a fetch-group in the jdo file with a depth attribute defined twice for a field
399: * one of them is unlimited
400: * <field name="a" depth="X"/>
401: */
402: public void testRefreshDoubleDepthUnlimited() {
403: logger
404: .log(BasicLevel.DEBUG,
405: "************testRefreshDoubleDepthUnlimited**************");
406:
407: Country country = new Country("p", "Portugal");
408: Address address = new Address("Rue Christiano", "Lisbonne",
409: country);
410: Person grandParent = new Person();
411: grandParent.setName("Simoes Joel");
412: grandParent.setAge(90);
413: grandParent.setAddress(address);
414: Person parent1 = new Person("Simoes Sofia", address, null, 70);
415: Person parent2 = new Person("Simoes Michael", address, null, 70);
416: Set parent = new HashSet();
417: parent.add(parent1);
418: parent.add(parent2);
419: grandParent.setChildren(parent);
420: Person child1 = new Person("Simoes Maria", address, null, 40);
421: Person child2 = new Person("Simoes Juan", address, null, 40);
422: Set children1 = new HashSet();
423: children1.add(child1);
424: Set children2 = new HashSet();
425: children2.add(child2);
426: parent1.setChildren(children1);
427: parent2.setChildren(children2);
428: Person grandChild1 = new Person("Simoes Leia", address, null,
429: 10);
430: Person grandChild2 = new Person("Simoes Carlos", address, null,
431: 10);
432: Set grandChildren1 = new HashSet();
433: grandChildren1.add(grandChild1);
434: Set grandChildren2 = new HashSet();
435: grandChildren2.add(grandChild2);
436: child1.setChildren(grandChildren1);
437: child2.setChildren(grandChildren2);
438:
439: PersistenceManager pm = pmf.getPersistenceManager();
440: FetchPlan fp = pm.getFetchPlan();
441: fp.clearGroups();
442: fp.addGroup("detailChildren3").removeGroup("default");
443: pm.currentTransaction().begin();
444: logger.log(BasicLevel.DEBUG, "make persistent the person "
445: + grandParent.toString());
446: pm.makePersistent(grandParent);
447: pm.currentTransaction().commit();
448: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
449:
450: try {
451: pm.currentTransaction().begin();
452: Address a = new Address("Rue From", "Moon", new Country(
453: "sl", "Solarus"));
454: Person pParent = (Person) grandParent.getChildren()
455: .iterator().next();
456: pParent.setAge(2000);
457: pParent.setAddress(a);
458: Person pChild = (Person) pParent.getChildren().iterator()
459: .next();
460: pChild.setAge(3000);
461: pChild.setAddress(a);
462: Person pGrandChild = (Person) pChild.getChildren()
463: .iterator().next();
464: pGrandChild.setAge(4000);
465: pGrandChild.setAddress(a);
466: pm.refresh(grandParent);
467: pm.currentTransaction().commit();
468:
469: Person cParent = (Person) grandParent.getChildren()
470: .iterator().next();
471: assertEquals(70, cParent.getAge());
472: assertEquals(address.getCity(), cParent.getAddress()
473: .getCity());
474: Person cChild = (Person) cParent.getChildren().iterator()
475: .next();
476: assertEquals(40, cChild.getAge());
477: assertEquals(address.getCity(), cChild.getAddress()
478: .getCity());
479: Person cGrandChild = (Person) cChild.getChildren()
480: .iterator().next();
481: assertEquals(10, cGrandChild.getAge());
482: assertEquals(address.getCity(), cGrandChild.getAddress()
483: .getCity());
484:
485: } catch (Exception e) {
486: if (pm.currentTransaction().isActive())
487: pm.currentTransaction().rollback();
488: fail(e.getMessage());
489: } finally {
490: pm.close();
491: }
492: }
493:
494: /**
495: * Test the refresh with the keyValue fetchgroup:
496: * test the definition of a fetch-group with a map, refresh both keys and values
497: * <field name="map#key">
498: * <field name="map#value">
499: */
500: public void testRefreshMapKeyValue() {
501: logger.log(BasicLevel.DEBUG,
502: "************testRefreshMapKeyValue**************");
503: Node n1 = new Node("n1");
504: Node n2 = new Node("n2");
505: Node n3 = new Node("n3");
506: Node n4 = new Node("n4");
507: Node n5 = new Node("n5");
508:
509: n1.addEdge(n2.getName(), new EdgeWeight(1));
510: n1.addEdge(n3.getName(), new EdgeWeight(2));
511:
512: n2.addEdge(n4.getName(), new EdgeWeight(7));
513: n2.addEdge(n5.getName(), new EdgeWeight(4));
514:
515: PersistenceManager pm = pmf.getPersistenceManager();
516: FetchPlan fp = pm.getFetchPlan();
517: fp.clearGroups();
518: fp.addGroup("keyValue").removeGroup("default");
519: pm.currentTransaction().begin();
520: logger.log(BasicLevel.DEBUG, "make persistent the nodes "
521: + n1.toString() + ", " + n2.toString() + ", "
522: + n3.toString() + ", " + n4.toString() + ", "
523: + n5.toString());
524: pm.makePersistent(n1);
525: pm.makePersistent(n2);
526: pm.makePersistent(n3);
527: pm.makePersistent(n4);
528: pm.makePersistent(n5);
529: pm.currentTransaction().commit();
530:
531: FetchPlan f = pm.getFetchPlan();
532: logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
533:
534: try {
535: pm.currentTransaction().begin();
536: String nodeName = (String) n1.getEdges().keySet()
537: .iterator().next();
538: EdgeWeight ew = (EdgeWeight) n1.getEdges().get(nodeName);
539: ew.setWeight(3000);
540: pm.refresh(n1);
541: pm.currentTransaction().commit();
542: assertEquals(2, ew.getWeight());
543: } catch (Exception e) {
544: if (pm.currentTransaction().isActive())
545: pm.currentTransaction().rollback();
546: fail(e.getMessage());
547: } finally {
548: pm.close();
549: }
550: }
551:
552: /**
553: * Test the refresh with the keyOnly fetchgroup:
554: * test the definition of a fetch-group with a map, refresh only keys
555: * <field name="map#key">
556: */
557: public void testRefreshMapKeyOnly() {
558: logger.log(BasicLevel.DEBUG,
559: "************testRefreshMapKeyOnly**************");
560:
561: Node n1 = new Node("n11");
562: Node n2 = new Node("n21");
563: Node n3 = new Node("n31");
564: Node n4 = new Node("n41");
565: Node n5 = new Node("n51");
566:
567: n1.addEdge(n2.getName(), new EdgeWeight(1));
568: n1.addEdge(n3.getName(), new EdgeWeight(2));
569:
570: n2.addEdge(n4.getName(), new EdgeWeight(7));
571: n2.addEdge(n5.getName(), new EdgeWeight(4));
572:
573: PersistenceManager pm = pmf.getPersistenceManager();
574: FetchPlan fp = pm.getFetchPlan();
575: fp.clearGroups();
576: fp.addGroup("keyOnly").removeGroup("default");
577: pm.currentTransaction().begin();
578: logger.log(BasicLevel.DEBUG, "make persistent the nodes "
579: + n1.toString() + ", " + n2.toString() + ", "
580: + n3.toString() + ", " + n4.toString() + ", "
581: + n5.toString());
582: pm.makePersistent(n1);
583: pm.makePersistent(n2);
584: pm.makePersistent(n3);
585: pm.makePersistent(n4);
586: pm.makePersistent(n5);
587: pm.currentTransaction().commit();
588:
589: FetchPlan f = pm.getFetchPlan();
590: logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
591:
592: try {
593: pm.currentTransaction().begin();
594: String nodeName = (String) n1.getEdges().keySet()
595: .iterator().next();
596: EdgeWeight ew = (EdgeWeight) n1.getEdges().get(nodeName);
597: ew.setWeight(3000);
598: pm.refresh(n1);
599: pm.currentTransaction().commit();
600: assertEquals(3000, ew.getWeight());
601: } catch (Exception e) {
602: if (pm.currentTransaction().isActive())
603: pm.currentTransaction().rollback();
604: fail(e.getMessage());
605: } finally {
606: pm.close();
607: }
608: }
609:
610: public void testRemovingOfPersistentObject() {
611: PersistenceManager pm = pmf.getPersistenceManager();
612: try {
613: Class[] cs = new Class[] { Address.class, Country.class,
614: Person.class, Node.class, EdgeWeight.class };
615: pm.currentTransaction().begin();
616: for (int i = 0; i < cs.length; i++) {
617: Query query = pm.newQuery(cs[i]);
618: Collection col = (Collection) query.execute();
619: Iterator it = col.iterator();
620: while (it.hasNext()) {
621: Object o = it.next();
622: Assert.assertNotNull(
623: "null object in the query result"
624: + cs[i].getName(), o);
625: pm.deletePersistent(o);
626:
627: }
628: query.close(col);
629: }
630: pm.currentTransaction().commit();
631: } catch (JDOException e) {
632: Exception ie = ExceptionHelper.getNested(e);
633: logger.log(BasicLevel.ERROR, "", ie);
634: fail(ie.getMessage());
635: } finally {
636: pm.close();
637: }
638: }
639: }
|