KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import javax.jdo.FetchPlan;
35 import javax.jdo.JDODetachedFieldAccessException;
36 import javax.jdo.JDOException;
37 import javax.jdo.PersistenceManager;
38 import javax.jdo.Query;
39
40 import org.objectweb.speedo.SpeedoTestHelper;
41 import org.objectweb.speedo.api.ExceptionHelper;
42 import org.objectweb.speedo.pm.api.ProxyManager;
43 import org.objectweb.speedo.pobjects.fetchgroup.Address;
44 import org.objectweb.speedo.pobjects.fetchgroup.Country;
45 import org.objectweb.speedo.pobjects.fetchgroup.Person;
46 import org.objectweb.util.monolog.api.BasicLevel;
47
48 /**
49  * Test the 4 pre-defined fetchgroups:
50  * 1- all : all the fields of the class are loaded
51  * 2- default: all the fields defined as default-fetch-group="true" in the jdo file are loaded
52  * 3- none: only the primary key of the class is loaded but int he case of speedo (and because of jorm)
53  * all primitive fields are loaded.
54  * 4- values: all the fields that are included in the default fetch group by default
55  * (primitives, wrappers, String, Date, etc...) are loaded.
56  * @author Y.Bersihand
57  */

58 public class TestPredefinedFetchGroup extends SpeedoTestHelper {
59
60     public TestPredefinedFetchGroup(String JavaDoc s) {
61         super(s);
62     }
63
64     protected String JavaDoc getLoggerName() {
65         return LOG_NAME + ".rt.fetchgroup.TestPredefinedFetchGroup";
66     }
67     
68     /**
69      * Test the loading with the "all" fetch group : all the fields of the class are loaded
70      */

71     public void testLoadingAll() {
72         logger.log(BasicLevel.DEBUG, "***************testLoadingAll*****************");
73         Country country = new Country("it","Italie");
74         Address address = new Address("Rue Spiaggi", "Milan", country);
75         Person parent = new Person();
76         parent.setName("Del Piero Joel");
77         parent.setAge(32);
78         parent.setAddress(address);
79         Person child1 = new Person("Del Piero Sophie", address, null, 14);
80         Person child2 = new Person("Del Piero Mikael", address, null, 11);
81         Set JavaDoc children = new HashSet JavaDoc();
82         children.add(child1);
83         children.add(child2);
84         parent.setChildren(children);
85         
86         PersistenceManager pm = pmf.getPersistenceManager();
87         FetchPlan fp = pm.getFetchPlan();
88         fp.addGroup("all").removeGroup("default");
89         pm.currentTransaction().begin();
90         logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
91         pm.makePersistent(parent);
92         pm.currentTransaction().commit();
93         
94         FetchPlan f = pm.getFetchPlan();
95         logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
96         
97         try{
98             pm.currentTransaction().begin();
99             Person detachedParent = (Person) pm.detachCopy(parent);
100             logger.log(BasicLevel.DEBUG, "All fields can be accessed: " + detachedParent.toString());
101             assertEquals(parent.getName(), detachedParent.getName());
102             assertEquals(parent.getAge(), detachedParent.getAge());
103             assertEquals(parent.getAddress().getCity(), detachedParent.getAddress().getCity());
104             assertEquals(parent.getAddress().getCountry().getCode(), detachedParent.getAddress().getCountry().getCode());
105             assertEquals(parent.getAddress().getCountry().getName(), detachedParent.getAddress().getCountry().getName());
106             assertEquals(parent.getAddress().getStreet(), detachedParent.getAddress().getStreet());
107             assertEquals(parent.getChildren().size(), detachedParent.getChildren().size());
108         }
109         catch(Exception JavaDoc e){
110                 logger.log(BasicLevel.DEBUG, "Error: " + e);
111         } finally {
112             if (pm.currentTransaction().isActive())
113                 pm.currentTransaction().rollback();
114             pm.close();
115         }
116     }
117     
118     /**
119      * Test the loading with the "default" fetch group
120      */

121     public void testLoadingDefault() {
122         logger.log(BasicLevel.DEBUG, "***************testLoadingDefault*****************");
123         Country country = new Country("fr","France");
124         Address address = new Address("rue Anatole France", "Tours", country);
125         Person parent = new Person();
126         parent.setName("Bordes Joel");
127         parent.setAge(32);
128         parent.setAddress(address);
129         Person child1 = new Person("Bordes Sophie", address, null, 14);
130         Person child2 = new Person("Bordes Mikael", address, null, 11);
131         Set JavaDoc children = new HashSet JavaDoc();
132         children.add(child1);
133         children.add(child2);
134         parent.setChildren(children);
135         
136         PersistenceManager pm = pmf.getPersistenceManager();
137         FetchPlan fp = pm.getFetchPlan();
138         fp.removeGroup("all").addGroup("default");
139         pm.currentTransaction().begin();
140         logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
141         pm.makePersistent(parent);
142         pm.currentTransaction().commit();
143         
144         FetchPlan f = pm.getFetchPlan();
145         logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
146         
147         try{
148             pm.currentTransaction().begin();
149             Person detachedParent = (Person) ((ProxyManager)pm).detachCopy(parent);
150             logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName());
151             logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge());
152             logger.log(BasicLevel.DEBUG, "Address can be accessed: " + detachedParent.getAddress().toString());
153             assertEquals(parent.getName(), detachedParent.getName());
154             assertEquals(parent.getAge(), detachedParent.getAge());
155             assertEquals(parent.getAddress().getCity(), detachedParent.getAddress().getCity());
156             assertEquals(parent.getAddress().getCountry().getCode(), detachedParent.getAddress().getCountry().getCode());
157             assertEquals(parent.getAddress().getCountry().getName(), detachedParent.getAddress().getCountry().getName());
158             assertEquals(parent.getAddress().getStreet(), detachedParent.getAddress().getStreet());
159             logger.log(BasicLevel.DEBUG, "Children should not be accessed: " + detachedParent.getChildren());
160         }
161         catch(Exception JavaDoc e){
162             assertEquals(e.getClass(), JDODetachedFieldAccessException.class);
163             pm.close();
164             if (e instanceof JDODetachedFieldAccessException)
165                 logger.log(BasicLevel.DEBUG, "Correct exception type caught: " + e);
166             else
167                 logger.log(BasicLevel.DEBUG, "Incorrect exception type caught: " + e);
168         } finally {
169             if (pm.currentTransaction().isActive())
170                 pm.currentTransaction().rollback();
171             pm.close();
172         }
173     }
174
175     
176     /**
177      * Test the loading with the "none" fetch group : only the pk is loaded
178      */

179     public void testLoadingNone() {
180         logger.log(BasicLevel.DEBUG, "***************testLoadingNone*****************");
181         Country country = new Country("uk","Royaume-Uni");
182         Address address = new Address("Sharrow Street", "Luton", country);
183         Person parent = new Person();
184         parent.setName("None Joel");
185         parent.setAge(32);
186         parent.setAddress(address);
187         Person child1 = new Person("None Sophie", address, null, 14);
188         Person child2 = new Person("None Mikael", address, null, 11);
189         Set JavaDoc children = new HashSet JavaDoc();
190         children.add(child1);
191         children.add(child2);
192         parent.setChildren(children);
193         
194         PersistenceManager pm = pmf.getPersistenceManager();
195         FetchPlan fp = pm.getFetchPlan();
196         fp.removeGroup("default").addGroup("none");
197         pm.currentTransaction().begin();
198         logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
199         pm.makePersistent(parent);
200         pm.currentTransaction().commit();
201         
202         FetchPlan f = pm.getFetchPlan();
203         logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
204         
205         try{
206             pm.currentTransaction().begin();
207             Person detachedParent = (Person) ((ProxyManager)pm).detachCopy(parent);
208             logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName());
209             logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge());
210             assertEquals(parent.getName(), detachedParent.getName());
211             assertEquals(parent.getAge(), detachedParent.getAge());
212             logger.log(BasicLevel.DEBUG, "Address should not be accessed: " + detachedParent.getAddress());
213             logger.log(BasicLevel.DEBUG, "Children should not be accessed: " + detachedParent.getChildren());
214         }
215         catch(Exception JavaDoc e){
216             assertEquals(e.getClass(), JDODetachedFieldAccessException.class);
217             if(e instanceof JDODetachedFieldAccessException)
218                 logger.log(BasicLevel.DEBUG, "Correct exception type caught: " + e);
219             else
220                 logger.log(BasicLevel.DEBUG, "Incorrect exception type caught: " + e);
221         } finally {
222             if (pm.currentTransaction().isActive())
223                 pm.currentTransaction().rollback();
224             pm.close();
225         }
226     }
227     
228     /**
229      * Test the loading with the "values" fetch group : only the values field of the default are loaded
230      */

231     public void testLoadingValues() {
232         logger.log(BasicLevel.DEBUG, "***************testLoadingValues*****************");
233         Country country = new Country("gr","Grece");
234         Address address = new Address("Feta", "Athenes", country);
235         Person parent = new Person();
236         parent.setName("Kapic Joel");
237         parent.setAge(32);
238         parent.setAddress(address);
239         Person child1 = new Person("Kapic Sophie", address, null, 14);
240         Person child2 = new Person("Kapic Mikael", address, null, 11);
241         Set JavaDoc children = new HashSet JavaDoc();
242         children.add(child1);
243         children.add(child2);
244         parent.setChildren(children);
245         
246         PersistenceManager pm = pmf.getPersistenceManager();
247         FetchPlan fp = pm.getFetchPlan();
248         fp.removeGroup("none").addGroup("values");
249         pm.currentTransaction().begin();
250         logger.log(BasicLevel.DEBUG, "make persistent the person " + parent.toString());
251         pm.makePersistent(parent);
252         pm.currentTransaction().commit();
253         
254         FetchPlan f = pm.getFetchPlan();
255         logger.log(BasicLevel.DEBUG, "FG: " + f.getGroups());
256         
257         try{
258             pm.currentTransaction().begin();
259             Person detachedParent = (Person) pm.detachCopy(parent);
260             logger.log(BasicLevel.DEBUG, "Name can be accessed: " + detachedParent.getName());
261             logger.log(BasicLevel.DEBUG, "Age can be accessed: " + detachedParent.getAge());
262             assertEquals(parent.getName(), detachedParent.getName());
263             assertEquals(parent.getAge(), detachedParent.getAge());
264             logger.log(BasicLevel.DEBUG, "Address should not be accessed: " + detachedParent.getAddress());
265             logger.log(BasicLevel.DEBUG, "Children should not be accessed: " + detachedParent.getChildren());
266         }
267         catch(Exception JavaDoc e){
268             assertEquals(e.getClass(), JDODetachedFieldAccessException.class);
269             if(e instanceof JDODetachedFieldAccessException)
270                 logger.log(BasicLevel.DEBUG, "Correct exception type caught: " + e);
271             else
272                 logger.log(BasicLevel.DEBUG, "Incorrect exception type caught: " + e);
273         } finally {
274             if (pm.currentTransaction().isActive())
275                 pm.currentTransaction().rollback();
276             pm.close();
277         }
278     }
279     
280     
281     public void testRemovingOfPersistentObject() {
282         PersistenceManager pm = pmf.getPersistenceManager();
283         try {
284             Class JavaDoc[] cs = new Class JavaDoc[]{Person.class, Address.class, Country.class};
285             pm.currentTransaction().begin();
286             for(int i=0; i<cs.length; i++) {
287                 Query query = pm.newQuery(cs[i]);
288                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
289                 Iterator JavaDoc it = col.iterator();
290                 while(it.hasNext()) {
291                     Object JavaDoc o = it.next();
292                     assertNotNull("null object in the query result"
293                         + cs[i].getName(), o);
294                     pm.deletePersistent(o);
295
296                 }
297                 query.close(col);
298             }
299             pm.currentTransaction().commit();
300         } catch (JDOException e) {
301             Exception JavaDoc ie = ExceptionHelper.getNested(e);
302             logger.log(BasicLevel.ERROR, "", ie);
303             fail(ie.getMessage());
304         } finally {
305             if (pm.currentTransaction().isActive())
306                 pm.currentTransaction().rollback();
307             pm.close();
308         }
309     }
310 }
311
Popular Tags