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 org.objectweb.speedo.SpeedoTestHelper;
038: import org.objectweb.speedo.api.ExceptionHelper;
039: import org.objectweb.speedo.pobjects.fetchgroup.Address;
040: import org.objectweb.speedo.pobjects.fetchgroup.Country;
041: import org.objectweb.speedo.pobjects.fetchgroup.Person;
042: import org.objectweb.util.monolog.api.BasicLevel;
043:
044: /**
045: * Test the 4 pre-defined fetchgroups:
046: * 1- all : all the fields of the class are loaded
047: * 2- default: all the fields defined as default-fetch-group="true" in the jdo file are loaded
048: * 3- none: only the primary key of the class is loaded but int he case of speedo (and because of jorm)
049: * all primitive fields are loaded.
050: * 4- values: all the fields that are included in the default fetch group by default
051: * (primitives, wrappers, String, Date, etc...) are loaded.
052: * @author Y.Bersihand
053: */
054: public class TestPredefinedFetchGroup extends SpeedoTestHelper {
055:
056: public TestPredefinedFetchGroup(String s) {
057: super (s);
058: }
059:
060: protected String getLoggerName() {
061: return LOG_NAME + ".rt.fetchgroup.TestPredefinedFetchGroup";
062: }
063:
064: /**
065: * Test the loading with the "all" fetch group : all the fields of the class are loaded
066: */
067: public void testLoadingAll() {
068: logger.log(BasicLevel.DEBUG,
069: "***************testLoadingAll*****************");
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.addGroup("all").removeGroup("default");
088: pm.currentTransaction().begin();
089: logger.log(BasicLevel.DEBUG, "make persistent the person "
090: + parent.toString());
091: pm.makePersistent(parent);
092: pm.currentTransaction().commit();
093:
094: FetchPlan f = pm.getFetchPlan();
095: logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
096:
097: try {
098: pm.currentTransaction().begin();
099: Person detachedParent = (Person) pm.detachCopy(parent);
100: logger.log(BasicLevel.DEBUG, "All fields can be accessed: "
101: + detachedParent.toString());
102: assertEquals(parent.getName(), detachedParent.getName());
103: assertEquals(parent.getAge(), detachedParent.getAge());
104: assertEquals(parent.getAddress().getCity(), detachedParent
105: .getAddress().getCity());
106: assertEquals(parent.getAddress().getCountry().getCode(),
107: detachedParent.getAddress().getCountry().getCode());
108: assertEquals(parent.getAddress().getCountry().getName(),
109: detachedParent.getAddress().getCountry().getName());
110: assertEquals(parent.getAddress().getStreet(),
111: detachedParent.getAddress().getStreet());
112: assertEquals(parent.getChildren().size(), detachedParent
113: .getChildren().size());
114: } catch (Exception e) {
115: logger.log(BasicLevel.DEBUG, "Error: " + e);
116: } finally {
117: if (pm.currentTransaction().isActive())
118: pm.currentTransaction().rollback();
119: pm.close();
120: }
121: }
122:
123: /**
124: * Test the loading with the "default" fetch group
125: */
126: public void testLoadingDefault() {
127: logger.log(BasicLevel.DEBUG,
128: "***************testLoadingDefault*****************");
129: Country country = new Country("fr", "France");
130: Address address = new Address("rue Anatole France", "Tours",
131: country);
132: Person parent = new Person();
133: parent.setName("Bordes Joel");
134: parent.setAge(32);
135: parent.setAddress(address);
136: Person child1 = new Person("Bordes Sophie", address, null, 14);
137: Person child2 = new Person("Bordes Mikael", address, null, 11);
138: Set children = new HashSet();
139: children.add(child1);
140: children.add(child2);
141: parent.setChildren(children);
142:
143: PersistenceManager pm = pmf.getPersistenceManager();
144: FetchPlan fp = pm.getFetchPlan();
145: fp.removeGroup("all").addGroup("default");
146: pm.currentTransaction().begin();
147: logger.log(BasicLevel.DEBUG, "make persistent the person "
148: + parent.toString());
149: pm.makePersistent(parent);
150: pm.currentTransaction().commit();
151:
152: FetchPlan f = pm.getFetchPlan();
153: logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
154:
155: try {
156: pm.currentTransaction().begin();
157: Person detachedParent = (Person) pm.detachCopy(parent);
158: logger.log(BasicLevel.DEBUG, "Name can be accessed: "
159: + detachedParent.getName());
160: logger.log(BasicLevel.DEBUG, "Age can be accessed: "
161: + detachedParent.getAge());
162: logger.log(BasicLevel.DEBUG, "Address can be accessed: "
163: + detachedParent.getAddress().toString());
164: assertEquals(parent.getName(), detachedParent.getName());
165: assertEquals(parent.getAge(), detachedParent.getAge());
166: assertEquals(parent.getAddress().getCity(), detachedParent
167: .getAddress().getCity());
168: assertEquals(parent.getAddress().getCountry().getCode(),
169: detachedParent.getAddress().getCountry().getCode());
170: assertEquals(parent.getAddress().getCountry().getName(),
171: detachedParent.getAddress().getCountry().getName());
172: assertEquals(parent.getAddress().getStreet(),
173: detachedParent.getAddress().getStreet());
174: logger.log(BasicLevel.DEBUG,
175: "Children should not be accessed: "
176: + detachedParent.getChildren());
177: } catch (Exception e) {
178: assertEquals(e.getClass(),
179: JDODetachedFieldAccessException.class);
180: pm.close();
181: if (e instanceof JDODetachedFieldAccessException)
182: logger.log(BasicLevel.DEBUG,
183: "Correct exception type caught: " + e);
184: else
185: logger.log(BasicLevel.DEBUG,
186: "Incorrect exception type caught: " + e);
187: } finally {
188: if (pm.currentTransaction().isActive())
189: pm.currentTransaction().rollback();
190: pm.close();
191: }
192: }
193:
194: /**
195: * Test the loading with the "none" fetch group : only the pk is loaded
196: */
197: public void testLoadingNone() {
198: logger.log(BasicLevel.DEBUG,
199: "***************testLoadingNone*****************");
200: Country country = new Country("uk", "Royaume-Uni");
201: Address address = new Address("Sharrow Street", "Luton",
202: country);
203: Person parent = new Person();
204: parent.setName("None Joel");
205: parent.setAge(32);
206: parent.setAddress(address);
207: Person child1 = new Person("None Sophie", address, null, 14);
208: Person child2 = new Person("None Mikael", address, null, 11);
209: Set children = new HashSet();
210: children.add(child1);
211: children.add(child2);
212: parent.setChildren(children);
213:
214: PersistenceManager pm = pmf.getPersistenceManager();
215: FetchPlan fp = pm.getFetchPlan();
216: fp.removeGroup("default").addGroup("none");
217: pm.currentTransaction().begin();
218: logger.log(BasicLevel.DEBUG, "make persistent the person "
219: + parent.toString());
220: pm.makePersistent(parent);
221: pm.currentTransaction().commit();
222:
223: FetchPlan f = pm.getFetchPlan();
224: logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
225:
226: try {
227: pm.currentTransaction().begin();
228: Person detachedParent = (Person) pm.detachCopy(parent);
229: logger.log(BasicLevel.DEBUG, "Name can be accessed: "
230: + detachedParent.getName());
231: logger.log(BasicLevel.DEBUG, "Age can be accessed: "
232: + detachedParent.getAge());
233: assertEquals(parent.getName(), detachedParent.getName());
234: assertEquals(parent.getAge(), detachedParent.getAge());
235: logger.log(BasicLevel.DEBUG,
236: "Address should not be accessed: "
237: + detachedParent.getAddress());
238: logger.log(BasicLevel.DEBUG,
239: "Children should not be accessed: "
240: + detachedParent.getChildren());
241: } catch (Exception e) {
242: assertEquals(e.getClass(),
243: JDODetachedFieldAccessException.class);
244: if (e instanceof JDODetachedFieldAccessException)
245: logger.log(BasicLevel.DEBUG,
246: "Correct exception type caught: " + e);
247: else
248: logger.log(BasicLevel.DEBUG,
249: "Incorrect exception type caught: " + e);
250: } finally {
251: if (pm.currentTransaction().isActive())
252: pm.currentTransaction().rollback();
253: pm.close();
254: }
255: }
256:
257: /**
258: * Test the loading with the "values" fetch group : only the values field of the default are loaded
259: */
260: public void testLoadingValues() {
261: logger.log(BasicLevel.DEBUG,
262: "***************testLoadingValues*****************");
263: Country country = new Country("gr", "Grece");
264: Address address = new Address("Feta", "Athenes", country);
265: Person parent = new Person();
266: parent.setName("Kapic Joel");
267: parent.setAge(32);
268: parent.setAddress(address);
269: Person child1 = new Person("Kapic Sophie", address, null, 14);
270: Person child2 = new Person("Kapic Mikael", address, null, 11);
271: Set children = new HashSet();
272: children.add(child1);
273: children.add(child2);
274: parent.setChildren(children);
275:
276: PersistenceManager pm = pmf.getPersistenceManager();
277: FetchPlan fp = pm.getFetchPlan();
278: fp.removeGroup("none").addGroup("values");
279: pm.currentTransaction().begin();
280: logger.log(BasicLevel.DEBUG, "make persistent the person "
281: + parent.toString());
282: pm.makePersistent(parent);
283: pm.currentTransaction().commit();
284:
285: FetchPlan f = pm.getFetchPlan();
286: logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
287:
288: try {
289: pm.currentTransaction().begin();
290: Person detachedParent = (Person) pm.detachCopy(parent);
291: logger.log(BasicLevel.DEBUG, "Name can be accessed: "
292: + detachedParent.getName());
293: logger.log(BasicLevel.DEBUG, "Age can be accessed: "
294: + detachedParent.getAge());
295: assertEquals(parent.getName(), detachedParent.getName());
296: assertEquals(parent.getAge(), detachedParent.getAge());
297: logger.log(BasicLevel.DEBUG,
298: "Address should not be accessed: "
299: + detachedParent.getAddress());
300: logger.log(BasicLevel.DEBUG,
301: "Children should not be accessed: "
302: + detachedParent.getChildren());
303: } catch (Exception e) {
304: assertEquals(e.getClass(),
305: JDODetachedFieldAccessException.class);
306: if (e instanceof JDODetachedFieldAccessException)
307: logger.log(BasicLevel.DEBUG,
308: "Correct exception type caught: " + e);
309: else
310: logger.log(BasicLevel.DEBUG,
311: "Incorrect exception type caught: " + e);
312: } finally {
313: if (pm.currentTransaction().isActive())
314: pm.currentTransaction().rollback();
315: pm.close();
316: }
317: }
318:
319: public void testRemovingOfPersistentObject() {
320: PersistenceManager pm = pmf.getPersistenceManager();
321: try {
322: Class[] cs = new Class[] { Person.class, Address.class,
323: Country.class };
324: pm.currentTransaction().begin();
325: for (int i = 0; i < cs.length; i++) {
326: Query query = pm.newQuery(cs[i]);
327: Collection col = (Collection) query.execute();
328: Iterator it = col.iterator();
329: while (it.hasNext()) {
330: Object o = it.next();
331: assertNotNull("null object in the query result"
332: + cs[i].getName(), o);
333: pm.deletePersistent(o);
334:
335: }
336: query.close(col);
337: }
338: pm.currentTransaction().commit();
339: } catch (JDOException e) {
340: Exception ie = ExceptionHelper.getNested(e);
341: logger.log(BasicLevel.ERROR, "", ie);
342: fail(ie.getMessage());
343: } finally {
344: if (pm.currentTransaction().isActive())
345: pm.currentTransaction().rollback();
346: pm.close();
347: }
348: }
349: }
|