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 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.mim.api.SpeedoProxy; 42 import org.objectweb.speedo.pm.api.ProxyManager; 43 import org.objectweb.speedo.pobjects.detach.Car; 44 import org.objectweb.speedo.pobjects.detach.Coach; 45 import org.objectweb.speedo.pobjects.detach.FormulaOne; 46 import org.objectweb.speedo.pobjects.detach.Player; 47 import org.objectweb.speedo.pobjects.detach.Team; 48 import org.objectweb.speedo.pobjects.detach.Vehicle; 49 import org.objectweb.util.monolog.api.BasicLevel; 50 51 54 public class TestAttach extends SpeedoTestHelper { 55 56 public TestAttach(String s) { 57 super(s); 58 } 59 60 protected String getLoggerName() { 61 return LOG_NAME + ".rt.detach.TestAttach"; 62 } 63 64 67 public void testAttachCopy() { 68 logger.log(BasicLevel.DEBUG, "***************testAttachCopy*****************"); 69 Team t = new Team("Bordeaux",null,null); 70 Collection players = new ArrayList (); 71 Player p1 = new Player("p1", t, 25); 72 players.add(p1); 73 Player p2 = new Player("p2", t, 32); 74 players.add(p2); 75 t.setPlayers(players); 76 Coach c = new Coach("c1", 5, t); 77 t.setCoach(c); 78 79 PersistenceManager pm = pmf.getPersistenceManager(); 80 pm.currentTransaction().begin(); 81 logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString()); 82 pm.makePersistent(t); 83 pm.currentTransaction().commit(); 84 try { 85 Team copyOfT = (Team) pm.detachCopy(t); 87 assertNotNull(copyOfT); 88 logger.log(BasicLevel.DEBUG, copyOfT.toString()); 90 pm.currentTransaction().begin(); 91 Team attachedTeam = (Team) pm.attachCopy(copyOfT,false); 93 assertEquals(copyOfT.getTown(), attachedTeam.getTown()); 94 assertEquals(copyOfT.getCoach().getExperience(), attachedTeam.getCoach().getExperience()); 95 assertEquals(copyOfT.getCoach().getName(), attachedTeam.getCoach().getName()); 96 pm.currentTransaction().commit(); 97 logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString()); 98 } catch (Exception e) { 99 fail(e.getMessage()); 100 } finally { 101 if (pm.currentTransaction().isActive()) 102 pm.currentTransaction().rollback(); 103 pm.close(); 104 } 105 } 106 107 110 public void testAttachModifiedCopy() { 111 logger.log(BasicLevel.DEBUG, "***************testAttachModifiedCopy*****************"); 112 Team t = new Team("Paris",null,null); 113 Collection players = new ArrayList (); 114 Player p1 = new Player("p3", t, 20); 115 players.add(p1); 116 Player p2 = new Player("p4", t, 30); 117 players.add(p2); 118 t.setPlayers(players); 119 Coach c = new Coach("c2", 10, t); 120 t.setCoach(c); 121 122 PersistenceManager pm = pmf.getPersistenceManager(); 123 pm.currentTransaction().begin(); 124 logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString()); 125 pm.makePersistent(t); 126 pm.currentTransaction().commit(); 127 Team copyOfT = (Team) pm.detachCopy(t); 129 copyOfT.getCoach().setExperience(99); 131 logger.log(BasicLevel.DEBUG, "Copy of team " + copyOfT.toString()); 133 pm.currentTransaction().begin(); 134 Team attachedTeam = (Team) pm.attachCopy(copyOfT,false); 136 try { 137 assertNotNull(attachedTeam); 138 logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString()); 139 assertEquals(99, attachedTeam.getCoach().getExperience()); 140 pm.currentTransaction().commit(); 141 } catch (Exception e) { 142 fail(e.getMessage()); 143 } finally { 144 if (pm.currentTransaction().isActive()) 145 pm.currentTransaction().rollback(); 146 pm.close(); 147 } 148 } 149 150 153 public void testAttachModifiedReference() { 154 logger.log(BasicLevel.DEBUG, "***************testAttachModifiedReference*****************"); 155 Team t = new Team("Lens",null,null); 156 Collection players = new ArrayList (); 157 Player p1 = new Player("p21", t, 20); 158 players.add(p1); 159 Player p2 = new Player("p22", t, 30); 160 players.add(p2); 161 t.setPlayers(players); 162 Coach c = new Coach("c23", 10, t); 163 t.setCoach(c); 164 165 PersistenceManager pm = pmf.getPersistenceManager(); 166 pm.currentTransaction().begin(); 167 logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString()); 168 pm.makePersistent(t); 169 pm.currentTransaction().commit(); 170 try { 171 Team copyOfT = (Team) pm.detachCopy(t); 173 Coach newCoach = new Coach("c33", 15, new Team("DummyTeam",null,null)); 174 copyOfT.setCoach(newCoach); 176 logger.log(BasicLevel.DEBUG, "Copy of team " + copyOfT.toString()); 178 pm.currentTransaction().begin(); 179 Team attachedTeam = (Team) pm.attachCopy(copyOfT,false); 181 assertNotNull("attachedTeam should not be null", attachedTeam); 182 logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString()); 183 assertEquals("Coach of the attached team is not the good one", newCoach, attachedTeam.getCoach()); 184 pm.currentTransaction().commit(); 185 } catch (Exception e) { 186 e.printStackTrace(); 187 fail(e.getMessage()); 188 } finally { 189 if (pm.currentTransaction().isActive()) 190 pm.currentTransaction().rollback(); 191 pm.close(); 192 } 193 } 194 195 198 public void testAttachModifiedCollectionCopy() { 199 logger.log(BasicLevel.DEBUG, "***************testAttachModifiedCollectionCopy*****************"); 200 Team t = new Team("Nantes",null,null); 201 Collection players = new ArrayList (); 202 Player p1 = new Player("p5", t, 20); 203 players.add(p1); 204 Player p2 = new Player("p6", t, 30); 205 players.add(p2); 206 t.setPlayers(players); 207 Coach c = new Coach("c3", 10, t); 208 t.setCoach(c); 209 210 PersistenceManager pm = pmf.getPersistenceManager(); 211 pm.currentTransaction().begin(); 212 logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString()); 213 pm.makePersistent(t); 214 pm.currentTransaction().commit(); 215 Team copyOfT = (Team) pm.detachCopy(t); 217 pm.close(); 218 copyOfT.getCoach().setExperience(99); 220 Iterator it = copyOfT.getPlayers().iterator(); 222 while(it.hasNext()){ 223 Player p = (Player) it.next(); 224 p.setAge(99); 225 } 226 logger.log(BasicLevel.DEBUG, "Copy of team " + copyOfT.toString()); 228 PersistenceManager pm2 = pmf.getPersistenceManager(); 229 try { 230 pm2.currentTransaction().begin(); 231 Team attachedTeam = (Team) ((ProxyManager)pm2).attachCopy(copyOfT,false); 233 234 Iterator itP = attachedTeam.getPlayers().iterator(); 235 while(itP.hasNext()){ 236 Player p = (Player) itP.next(); 237 assertEquals(99, p.getAge()); 238 } 239 pm2.currentTransaction().commit(); 240 logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString()); 241 } catch (Exception e) { 242 fail(e.getMessage()); 243 } finally { 244 if (pm.currentTransaction().isActive()) 245 pm.currentTransaction().rollback(); 246 pm.close(); 247 if (pm2.currentTransaction().isActive()) 248 pm2.currentTransaction().rollback(); 249 pm2.close(); 250 } 251 } 252 253 256 public void testAttachNewObject() { 257 logger.log(BasicLevel.DEBUG, "***************testAttachNewObject*****************"); 258 Team t = new Team("Bastia",null,null); 259 Collection players = new ArrayList (); 260 Player p1 = new Player("p7", t, 20); 261 players.add(p1); 262 Player p2 = new Player("p8", t, 30); 263 players.add(p2); 264 t.setPlayers(players); 265 Coach c = new Coach("c4", 10, t); 266 t.setCoach(c); 267 268 PersistenceManager pm = pmf.getPersistenceManager(); 269 pm.currentTransaction().begin(); 270 logger.log(BasicLevel.DEBUG, "attach the team " + t.toString()); 271 Team attachedTeam = (Team) pm.attachCopy(t, false); 272 pm.currentTransaction().commit(); 273 try { 274 assertTrue( ((SpeedoProxy) attachedTeam).jdoIsPersistent()); 275 logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString()); 276 } catch (Exception e) { 277 fail(e.getMessage()); 278 } finally { 279 if (pm.currentTransaction().isActive()) 280 pm.currentTransaction().rollback(); 281 pm.close(); 282 } 283 } 284 285 288 public void testAttachNullRef() { 289 logger.log(BasicLevel.DEBUG, "***************testAttachNullRef*****************"); 290 Team t = new Team("Istres",null,null); 291 Collection players = new ArrayList (); 292 Player p1 = new Player("p9", t, 20); 293 players.add(p1); 294 Player p2 = new Player("p10", t, 30); 295 players.add(p2); 296 t.setPlayers(players); 297 Coach c = new Coach("c5", 10, t); 298 t.setCoach(c); 299 300 PersistenceManager pm = pmf.getPersistenceManager(); 301 pm.currentTransaction().begin(); 302 logger.log(BasicLevel.DEBUG, "make persistent the team " + t.toString()); 303 pm.makePersistent(t); 304 pm.currentTransaction().commit(); 305 try { 306 Team copyOfT = (Team) pm.detachCopy(t); 308 assertNotNull(copyOfT); 309 assertNotNull("Coach of detached team should not be null.", copyOfT.getCoach()); 310 copyOfT.setCoach(null); 312 assertNull(copyOfT.getCoach()); 313 logger.log(BasicLevel.DEBUG, copyOfT.toString()); 315 pm.currentTransaction().begin(); 316 Team attachedTeam = (Team) pm.attachCopy(copyOfT,false); 318 assertEquals(copyOfT.getTown(), attachedTeam.getTown()); 319 assertNull("Coach of attached team should be null", attachedTeam.getCoach()); 320 pm.currentTransaction().commit(); 321 logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString()); 322 } catch (Exception e) { 323 fail(e.getMessage()); 324 } finally { 325 if (pm.currentTransaction().isActive()) 326 pm.currentTransaction().rollback(); 327 pm.close(); 328 } 329 } 330 331 334 public void testAttachInherited() { 335 logger.log(BasicLevel.DEBUG, "***************testAttachInherited*****************"); 336 Car c = new Car("r5", 4, "red"); 337 FormulaOne f = new FormulaOne("williams", 4, "green", 262); 338 339 PersistenceManager pm = pmf.getPersistenceManager(); 340 pm.currentTransaction().begin(); 341 logger.log(BasicLevel.DEBUG, "make persistent the car " + c.toString() ); 342 logger.log(BasicLevel.DEBUG, "make persistent the formula one " + f.toString()); 343 pm.makePersistent(c); 344 pm.makePersistent(f); 345 pm.currentTransaction().commit(); 346 Car copyOfC = (Car) pm.detachCopy(c); 348 logger.log(BasicLevel.DEBUG, copyOfC.toString()); 350 FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f); 352 logger.log(BasicLevel.DEBUG, copyOfF.toString()); 354 pm.currentTransaction().begin(); 355 Car attachedCar = (Car) pm.attachCopy(copyOfC,false); 357 FormulaOne attachedF = (FormulaOne) pm.attachCopy(copyOfF,false); 359 pm.currentTransaction().commit(); 360 try { 361 assertNotNull(attachedCar); 362 assertEquals(copyOfC.getColor(), attachedCar.getColor()); 363 assertEquals(copyOfC.getName(), attachedCar.getName()); 364 assertEquals(copyOfC.getNbOfWheels(), attachedCar.getNbOfWheels()); 365 assertEquals(copyOfC.getType(), attachedCar.getType()); 366 367 assertNotNull(attachedF); 368 assertEquals(copyOfF.getColor(), attachedF.getColor()); 369 assertEquals(copyOfF.getName(), attachedF.getName()); 370 assertEquals(copyOfF.getNbOfWheels(), attachedF.getNbOfWheels()); 371 assertEquals(copyOfF.getType(), attachedF.getType()); 372 assertEquals(copyOfF.getSpeedMax(), attachedF.getSpeedMax()); 373 374 logger.log(BasicLevel.DEBUG,"The attached version of the car is as follows:\n " + attachedCar.toString()); 375 logger.log(BasicLevel.DEBUG,"The attached version of the formula one is as follows:\n " + attachedF.toString()); 376 } catch (Exception e) { 377 fail(e.getMessage()); 378 } finally { 379 if (pm.currentTransaction().isActive()) 380 pm.currentTransaction().rollback(); 381 pm.close(); 382 } 383 } 384 385 388 public void testAttachModifiedInherited() { 389 logger.log(BasicLevel.DEBUG, "***************testAttachModifiedInherited*****************"); 390 FormulaOne f = new FormulaOne("renault", 4, "yellow", 262); 391 392 PersistenceManager pm = pmf.getPersistenceManager(); 393 pm.currentTransaction().begin(); 394 logger.log(BasicLevel.DEBUG, "make persistent the formula one " + f.toString()); 395 pm.makePersistent(f); 396 pm.currentTransaction().commit(); 397 FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f); 399 copyOfF.setNbOfWheels(2); 401 copyOfF.setSpeedMax(320); 402 logger.log(BasicLevel.DEBUG, "Copy of formula one " + copyOfF.toString()); 404 pm.currentTransaction().begin(); 405 FormulaOne attachedF = (FormulaOne) pm.attachCopy(copyOfF,false); 407 try { 408 assertEquals(2, attachedF.getNbOfWheels()); 409 assertEquals(320, attachedF.getSpeedMax()); 410 pm.currentTransaction().commit(); 411 logger.log(BasicLevel.DEBUG,"The attached version of the formula one is as follows:\n " + attachedF.toString()); 412 } catch (Exception e) { 413 fail(e.getMessage()); 414 } finally { 415 if (pm.currentTransaction().isActive()) 416 pm.currentTransaction().rollback(); 417 pm.close(); 418 } 419 } 420 421 422 425 public void testAttachNewInherited() { 426 logger.log(BasicLevel.DEBUG, "***************testAttachNewInherited*****************"); 427 FormulaOne f = new FormulaOne("sauber", 4, "red", 262); 428 PersistenceManager pm = pmf.getPersistenceManager(); 429 pm.currentTransaction().begin(); 430 logger.log(BasicLevel.DEBUG, "attach the f1 " + f.toString()); 431 FormulaOne attachedF = (FormulaOne) pm.attachCopy(f,false); 432 try { 433 assertTrue(((SpeedoProxy)attachedF).jdoIsPersistent()); 434 pm.currentTransaction().commit(); 435 logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedF.toString()); 436 } catch (Exception e) { 437 fail(e.getMessage()); 438 } finally { 439 if (pm.currentTransaction().isActive()) 440 pm.currentTransaction().rollback(); 441 pm.close(); 442 } 443 } 444 445 446 449 public void testAttachInvalidInherited() { 450 logger.log(BasicLevel.DEBUG, "***************testAttachInvalidInherited*****************"); 451 FormulaOne f = new FormulaOne("ferrari", 4, "red", 262); 452 PersistenceManager pm = pmf.getPersistenceManager(); 453 pm.currentTransaction().begin(); 454 logger.log(BasicLevel.DEBUG, "make persistent the f1 " + f.toString()); 455 pm.makePersistent(f); 456 pm.currentTransaction().commit(); 457 pm.currentTransaction().begin(); 458 f.setNbOfWheels(1); 460 FormulaOne copyOfF = (FormulaOne) pm.detachCopy(f); 462 pm.currentTransaction().rollback(); 464 logger.log(BasicLevel.DEBUG, "Copy of f1 " + copyOfF.toString()); 466 logger.log(BasicLevel.DEBUG, "rollback of the tx: the attach shouldn't work."); 467 try{ 468 pm.currentTransaction().begin(); 470 FormulaOne attachedF = (FormulaOne) pm.attachCopy(copyOfF,false); 471 pm.currentTransaction().commit(); 472 logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedF.toString()); 473 } catch(Exception e){ 474 assertEquals("Wrong exception thrown: " + e.getMessage(), JDOException.class, e.getClass()); 475 } finally { 476 if (pm.currentTransaction().isActive()) { 477 pm.currentTransaction().rollback(); 478 } 479 pm.close(); 480 } 481 } 482 483 public void testRemovingOfPersistentObject() { 484 PersistenceManager pm = pmf.getPersistenceManager(); 485 try { 486 Class [] cs = new Class []{Car.class, Coach.class, FormulaOne.class, 487 Player.class, Team.class, Vehicle.class}; 488 pm.currentTransaction().begin(); 489 for(int i=0; i<cs.length; i++) { 490 Query query = pm.newQuery(cs[i]); 491 Collection col = (Collection ) query.execute(); 492 Iterator it = col.iterator(); 493 while(it.hasNext()) { 494 Object o = it.next(); 495 Assert.assertNotNull("null object in the query result" 496 + cs[i].getName(), o); 497 pm.deletePersistent(o); 498 499 } 500 query.close(col); 501 } 502 pm.currentTransaction().commit(); 503 } catch (JDOException e) { 504 Exception ie = ExceptionHelper.getNested(e); 505 logger.log(BasicLevel.ERROR, "", ie); 506 fail(ie.getMessage()); 507 } finally { 508 pm.close(); 509 } 510 } 511 } 512 | Popular Tags |