1 25 26 package org.objectweb.speedo.runtime.inheritance; 27 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 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 54 public class TestPrefetchQueries extends SpeedoTestHelper { 55 56 public TestPrefetchQueries(String s) { 57 super(s); 58 } 59 60 protected String getLoggerName() { 61 return LOG_NAME + ".rt.inheritance.TestPrefetchQueries"; 62 } 63 64 static final int AGE_LIMIT = 25; 65 protected int nbOverLimit; 66 67 71 public void testInheritance() { 72 logger.log(BasicLevel.DEBUG, "***************testInheritance*****************"); 73 PersistenceManager pm = pmf.getPersistenceManager(); 74 createInheritedObjects(pm); 76 pm.evictAll(); 78 countAllEmployees(pm); 79 93 pm.close(); 94 } 95 96 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 days = new ArrayList (); 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 employees = new ArrayList (); 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 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 pm.currentTransaction().begin(); 155 pm.makePersistentAll(employees); 156 pm.currentTransaction().commit(); 157 } 158 159 public void iterateExtent(PersistenceManager pm, Class cl){ 161 Extent extent = pm.getExtent(cl, true); 162 Iterator it = extent.iterator(); 163 String 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 public void retrieveOverXX(PersistenceManager pm, int limit){ 175 Query query = pm.newQuery(Employee.class,"age > " + limit); 176 query.setOrdering("age ascending"); 177 Collection results = (Collection )query.execute(); 178 assertEquals(nbOverLimit, results.size()); 179 logger.log(BasicLevel.DEBUG, "Over-" + limit + " ordered:"); 180 Iterator 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 public void retrieveOverXXWorker(PersistenceManager pm, int limit){ 193 Query query = pm.newQuery(Worker.class,"age > " + limit); 194 query.setOrdering("age ascending"); 195 Collection results = (Collection )query.execute(); 196 logger.log(BasicLevel.DEBUG, "Over-" + limit + " ordered:"); 197 Iterator 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 public void retrieveOverXXManager(PersistenceManager pm, int limit){ 210 Query query = pm.newQuery(Manager.class,"age > " + limit); 211 query.setOrdering("age ascending"); 212 Collection results = (Collection )query.execute(); 213 logger.log(BasicLevel.DEBUG, "Over-" + limit + " ordered:"); 214 Iterator 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 public void retrieveOverXXSecretary(PersistenceManager pm, int limit){ 227 Query query = pm.newQuery(Secretary.class,"age > " + limit); 228 query.setOrdering("age ascending"); 229 Collection results = (Collection )query.execute(); 230 logger.log(BasicLevel.DEBUG, "Over-" + limit + " ordered:"); 231 Iterator 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 name; 244 public String type; 245 public Integer count; 246 247 public CountResult(String name, String type, Integer count) { 248 this.name = name; 249 this.type = type; 250 this.count = count; 251 } 252 253 public String toString() { 254 return "name: " + name + ", type: " + type + ", count: " + count; 255 } 256 } 257 258 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 results = (Collection )query.execute(); 265 logger.log(BasicLevel.DEBUG, "Count:"); 266 Iterator 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 [] cs = new Class []{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 col = (Collection ) query.execute(); 282 Iterator it = col.iterator(); 283 while(it.hasNext()) { 284 Object 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 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 |