KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
31
32 import javax.jdo.FetchPlan;
33 import javax.jdo.JDODetachedFieldAccessException;
34 import javax.jdo.JDOException;
35 import javax.jdo.PersistenceManager;
36 import javax.jdo.Query;
37
38 import org.objectweb.speedo.SpeedoTestHelper;
39 import org.objectweb.speedo.api.ExceptionHelper;
40 import org.objectweb.speedo.pobjects.fetchgroup.Author;
41 import org.objectweb.speedo.pobjects.fetchgroup.Dictionnary;
42 import org.objectweb.speedo.pobjects.fetchgroup.Editor;
43 import org.objectweb.speedo.pobjects.fetchgroup.Novel;
44 import org.objectweb.speedo.pobjects.fetchgroup.PocketNovel;
45 import org.objectweb.speedo.pobjects.fetchgroup.Style;
46 import org.objectweb.speedo.pobjects.fetchgroup.Thesis;
47 import org.objectweb.speedo.pobjects.fetchgroup.University;
48 import org.objectweb.util.monolog.api.BasicLevel;
49
50 /**
51  *
52  * @author Y.Bersihand
53  */

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

69     public void testInheritanceReference() {
70         logger.log(BasicLevel.DEBUG, "***************testInheritanceReference*****************");
71         Author author1 = new Author("Marx", "Karl");
72         Author author2 = new Author("Kissinger", "Henry");
73         
74         Editor editor1 = new Editor("Plon");
75         Editor editor2 = new Editor("Robert");
76         
77         Style style1 = new Style("act", "action");
78         Style style2 = new Style("thr", "thriller");
79         
80         Dictionnary dictionnary = new Dictionnary(332, author2, 950, 12000, 1999, editor2);
81         Novel novel = new Novel(724, author1, 325, "Am I", style2);
82         PocketNovel pocketNovel = new PocketNovel(945, author1, 230, "Why o why", style1, 12);
83         
84         PersistenceManager pm = pmf.getPersistenceManager();
85         FetchPlan fp = pm.getFetchPlan();
86         fp.addGroup("detail").removeGroup("default");
87         
88         pm.currentTransaction().begin();
89         logger.log(BasicLevel.DEBUG, "make persistent the dictionnary\n " + dictionnary.toString());
90         pm.makePersistent(dictionnary);
91         logger.log(BasicLevel.DEBUG, "make persistent the novel\n" + novel.toString());
92         pm.makePersistent(novel);
93         logger.log(BasicLevel.DEBUG, "make persistent the pocketNovel\n" + pocketNovel.toString());
94         pm.makePersistent(pocketNovel);
95         pm.currentTransaction().commit();
96         
97         logger.log(BasicLevel.DEBUG, "FG: " + fp.getGroups());
98         
99         Dictionnary detachedDictionnary = (Dictionnary) pm.detachCopy(dictionnary);
100         try{
101             assertEquals(dictionnary.getIsbn(), detachedDictionnary.getIsbn());
102             assertEquals(dictionnary.getType(), detachedDictionnary.getType());
103             assertEquals(dictionnary.getPageNb(), detachedDictionnary.getPageNb());
104             assertEquals(dictionnary.getAuthor().getFirstName(), detachedDictionnary.getAuthor().getFirstName());
105             assertEquals(dictionnary.getAuthor().getName(), detachedDictionnary.getAuthor().getName());
106             assertEquals(dictionnary.getWordNb(), detachedDictionnary.getWordNb());
107             assertEquals(dictionnary.getYear(), detachedDictionnary.getYear());
108             assertEquals(dictionnary.getEditor().getName(), detachedDictionnary.getEditor().getName());
109             logger.log(BasicLevel.DEBUG, "Isbn can be accessed: " + detachedDictionnary.getIsbn());
110             logger.log(BasicLevel.DEBUG, "Type can be accessed: " + detachedDictionnary.getType());
111             logger.log(BasicLevel.DEBUG, "PageNb can be accessed: " + detachedDictionnary.getPageNb());
112             logger.log(BasicLevel.DEBUG, "Author can be accessed: " + detachedDictionnary.getAuthor().toString());
113             logger.log(BasicLevel.DEBUG, "WordNb can be accessed: " + detachedDictionnary.getWordNb());
114             logger.log(BasicLevel.DEBUG, "Year can be accessed: " + detachedDictionnary.getYear());
115             logger.log(BasicLevel.DEBUG, "Editor can be accessed: " + detachedDictionnary.getEditor().toString() + "\n");
116         } catch(Exception JavaDoc e){
117             fail("Detach failed: " + e.getMessage());
118         }
119         Novel detachedNovel = (Novel) pm.detachCopy(novel);
120         try{
121             assertEquals(novel.getIsbn(), detachedNovel.getIsbn());
122             assertEquals(novel.getType(), detachedNovel.getType());
123             assertEquals(novel.getPageNb(), detachedNovel.getPageNb());
124             assertEquals(novel.getAuthor().getFirstName(), detachedNovel.getAuthor().getFirstName());
125             assertEquals(novel.getAuthor().getName(), detachedNovel.getAuthor().getName());
126             assertEquals(novel.getTitle(), detachedNovel.getTitle());
127             
128             logger.log(BasicLevel.DEBUG, "Isbn can be accessed: " + detachedNovel.getIsbn());
129             logger.log(BasicLevel.DEBUG, "Type can be accessed: " + detachedNovel.getType());
130             logger.log(BasicLevel.DEBUG, "PageNb can be accessed: " + detachedNovel.getPageNb());
131             logger.log(BasicLevel.DEBUG, "Author can be accessed: " + detachedNovel.getAuthor().toString());
132             logger.log(BasicLevel.DEBUG, "Title can be accessed: " + detachedNovel.getTitle());
133             logger.log(BasicLevel.DEBUG, "Style should not be accessed: " + detachedNovel.getStyle().toString() + "\n");
134         }
135         catch(Exception JavaDoc e){
136             assertEquals(JDODetachedFieldAccessException.class, e.getClass());
137             assertTrue(e.getMessage().indexOf("style") != -1);
138             if(e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("style") != -1)
139                 logger.log(BasicLevel.DEBUG, "correct type exception caught: " + e + "\n");
140             else {
141                 logger.log(BasicLevel.DEBUG, "Error: " + e);
142                 fail(e.getMessage());
143             }
144         }
145         
146         PocketNovel detachedPocketNovel = (PocketNovel) pm.detachCopy(pocketNovel);
147         try{
148             assertEquals(pocketNovel.getIsbn(), detachedPocketNovel.getIsbn());
149             assertEquals(pocketNovel.getType(), detachedPocketNovel.getType());
150             assertEquals(pocketNovel.getPageNb(), detachedPocketNovel.getPageNb());
151             assertEquals(pocketNovel.getAuthor().getFirstName(), detachedPocketNovel.getAuthor().getFirstName());
152             assertEquals(pocketNovel.getAuthor().getName(), detachedPocketNovel.getAuthor().getName());
153             assertEquals(pocketNovel.getTitle(), detachedPocketNovel.getTitle());
154             assertEquals(pocketNovel.getSize(), detachedPocketNovel.getSize());
155             
156             logger.log(BasicLevel.DEBUG, "Isbn can be accessed: " + detachedPocketNovel.getIsbn());
157             logger.log(BasicLevel.DEBUG, "Type can be accessed: " + detachedPocketNovel.getType());
158             logger.log(BasicLevel.DEBUG, "PageNb can be accessed: " + detachedPocketNovel.getPageNb());
159             logger.log(BasicLevel.DEBUG, "Author can be accessed: " + detachedPocketNovel.getAuthor().toString());
160             logger.log(BasicLevel.DEBUG, "Title can be accessed: " + detachedPocketNovel.getTitle());
161             logger.log(BasicLevel.DEBUG, "Size can be accessed: " + detachedPocketNovel.getSize());
162             logger.log(BasicLevel.DEBUG, "Style should not be accessed: " + detachedPocketNovel.getStyle().toString() + "\n");
163         }
164         catch(Exception JavaDoc e){
165             assertEquals(JDODetachedFieldAccessException.class, e.getClass());
166             assertTrue(e.getMessage().indexOf("style") != -1);
167             if (pm.currentTransaction().isActive())
168                 pm.currentTransaction().rollback();
169             if(e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("style") != -1)
170                 logger.log(BasicLevel.DEBUG, "correct type exception caught: " + e + "\n");
171             else
172                 logger.log(BasicLevel.DEBUG, "Error: " + e);
173         } finally {
174             pm.close();
175         }
176     }
177     
178     
179     /**
180      * Test the Inheritance with the detail fetch group :
181      * test the definition of a fetch-group in the jdo file with a a@X field
182      * <field name="a" depth="1">
183      */

184     public void testInheritanceDepth() {
185         logger.log(BasicLevel.DEBUG, "***************testInheritanceDepth*****************");
186         Author author1 = new Author("Nyberg", "Carl");
187         Author author2 = new Author("Levy", "Eric");
188         Author author3 = new Author("Sudarshan", "Sam");
189         
190         University uni1 = new University("Wisconsin");
191         University uni2 = new University("Arkansas");
192         University uni3 = new University("Tokyo");
193         
194         Thesis thesis1 = new Thesis(400, author1, 124, "Performance in mmdbms", null, uni1);
195         Thesis thesis2 = new Thesis(401, author2, 156, "QoS in mmdbms", thesis1, uni2);
196         Thesis thesis3 = new Thesis(402, author3, 113, "Why mmdbms?", thesis2, uni3);
197         
198         
199         PersistenceManager pm = pmf.getPersistenceManager();
200         FetchPlan fp = pm.getFetchPlan();
201         fp.clearGroups();
202         fp.addGroup("detail").removeGroup("default");
203         pm.currentTransaction().begin();
204         logger.log(BasicLevel.DEBUG, "make persistent the thesis\n " + thesis1.toString() + "\n");
205         pm.makePersistent(thesis1);
206         logger.log(BasicLevel.DEBUG, "make persistent the thesis\n " + thesis2.toString() + "\n");
207         pm.makePersistent(thesis2);
208         logger.log(BasicLevel.DEBUG, "make persistent the thesis\n " + thesis3.toString() + "\n");
209         pm.makePersistent(thesis3);
210         pm.currentTransaction().commit();
211         
212         FetchPlan f = pm.getFetchPlan();
213         logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
214         
215         pm.currentTransaction().begin();
216         Thesis detachedT3 = (Thesis) pm.detachCopy(thesis3);
217         try{
218             assertEquals(thesis3.getIsbn(), detachedT3.getIsbn());
219             assertEquals(thesis3.getType(), detachedT3.getType());
220             assertEquals(thesis3.getPageNb(), detachedT3.getPageNb());
221             assertEquals(thesis3.getAuthor().getFirstName(), detachedT3.getAuthor().getFirstName());
222             assertEquals(thesis3.getAuthor().getName(), detachedT3.getAuthor().getName());
223             assertEquals(thesis3.getTitle(), detachedT3.getTitle());
224             assertEquals(thesis3.getUniversity().getName(), detachedT3.getUniversity().getName());
225             logger.log(BasicLevel.DEBUG, "Isbn can be accessed: " + detachedT3.getIsbn());
226             logger.log(BasicLevel.DEBUG, "Type can be accessed: " + detachedT3.getType());
227             logger.log(BasicLevel.DEBUG, "PageNb can be accessed: " + detachedT3.getPageNb());
228             logger.log(BasicLevel.DEBUG, "Author can be accessed: " + detachedT3.getAuthor().toString());
229             logger.log(BasicLevel.DEBUG, "Title can be accessed: " + detachedT3.getTitle());
230             logger.log(BasicLevel.DEBUG, "University can be accessed: " + detachedT3.getUniversity().toString());
231             logger.log(BasicLevel.DEBUG, "Reference can be accessed: " );
232         }
233         catch(Exception JavaDoc e){
234             if (pm.currentTransaction().isActive())
235                 pm.currentTransaction().rollback();
236             logger.log(BasicLevel.DEBUG, "Error: " + e);
237             fail(e.getMessage());
238         }
239         
240         Thesis detachedT2 = detachedT3.getReference();
241         try{
242             assertEquals(thesis2.getIsbn(), detachedT2.getIsbn());
243             assertEquals(thesis2.getType(), detachedT2.getType());
244             assertEquals(thesis2.getPageNb(), detachedT2.getPageNb());
245             assertEquals(thesis2.getAuthor().getFirstName(), detachedT2.getAuthor().getFirstName());
246             assertEquals(thesis2.getAuthor().getName(), detachedT2.getAuthor().getName());
247             assertEquals(thesis2.getTitle(), detachedT2.getTitle());
248             assertEquals(thesis2.getUniversity().getName(), detachedT2.getUniversity().getName());
249             
250             logger.log(BasicLevel.DEBUG, "\tIsbn can be accessed: " + detachedT2.getIsbn());
251             logger.log(BasicLevel.DEBUG, "\tType can be accessed: " + detachedT2.getType());
252             logger.log(BasicLevel.DEBUG, "\tPageNb can be accessed: " + detachedT2.getPageNb());
253             logger.log(BasicLevel.DEBUG, "\tAuthor can be accessed: " + detachedT2.getAuthor().toString());
254             logger.log(BasicLevel.DEBUG, "\tTitle can be accessed: " + detachedT2.getTitle());
255             logger.log(BasicLevel.DEBUG, "\tUniversity can be accessed: " + detachedT2.getUniversity().toString());
256             logger.log(BasicLevel.DEBUG, "\tReference should not be accessed: " + detachedT2.getReference().toString());
257         }
258         catch(Exception JavaDoc e){
259             assertEquals(JDODetachedFieldAccessException.class, e.getClass());
260             assertTrue(e.getMessage().indexOf("reference") != -1);
261             if (pm.currentTransaction().isActive())
262                 pm.currentTransaction().rollback();
263             if(e instanceof JDODetachedFieldAccessException && e.getMessage().indexOf("reference") != -1)
264                 logger.log(BasicLevel.DEBUG, "correct type exception caught: " + e);
265             else
266                 logger.log(BasicLevel.DEBUG, "Error: " + e);
267         } finally {
268             pm.close();
269         }
270     }
271     
272     public void testRemovingOfPersistentObject() {
273         PersistenceManager pm = pmf.getPersistenceManager();
274         try {
275             Class JavaDoc[] cs = new Class JavaDoc[]{Author.class, Dictionnary.class, Editor.class, Novel.class, PocketNovel.class,
276                     Style.class, Thesis.class, University.class};
277             pm.currentTransaction().begin();
278             for(int i=0; i<cs.length; i++) {
279                 Query query = pm.newQuery(cs[i]);
280                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
281                 Iterator JavaDoc it = col.iterator();
282                 while(it.hasNext()) {
283                     Object JavaDoc o = it.next();
284                     assertNotNull("null object in the query result"
285                         + cs[i].getName(), o);
286                     pm.deletePersistent(o);
287
288                 }
289                 query.close(col);
290             }
291             pm.currentTransaction().commit();
292         } catch (JDOException e) {
293             Exception JavaDoc ie = ExceptionHelper.getNested(e);
294             logger.log(BasicLevel.ERROR, "", ie);
295             fail(ie.getMessage());
296         } finally {
297             pm.close();
298         }
299     }
300     
301 }
302
Popular Tags