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.pobjects.detach.Coach; 42 import org.objectweb.speedo.pobjects.detach.Player; 43 import org.objectweb.speedo.pobjects.detach.Team; 44 import org.objectweb.util.monolog.api.BasicLevel; 45 46 49 public class TestVersion extends SpeedoTestHelper { 50 51 public TestVersion(String s) { 52 super(s); 53 } 54 55 protected String getLoggerName() { 56 return LOG_NAME + ".rt.detach.TestVersion"; 57 } 58 59 67 public void testVersion1() { 68 logger.log(BasicLevel.DEBUG, "***************testVersion1*****************"); 69 Team t = new Team("T1",null,null); 70 Collection players = new ArrayList (); 71 Player p1 = new Player("p1", t, 20); 72 players.add(p1); 73 Player p2 = new Player("p2", t, 30); 74 players.add(p2); 75 t.setPlayers(players); 76 Coach c = new Coach("c1", 10, t); 77 t.setCoach(c); 78 PersistenceManager pm = pmf.getPersistenceManager(); 79 pm.currentTransaction().begin(); 80 pm.makePersistent(t); 81 pm.currentTransaction().commit(); 82 pm.currentTransaction().begin(); 83 Team copyOfT = (Team) pm.detachCopy(t); 85 pm.currentTransaction().commit(); 87 pm.currentTransaction().begin(); 88 logger.log(BasicLevel.DEBUG, "ATTACH BEGIN"); 89 try{ 90 Team attachedTeam = (Team) pm.attachCopy(copyOfT,false); 92 logger.log(BasicLevel.DEBUG, "ATTACH END"); 93 pm.currentTransaction().commit(); 94 logger.log(BasicLevel.DEBUG,"The attached version of the team is as follows:\n " + attachedTeam.toString()); 95 }catch(Exception e){ 96 fail("The attached is supposed to be ok"); 97 } finally { 98 if (pm.currentTransaction().isActive()) { 99 pm.currentTransaction().rollback(); 100 } 101 pm.close(); 102 } 103 } 104 105 106 115 public void testVersion2() { 116 logger.log(BasicLevel.DEBUG, "***************testVersion2*****************"); 117 Team t = new Team("T2",null,null); 118 Collection players = new ArrayList (); 119 Player p1 = new Player("p3", t, 20); 120 players.add(p1); 121 Player p2 = new Player("p4", t, 30); 122 players.add(p2); 123 t.setPlayers(players); 124 Coach c = new Coach("c2", 10, t); 125 t.setCoach(c); 126 PersistenceManager pm = pmf.getPersistenceManager(); 127 pm.currentTransaction().begin(); 128 pm.makePersistent(t); 129 pm.currentTransaction().commit(); 130 pm.currentTransaction().begin(); 132 t.getCoach().setExperience(99); 134 Iterator it = t.getPlayers().iterator(); 135 while(it.hasNext()){ 137 Player p = (Player) it.next(); 138 p.setAge(99); 139 } 140 Team copyOfT = (Team) pm.detachCopy(t); 142 pm.currentTransaction().commit(); 144 pm.currentTransaction().begin(); 145 try{ 146 Team attachedTeam = (Team) pm.attachCopy(copyOfT,false); 148 pm.currentTransaction().commit(); 149 }catch(Exception e){ 150 fail("The attached is supposed to be ok"); 151 } finally { 152 if (pm.currentTransaction().isActive()) { 153 pm.currentTransaction().rollback(); 154 } 155 pm.close(); 156 } 157 } 158 159 168 public void testVersion3() { 169 logger.log(BasicLevel.DEBUG, "***************testVersion3*****************"); 170 Team t = new Team("T3",null,null); 171 Collection players = new ArrayList (); 172 Player p1 = new Player("p5", t, 20); 173 players.add(p1); 174 Player p2 = new Player("p6", t, 30); 175 players.add(p2); 176 t.setPlayers(players); 177 Coach c = new Coach("c3", 10, t); 178 t.setCoach(c); 179 PersistenceManager pm = pmf.getPersistenceManager(); 180 pm.currentTransaction().begin(); 181 pm.makePersistent(t); 182 pm.currentTransaction().commit(); 183 pm.currentTransaction().begin(); 185 Team copyOfT = (Team) pm.detachCopy(t); 187 t.setCoach(new Coach("c31", 10, t)); 189 Iterator it = t.getPlayers().iterator(); 190 while(it.hasNext()){ 192 Player p = (Player) it.next(); 193 p.setAge(99); 194 } 195 pm.currentTransaction().commit(); 197 pm.currentTransaction().begin(); 198 try{ 199 Team attachedTeam = (Team) pm.attachCopy(copyOfT,false); 201 pm.currentTransaction().commit(); 202 fail("The attached is supposed to fail"); 203 }catch(Exception e){ 204 assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass()); 205 } finally { 206 if (pm.currentTransaction().isActive()) { 207 pm.currentTransaction().rollback(); 208 } 209 pm.close(); 210 } 211 } 212 213 214 222 public void testVersion4() { 223 logger.log(BasicLevel.DEBUG, "***************testVersion4*****************"); 224 Team t = new Team("T4",null,null); 225 Collection players = new ArrayList (); 226 Player p1 = new Player("p7", t, 20); 227 players.add(p1); 228 Player p2 = new Player("p8", t, 30); 229 players.add(p2); 230 t.setPlayers(players); 231 Coach c = new Coach("c4", 10, t); 232 t.setCoach(c); 233 PersistenceManager pm = pmf.getPersistenceManager(); 234 pm.currentTransaction().begin(); 235 pm.makePersistent(t); 236 pm.currentTransaction().commit(); 237 pm.currentTransaction().begin(); 238 Team copyOfT = (Team) pm.detachCopy(t); 240 pm.currentTransaction().rollback(); 242 pm.currentTransaction().begin(); 243 try{ 244 Team attachedTeam = (Team) pm.attachCopy(copyOfT,false); 246 pm.currentTransaction().commit(); 247 }catch(Exception e){ 248 fail("The attached is supposed to be ok"); 249 } finally { 250 if (pm.currentTransaction().isActive()) { 251 pm.currentTransaction().rollback(); 252 } 253 pm.close(); 254 } 255 } 256 257 258 267 public void testVersion5() { 268 logger.log(BasicLevel.DEBUG, "***************testVersion5*****************"); 269 Team t = new Team("T5",null,null); 270 Collection players = new ArrayList (); 271 Player p1 = new Player("p9", t, 20); 272 players.add(p1); 273 Player p2 = new Player("p10", t, 30); 274 players.add(p2); 275 t.setPlayers(players); 276 Coach c = new Coach("c5", 10, t); 277 t.setCoach(c); 278 PersistenceManager pm = pmf.getPersistenceManager(); 279 pm.currentTransaction().begin(); 280 pm.makePersistent(t); 281 pm.currentTransaction().commit(); 282 pm.currentTransaction().begin(); 284 t.setCoach(new Coach("c51", 10, t)); 286 Iterator it = t.getPlayers().iterator(); 287 while(it.hasNext()){ 289 Player p = (Player) it.next(); 290 p.setAge(99); 291 } 292 Team copyOfT = (Team) pm.detachCopy(t); 294 pm.currentTransaction().rollback(); 296 pm.currentTransaction().begin(); 297 try{ 298 Team attachedTeam = (Team) pm.attachCopy(copyOfT,false); 300 pm.currentTransaction().commit(); 301 fail("The attached is supposed to fail."); 302 }catch(Exception e){ 303 assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass()); 304 } finally { 305 if (pm.currentTransaction().isActive()) { 306 pm.currentTransaction().rollback(); 307 } 308 pm.close(); 309 } 310 } 311 312 321 public void testVersion6() { 322 logger.log(BasicLevel.DEBUG, "***************testVersion6*****************"); 323 Team t = new Team("T6",null,null); 324 Collection players = new ArrayList (); 325 Player p1 = new Player("p11", t, 20); 326 players.add(p1); 327 Player p2 = new Player("p12", t, 30); 328 players.add(p2); 329 t.setPlayers(players); 330 Coach c = new Coach("c6", 10, t); 331 t.setCoach(c); 332 PersistenceManager pm = pmf.getPersistenceManager(); 333 pm.currentTransaction().begin(); 334 pm.makePersistent(t); 335 pm.currentTransaction().commit(); 336 pm.currentTransaction().begin(); 338 Team copyOfT = (Team) pm.detachCopy(t); 340 t.getCoach().setExperience(99); 342 Iterator it = t.getPlayers().iterator(); 343 while(it.hasNext()){ 345 Player p = (Player) it.next(); 346 p.setAge(99); 347 } 348 pm.currentTransaction().rollback(); 350 pm.currentTransaction().begin(); 351 try{ 352 Team attachedTeam = (Team) pm.attachCopy(copyOfT,false); 354 pm.currentTransaction().commit(); 355 }catch(Exception e){ 356 fail("The attached is supposed to be ok."); 357 } finally { 358 if (pm.currentTransaction().isActive()) { 359 pm.currentTransaction().rollback(); 360 } 361 pm.close(); 362 } 363 } 364 365 381 public void testVersion7() { 382 logger.log(BasicLevel.DEBUG, "***************testVersion7*****************"); 383 Team t = new Team("T7",null,null); 384 Collection players = new ArrayList (); 385 Player p1 = new Player("p13", t, 20); 386 players.add(p1); 387 Player p2 = new Player("p14", t, 30); 388 players.add(p2); 389 t.setPlayers(players); 390 Coach c = new Coach("c7", 10, t); 391 t.setCoach(c); 392 PersistenceManager pm = pmf.getPersistenceManager(); 393 pm.currentTransaction().begin(); 394 pm.makePersistent(t); 395 pm.currentTransaction().commit(); 396 pm.currentTransaction().begin(); 398 Team copyOfT1 = (Team) pm.detachCopy(t); 400 Team copyOfT2 = (Team) pm.detachCopy(t); 402 pm.currentTransaction().commit(); 404 405 copyOfT1.getCoach().setExperience(99); 407 Iterator it = copyOfT1.getPlayers().iterator(); 408 while(it.hasNext()){ 410 Player p = (Player) it.next(); 411 p.setAge(99); 412 } 413 copyOfT2.getCoach().setExperience(99); 415 it = copyOfT2.getPlayers().iterator(); 416 while(it.hasNext()){ 418 Player p = (Player) it.next(); 419 p.setAge(99); 420 } 421 422 boolean attach1OK = false; 423 try{ 424 pm.currentTransaction().begin(); 425 Team attachedTeam1 = (Team) pm.attachCopy(copyOfT1,false); 427 pm.currentTransaction().commit(); 428 attach1OK = true; 429 pm.currentTransaction().begin(); 430 Team attachedTeam2 = (Team) pm.attachCopy(copyOfT2,false); 432 pm.currentTransaction().commit(); 433 fail("The second attach is supposed to fail"); 434 }catch(Exception e){ 435 assertTrue("The first attach is supposed to be ok.", attach1OK); 436 assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass()); 437 } finally { 438 if (pm.currentTransaction().isActive()) { 439 pm.currentTransaction().rollback(); 440 } 441 pm.close(); 442 } 443 } 444 445 461 public void testVersion8() { 462 logger.log(BasicLevel.DEBUG, "***************testVersion8*****************"); 463 Team t = new Team("T8",null,null); 464 Collection players = new ArrayList (); 465 Player p1 = new Player("p15", t, 20); 466 players.add(p1); 467 Player p2 = new Player("p16", t, 30); 468 players.add(p2); 469 t.setPlayers(players); 470 Coach c = new Coach("c8", 10, t); 471 t.setCoach(c); 472 PersistenceManager pm = pmf.getPersistenceManager(); 473 pm.currentTransaction().begin(); 474 pm.makePersistent(t); 475 pm.currentTransaction().commit(); 476 pm.currentTransaction().begin(); 478 Team copyOfT1 = (Team) pm.detachCopy(t); 480 Team copyOfT2 = (Team) pm.detachCopy(t); 482 pm.currentTransaction().commit(); 484 485 copyOfT1.getCoach().setExperience(99); 487 Iterator it = copyOfT1.getPlayers().iterator(); 488 while(it.hasNext()){ 490 Player p = (Player) it.next(); 491 p.setAge(99); 492 } 493 copyOfT2.getCoach().setExperience(99); 495 it = copyOfT2.getPlayers().iterator(); 496 while(it.hasNext()){ 498 Player p = (Player) it.next(); 499 p.setAge(99); 500 } 501 502 boolean attach2OK = false; 503 try{ 504 pm.currentTransaction().begin(); 505 Team attachedTeam2 = (Team) pm.attachCopy(copyOfT2,false); 507 pm.currentTransaction().commit(); 508 attach2OK = true; 509 pm.currentTransaction().begin(); 510 Team attachedTeam1 = (Team) pm.attachCopy(copyOfT1,false); 512 pm.currentTransaction().commit(); 513 fail("The attach of copy1 is supposed to fail"); 514 }catch(Exception e){ 515 assertTrue("The attach of copy2 is supposed to be ok.", attach2OK); 516 assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass()); 517 } finally { 518 if (pm.currentTransaction().isActive()) { 519 pm.currentTransaction().rollback(); 520 } 521 pm.close(); 522 } 523 } 524 525 534 public void testVersion9() { 535 logger.log(BasicLevel.DEBUG, "***************testVersion9*****************"); 536 Team t = new Team("T9",null,null); 537 Collection players = new ArrayList (); 538 Player p1 = new Player("p17", t, 20); 539 players.add(p1); 540 Player p2 = new Player("p18", t, 30); 541 players.add(p2); 542 t.setPlayers(players); 543 Coach c = new Coach("c9", 10, t); 544 t.setCoach(c); 545 PersistenceManager pm = pmf.getPersistenceManager(); 546 pm.currentTransaction().begin(); 547 pm.makePersistent(t); 548 pm.currentTransaction().commit(); 549 Team copyOfT = (Team) pm.detachCopy(t); 551 pm.currentTransaction().begin(); 553 t.getCoach().setExperience(99); 555 Iterator it = t.getPlayers().iterator(); 556 while(it.hasNext()){ 558 Player p = (Player) it.next(); 559 p.setAge(99); 560 } 561 try{ 562 Team attachedTeam = (Team) pm.attachCopy(copyOfT,false); 564 pm.currentTransaction().commit(); 565 fail("The attached is supposed to fail."); 566 }catch(Exception e){ 567 assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass()); 568 } finally { 569 if (pm.currentTransaction().isActive()) { 570 pm.currentTransaction().rollback(); 571 } 572 pm.close(); 573 } 574 } 575 576 587 public void testVersion10() { 588 logger.log(BasicLevel.DEBUG, "***************testVersion10*****************"); 589 Team t = new Team("T10",null,null); 590 Collection players = new ArrayList (); 591 Player p1 = new Player("p19", t, 20); 592 players.add(p1); 593 Player p2 = new Player("p20", t, 30); 594 players.add(p2); 595 t.setPlayers(players); 596 Coach c = new Coach("c10", 10, t); 597 t.setCoach(c); 598 PersistenceManager pm = pmf.getPersistenceManager(); 599 pm.currentTransaction().begin(); 600 pm.makePersistent(t); 601 pm.currentTransaction().commit(); 602 Team copyOfT = (Team) pm.detachCopy(t); 604 pm.currentTransaction().begin(); 606 t.getCoach().setExperience(99); 608 Iterator it = t.getPlayers().iterator(); 609 while(it.hasNext()){ 611 Player p = (Player) it.next(); 612 p.setAge(99); 613 } 614 pm.currentTransaction().commit(); 616 pm.currentTransaction().begin(); 618 try{ 619 Team attachedTeam = (Team) pm.attachCopy(copyOfT,false); 621 pm.currentTransaction().commit(); 622 fail("The attached is supposed to fail."); 623 }catch(Exception e){ 624 assertEquals("Wrong exception thrown: " + e.getClass(), JDOException.class, e.getClass()); 625 } finally { 626 if (pm.currentTransaction().isActive()) { 627 pm.currentTransaction().rollback(); 628 } 629 pm.close(); 630 } 631 } 632 633 644 public void testVersion11() { 645 logger.log(BasicLevel.DEBUG, "***************testVersion11*****************"); 646 Team t = new Team("T11",null,null); 647 Collection players = new ArrayList (); 648 Player p1 = new Player("p21", t, 20); 649 players.add(p1); 650 Player p2 = new Player("p22", t, 30); 651 players.add(p2); 652 t.setPlayers(players); 653 Coach c = new Coach("c11", 10, t); 654 t.setCoach(c); 655 PersistenceManager pm = pmf.getPersistenceManager(); 656 pm.currentTransaction().begin(); 657 pm.makePersistent(t); 658 pm.currentTransaction().commit(); 659 Team copyOfT = (Team) pm.detachCopy(t); 661 pm.currentTransaction().begin(); 663 t.getCoach().setExperience(99); 665 Iterator it = t.getPlayers().iterator(); 666 while(it.hasNext()){ 668 Player p = (Player) it.next(); 669 p.setAge(99); 670 } 671 pm.currentTransaction().rollback(); 673 pm.currentTransaction().begin(); 675 try{ 676 Team attachedTeam = (Team) pm.attachCopy(copyOfT,false); 678 pm.currentTransaction().commit(); 679 }catch(Exception e){ 680 fail("The attached is supposed to be ok."); 681 } finally { 682 if (pm.currentTransaction().isActive()) { 683 pm.currentTransaction().rollback(); 684 } 685 pm.close(); 686 } 687 } 688 689 public void testRemovingOfPersistentObject() { 690 PersistenceManager pm = pmf.getPersistenceManager(); 691 try { 692 Class [] cs = new Class []{Coach.class, Player.class, Team.class}; 693 pm.currentTransaction().begin(); 694 for(int i=0; i<cs.length; i++) { 695 Query query = pm.newQuery(cs[i]); 696 Collection col = (Collection ) query.execute(); 697 Iterator it = col.iterator(); 698 while(it.hasNext()) { 699 Object o = it.next(); 700 Assert.assertNotNull("null object in the query result" 701 + cs[i].getName(), o); 702 pm.deletePersistent(o); 703 704 } 705 query.close(col); 706 } 707 pm.currentTransaction().commit(); 708 } catch (JDOException e) { 709 Exception ie = ExceptionHelper.getNested(e); 710 logger.log(BasicLevel.ERROR, "", ie); 711 fail(ie.getMessage()); 712 } finally { 713 pm.close(); 714 } 715 } 716 } 717 | Popular Tags |