1 22 package org.jboss.test.cts.test; 23 24 25 import java.io.ByteArrayInputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.ObjectOutputStream ; 29 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 import java.util.Properties ; 33 34 import javax.ejb.Handle ; 35 import javax.ejb.HomeHandle ; 36 import javax.ejb.EJBMetaData ; 37 38 import javax.naming.InitialContext ; 39 40 import javax.rmi.PortableRemoteObject ; 41 42 import javax.transaction.UserTransaction ; 43 44 import org.jboss.test.cts.jms.ContainerMBox; 45 import org.jboss.test.cts.interfaces.CtsBmpHome; 46 import org.jboss.test.cts.interfaces.CtsBmp; 47 import org.jboss.test.cts.interfaces.UserTransactionTester; 48 import org.jboss.test.cts.interfaces.StatelessSession; 49 import org.jboss.test.cts.keys.AccountPK; 50 51 import junit.framework.Test; 52 53 import org.jboss.test.JBossTestCase; 54 55 56 62 63 64 public class BmpUnitTestCase 65 extends JBossTestCase 66 { 67 private ContainerMBox mbx = null; 68 public static final String BEAN_NAME = "GuysName"; 69 public static final String BEAN_OTHER_NAME = "OtherGuysName"; 70 public static final String BEAN_PK_007 = "007"; 71 72 78 public BmpUnitTestCase(String name) 79 { 80 super(name); 81 } 82 83 86 private CtsBmpHome getHome() 87 throws Exception 88 { 89 return (CtsBmpHome)getInitialContext().lookup("ejbcts/BMPBean"); 90 } 91 92 95 private CtsBmp doEjbCreate(AccountPK pk, String name) 96 throws Exception 97 { 98 return getHome().create(pk, name); 99 } 100 101 110 public void testEjbCreate() 111 throws Exception 112 { 113 getLog().debug( 114 "**************************************************************"); 115 getLog().debug(" testEjbCreate()"); 116 117 CtsBmp bean = null; 118 119 try { 120 getLog().debug("create bean, name=" + BEAN_NAME); 121 122 bean = doEjbCreate(new AccountPK(BEAN_PK_007), BEAN_NAME); 123 } catch (Exception ex) { 124 getLog().error("Error in bmptest", ex); 125 fail("testEjbCreate has failed!"); 126 } 127 128 assertEquals(BEAN_NAME, bean.getPersonsName()); 129 130 getLog().debug( 131 "**************************************************************"); 132 } 133 134 150 public void testEjbFinder() 151 throws Exception 152 { 153 getLog().debug( 154 "**************************************************************"); 155 getLog().debug(" testEjbFinder()"); 156 157 CtsBmp bean = null; 158 159 try { 160 CtsBmpHome home = getHome(); 161 162 getLog().debug("Create bean, name=" + BEAN_NAME); 164 doEjbCreate(new AccountPK(BEAN_PK_007), BEAN_NAME); 165 166 getLog().debug("Find bean, name=" + BEAN_NAME); 167 168 Collection clct = home.findByPersonsName(BEAN_NAME); 169 getLog().debug("Verify result set not empty"); 170 assertTrue(!clct.isEmpty()); 171 getLog().debug("OK"); 172 getLog().debug("Bean result set:"); 173 for(Iterator itr=clct.iterator(); itr.hasNext();) 174 { 175 bean = (CtsBmp)itr.next(); 176 getLog().debug("Name from Bean=" + bean.getPersonsName()); 177 getLog().debug("Verify bean name equals: " + BEAN_NAME); 178 assertTrue(bean.getPersonsName().trim().equals(BEAN_NAME)); 179 getLog().debug("OK"); 180 } 181 } catch (Exception ex) { 182 getLog().error("Error in bmptest", ex); 183 fail("testEjbFinder has failed!"); 184 } 185 186 getLog().debug( 187 "**************************************************************"); 188 } 189 190 206 public void testEjbRemove() 207 throws Exception 208 { 209 getLog().debug( 210 "**************************************************************"); 211 getLog().debug(" testEjbRemove()"); 212 213 CtsBmp bean = null; 214 215 try { 216 CtsBmpHome home = getHome(); 217 AccountPK pk = new AccountPK(BEAN_PK_007); 218 219 getLog().debug("Create a bean..."); 220 bean = doEjbCreate(pk, BEAN_NAME); 221 getLog().debug("OK"); 222 223 getLog().debug("Delete with bean.remove()..."); 224 bean.remove(); 225 getLog().debug("OK"); 226 227 getLog().debug("Recreate the bean..."); 228 bean = doEjbCreate(pk, BEAN_NAME); 229 getLog().debug("OK"); 230 231 getLog().debug("Remove the bean using primary key..."); 232 home.remove(pk); 233 getLog().debug("OK"); 234 235 getLog().debug("Reconstitute the bean..."); 236 bean = doEjbCreate(pk, BEAN_NAME); 237 getLog().debug("OK"); 238 239 getLog().debug("Get Handle object..."); 240 Handle hn = bean.getHandle( ); 241 getLog().debug("OK"); 242 243 getLog().debug("Remove the bean using the handle..."); 244 home.remove(hn); 245 getLog().debug("OK"); 246 247 getLog().debug("Bean remove, try to use.. " + 248 "Should get 'java.rmi.NoSuchObjectException'..." ); 249 try { 250 bean.getPersonsName(); 251 } catch(java.rmi.NoSuchObjectException nsoex) { 252 getLog().debug("OK"); 253 } catch(Exception ex) { 254 fail("Got Exception: expecting NoSuchObjectException" + ex.toString() ); 255 } 256 } catch (Exception ex) { 257 getLog().error("Error in bmptest", ex); 258 fail("testEjbRemove has failed!"); 259 } 260 261 getLog().debug( 262 "**************************************************************"); 263 } 264 265 278 public void testEjbLifeCycle() 279 { 280 getLog().debug( 281 "**************************************************************"); 282 getLog().debug(" testEjbLifeCycle()"); 283 284 CtsBmp bean = null; 285 286 try { 287 CtsBmpHome home = getHome(); 288 AccountPK pk = new AccountPK(BEAN_PK_007); 289 290 getLog().debug("Create a bean..."); 291 doEjbCreate(pk, BEAN_NAME); 292 getLog().debug("OK"); 293 294 getLog().debug("Use a finder method to retrieve the bean..."); 295 bean = home.findByPrimaryKey( pk ); 296 getLog().debug("OK"); 297 298 getLog().debug("Assert it is the same bean as passed to a method..." ); 299 assertTrue( this.gotRefOkay(bean, BEAN_NAME) ); 301 getLog().debug("OK"); 302 303 getLog().debug("Calling setter as a business method..."); 305 bean.setPersonsName(BEAN_OTHER_NAME); 306 getLog().debug("OK"); 307 308 getLog().debug("Get the HOME interface..."); 310 home = (CtsBmpHome)bean.getEJBHome(); 311 getLog().debug("OK"); 312 313 getLog().debug("Get the bean's Primary Key..."); 315 pk = (AccountPK)bean.getPrimaryKey(); 316 getLog().debug("OK"); 317 318 getLog().debug("Get the bean's handle..."); 319 Handle hn = bean.getHandle(); 320 getLog().debug("OK"); 321 322 getLog().debug("Remove the bean..."); 324 bean.remove(); 325 getLog().debug("OK"); 326 } catch (Exception ex) { 327 getLog().error("Error in bmptest", ex); 328 fail("testEjbCreate has failed!"); 329 } 330 331 getLog().debug( 332 "**************************************************************"); 333 } 334 335 355 public void testPrimaryKeyObjectIdentity() 356 { 357 getLog().debug( 358 "**************************************************************"); 359 getLog().debug(" testPrimaryKeyObjectIdentity()"); 360 361 CtsBmp bean = null; 362 CtsBmp anotherBean = null; 363 CtsBmp differentBean = null; 364 365 try { 366 CtsBmpHome home = getHome(); 367 AccountPK pk = new AccountPK(BEAN_PK_007); 368 369 getLog().debug("Create a bean..."); 370 bean = doEjbCreate(pk, BEAN_NAME); 371 getLog().debug("OK"); 372 373 getLog().debug("Now query based on the 'PersonsName': " + 374 BEAN_NAME + "..."); 375 Collection clct = home.findByPersonsName(BEAN_NAME); 376 getLog().debug("OK"); 377 378 getLog().debug("Verify result set not empty..."); 379 assertTrue(!clct.isEmpty()); 380 getLog().debug("OK"); 381 382 getLog().debug("Bean result set:"); 383 for (Iterator itr=clct.iterator(); itr.hasNext();) 384 { 385 anotherBean = (CtsBmp)itr.next(); 386 getLog().debug("Use 'isIdentical()' to compare beans"); 387 assertTrue(anotherBean.isIdentical(bean)); 388 getLog().debug( "beans match..OK" ); 389 } 390 391 getLog().debug("Make a bean that doesn't match.."); 392 AccountPK anotherPK = new AccountPK("123"); 393 differentBean = doEjbCreate(anotherPK, "SomeOtherGuy"); 394 getLog().debug("OK"); 395 396 getLog().debug("Use 'isIdentical()' to verify different beans..."); 397 assertTrue(!differentBean.isIdentical(bean)); 398 getLog().debug("OK...beans are different!"); 399 400 getLog().debug("Test the Primary Keys..."); 401 AccountPK beansPK = (AccountPK)bean.getPrimaryKey(); 402 AccountPK anotherBeansPK = (AccountPK)anotherBean.getPrimaryKey(); 403 assertTrue(beansPK.equals(anotherBeansPK)); 404 getLog().debug("OK...they're the same"); 405 406 getLog().debug("Compare different keys..."); 407 assertTrue(!beansPK.equals(anotherPK)); 408 getLog().debug("OK...they're different"); 409 410 getLog().debug( 411 "**************************************************************"); 412 413 } catch(Exception ex) { 414 getLog().error("Error in bmptest", ex); 415 fail("Caught an unknown exception: " + ex.toString() ); 416 } 417 } 418 419 433 public void testEjbRemoteIF() 434 { 435 getLog().debug( 436 "**************************************************************"); 437 getLog().debug(" testEjbRemoteIF ()"); 438 439 CtsBmp bean = null; 440 441 try { 442 CtsBmpHome home = getHome(); 443 AccountPK pk = new AccountPK(BEAN_PK_007); 444 445 getLog().debug("Create a bean..."); 446 bean = doEjbCreate(pk, BEAN_NAME); 447 getLog().debug("OK"); 448 449 getLog().debug("Obtain the HOME interface..."); 450 home = (CtsBmpHome)bean.getEJBHome(); 451 assertTrue(home != null); 452 getLog().debug("OK"); 453 454 getLog().debug("Obtain the HANDLE..."); 455 Handle han = bean.getHandle(); 456 assertTrue(han != null); 457 getLog().debug("OK"); 458 459 getLog().debug("Obtain the primary key..."); 460 pk = (AccountPK)bean.getPrimaryKey(); 461 assertTrue(pk != null); 462 getLog().debug("OK"); 463 464 getLog().debug("Remove the entity bean"); 465 bean.remove(); 466 getLog().debug("OK"); 467 } catch(Exception ex) { 468 getLog().error("Error in bmptest", ex); 469 fail("Caught an unknown exception" + ex.toString()); 470 } 471 472 getLog().debug( 473 "**************************************************************"); 474 } 475 476 490 public void testEntityHandle() 491 { 492 getLog().debug( 493 "**************************************************************"); 494 getLog().debug(" testEntityHandle()"); 495 496 CtsBmp bean = null; 497 498 try { 499 CtsBmpHome home = getHome(); 500 AccountPK pk = new AccountPK(BEAN_PK_007); 501 502 getLog().debug("Create a bean..."); 503 bean = doEjbCreate(pk, BEAN_NAME); 504 getLog().debug("OK"); 505 506 getLog().debug("Get a Handle reference and serialize it..."); 507 Handle beanHandle = bean.getHandle(); 508 ByteArrayOutputStream out = new ByteArrayOutputStream (); 509 ObjectOutputStream sOut = new ObjectOutputStream (out); 510 sOut.writeObject(beanHandle); 511 sOut.flush(); 512 byte[] bytes = out.toByteArray(); 513 getLog().debug("OK"); 514 515 getLog().debug("Unserialize bean handle..."); 516 ByteArrayInputStream in = new ByteArrayInputStream (bytes); 517 ObjectInputStream sIn = new ObjectInputStream (in); 518 beanHandle = (Handle )sIn.readObject(); 519 getLog().debug("OK"); 520 521 getLog().debug("Use PortableRemoteObject to narrow result..."); 522 bean = (CtsBmp)PortableRemoteObject.narrow(beanHandle.getEJBObject(), 523 CtsBmp.class); 524 getLog().debug("OK"); 525 526 getLog().debug("Check that new reference works..."); 527 assertTrue(bean.getPersonsName().trim().equals(BEAN_NAME)); 528 getLog().debug("OK"); 529 } catch(Exception ex) { 530 getLog().error("Error in bmptest", ex); 531 fail("Caught an unknown exeption: " + ex.toString()); 532 } 533 534 getLog().debug( 535 "**************************************************************"); 536 537 } 538 539 543 public void testSessionHandleNoDefaultJNDI() 544 throws Exception 545 { 546 getLog().debug("+++ testSessionHandleNoDefaultJNDI()"); 547 548 552 Properties homeProps = new Properties (); 553 homeProps.setProperty("java.naming.factory.initial", "org.jboss.naming.NamingContextFactory"); 554 InitialContext ic = new InitialContext (homeProps); 555 CtsBmpHome home = (CtsBmpHome) ic.lookup("ejbcts/BMPBean"); 556 AccountPK pk = new AccountPK(BEAN_PK_007); 557 CtsBmp bean = doEjbCreate(pk, BEAN_NAME); 558 Handle beanHandle = bean.getHandle(); 559 ByteArrayOutputStream out = new ByteArrayOutputStream (); 560 ObjectOutputStream oos = new ObjectOutputStream (out); 561 oos.writeObject(beanHandle); 562 oos.flush(); 563 byte[] bytes = out.toByteArray(); 564 565 Properties sysProps = System.getProperties(); 566 Properties newProps = new Properties (sysProps); 567 newProps.setProperty("java.naming.factory.initial", "badFactory"); 568 newProps.setProperty("java.naming.provider.url", "jnp://badhost:12345"); 569 System.setProperties(newProps); 570 try 571 { 572 getLog().debug("Unserialize bean handle..."); 573 ByteArrayInputStream in = new ByteArrayInputStream (bytes); 574 ObjectInputStream ois = new ObjectInputStream (in); 575 beanHandle = (Handle ) ois.readObject(); 576 bean = (CtsBmp) beanHandle.getEJBObject(); 577 String name = bean.getPersonsName(); 578 getLog().debug("getPersonsName: "+name); 579 } 580 finally 581 { 582 System.setProperties(sysProps); 583 } 584 } 585 586 589 public void testProbeContainerCallbacks() 590 { 591 getLog().debug( 592 "**************************************************************"); 593 getLog().debug(" testProbeContainerCallbacks()"); 594 595 CtsBmp bean = null; 596 597 try { 598 CtsBmpHome home = getHome(); 599 AccountPK pk = new AccountPK(BEAN_PK_007); 600 601 mbx.clearMessages(); 602 603 getLog().debug("Create a bean..."); 604 bean = doEjbCreate(pk, BEAN_NAME); 605 getLog().debug("OK"); 606 607 getLog().debug("Check for set entity context, create " + 608 "and post create messages..."); 609 Thread.sleep(2500); 610 assertTrue("Expected to receive notification of EJB_CREATE_MSG",mbx.messageReceived(ContainerMBox.EJB_CREATE_MSG)); 616 assertTrue("Expected to receive notification of EJB_POST_CREATE_MSG",mbx.messageReceived(ContainerMBox.EJB_POST_CREATE_MSG)); 617 getLog().debug("OK"); 618 619 getLog().debug("Calling setter as a business method..."); 621 bean.setPersonsName(BEAN_OTHER_NAME); 622 getLog().debug("OK"); 623 624 getLog().debug("Remove the bean..."); 626 bean.remove(); 627 Thread.sleep(3000); 628 assertTrue("Expected to receive notification of EJB_STORE_MSG",mbx.messageReceived(ContainerMBox.EJB_STORE_MSG)); 629 assertTrue("Expected to receive notification of EJB_REMOVE_MSG",mbx.messageReceived(ContainerMBox.EJB_REMOVE_MSG)); 630 getLog().debug("OK"); 631 } catch (Exception ex) { 632 getLog().error("Error in bmptest", ex); 633 fail("testEjbCreate has failed!"); 634 } 635 636 getLog().debug( 637 "**************************************************************"); 638 } 639 640 650 public void testContainerObjects() 651 { 652 getLog().debug( 653 "**************************************************************"); 654 getLog().debug(" testContainerObjects()"); 655 656 CtsBmp bean = null; 657 658 try { 659 CtsBmpHome home = getHome(); 660 AccountPK pk = new AccountPK(BEAN_PK_007); 661 662 mbx.clearMessages(); 663 664 getLog().debug("Create a bean..."); 665 bean = doEjbCreate(pk, BEAN_NAME); 666 getLog().debug("OK"); 667 668 getLog().debug("Get HomeHandle..." ); 669 HomeHandle homeHan = home.getHomeHandle(); 670 assertTrue(homeHan != null); 671 getLog().debug("OK"); 672 673 getLog().debug("Get another home from the HomeHandle..."); 674 CtsBmpHome anotherHome = (CtsBmpHome)homeHan.getEJBHome(); 675 assertTrue(anotherHome != null); 676 getLog().debug("OK"); 677 678 getLog().debug("Get the Meta-data object..."); 679 EJBMetaData md = anotherHome.getEJBMetaData(); 680 assertTrue(md != null); 681 getLog().debug("OK"); 682 683 getLog().debug("Probe the Meta-data object:"); 684 String homeInterface = md.getHomeInterfaceClass().getName(); 685 String primaryKey = md.getPrimaryKeyClass().getName(); 686 String remoteInterface = md.getRemoteInterfaceClass().getName(); 687 getLog().debug(" Home Interface : " + homeInterface); 688 getLog().debug(" PrimaryKey : " + primaryKey); 689 getLog().debug(" Remote Interface: " + remoteInterface); 690 assertTrue(homeInterface.equals("org.jboss.test.cts.interfaces.CtsBmpHome")); 691 assertTrue(primaryKey.equals("org.jboss.test.cts.keys.AccountPK")); 692 assertTrue(remoteInterface.equals("org.jboss.test.cts.interfaces.CtsBmp")); 693 getLog().debug("Meta-data OK"); 694 695 getLog().debug("Check isSession()==false ..."); 696 assertTrue(!md.isSession()); 697 getLog().debug("OK"); 698 699 getLog().debug("Check isStatelessSession()==false ..."); 700 assertTrue(!md.isStatelessSession()); 701 getLog().debug("OK"); 702 703 getLog().debug("Test EJBHome.remove(PrimaryKey)"); 704 anotherHome.remove(pk); 705 getLog().debug("OK"); 706 707 } catch (Exception ex) { 708 getLog().error("Error in bmptest", ex); 709 fail("testEjbCreate has failed!"); 710 } 711 712 getLog().debug( 713 "**************************************************************"); 714 } 715 716 719 public void testUserTransaction() 720 throws Exception 721 { 722 getLog().debug( 723 "**************************************************************"); 724 getLog().debug(" testUserTransaction()"); 725 726 CtsBmpHome home = getHome(); 727 UserTransaction ut; 728 729 getLog().debug("Obtain UserTransaction..."); 730 Object o = new InitialContext ().lookup("UserTransaction"); 731 ut = (UserTransaction )PortableRemoteObject.narrow(o, UserTransaction .class); 732 assertTrue(ut != null); 733 getLog().debug("OK"); 734 735 getLog().debug("Do UserTransaction tests..."); 736 UserTransactionTester utt = new UserTransactionTester(home, ut); 737 assertTrue(ut != null); 738 assertTrue(utt.runAllTests()); 739 getLog().debug("Ok"); 740 741 getLog().debug( 742 "**************************************************************"); 743 } 744 745 private boolean gotRefOkay(CtsBmp bean, String expectedName) 748 { 749 boolean retVal = false; 750 751 try { 752 getLog().debug(expectedName + "==" + bean.getPersonsName()+"?"); 753 retVal = (bean.getPersonsName().equals(expectedName)); 754 } catch(Exception ex) { 755 getLog().debug("Unknown Exception : " + ex.toString() ); 756 } 757 758 return retVal; 759 } 760 761 protected void setUp() 763 throws Exception 764 { 765 super.setUp(); 766 getLog().debug("Build Container MBX for BMP"); 767 mbx = new ContainerMBox(); 768 769 getLog().debug("Initialize to empty BMP table."); 770 CtsBmpHome home = getHome(); 771 Collection clct = home.findAll(); 772 if (clct.size() != 0) { 773 getLog().debug("Removing " + clct.size() + " old beans."); 774 for (Iterator itr=clct.iterator(); itr.hasNext();) { 775 CtsBmp bean = (CtsBmp)itr.next(); 776 bean.remove(); 777 } 778 getLog().debug("Removal done."); 779 } 780 } 781 782 protected void tearDown() 783 throws Exception 784 { 785 try { 786 mbx.close(); 787 } catch (Exception ignoredBecauseProblemsWillBeHighlightedAsTestFailures) 788 {} 789 } 790 791 public static Test suite() throws Exception 793 { 794 return getDeploySetup(BmpUnitTestCase.class, "cts.jar"); 795 } 796 797 798 } 799 | Popular Tags |