1 25 26 package org.objectweb.jonas.jtests.clients.entity; 27 28 import java.rmi.RemoteException ; 29 import java.util.Enumeration ; 30 31 import javax.ejb.DuplicateKeyException ; 32 import javax.ejb.EJBException ; 33 import javax.ejb.FinderException ; 34 import javax.ejb.Handle ; 35 import javax.ejb.RemoveException ; 36 import javax.rmi.PortableRemoteObject ; 37 import javax.transaction.RollbackException ; 38 39 import org.objectweb.jonas.jtests.beans.annuaire.Personne; 40 import org.objectweb.jonas.jtests.beans.annuaire.PersonneHome; 41 import org.objectweb.jonas.jtests.util.JTestCase; 42 43 48 public abstract class A_AdvancedHomeEC extends JTestCase { 49 50 String mynum = "638"; 51 String myname = "Philippe Durieux"; 52 53 public A_AdvancedHomeEC(String name) { 54 super(name); 55 } 56 57 protected void setUp() { 58 super.setUp(); 59 } 60 61 64 abstract public PersonneHome getHome(); 65 66 70 public void testFindAllEnum() throws Exception { 71 Enumeration e = null; 72 e = getHome().findAll(); 73 int count = 0; 74 while (e.hasMoreElements()) { 75 count++; 76 Personne p = (Personne) 77 PortableRemoteObject.narrow(e.nextElement(), Personne.class); 78 } 79 assertEquals("Wrong number of bean (case 1)", 10, count); 80 Personne p1 = getHome().create("Paul Morgan", "1234256"); 81 Personne p2 = getHome().create("Jean Richard", "1234356"); 82 Personne p3 = getHome().create("Jean-Paul Landry", "1234556"); 83 e = getHome().findAll(); 84 count = 0; 85 while (e.hasMoreElements()) { 86 count++; 87 Personne p = (Personne) 88 PortableRemoteObject.narrow(e.nextElement(), Personne.class); 89 } 90 assertEquals("Wrong number of bean (case 2)", 13, count); 91 p1.remove(); 93 p2.remove(); 94 p3.remove(); 95 e = getHome().findAll(); 96 count = 0; 97 while (e.hasMoreElements()) { 98 count++; 99 Personne p = (Personne) PortableRemoteObject.narrow(e.nextElement(), Personne.class); 100 } 101 assertEquals("Wrong number of bean (case 3)", 10, count); 102 } 103 104 107 public void testCreateInTx() throws Exception { 108 utx.begin(); 109 try { 110 getHome().create("Duke Ellington", "12235446"); 111 utx.commit(); 112 Personne p = getHome().findByNom("Duke Ellington"); 113 p.remove(); 114 utx.begin(); 115 getHome().create("Louis Armstrong", "12035446"); 116 } catch (Exception e) { 117 fail(e.getMessage()); 118 } finally { 119 utx.rollback(); 120 } 121 try { 122 getHome().findByNom("Louis Armstrong"); 123 fail("Should not find it"); 124 } catch (FinderException e) { 125 } 126 } 127 128 131 public void testSimpleCreateInTx() throws Exception { 132 utx.begin(); 133 try { 134 getHome().create("Mike", "12235846"); 135 } catch (Exception e) { 136 fail(e.getMessage()); 137 } finally { 138 utx.commit(); 139 } 140 Personne p = getHome().findByNom("Mike"); 141 p.remove(); 142 } 143 144 148 public void testSimpleRemove() throws Exception { 149 Personne p = getHome().findByNom("Adriana Danes"); 150 p.remove(); 151 getHome().create("Adriana Danes", "777"); } 153 154 157 public void testRemoveTwice() throws Exception { 158 Personne p = getHome().create("Andre", "77710"); p.remove(); 160 try { 161 p.remove(); 162 fail("Should not be able to remove this object twice"); 163 } catch (RemoteException e) { 164 } catch (Exception e) { 166 fail("Bad exception raised: " + e); 167 } 168 } 169 170 173 public void testRemoveTwiceTx() throws Exception { 174 Personne p = getHome().create("Andre", "77710"); utx.begin(); 176 try { 177 p.remove(); 178 p.remove(); 179 fail("Should not be able to remove this object twice"); 180 } catch (RemoteException e) { 181 } catch (Exception e) { 183 fail("Bad exception raised: " + e); 184 } finally { 185 try { 186 utx.commit(); 187 } catch (RollbackException e) { 188 getHome().remove("Andre"); 192 } 193 } 194 } 195 196 199 public void testCreateAndBusiness() throws Exception { 200 String num = "00000001"; 201 Personne p = null; 202 utx.begin(); 203 try { 204 p = getHome().create("Lionel Hampton", "92235446"); 205 p.setNumero(num); 206 } 207 catch (Exception e) { 208 fail(e.getMessage()); 209 } 210 finally { 211 utx.commit(); 212 } 213 assertTrue(num.equals(p.getNumero())); 214 p.remove(); } 216 217 220 public void testRemovePk() throws Exception { 221 Personne p = null; 222 String newman = "Xavier Spengler"; 223 p = getHome().create(newman, "6"); 224 getHome().findByPrimaryKey(newman); 225 getHome().remove(newman); 226 try { 227 getHome().findByPrimaryKey(newman); 228 fail("Should not exist anymore"); 229 } 230 catch (FinderException e) { 231 } 232 } 233 234 238 public void testRemoveHandle() throws Exception { 239 Personne p = null; 240 String newman = "Xavier Spengler"; 241 p = getHome().create(newman, "6"); 242 Handle h = p.getHandle(); 243 getHome().remove(h); 244 try { 245 getHome().findByPrimaryKey(newman); 246 fail("Should not exist anymore"); 247 } catch (FinderException e) { 248 } 249 } 250 251 255 public void testRemovePkInTx() throws Exception { 256 Personne p = null; 257 String newman = "Xavier Spengler"; 258 p = getHome().create(newman, "6"); 259 getHome().findByPrimaryKey(newman); 260 utx.begin(); 261 try { 262 getHome().remove(newman); 263 } 264 catch (Exception e) { 265 fail(e.getMessage()); 266 } 267 finally { 268 utx.commit(); 269 } 270 try { 271 getHome().findByPrimaryKey(newman); 272 fail("Should not exist anymore"); 273 } 274 catch (FinderException e) { 275 } 276 } 277 278 281 public void testRemovePkNonExistent() throws Exception { 282 String man = "NonExistent"; 283 try { 284 getHome().remove(man); 285 fail("No RemoteException"); 286 } 287 catch (RemoteException e) { 288 } 289 } 290 291 295 public void testRemovePkNonExistentInTx() throws Exception { 296 String man = "NonExistent"; 297 utx.begin(); 298 try { 299 getHome().remove(man); 300 fail("No RemoteException"); 301 } 302 catch (RemoteException e) { 303 } finally { 304 try { 305 utx.commit(); 306 fail("transaction should be marked as rollback"); 307 } catch (RollbackException e) { 308 } 309 } 310 } 311 312 316 public void testRemove() throws Exception { 317 Personne p = null; 318 String newman = "Jeannot"; 319 p = getHome().create(newman, "5"); 320 getHome().findByPrimaryKey(newman); 321 p.remove(); 322 try { 323 getHome().findByPrimaryKey(newman); 324 fail("Should not exist anymore"); 325 } 326 catch (FinderException e) { 327 } 328 } 329 330 336 public void testRemove2() throws Exception { 337 Personne p = null; 338 String newman = "Jeannot2"; 339 p = getHome().create(newman, "5"); 340 getHome().findByPrimaryKey(newman); 341 p.remove(); 342 try { 343 p.getNumero(); 344 fail("Should not exist anymore"); 345 } 346 catch (Exception e) { 347 } 348 } 349 350 355 public void testRemovePKRB() throws Exception { 356 String newnum = "344"; 357 String name = "remove-by-pk"; 358 Personne p = getHome().create(name, "420"); 359 p.setNumero(newnum); 360 utx.begin(); 361 getHome().remove(name); 362 utx.rollback(); 363 try { 364 assertEquals("Lost modified value", newnum, p.getNumero()); 365 } finally { 366 getHome().remove(name); 368 } 369 } 370 371 376 public void testRemoveRB() throws Exception { 377 String newnum = "345"; 378 String name = "remove-instance"; 379 Personne p = getHome().create(name, "421"); 380 p.setNumero(newnum); 381 utx.begin(); 382 p.remove(); 383 utx.rollback(); 384 try { 385 assertEquals("Lost modified value", newnum, p.getNumero()); 386 } finally { 387 getHome().remove(name); 390 } 391 } 392 393 397 public void testRemoveInTx() throws Exception { 398 Personne p = null; 399 String newman = "Jeannot"; 400 p = getHome().create(newman, "5"); 401 getHome().findByPrimaryKey(newman); 402 utx.begin(); 403 try { 404 p.remove(); 405 } 406 catch (Exception e) { 407 fail(e.getMessage()); 408 } 409 finally { 410 utx.commit(); 411 } 412 try { 413 getHome().findByPrimaryKey(newman); 414 fail("Should not exist anymore"); 415 } 416 catch (FinderException e) { 417 } 418 } 419 420 423 public void testFinderInTx() throws Exception { 424 Personne p = null; 425 426 for (int i = 0; i < 5; i++) { 428 utx.begin(); 429 try { 430 p = getHome().findByPrimaryKey(myname); 431 } 432 catch (Exception e) { 433 fail(e.getMessage()); 434 } 435 finally { 436 utx.commit(); 437 } 438 } 439 assertTrue(p.getNumero().equals(mynum)); 440 441 for (int i = 0; i < 5; i++) { 443 utx.begin(); 444 try { 445 p = getHome().findByNom(myname); 446 } 447 catch (Exception e) { 448 fail(e.getMessage()); 449 } 450 finally { 451 utx.commit(); 452 } 453 } 454 assertTrue(p.getNumero().equals(mynum)); 455 456 for (int i = 0; i < 5; i++) { 458 utx.begin(); 459 try { 460 getHome().findAll(); 461 } 462 catch (Exception e) { 463 fail(e.getMessage()); 464 } 465 finally { 466 utx.commit(); 467 } 468 } 469 } 470 471 474 public void testCreateRemoveInTx() throws Exception { 475 for (int i = 0; i < 20; i++) { 476 utx.begin(); 477 try { 478 getHome().create("Eric Paire", "500"); 479 getHome().remove("Eric Paire"); 480 } 481 catch (Exception e) { 482 fail(e.getMessage()); 483 } 484 finally { 485 utx.commit(); 486 } 487 } 488 } 489 490 495 public void testFindTwice() throws Exception { 496 String num = "0032"; 497 String oldnum = ""; 498 utx.begin(); 499 Personne p = null; 500 try { 501 p = getHome().findByPrimaryKey(myname); 502 oldnum = p.getNumero(); 503 p.setNumero(num); 504 Personne p2 = getHome().findByPrimaryKey(myname); 505 assertTrue(p2.getNumero().equals(num)); 506 } 507 catch (Exception e) { 508 fail(e.getMessage()); 509 } 510 finally { 511 utx.rollback(); 512 } 513 p = getHome().findByPrimaryKey(myname); 514 assertTrue(p.getNumero().equals(oldnum)); 515 } 516 517 522 public void testDuplicateKeyTx() throws Exception { 523 getHome().findByPrimaryKey(myname); 524 try { 525 getHome().create(myname, "700"); 526 fail("DuplicateKeyException not raised"); 527 } catch (DuplicateKeyException e) { 528 } catch (Exception e) { 529 fail("Bad Exception raised:" + e); 530 } 531 } 532 533 538 public void testDuplicateKey() throws Exception { 539 try { 540 getHome().create(myname, "700", true); 541 fail("DuplicateKeyException not raised"); 542 } catch (DuplicateKeyException e) { 543 } catch (Exception e) { 544 fail("Bad exception raised: " + e); 545 } 546 } 547 548 public void testDuplicateKey2() throws Exception { 549 getHome().findByPrimaryKey(myname); 550 try { 551 getHome().create(myname, "700", true); 552 fail("DuplicateKeyException not raised"); 553 } catch (DuplicateKeyException e) { 554 } catch (Exception e) { 556 } 558 utx.begin(); 559 Personne p0 = getHome().findByPrimaryKey(myname); 560 utx.commit(); 561 String num = p0.getNumero(); 562 assertTrue("bad value: " + num + ", should be: " + mynum, num.equals(mynum)); 563 } 564 565 public void testRemoveCreateTx() throws Exception { 566 utx.begin(); 567 try { 568 getHome().remove(myname); 569 getHome().create(myname, mynum, true); 570 } finally { 571 utx.commit(); 572 } 573 getHome().findByPrimaryKey(myname); 574 } 575 576 580 public void testCreateNull() throws Exception { 581 getHome().create("nullv", null); 582 getHome().remove("nullv"); } 584 585 588 public void testManyCreate() throws Exception { 589 int nbCreate = 20; 590 for (int i = 1; i <= nbCreate; i++) { 591 Personne p = getHome().create("manycreate" + i, "num" + i, true); 592 } 593 for (int i = 1; i <= nbCreate; i++) { 594 getHome().remove("manycreate" + i); 595 } 596 } 597 598 public void testRemoveCreate() throws Exception { 599 Personne p1 = null; 600 String man1 = "Count"; 601 utx.begin(); 602 getHome().create(man1, "1"); 603 utx.commit(); 604 p1 = getHome().findByNom(man1); 605 p1.remove(); 606 getHome().create(man1, "1"); 607 p1 = getHome().findByNom(man1); 608 p1.remove(); 609 } 610 611 public void testRemoveCreate2() throws Exception { 612 Personne p1 = null; 613 String man1 = "Duke"; 614 p1 = getHome().create(man1, "1"); 615 p1.remove(); 616 p1 = getHome().create(man1, "1"); 617 p1.remove(); 618 } 619 620 public void testRemoveCreate3() throws Exception { 621 Personne p4 = null; 622 String man4 = "Dexter"; 623 p4 = getHome().create(man4, "4"); 624 getHome().findByPrimaryKey(man4); 625 getHome().remove(man4); 626 p4 = getHome().create(man4, "44"); 627 getHome().remove(man4); 628 } 629 630 635 public void testIsolation() throws Exception { 636 Personne p0 = null; 637 Personne p1 = null; 638 Personne p2 = null; 639 Personne p3 = null; 640 Personne p4 = null; 641 Personne p5 = null; 642 String n = null; 643 String man1 = "Duke Ellington"; 644 String man2 = "Louis Armstrong"; 645 String man3 = "Lionel Hampton"; 646 String man4 = "Dexter Gordon"; 647 String man5 = "Bill Evans"; 648 649 utx.begin(); 651 try { 652 getHome().create(man1, "1"); 653 } 654 catch (Exception e) { 655 fail(e.getMessage()); 656 } 657 finally { 658 utx.commit(); 659 } 660 p1 = getHome().findByNom(man1); 661 662 utx.begin(); 664 try { 665 getHome().create(man2, "2"); 666 } 667 catch (Exception e) { 668 fail(e.getMessage()); 669 } 670 finally { 671 utx.rollback(); 672 } 673 674 utx.begin(); 676 try { 677 p3 = getHome().create(man3, "3"); 678 p3.setNumero("33"); 679 } 680 catch (Exception e) { 681 fail(e.getMessage()); 682 } 683 finally { 684 utx.commit(); 685 } 686 assertTrue(p3.getNumero().equals("33")); 687 688 p1.remove(); 690 p3.remove(); 691 692 p4 = getHome().create(man4, "4"); 694 getHome().findByPrimaryKey(man4); 695 getHome().remove(man4); 696 p4 = getHome().create(man4, "44"); 697 getHome().findByPrimaryKey(man4); 698 utx.begin(); 699 try { 700 getHome().remove(man4); 701 } 702 catch (Exception e) { 703 fail(e.getMessage()); 704 } 705 finally { 706 utx.commit(); 707 } 708 709 p5 = getHome().create(man5, "5"); 711 getHome().findByPrimaryKey(man5); 712 p5.remove(); 713 p5 = getHome().create(man5, "5"); 714 getHome().findByPrimaryKey(man5); 715 utx.begin(); 716 try { 717 p5.remove(); 718 } 719 catch (Exception e) { 720 fail(e.getMessage()); 721 } 722 finally { 723 utx.commit(); 724 } 725 726 for (int i = 0; i < 5; i++) { 728 utx.begin(); 729 try { 730 p0 = getHome().findByPrimaryKey(myname); 731 } 732 catch (Exception e) { 733 fail(e.getMessage()); 734 } 735 finally { 736 utx.commit(); 737 } 738 } 739 assertTrue(p0.getNumero().equals(mynum)); 740 741 for (int i = 0; i < 5; i++) { 743 utx.begin(); 744 try { 745 getHome().findAll(); 746 } 747 catch (Exception e) { 748 fail(e.getMessage()); 749 } 750 finally { 751 utx.commit(); 752 } 753 } 754 755 for (int i = 0; i < 20; i++) { 757 utx.begin(); 758 try { 759 getHome().create(man2, "500"); 760 getHome().remove(man2); 761 } 762 catch (Exception e) { 763 fail(e.getMessage()); 764 } 765 finally { 766 utx.commit(); 767 } 768 } 769 770 utx.begin(); 772 try { 773 p0 = getHome().findByPrimaryKey(myname); 774 p0.setNumero("0"); 775 p2 = getHome().findByPrimaryKey(myname); 776 assertTrue(p2.getNumero().equals("0")); 777 } 778 catch (Exception e) { 779 fail(e.getMessage()); 780 } 781 finally { 782 utx.rollback(); 783 } 784 785 } 786 787 } 788 | Popular Tags |