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.Iterator;
028:
029: import javax.jdo.FetchPlan;
030: import javax.jdo.JDODetachedFieldAccessException;
031: import javax.jdo.JDOException;
032: import javax.jdo.PersistenceManager;
033: import javax.jdo.Query;
034:
035: import org.objectweb.speedo.SpeedoTestHelper;
036: import org.objectweb.speedo.api.ExceptionHelper;
037: import org.objectweb.speedo.pobjects.fetchgroup.Author;
038: import org.objectweb.speedo.pobjects.fetchgroup.Dictionnary;
039: import org.objectweb.speedo.pobjects.fetchgroup.Editor;
040: import org.objectweb.speedo.pobjects.fetchgroup.Novel;
041: import org.objectweb.speedo.pobjects.fetchgroup.PocketNovel;
042: import org.objectweb.speedo.pobjects.fetchgroup.Style;
043: import org.objectweb.speedo.pobjects.fetchgroup.Thesis;
044: import org.objectweb.speedo.pobjects.fetchgroup.University;
045: import org.objectweb.util.monolog.api.BasicLevel;
046:
047: /**
048: *
049: * @author Y.Bersihand
050: */
051: public class TestInheritanceFetchGroup extends SpeedoTestHelper {
052:
053: public TestInheritanceFetchGroup(String s) {
054: super (s);
055: }
056:
057: protected String getLoggerName() {
058: return LOG_NAME + ".rt.fetchgroup.TestInheritanceFetchGroup";
059: }
060:
061: /**
062: * Test the Inheritance 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 testInheritanceReference() {
067: logger
068: .log(BasicLevel.DEBUG,
069: "***************testInheritanceReference*****************");
070: Author author1 = new Author("Marx", "Karl");
071: Author author2 = new Author("Kissinger", "Henry");
072:
073: Editor editor1 = new Editor("Plon");
074: Editor editor2 = new Editor("Robert");
075:
076: Style style1 = new Style("act", "action");
077: Style style2 = new Style("thr", "thriller");
078:
079: Dictionnary dictionnary = new Dictionnary(332, author2, 950,
080: 12000, 1999, editor2);
081: Novel novel = new Novel(724, author1, 325, "Am I", style2);
082: PocketNovel pocketNovel = new PocketNovel(945, author1, 230,
083: "Why o why", style1, 12);
084:
085: PersistenceManager pm = pmf.getPersistenceManager();
086: FetchPlan fp = pm.getFetchPlan();
087: fp.addGroup("detail").removeGroup("default");
088:
089: pm.currentTransaction().begin();
090: logger.log(BasicLevel.DEBUG,
091: "make persistent the dictionnary\n "
092: + dictionnary.toString());
093: pm.makePersistent(dictionnary);
094: logger.log(BasicLevel.DEBUG, "make persistent the novel\n"
095: + novel.toString());
096: pm.makePersistent(novel);
097: logger.log(BasicLevel.DEBUG,
098: "make persistent the pocketNovel\n"
099: + pocketNovel.toString());
100: pm.makePersistent(pocketNovel);
101: pm.currentTransaction().commit();
102:
103: logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
104:
105: Dictionnary detachedDictionnary = (Dictionnary) pm
106: .detachCopy(dictionnary);
107: try {
108: assertEquals(dictionnary.getIsbn(), detachedDictionnary
109: .getIsbn());
110: assertEquals(dictionnary.getType(), detachedDictionnary
111: .getType());
112: assertEquals(dictionnary.getPageNb(), detachedDictionnary
113: .getPageNb());
114: assertEquals(dictionnary.getAuthor().getFirstName(),
115: detachedDictionnary.getAuthor().getFirstName());
116: assertEquals(dictionnary.getAuthor().getName(),
117: detachedDictionnary.getAuthor().getName());
118: assertEquals(dictionnary.getWordNb(), detachedDictionnary
119: .getWordNb());
120: assertEquals(dictionnary.getYear(), detachedDictionnary
121: .getYear());
122: assertEquals(dictionnary.getEditor().getName(),
123: detachedDictionnary.getEditor().getName());
124: logger.log(BasicLevel.DEBUG, "Isbn can be accessed: "
125: + detachedDictionnary.getIsbn());
126: logger.log(BasicLevel.DEBUG, "Type can be accessed: "
127: + detachedDictionnary.getType());
128: logger.log(BasicLevel.DEBUG, "PageNb can be accessed: "
129: + detachedDictionnary.getPageNb());
130: logger.log(BasicLevel.DEBUG, "Author can be accessed: "
131: + detachedDictionnary.getAuthor().toString());
132: logger.log(BasicLevel.DEBUG, "WordNb can be accessed: "
133: + detachedDictionnary.getWordNb());
134: logger.log(BasicLevel.DEBUG, "Year can be accessed: "
135: + detachedDictionnary.getYear());
136: logger
137: .log(BasicLevel.DEBUG, "Editor can be accessed: "
138: + detachedDictionnary.getEditor()
139: .toString() + "\n");
140: } catch (Exception e) {
141: fail("Detach failed: " + e.getMessage());
142: }
143: Novel detachedNovel = (Novel) pm.detachCopy(novel);
144: try {
145: assertEquals(novel.getIsbn(), detachedNovel.getIsbn());
146: assertEquals(novel.getType(), detachedNovel.getType());
147: assertEquals(novel.getPageNb(), detachedNovel.getPageNb());
148: assertEquals(novel.getAuthor().getFirstName(),
149: detachedNovel.getAuthor().getFirstName());
150: assertEquals(novel.getAuthor().getName(), detachedNovel
151: .getAuthor().getName());
152: assertEquals(novel.getTitle(), detachedNovel.getTitle());
153:
154: logger.log(BasicLevel.DEBUG, "Isbn can be accessed: "
155: + detachedNovel.getIsbn());
156: logger.log(BasicLevel.DEBUG, "Type can be accessed: "
157: + detachedNovel.getType());
158: logger.log(BasicLevel.DEBUG, "PageNb can be accessed: "
159: + detachedNovel.getPageNb());
160: logger.log(BasicLevel.DEBUG, "Author can be accessed: "
161: + detachedNovel.getAuthor().toString());
162: logger.log(BasicLevel.DEBUG, "Title can be accessed: "
163: + detachedNovel.getTitle());
164: logger.log(BasicLevel.DEBUG,
165: "Style should not be accessed: "
166: + detachedNovel.getStyle().toString()
167: + "\n");
168: } catch (Exception e) {
169: assertEquals(JDODetachedFieldAccessException.class, e
170: .getClass());
171: assertTrue(e.getMessage().indexOf("style") != -1);
172: if (e instanceof JDODetachedFieldAccessException
173: && e.getMessage().indexOf("style") != -1)
174: logger.log(BasicLevel.DEBUG,
175: "correct type exception caught: " + e + "\n");
176: else {
177: logger.log(BasicLevel.DEBUG, "Error: " + e);
178: fail(e.getMessage());
179: }
180: }
181:
182: PocketNovel detachedPocketNovel = (PocketNovel) pm
183: .detachCopy(pocketNovel);
184: try {
185: assertEquals(pocketNovel.getIsbn(), detachedPocketNovel
186: .getIsbn());
187: assertEquals(pocketNovel.getType(), detachedPocketNovel
188: .getType());
189: assertEquals(pocketNovel.getPageNb(), detachedPocketNovel
190: .getPageNb());
191: assertEquals(pocketNovel.getAuthor().getFirstName(),
192: detachedPocketNovel.getAuthor().getFirstName());
193: assertEquals(pocketNovel.getAuthor().getName(),
194: detachedPocketNovel.getAuthor().getName());
195: assertEquals(pocketNovel.getTitle(), detachedPocketNovel
196: .getTitle());
197: assertEquals(pocketNovel.getSize(), detachedPocketNovel
198: .getSize());
199:
200: logger.log(BasicLevel.DEBUG, "Isbn can be accessed: "
201: + detachedPocketNovel.getIsbn());
202: logger.log(BasicLevel.DEBUG, "Type can be accessed: "
203: + detachedPocketNovel.getType());
204: logger.log(BasicLevel.DEBUG, "PageNb can be accessed: "
205: + detachedPocketNovel.getPageNb());
206: logger.log(BasicLevel.DEBUG, "Author can be accessed: "
207: + detachedPocketNovel.getAuthor().toString());
208: logger.log(BasicLevel.DEBUG, "Title can be accessed: "
209: + detachedPocketNovel.getTitle());
210: logger.log(BasicLevel.DEBUG, "Size can be accessed: "
211: + detachedPocketNovel.getSize());
212: logger.log(BasicLevel.DEBUG,
213: "Style should not be accessed: "
214: + detachedPocketNovel.getStyle().toString()
215: + "\n");
216: } catch (Exception e) {
217: assertEquals(JDODetachedFieldAccessException.class, e
218: .getClass());
219: assertTrue(e.getMessage().indexOf("style") != -1);
220: if (pm.currentTransaction().isActive())
221: pm.currentTransaction().rollback();
222: if (e instanceof JDODetachedFieldAccessException
223: && e.getMessage().indexOf("style") != -1)
224: logger.log(BasicLevel.DEBUG,
225: "correct type exception caught: " + e + "\n");
226: else
227: logger.log(BasicLevel.DEBUG, "Error: " + e);
228: } finally {
229: pm.close();
230: }
231: }
232:
233: /**
234: * Test the Inheritance with the detail fetch group :
235: * test the definition of a fetch-group in the jdo file with a a@X field
236: * <field name="a" depth="1">
237: */
238: public void testInheritanceDepth() {
239: logger.log(BasicLevel.DEBUG,
240: "***************testInheritanceDepth*****************");
241: Author author1 = new Author("Nyberg", "Carl");
242: Author author2 = new Author("Levy", "Eric");
243: Author author3 = new Author("Sudarshan", "Sam");
244:
245: University uni1 = new University("Wisconsin");
246: University uni2 = new University("Arkansas");
247: University uni3 = new University("Tokyo");
248:
249: Thesis thesis1 = new Thesis(400, author1, 124,
250: "Performance in mmdbms", null, uni1);
251: Thesis thesis2 = new Thesis(401, author2, 156, "QoS in mmdbms",
252: thesis1, uni2);
253: Thesis thesis3 = new Thesis(402, author3, 113, "Why mmdbms?",
254: thesis2, uni3);
255:
256: PersistenceManager pm = pmf.getPersistenceManager();
257: FetchPlan fp = pm.getFetchPlan();
258: fp.clearGroups();
259: fp.addGroup("detail").removeGroup("default");
260: pm.currentTransaction().begin();
261: logger.log(BasicLevel.DEBUG, "make persistent the thesis\n "
262: + thesis1.toString() + "\n");
263: pm.makePersistent(thesis1);
264: logger.log(BasicLevel.DEBUG, "make persistent the thesis\n "
265: + thesis2.toString() + "\n");
266: pm.makePersistent(thesis2);
267: logger.log(BasicLevel.DEBUG, "make persistent the thesis\n "
268: + thesis3.toString() + "\n");
269: pm.makePersistent(thesis3);
270: pm.currentTransaction().commit();
271:
272: FetchPlan f = pm.getFetchPlan();
273: logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
274:
275: pm.currentTransaction().begin();
276: Thesis detachedT3 = (Thesis) pm.detachCopy(thesis3);
277: try {
278: assertEquals(thesis3.getIsbn(), detachedT3.getIsbn());
279: assertEquals(thesis3.getType(), detachedT3.getType());
280: assertEquals(thesis3.getPageNb(), detachedT3.getPageNb());
281: assertEquals(thesis3.getAuthor().getFirstName(), detachedT3
282: .getAuthor().getFirstName());
283: assertEquals(thesis3.getAuthor().getName(), detachedT3
284: .getAuthor().getName());
285: assertEquals(thesis3.getTitle(), detachedT3.getTitle());
286: assertEquals(thesis3.getUniversity().getName(), detachedT3
287: .getUniversity().getName());
288: logger.log(BasicLevel.DEBUG, "Isbn can be accessed: "
289: + detachedT3.getIsbn());
290: logger.log(BasicLevel.DEBUG, "Type can be accessed: "
291: + detachedT3.getType());
292: logger.log(BasicLevel.DEBUG, "PageNb can be accessed: "
293: + detachedT3.getPageNb());
294: logger.log(BasicLevel.DEBUG, "Author can be accessed: "
295: + detachedT3.getAuthor().toString());
296: logger.log(BasicLevel.DEBUG, "Title can be accessed: "
297: + detachedT3.getTitle());
298: logger.log(BasicLevel.DEBUG, "University can be accessed: "
299: + detachedT3.getUniversity().toString());
300: logger.log(BasicLevel.DEBUG, "Reference can be accessed: ");
301: } catch (Exception e) {
302: if (pm.currentTransaction().isActive())
303: pm.currentTransaction().rollback();
304: logger.log(BasicLevel.DEBUG, "Error: " + e);
305: fail(e.getMessage());
306: }
307:
308: Thesis detachedT2 = detachedT3.getReference();
309: try {
310: assertEquals(thesis2.getIsbn(), detachedT2.getIsbn());
311: assertEquals(thesis2.getType(), detachedT2.getType());
312: assertEquals(thesis2.getPageNb(), detachedT2.getPageNb());
313: assertEquals(thesis2.getAuthor().getFirstName(), detachedT2
314: .getAuthor().getFirstName());
315: assertEquals(thesis2.getAuthor().getName(), detachedT2
316: .getAuthor().getName());
317: assertEquals(thesis2.getTitle(), detachedT2.getTitle());
318: assertEquals(thesis2.getUniversity().getName(), detachedT2
319: .getUniversity().getName());
320:
321: logger.log(BasicLevel.DEBUG, "\tIsbn can be accessed: "
322: + detachedT2.getIsbn());
323: logger.log(BasicLevel.DEBUG, "\tType can be accessed: "
324: + detachedT2.getType());
325: logger.log(BasicLevel.DEBUG, "\tPageNb can be accessed: "
326: + detachedT2.getPageNb());
327: logger.log(BasicLevel.DEBUG, "\tAuthor can be accessed: "
328: + detachedT2.getAuthor().toString());
329: logger.log(BasicLevel.DEBUG, "\tTitle can be accessed: "
330: + detachedT2.getTitle());
331: logger.log(BasicLevel.DEBUG,
332: "\tUniversity can be accessed: "
333: + detachedT2.getUniversity().toString());
334: logger.log(BasicLevel.DEBUG,
335: "\tReference should not be accessed: "
336: + detachedT2.getReference().toString());
337: } catch (Exception e) {
338: assertEquals(JDODetachedFieldAccessException.class, e
339: .getClass());
340: assertTrue(e.getMessage().indexOf("reference") != -1);
341: if (pm.currentTransaction().isActive())
342: pm.currentTransaction().rollback();
343: if (e instanceof JDODetachedFieldAccessException
344: && e.getMessage().indexOf("reference") != -1)
345: logger.log(BasicLevel.DEBUG,
346: "correct type exception caught: " + e);
347: else
348: logger.log(BasicLevel.DEBUG, "Error: " + e);
349: } finally {
350: pm.close();
351: }
352: }
353:
354: public void testRemovingOfPersistentObject() {
355: PersistenceManager pm = pmf.getPersistenceManager();
356: try {
357: Class[] cs = new Class[] { Author.class, Dictionnary.class,
358: Editor.class, Novel.class, PocketNovel.class,
359: Style.class, Thesis.class, University.class };
360: pm.currentTransaction().begin();
361: for (int i = 0; i < cs.length; i++) {
362: Query query = pm.newQuery(cs[i]);
363: Collection col = (Collection) query.execute();
364: Iterator it = col.iterator();
365: while (it.hasNext()) {
366: Object o = it.next();
367: assertNotNull("null object in the query result"
368: + cs[i].getName(), o);
369: pm.deletePersistent(o);
370:
371: }
372: query.close(col);
373: }
374: pm.currentTransaction().commit();
375: } catch (JDOException e) {
376: Exception ie = ExceptionHelper.getNested(e);
377: logger.log(BasicLevel.ERROR, "", ie);
378: fail(ie.getMessage());
379: } finally {
380: pm.close();
381: }
382: }
383:
384: }
|