KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > inheritance > TestPrefetchQueries


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.inheritance;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.jdo.Extent;
33 import javax.jdo.JDOException;
34 import javax.jdo.PersistenceManager;
35 import javax.jdo.Query;
36
37 import junit.framework.Assert;
38
39 import org.objectweb.speedo.SpeedoTestHelper;
40 import org.objectweb.speedo.api.ExceptionHelper;
41 import org.objectweb.speedo.pobjects.inheritance.prefetch.AbstractAssistant;
42 import org.objectweb.speedo.pobjects.inheritance.prefetch.Day;
43 import org.objectweb.speedo.pobjects.inheritance.prefetch.Department;
44 import org.objectweb.speedo.pobjects.inheritance.prefetch.Employee;
45 import org.objectweb.speedo.pobjects.inheritance.prefetch.Manager;
46 import org.objectweb.speedo.pobjects.inheritance.prefetch.Secretary;
47 import org.objectweb.speedo.pobjects.inheritance.prefetch.Worker;
48 import org.objectweb.util.monolog.api.BasicLevel;
49
50 /**
51  *
52  * @author Y.Bersihand
53  */

54 public class TestPrefetchQueries extends SpeedoTestHelper {
55     
56     public TestPrefetchQueries(String JavaDoc s) {
57         super(s);
58     }
59
60     protected String JavaDoc getLoggerName() {
61         return LOG_NAME + ".rt.inheritance.TestPrefetchQueries";
62     }
63     
64     static final int AGE_LIMIT = 25;
65     protected int nbOverLimit;
66     
67     /**This steps describes how to:
68      * make persistent inherited objects
69      *
70      */

71     public void testInheritance() {
72         logger.log(BasicLevel.DEBUG, "***************testInheritance*****************");
73         PersistenceManager pm = pmf.getPersistenceManager();
74         //create the employees, workers and managers
75
createInheritedObjects(pm);
76         //remove all the objects from the cache
77
pm.evictAll();
78         countAllEmployees(pm);
79         //get all the employees
80
/*iterateExtent(pm, Employee.class);
81         //get all the workers
82         iterateExtent(pm, Worker.class);
83         //get all the managers
84         iterateExtent(pm, Manager.class);
85         //get all the abstract assistants
86         iterateExtent(pm, AbstractAssistant.class);
87         //get all the secretaries
88         iterateExtent(pm, Secretary.class);
89         retrieveOverXX(pm, AGE_LIMIT);
90         retrieveOverXXWorker(pm, AGE_LIMIT);
91         retrieveOverXXManager(pm, AGE_LIMIT);
92         retrieveOverXXSecretary(pm, AGE_LIMIT);*/

93         pm.close();
94     }
95     
96     /**
97      * Create persistent objects
98      */

99     public void createInheritedObjects(PersistenceManager pm){
100         
101         Employee employee1 = new Employee("Herve Landry", 42.4, "Tetra Pack");
102         Employee employee2 = new Employee("Vincent Racado", 59, "Tetra Pack");
103         
104         Department dept = new Department("Production");
105         
106         Worker worker1 = new Worker("Caroline Bret", 33, "Tetra Pack", true, dept);
107         Worker worker2 = new Worker("Evelyne Jain", 54, "Tetra Pack", false, dept);
108         Worker worker3 = new Worker("Tim Jorge", 28, "Tetra Pack", false, dept);
109         
110         Collection JavaDoc days = new ArrayList JavaDoc();
111         Day day1 = new Day("monday", 1);
112         Day day2 = new Day("tuesday", 2);
113         Day day3 = new Day("wednesday", 3);
114         Day day4 = new Day("thursday", 4);
115         Day day5 = new Day("friday", 5);
116         days.add(day1);
117         days.add(day2);
118         days.add(day3);
119         days.add(day4);
120         days.add(day5);
121         
122         worker1.setDays(days);
123         worker3.setDays(days);
124         Manager manager1 = new Manager("Jean Duverge", 57, "Tetra Pack", "Sales");
125         Manager manager2 = new Manager("Eric Mento", 49, "Tetra Pack", "Marketing");
126         
127         worker1.setMainManager(manager1);
128         worker2.setMainManager(manager1);
129         worker3.setMainManager(manager2);
130         
131         Secretary sec1 = new Secretary("Durand Luc", 27, "Tetra Pack", 12);
132         Secretary sec2 = new Secretary("Serve Amandine", 34, "Tetra Pack", 10);
133         
134         Collection JavaDoc employees = new ArrayList JavaDoc();
135         employees.add(employee1);
136         employees.add(employee2);
137         employees.add(worker1);
138         employees.add(worker2);
139         employees.add(worker3);
140         employees.add(manager1);
141         employees.add(manager2);
142         employees.add(sec1);
143         employees.add(sec2);
144         
145         Iterator JavaDoc it = employees.iterator();
146         nbOverLimit = 0;
147         while(it.hasNext()){
148             Employee e = (Employee) it.next();
149             if(e.getAge() > AGE_LIMIT)
150                 nbOverLimit++;
151         }
152         
153         //make persistent all the persons using a collection
154
pm.currentTransaction().begin();
155         pm.makePersistentAll(employees);
156         pm.currentTransaction().commit();
157     }
158
159     //iterate over all the instances of a class
160
public void iterateExtent(PersistenceManager pm, Class JavaDoc cl){
161         Extent extent = pm.getExtent(cl, true);
162         Iterator JavaDoc it = extent.iterator();
163         String JavaDoc className = cl.getName().substring(cl.getName().lastIndexOf("."));
164         logger.log(BasicLevel.DEBUG, "All " + cl.getName() + " instances:");
165         while(it.hasNext()){
166             Employee e = (Employee) it.next();
167             assertNotNull(e);
168             logger.log(BasicLevel.DEBUG, e.toString());
169         }
170         extent.close(it);
171     }
172     
173     //retrieve all the over-XX
174
public void retrieveOverXX(PersistenceManager pm, int limit){
175         Query query = pm.newQuery(Employee.class,"age > " + limit);
176         query.setOrdering("age ascending");
177         Collection JavaDoc results = (Collection JavaDoc)query.execute();
178         assertEquals(nbOverLimit, results.size());
179         logger.log(BasicLevel.DEBUG, "Over-" + limit + " ordered:");
180         Iterator JavaDoc it = results.iterator();
181         double age = 0;
182         while(it.hasNext()){
183             Employee e = (Employee) it.next();
184             assertTrue(age < e.getAge());
185             age = e.getAge();
186             logger.log(BasicLevel.DEBUG, e.toString());
187         }
188         query.closeAll();
189     }
190     
191     //retrieve all the over-XX worker
192
public void retrieveOverXXWorker(PersistenceManager pm, int limit){
193         Query query = pm.newQuery(Worker.class,"age > " + limit);
194         query.setOrdering("age ascending");
195         Collection JavaDoc results = (Collection JavaDoc)query.execute();
196         logger.log(BasicLevel.DEBUG, "Over-" + limit + " ordered:");
197         Iterator JavaDoc it = results.iterator();
198         double age = 0;
199         while(it.hasNext()){
200             Employee e = (Employee) it.next();
201             assertTrue(age < e.getAge());
202             age = e.getAge();
203             logger.log(BasicLevel.DEBUG, e.toString());
204         }
205         query.closeAll();
206     }
207     
208     //retrieve all the over-XX manager
209
public void retrieveOverXXManager(PersistenceManager pm, int limit){
210         Query query = pm.newQuery(Manager.class,"age > " + limit);
211         query.setOrdering("age ascending");
212         Collection JavaDoc results = (Collection JavaDoc)query.execute();
213         logger.log(BasicLevel.DEBUG, "Over-" + limit + " ordered:");
214         Iterator JavaDoc it = results.iterator();
215         double age = 0;
216         while(it.hasNext()){
217             Employee e = (Employee) it.next();
218             assertTrue(age < e.getAge());
219             age = e.getAge();
220             logger.log(BasicLevel.DEBUG, e.toString());
221         }
222         query.closeAll();
223     }
224     
225     //retrieve all the over-XX secretary
226
public void retrieveOverXXSecretary(PersistenceManager pm, int limit){
227         Query query = pm.newQuery(Secretary.class,"age > " + limit);
228         query.setOrdering("age ascending");
229         Collection JavaDoc results = (Collection JavaDoc)query.execute();
230         logger.log(BasicLevel.DEBUG, "Over-" + limit + " ordered:");
231         Iterator JavaDoc it = results.iterator();
232         double age = 0;
233         while(it.hasNext()){
234             Employee e = (Employee) it.next();
235             assertTrue(age < e.getAge());
236             age = e.getAge();
237             logger.log(BasicLevel.DEBUG, e.toString());
238         }
239         query.closeAll();
240     }
241     
242     public static class CountResult {
243         public String JavaDoc name;
244         public String JavaDoc type;
245         public Integer JavaDoc count;
246         
247         public CountResult(String JavaDoc name, String JavaDoc type, Integer JavaDoc count) {
248             this.name = name;
249             this.type = type;
250             this.count = count;
251         }
252         
253          public String JavaDoc toString() {
254             return "name: " + name + ", type: " + type + ", count: " + count;
255         }
256     }
257     
258     // count all employees
259
public void countAllEmployees(PersistenceManager pm){
260         Query query = pm.newQuery(Employee.class);
261         query.setResult("name, type, count(this)");
262         query.setGrouping("name, type");
263         query.setResultClass(CountResult.class);
264         Collection JavaDoc results = (Collection JavaDoc)query.execute();
265         logger.log(BasicLevel.DEBUG, "Count:");
266         Iterator JavaDoc it = results.iterator();
267         while(it.hasNext()){
268             CountResult cr = (CountResult) it.next();
269             logger.log(BasicLevel.DEBUG, "" + cr.toString());
270         }
271         query.closeAll();
272     }
273     
274     public void testRemovingOfPersistentObject() {
275         PersistenceManager pm = pmf.getPersistenceManager();
276         try {
277             Class JavaDoc[] cs = new Class JavaDoc[]{Employee.class,Day.class,Department.class};
278             pm.currentTransaction().begin();
279             for(int i=0; i<cs.length; i++) {
280                 Query query = pm.newQuery(cs[i]);
281                 Collection JavaDoc col = (Collection JavaDoc) query.execute();
282                 Iterator JavaDoc it = col.iterator();
283                 while(it.hasNext()) {
284                     Object JavaDoc o = it.next();
285                     Assert.assertNotNull("null object in the query result"
286                         + cs[i].getName(), o);
287                     pm.deletePersistent(o);
288
289                 }
290                 query.close(col);
291             }
292             pm.currentTransaction().commit();
293         } catch (JDOException e) {
294             Exception JavaDoc ie = ExceptionHelper.getNested(e);
295             logger.log(BasicLevel.ERROR, "", ie);
296             fail(ie.getMessage());
297         } finally {
298             pm.close();
299         }
300     }
301 }
302
Popular Tags