1 25 26 package org.objectweb.speedo.runtime.detach; 27 28 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import javax.jdo.Extent; 35 import javax.jdo.JDOException; 36 import javax.jdo.JDOUserException; 37 import javax.jdo.PersistenceManager; 38 import javax.jdo.Query; 39 40 import junit.framework.Assert; 41 42 import org.objectweb.speedo.SpeedoTestHelper; 43 import org.objectweb.speedo.api.ExceptionHelper; 44 import org.objectweb.speedo.mim.api.SpeedoProxy; 45 import org.objectweb.speedo.pobjects.detach.Car; 46 import org.objectweb.speedo.pobjects.detach.Coach; 47 import org.objectweb.speedo.pobjects.detach.FormulaOne; 48 import org.objectweb.speedo.pobjects.detach.Player; 49 import org.objectweb.speedo.pobjects.detach.Team; 50 import org.objectweb.speedo.pobjects.detach.Vehicle; 51 import org.objectweb.util.monolog.api.BasicLevel; 52 53 56 public class TestDetach extends SpeedoTestHelper { 57 58 public TestDetach(String s) { 59 super(s); 60 } 61 62 protected String getLoggerName() { 63 return LOG_NAME + ".rt.detach.TestDetach"; 64 } 65 66 69 public void testDetachCopy() { 70 logger.log(BasicLevel.DEBUG, "***************testDetachCopy*****************"); 71 Team t = new Team("Bordeaux",null,null); 72 Collection players = new ArrayList (); 73 Player p1 = new Player("p1", t, 25); 74 players.add(p1); 75 Player p2 = new Player("p2", t, 32); 76 players.add(p2); 77 t.setPlayers(players); 78 Coach c = new Coach("c1", 5, t); 79 t.setCoach(c); 80 81 PersistenceManager pm = pmf.getPersistenceManager(); 82 pm.currentTransaction().begin(); 83 logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString()); 84 pm.makePersistent(t); 85 pm.currentTransaction().commit(); 86 87 t = null; 88 p1 = null; 89 p2 = null; 90 c = null; 91 92 pm.evictAll(); 93 pm.close(); 94 95 96 pm = pmf.getPersistenceManager(); 97 Query query = pm.newQuery(Team.class, "players.contains(player) & player.age<12"); 98 query.declareVariables("Player player"); 99 Collection results = (Collection )query.execute(); 100 assertTrue("The result of the query should be empty.", results.isEmpty()); 101 query.closeAll(); 102 103 query = pm.newQuery(Team.class, "players.contains(player) & player.age>12"); 104 query.declareVariables("Player player"); 105 results = (Collection )query.execute(); 106 assertTrue("The result of the query shouldn't be empty.", !results.isEmpty()); 107 Team res = (Team) results.iterator().next(); 108 Collection cpl = res.getPlayers(); 109 Iterator itPlayer = cpl.iterator(); 110 while(itPlayer.hasNext()) { 111 Player pl = (Player) itPlayer.next(); 112 assertTrue("The age of the player should be over 12. It is " + pl.getAge() + ".", pl.getAge()>12); 113 } 114 query.closeAll(); 115 116 query = pm.newQuery(Team.class, "town==\"Bordeaux\""); 117 results = (Collection )query.execute(); 118 assertEquals("The result of the query shouldn't be empty.", false, results.isEmpty()); 119 Team toDetach = (Team) results.iterator().next(); 120 query.closeAll(); 121 122 Team copyOfT = (Team) pm.detachCopy(toDetach); 124 try { 125 assertNotNull(copyOfT); 126 assertEquals("Town of team and detached team are not the same", toDetach.getTown(), copyOfT.getTown()); 127 assertEquals("Coach experience of team and detached team are not the same",toDetach.getCoach().getExperience(), copyOfT.getCoach().getExperience()); 128 assertEquals("Coach name of team and detached team are not the same",toDetach.getCoach().getName(), copyOfT.getCoach().getName()); 129 logger.log(BasicLevel.DEBUG, copyOfT.toString()); 131 } catch (Exception e) { 132 fail(e.getMessage()); 133 } finally { 134 if (pm.currentTransaction().isActive()) 135 pm.currentTransaction().rollback(); 136 pm.close(); 137 } 138 } 139 140 141 144 public void testDetachNonPersistentCopy() { 145 logger.log(BasicLevel.DEBUG, "*************testDetachNonPersistentCopy*****************"); 146 Team t = new Team("Marseille",null,null); 147 Coach c = new Coach("c2", 5, t); 148 t.setCoach(c); 149 Player p = new Player("p3", t, 25); 150 t.addPlayer(p); 151 PersistenceManager pm = pmf.getPersistenceManager(); 152 pm.currentTransaction().begin(); 153 Team copyOfT = (Team) pm.detachCopy(t); 155 pm.currentTransaction().commit(); 156 try { 157 assertNotNull(copyOfT); 158 assertTrue(((SpeedoProxy )t).jdoIsPersistent()); 159 logger.log(BasicLevel.DEBUG, copyOfT.toString()); 161 } catch(Exception e) { 162 fail(e.getMessage()); 163 } finally { 164 if (pm.currentTransaction().isActive()) 165 pm.currentTransaction().rollback(); 166 pm.close(); 167 } 168 } 169 170 173 public void testDetachPersistentNew() { 174 logger.log(BasicLevel.DEBUG, "******************testDetachPersistentNew****************"); 175 Team t = new Team("Monaco",null,null); 176 Coach c = new Coach("c3", 5, t); 177 t.setCoach(c); 178 Player p = new Player("p4", t, 25); 179 t.addPlayer(p); 180 181 PersistenceManager pm = pmf.getPersistenceManager(); 182 pm.currentTransaction().begin(); 183 pm.makePersistent(c); 184 Team copyOfT = (Team) pm.detachCopy(t); 186 try { 187 assertNotNull(copyOfT); 188 assertEquals("Town of team and detached team are not the same", t.getTown(), copyOfT.getTown()); 189 assertEquals("Coach experience of team and detached team are not the same",t.getCoach().getExperience(), copyOfT.getCoach().getExperience()); 190 assertEquals("Coach name of team and detached team are not the same",t.getCoach().getName(), copyOfT.getCoach().getName()); 191 pm.currentTransaction().commit(); 192 logger.log(BasicLevel.DEBUG, copyOfT.toString()); 194 } catch(Exception e) { 195 fail(e.getMessage()); 196 } finally { 197 if (pm.currentTransaction().isActive()) 198 pm.currentTransaction().rollback(); 199 pm.close(); 200 } 201 } 202 203 206 public void testDetachPersistentDirty() { 207 logger.log(BasicLevel.DEBUG, "******************testDetachPersistentDirty****************"); 208 Team t = new Team("Nantes",null,null); 209 Coach c = new Coach("c4", 5, t); 210 t.setCoach(c); 211 Player p = new Player("p5", t, 28); 212 t.addPlayer(p); 213 214 PersistenceManager pm = pmf.getPersistenceManager(); 215 pm.currentTransaction().begin(); 217 pm.makePersistent(c); 218 pm.currentTransaction().commit(); 219 pm.currentTransaction().begin(); 221 c.setExperience(10); 222 Team copyOfT = (Team) pm.detachCopy(t); 224 pm.currentTransaction().commit(); 225 try { 226 assertEquals(10, copyOfT.getCoach().getExperience()); 227 logger.log(BasicLevel.DEBUG, copyOfT.toString()); 228 } catch(Exception e) { 229 fail(e.getMessage()); 230 } finally { 231 if (pm.currentTransaction().isActive()) 232 pm.currentTransaction().rollback(); 233 pm.close(); 234 } 235 } 236 237 240 public void testDetachPersistentDeleted() { 241 logger.log(BasicLevel.DEBUG, "******************testDetachPersistentDeleted****************"); 242 Team t = new Team("Paris",null, null); 243 Coach c = new Coach("c5", 5, t); 244 t.setCoach(c); 245 Player p = new Player("p6", t, 28); 246 t.addPlayer(p); 247 248 PersistenceManager pm = pmf.getPersistenceManager(); 249 pm.currentTransaction().begin(); 251 pm.makePersistent(c); 252 pm.currentTransaction().commit(); 253 pm.currentTransaction().begin(); 255 pm.deletePersistent(c); 256 try { 257 pm.detachCopy(c); 259 pm.currentTransaction().commit(); 260 } catch (Exception e) { 261 262 assertEquals("Wrong exception thrown: " + e.getMessage(), 263 JDOUserException.class, e.getClass()); 264 } finally { 265 if (pm.currentTransaction().isActive()) { 266 pm.currentTransaction().commit(); 267 } 268 pm.close(); 269 } 270 } 271 272 275 public void testDetachPersistentDeletedCollection() { 276 logger.log(BasicLevel.DEBUG, "******************testDetachPersistentDeletedCollection****************"); 277 Team t = new Team("Auxerre",null, null); 278 Coach c = new Coach("c6", 10, t); 279 t.setCoach(c); 280 Player p = new Player("p7", t, 28); 281 t.addPlayer(p); 282 283 PersistenceManager pm = pmf.getPersistenceManager(); 284 pm.currentTransaction().begin(); 286 pm.makePersistent(c); 287 pm.currentTransaction().commit(); 288 pm.currentTransaction().begin(); 290 pm.deletePersistent(p); 291 try{ 292 pm.detachCopy(c); 295 pm.currentTransaction().commit(); 296 } catch (Exception e) { 297 assertEquals("Wrong exception thrown: " + e.getMessage(), 298 JDOUserException.class, e.getClass()); 299 } finally { 300 if (pm.currentTransaction().isActive()) { 301 pm.currentTransaction().commit(); 302 } 303 pm.close(); 304 } 305 } 306 307 310 public void testDetachNullReference() { 311 logger.log(BasicLevel.DEBUG, "***************testDetachNullReference*****************"); 312 Team t = new Team("Niort",null,null); 313 Collection players = new ArrayList (); 314 Player p1 = new Player("p8", t, 25); 315 players.add(p1); 316 Player p2 = new Player("p9", t, 32); 317 players.add(p2); 318 t.setPlayers(players); 319 320 PersistenceManager pm = pmf.getPersistenceManager(); 321 pm.currentTransaction().begin(); 322 logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString()); 323 pm.makePersistent(t); 324 pm.currentTransaction().commit(); 325 326 try { 327 List result = new ArrayList (); 328 Extent extent = pm.getExtent(Team.class, false); 329 Query query = pm.newQuery(extent, "town == townName"); 330 query.declareParameters("String townName"); 331 Collection collection = (Collection ) query.execute(t.getTown()); 332 assertTrue("The result should not be empty", !collection.isEmpty()); 333 Iterator itr = collection.iterator(); 334 Team copyOfT = null; 335 while (itr.hasNext()) { 336 Team team = (Team) itr.next(); 337 assertEquals("The town should be " + t.getTown(), t.getTown(), team.getTown()); 338 copyOfT = (Team) pm.detachCopy(team); 340 } 341 query.close(collection); 342 extent.closeAll(); 343 assertNotNull(copyOfT); 344 assertEquals("Town of team and detached team are not the same", t.getTown(), copyOfT.getTown()); 345 assertNull("The coach reference is supposed to be null", copyOfT.getCoach()); 346 } catch (Exception e) { 347 fail(e.getMessage()); 348 } finally { 349 if (pm.currentTransaction().isActive()) 350 pm.currentTransaction().rollback(); 351 pm.close(); 352 } 353 } 354 355 358 public void testDetachInherited() { 359 logger.log(BasicLevel.DEBUG, "***************testDetachInherited*****************"); 360 Car c = new Car("r5", 4, "red"); 361 FormulaOne f = new FormulaOne("williams", 4, "green", 262); 362 363 PersistenceManager pm = pmf.getPersistenceManager(); 364 pm.currentTransaction().begin(); 365 logger.log(BasicLevel.DEBUG, "make persistent the car " + c.toString() + 366 " and the formula one " + f.toString()); 367 pm.makePersistent(c); 368 pm.makePersistent(f); 369 pm.currentTransaction().commit(); 370 try { 371 Car copyOfC = (Car) pm.detachCopy(c); 373 assertNotNull(copyOfC); 374 assertEquals(c.getName(), copyOfC.getName()); 375 assertEquals(c.getColor(), copyOfC.getColor()); 376 assertEquals(c.getNbOfWheels(), copyOfC.getNbOfWheels()); 377 assertEquals(c.getType(), copyOfC.getType()); 378 logger.log(BasicLevel.DEBUG, copyOfC.toString()); 380 FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f); 382 assertNotNull(copyOfF); 383 assertEquals(f.getName(), copyOfF.getName()); 384 assertEquals(f.getColor(), copyOfF.getColor()); 385 assertEquals(f.getNbOfWheels(), copyOfF.getNbOfWheels()); 386 assertEquals(f.getType(), copyOfF.getType()); 387 assertEquals(f.getSpeedMax(), copyOfF.getSpeedMax()); 388 logger.log(BasicLevel.DEBUG, copyOfF.toString()); 390 } catch(Exception e) { 391 fail(e.getMessage()); 392 } finally { 393 if (pm.currentTransaction().isActive()) 394 pm.currentTransaction().rollback(); 395 pm.close(); 396 } 397 } 398 399 402 public void testDetachNonPersistentInherited() { 403 logger.log(BasicLevel.DEBUG, "*************testDetachNonPersistentInherited*****************"); 404 FormulaOne f = new FormulaOne("sauber", 4, "blue", 274); 405 PersistenceManager pm = pmf.getPersistenceManager(); 406 pm.currentTransaction().begin(); 407 FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f); 409 assertTrue(((SpeedoProxy) f).jdoIsPersistent()); 410 pm.currentTransaction().commit(); 411 try { 412 assertNotNull(copyOfF); 413 assertEquals(f.getName(), copyOfF.getName()); 414 assertEquals(f.getColor(), copyOfF.getColor()); 415 assertEquals(f.getNbOfWheels(), copyOfF.getNbOfWheels()); 416 assertEquals(f.getType(), copyOfF.getType()); 417 assertEquals(f.getSpeedMax(), copyOfF.getSpeedMax()); 418 logger.log(BasicLevel.DEBUG, copyOfF.toString()); 420 } catch(Exception e) { 421 fail(e.getMessage()); 422 } finally { 423 if (pm.currentTransaction().isActive()) 424 pm.currentTransaction().rollback(); 425 pm.close(); 426 } 427 } 428 429 432 public void testDetachPersistentDeletedInherited() { 433 logger.log(BasicLevel.DEBUG, "******************testDetachPersistentDeletedInherited****************"); 434 FormulaOne f = new FormulaOne("honda", 4, "yellow", 240); 435 436 PersistenceManager pm = pmf.getPersistenceManager(); 437 pm.currentTransaction().begin(); 439 pm.makePersistent(f); 440 pm.currentTransaction().commit(); 441 pm.currentTransaction().begin(); 443 pm.deletePersistent(f); 444 try{ 445 pm.detachCopy(f); 447 } catch(Exception e){ 448 assertEquals("Wrong exception thrown: ",JDOUserException.class, e.getClass()); 449 } finally { 450 if (pm.currentTransaction().isActive()) 451 pm.currentTransaction().rollback(); 452 pm.close(); 453 } 454 } 455 456 457 460 public void testDetachPersistentDirtyInherited() { 461 logger.log(BasicLevel.DEBUG, "******************testDetachPersistentDirtyInherited****************"); 462 FormulaOne f = new FormulaOne("ferrari", 4, "yellow", 240); 463 PersistenceManager pm = pmf.getPersistenceManager(); 464 pm.currentTransaction().begin(); 466 pm.makePersistent(f); 467 pm.currentTransaction().commit(); 468 pm.currentTransaction().begin(); 470 f.setNbOfWheels(3); 471 f.setSpeedMax(300); 472 pm.currentTransaction().commit(); 473 try { 474 FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f); 476 assertEquals(f.getName(), copyOfF.getName()); 477 assertEquals(f.getColor(), copyOfF.getColor()); 478 assertEquals(3, copyOfF.getNbOfWheels()); 479 assertEquals(f.getType(), copyOfF.getType()); 480 assertEquals(300, copyOfF.getSpeedMax()); 481 logger.log(BasicLevel.DEBUG, copyOfF.toString()); 483 } catch(Exception e) { 484 fail(e.getMessage()); 485 } finally { 486 if (pm.currentTransaction().isActive()) 487 pm.currentTransaction().rollback(); 488 pm.close(); 489 } 490 } 491 492 495 public void testDetachPersistentNewInherited() { 496 logger.log(BasicLevel.DEBUG, "******************testDetachPersistentNewInherited****************"); 497 FormulaOne f = new FormulaOne("renault", 4, "yellow", 260); 498 499 PersistenceManager pm = pmf.getPersistenceManager(); 500 pm.currentTransaction().begin(); 501 pm.makePersistent(f); 502 pm.currentTransaction().commit(); 503 try { 504 FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f); 506 assertEquals(f.getName(), copyOfF.getName()); 507 assertEquals(f.getColor(), copyOfF.getColor()); 508 assertEquals(f.getNbOfWheels(), copyOfF.getNbOfWheels()); 509 assertEquals(f.getType(), copyOfF.getType()); 510 assertEquals(f.getSpeedMax(), copyOfF.getSpeedMax()); 511 logger.log(BasicLevel.DEBUG, copyOfF.toString()); 513 } catch(Exception e) { 514 fail(e.getMessage()); 515 } finally { 516 if (pm.currentTransaction().isActive()) 517 pm.currentTransaction().rollback(); 518 pm.close(); 519 } 520 } 521 522 public void testRemovingOfPersistentObject() { 523 PersistenceManager pm = pmf.getPersistenceManager(); 524 try { 525 Class [] cs = new Class []{Coach.class, Player.class, Team.class, Vehicle.class}; 526 pm.currentTransaction().begin(); 527 for(int i=0; i<cs.length; i++) { 528 Query query = pm.newQuery(cs[i]); 529 Collection col = (Collection ) query.execute(); 530 Iterator it = col.iterator(); 531 while(it.hasNext()) { 532 Object o = it.next(); 533 Assert.assertNotNull("null object in the query result" 534 + cs[i].getName(), o); 535 pm.deletePersistent(o); 536 537 } 538 query.close(col); 539 } 540 pm.currentTransaction().commit(); 541 } catch (JDOException e) { 542 Exception ie = ExceptionHelper.getNested(e); 543 logger.log(BasicLevel.ERROR, "", ie); 544 fail(ie.getMessage()); 545 } finally { 546 if (pm.currentTransaction().isActive()) 547 pm.currentTransaction().rollback(); 548 pm.close(); 549 } 550 } 551 } 552 | Popular Tags |