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.inheritance;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: import javax.jdo.Extent;
031: import javax.jdo.JDOException;
032: import javax.jdo.PersistenceManager;
033: import javax.jdo.Query;
034:
035: import junit.framework.Assert;
036:
037: import org.objectweb.speedo.SpeedoTestHelper;
038: import org.objectweb.speedo.api.ExceptionHelper;
039: import org.objectweb.speedo.pobjects.inheritance.prefetch.AbstractAssistant;
040: import org.objectweb.speedo.pobjects.inheritance.prefetch.Day;
041: import org.objectweb.speedo.pobjects.inheritance.prefetch.Department;
042: import org.objectweb.speedo.pobjects.inheritance.prefetch.Employee;
043: import org.objectweb.speedo.pobjects.inheritance.prefetch.Manager;
044: import org.objectweb.speedo.pobjects.inheritance.prefetch.Secretary;
045: import org.objectweb.speedo.pobjects.inheritance.prefetch.Worker;
046: import org.objectweb.util.monolog.api.BasicLevel;
047:
048: /**
049: *
050: * @author Y.Bersihand
051: */
052: public class TestPrefetchQueries extends SpeedoTestHelper {
053:
054: public TestPrefetchQueries(String s) {
055: super (s);
056: }
057:
058: protected String getLoggerName() {
059: return LOG_NAME + ".rt.inheritance.TestPrefetchQueries";
060: }
061:
062: static final int AGE_LIMIT = 25;
063: protected int nbOverLimit;
064:
065: /**This steps describes how to:
066: * make persistent inherited objects
067: *
068: */
069: public void testInheritance() {
070: logger.log(BasicLevel.DEBUG,
071: "***************testInheritance*****************");
072: PersistenceManager pm = pmf.getPersistenceManager();
073: //create the employees, workers and managers
074: createInheritedObjects(pm);
075: //remove all the objects from the cache
076: pm.evictAll();
077: countAllEmployees(pm);
078: //get all the employees
079: iterateExtent(pm, Employee.class);
080: //get all the workers
081: iterateExtent(pm, Worker.class);
082: //get all the managers
083: iterateExtent(pm, Manager.class);
084: //get all the abstract assistants
085: iterateExtent(pm, AbstractAssistant.class);
086: //get all the secretaries
087: iterateExtent(pm, Secretary.class);
088: retrieveOverXX(pm, AGE_LIMIT);
089: retrieveOverXXWorker(pm, AGE_LIMIT);
090: retrieveOverXXManager(pm, AGE_LIMIT);
091: retrieveOverXXSecretary(pm, AGE_LIMIT);
092: pm.close();
093: }
094:
095: /**
096: * Create persistent objects
097: */
098: public void createInheritedObjects(PersistenceManager pm) {
099:
100: Employee employee1 = new Employee("Herve Landry", 42.4,
101: "Tetra Pack");
102: Employee employee2 = new Employee("Vincent Racado", 59,
103: "Tetra Pack");
104:
105: Department dept = new Department("Production");
106:
107: Worker worker1 = new Worker("Caroline Bret", 33, "Tetra Pack",
108: true, dept);
109: Worker worker2 = new Worker("Evelyne Jain", 54, "Tetra Pack",
110: false, dept);
111: Worker worker3 = new Worker("Tim Jorge", 28, "Tetra Pack",
112: false, dept);
113:
114: Collection days = new ArrayList();
115: Day day1 = new Day("monday", 1);
116: Day day2 = new Day("tuesday", 2);
117: Day day3 = new Day("wednesday", 3);
118: Day day4 = new Day("thursday", 4);
119: Day day5 = new Day("friday", 5);
120: days.add(day1);
121: days.add(day2);
122: days.add(day3);
123: days.add(day4);
124: days.add(day5);
125:
126: worker1.setDays(days);
127: worker3.setDays(days);
128: Manager manager1 = new Manager("Jean Duverge", 57,
129: "Tetra Pack", "Sales");
130: Manager manager2 = new Manager("Eric Mento", 49, "Tetra Pack",
131: "Marketing");
132:
133: worker1.setMainManager(manager1);
134: worker2.setMainManager(manager1);
135: worker3.setMainManager(manager2);
136:
137: Secretary sec1 = new Secretary("Durand Luc", 27, "Tetra Pack",
138: 12);
139: Secretary sec2 = new Secretary("Serve Amandine", 34,
140: "Tetra Pack", 10);
141:
142: Collection employees = new ArrayList();
143: employees.add(employee1);
144: employees.add(employee2);
145: employees.add(worker1);
146: employees.add(worker2);
147: employees.add(worker3);
148: employees.add(manager1);
149: employees.add(manager2);
150: employees.add(sec1);
151: employees.add(sec2);
152:
153: Iterator it = employees.iterator();
154: nbOverLimit = 0;
155: while (it.hasNext()) {
156: Employee e = (Employee) it.next();
157: if (e.getAge() > AGE_LIMIT)
158: nbOverLimit++;
159: }
160:
161: //make persistent all the persons using a collection
162: pm.currentTransaction().begin();
163: pm.makePersistentAll(employees);
164: pm.currentTransaction().commit();
165: }
166:
167: //iterate over all the instances of a class
168: public void iterateExtent(PersistenceManager pm, Class cl) {
169: Extent extent = pm.getExtent(cl, true);
170: Iterator it = extent.iterator();
171: String className = cl.getName().substring(
172: cl.getName().lastIndexOf("."));
173: logger.log(BasicLevel.DEBUG, "All " + cl.getName()
174: + " instances:");
175: while (it.hasNext()) {
176: Employee e = (Employee) it.next();
177: assertNotNull(e);
178: logger.log(BasicLevel.DEBUG, e.toString());
179: }
180: extent.close(it);
181: }
182:
183: //retrieve all the over-XX
184: public void retrieveOverXX(PersistenceManager pm, int limit) {
185: Query query = pm.newQuery(Employee.class, "age > " + limit);
186: query.setOrdering("age ascending");
187: Collection results = (Collection) query.execute();
188: assertEquals(nbOverLimit, results.size());
189: logger.log(BasicLevel.DEBUG, "Over-" + limit + " ordered:");
190: Iterator it = results.iterator();
191: double age = 0;
192: while (it.hasNext()) {
193: Employee e = (Employee) it.next();
194: assertTrue(age < e.getAge());
195: age = e.getAge();
196: logger.log(BasicLevel.DEBUG, e.toString());
197: }
198: query.closeAll();
199: }
200:
201: //retrieve all the over-XX worker
202: public void retrieveOverXXWorker(PersistenceManager pm, int limit) {
203: Query query = pm.newQuery(Worker.class, "age > " + limit);
204: query.setOrdering("age ascending");
205: Collection results = (Collection) query.execute();
206: logger.log(BasicLevel.DEBUG, "Over-" + limit + " ordered:");
207: Iterator it = results.iterator();
208: double age = 0;
209: while (it.hasNext()) {
210: Employee e = (Employee) it.next();
211: assertTrue(age < e.getAge());
212: age = e.getAge();
213: logger.log(BasicLevel.DEBUG, e.toString());
214: }
215: query.closeAll();
216: }
217:
218: //retrieve all the over-XX manager
219: public void retrieveOverXXManager(PersistenceManager pm, int limit) {
220: Query query = pm.newQuery(Manager.class, "age > " + limit);
221: query.setOrdering("age ascending");
222: Collection results = (Collection) query.execute();
223: logger.log(BasicLevel.DEBUG, "Over-" + limit + " ordered:");
224: Iterator it = results.iterator();
225: double age = 0;
226: while (it.hasNext()) {
227: Employee e = (Employee) it.next();
228: assertTrue(age < e.getAge());
229: age = e.getAge();
230: logger.log(BasicLevel.DEBUG, e.toString());
231: }
232: query.closeAll();
233: }
234:
235: //retrieve all the over-XX secretary
236: public void retrieveOverXXSecretary(PersistenceManager pm, int limit) {
237: Query query = pm.newQuery(Secretary.class, "age > " + limit);
238: query.setOrdering("age ascending");
239: Collection results = (Collection) query.execute();
240: logger.log(BasicLevel.DEBUG, "Over-" + limit + " ordered:");
241: Iterator it = results.iterator();
242: double age = 0;
243: while (it.hasNext()) {
244: Employee e = (Employee) it.next();
245: assertTrue(age < e.getAge());
246: age = e.getAge();
247: logger.log(BasicLevel.DEBUG, e.toString());
248: }
249: query.closeAll();
250: }
251:
252: public static class CountResult {
253: public String name;
254: public String type;
255: public Long count;
256:
257: public CountResult(String name, String type, Long count) {
258: this .name = name;
259: this .type = type;
260: this .count = count;
261: }
262:
263: public String toString() {
264: return "name: " + name + ", type: " + type + ", count: "
265: + count;
266: }
267: }
268:
269: // count all employees
270: public void countAllEmployees(PersistenceManager pm) {
271: Query query = pm.newQuery(Employee.class);
272: query.setResult("name, type, count(this)");
273: query.setGrouping("name, type");
274: query.setResultClass(CountResult.class);
275: Collection results = (Collection) query.execute();
276: logger.log(BasicLevel.DEBUG, "Count:");
277: Iterator it = results.iterator();
278: while (it.hasNext()) {
279: CountResult cr = (CountResult) it.next();
280: logger.log(BasicLevel.DEBUG, "" + cr.toString());
281: }
282: query.closeAll();
283: }
284:
285: public void testRemovingOfPersistentObject() {
286: PersistenceManager pm = pmf.getPersistenceManager();
287: try {
288: Class[] cs = new Class[] { Employee.class, Day.class,
289: Department.class };
290: pm.currentTransaction().begin();
291: for (int i = 0; i < cs.length; i++) {
292: Query query = pm.newQuery(cs[i]);
293: Collection col = (Collection) query.execute();
294: Iterator it = col.iterator();
295: while (it.hasNext()) {
296: Object o = it.next();
297: Assert.assertNotNull(
298: "null object in the query result"
299: + cs[i].getName(), o);
300: pm.deletePersistent(o);
301:
302: }
303: query.close(col);
304: }
305: pm.currentTransaction().commit();
306: } catch (JDOException e) {
307: Exception ie = ExceptionHelper.getNested(e);
308: logger.log(BasicLevel.ERROR, "", ie);
309: fail(ie.getMessage());
310: } finally {
311: pm.close();
312: }
313: }
314: }
|