1 22 package org.jboss.test.cts.test; 23 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.ByteArrayInputStream ; 30 import java.rmi.RemoteException ; 31 import java.util.Properties ; 32 import javax.ejb.Handle ; 33 import javax.ejb.RemoveException ; 34 import javax.management.MBeanServerConnection ; 35 import javax.naming.InitialContext ; 36 import javax.naming.Context ; 37 import javax.rmi.PortableRemoteObject ; 38 import javax.transaction.UserTransaction ; 39 40 import org.jboss.test.cts.interfaces.StatefulSession; 41 import org.jboss.test.cts.interfaces.StatefulSessionHome; 42 import org.jboss.test.cts.interfaces.BeanContextInfo; 43 import org.jboss.test.cts.interfaces.StrictlyPooledSessionHome; 44 import org.jboss.test.cts.keys.AccountPK; 45 import org.jboss.test.JBossTestCase; 46 import org.jboss.tm.TransactionManagerServiceMBean; 47 48 import junit.framework.Test; 49 50 import EDU.oswego.cs.dl.util.concurrent.CountDown; 51 52 53 60 public class StatefulSessionUnitTestCase 61 extends JBossTestCase 62 { 63 static final int MAX_SIZE = 20; 64 65 public StatefulSessionUnitTestCase (String name) 66 { 67 super(name); 68 } 69 70 76 public void testLocalInterfacePassivation() throws Exception 77 { 78 Context ctx = new InitialContext (); 79 getLog().debug("+++ testLocalInterfacePassivation"); 80 StatefulSessionHome sessionHome = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); 81 StatefulSession sessionBean = sessionHome.create("testLocalInterfacePassivation"); 82 83 getLog().debug("Creating local home"); 84 AccountPK pk = new AccountPK("123456789"); 85 sessionBean.createLocalEntity(pk, "jduke"); 86 87 getLog().debug("Creating a second session bean, forcing the first one to be passivated?"); 88 StatefulSession anotherSession = sessionHome.create("testLocalInterfacePassivation2"); 92 getLog().debug("OK, anotherSession="+anotherSession); 93 94 getLog().debug("Checking for complete passivation/activation"); 95 Thread.sleep(1000); 96 assertTrue(sessionBean.getWasPassivated() == true); 97 getLog().debug("OK"); 98 99 getLog().debug("Checking reactivation of session bean attributes"); 101 String name = sessionBean.readAndRemoveEntity(); 102 assertTrue(name.equals("jduke")); 103 getLog().debug("OK"); 104 } 105 106 112 public void testSessionRefPassivation() throws Exception 113 { 114 Context ctx = new InitialContext (); 115 getLog().debug("+++ testSessionRefPassivation"); 116 StatefulSessionHome sessionHome = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); 117 StatefulSession sessionBean = sessionHome.create("testSessionRefPassivation"); 118 119 getLog().debug("Creating session ref"); 120 sessionBean.createSessionRef(); 121 122 getLog().debug("Creating a second session bean, forcing the first one to be passivated?"); 123 StatefulSession anotherSession = sessionHome.create("testSessionRefPassivation2"); 127 getLog().debug("OK, anotherSession="+anotherSession); 128 129 getLog().debug("Checking for complete passivation/activation"); 130 Thread.sleep(1000); 131 assertTrue(sessionBean.getWasPassivated() == true); 132 getLog().debug("OK"); 133 134 getLog().debug("Checking reactivation of session ref"); 136 String handle = sessionBean.useSessionRef(); 137 assertTrue(handle != null); 138 getLog().debug("OK"); 139 } 140 141 147 public void testSessionHandlePassivation() throws Exception 148 { 149 Context ctx = new InitialContext (); 150 getLog().debug("+++ testSessionHandlePassivation"); 151 StatefulSessionHome sessionHome = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); 152 StatefulSession sessionBean = sessionHome.create("testSessionHandlePassivation"); 153 154 getLog().debug("Creating session handle"); 155 sessionBean.createSessionHandle(); 156 157 getLog().debug("Creating a second session bean, forcing the first one to be passivated?"); 158 StatefulSession anotherSession = sessionHome.create("testSessionHandlePassivation2"); 162 getLog().debug("OKm anotherSession="+anotherSession); 163 164 getLog().debug("Checking for complete passivation/activation"); 165 Thread.sleep(1000); 166 assertTrue(sessionBean.getWasPassivated() == true); 167 getLog().debug("OK"); 168 169 getLog().debug("Checking reactivation of session bean handle"); 171 String name = sessionBean.useSessionHandle("Hello"); 172 assertTrue(name.equals("Hello")); 173 getLog().debug("OK"); 174 } 175 176 186 public void testInVMSessionHandlePassivation() throws Exception 187 { 188 Context ctx = new InitialContext (); 189 getLog().debug("+++ testInVMSessionHandlePassivation"); 190 StatefulSessionHome sessionHome = (StatefulSessionHome) ctx.lookup("ejbcts/FacadeStatefulSessionBean"); 191 StatefulSession sessionBean = sessionHome.create("testInVMSessionHandlePassivation"); 192 193 getLog().debug("Creating stateful session handle"); 194 sessionBean.createStatefulSessionHandle("testInVMSessionHandlePassivation2"); 195 sessionBean.useStatefulSessionHandle(); 196 } 197 198 203 public void testPassivationBySize() throws Exception 204 { 205 Context ctx = new InitialContext (); 206 getLog().debug("+++ testPassivationBySize"); 207 StatefulSessionHome sessionHome = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); 208 StatefulSession sessionBean1 = sessionHome.create("testPassivationBySize"); 209 sessionBean1.method1("hello"); 210 211 StatefulSession sessionBean2 = sessionHome.create("testPassivationBySize2"); 213 sessionBean2.method1("hello"); 214 215 Thread.sleep(1000); 217 boolean passivated = sessionBean1.getWasPassivated(); 218 assertTrue("sessionBean1 WasPassivated", passivated); 219 boolean activated = sessionBean1.getWasActivated(); 220 assertTrue("sessionBean1 WasActivated", activated); 221 } 222 223 231 public void testPassivationByTime() throws Exception 232 { 233 Context ctx = new InitialContext (); 234 getLog().debug("+++ testPassivationByTime"); 235 StatefulSessionHome sessionHome = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); 236 StatefulSession sessionBean1 = sessionHome.create("testPassivationByTime"); 237 sessionBean1.method1("hello"); 238 239 getLog().debug("Retrieving handle for object"); 240 Handle handle = sessionBean1.getHandle(); 241 242 getLog().debug("Waiting 41 seconds for passivation..."); 243 Thread.sleep(41*1000); 244 245 boolean passivated = sessionBean1.getWasPassivated(); 247 assertTrue("sessionBean1 WasPassivated", passivated); 248 boolean activated = sessionBean1.getWasActivated(); 249 assertTrue("sessionBean1 WasActivated", activated); 250 251 getLog().debug("Waiting 90 seconds for removal due to age..."); 252 Thread.sleep(90*1000); 253 254 try 255 { 256 handle.getEJBObject(); 257 fail("Was able to get the remote interface for a removed session"); 258 } 259 catch (RemoteException expected) 260 { 261 getLog().debug("Handle access failed as expected", expected); 262 } 263 264 try 265 { 266 passivated = sessionBean1.getWasPassivated(); 267 fail("Was able to invoke getWasPassivated after bean should have been removed"); 268 } 269 catch(Exception e) 270 { 271 getLog().debug("Bean access failed as expected", e); 272 } 273 } 274 275 281 public void testBasicSession () 282 throws Exception 283 { 284 getLog().debug("+++ testBasicSession()"); 285 Context ctx = new InitialContext (); 286 StatefulSessionHome sessionHome = 287 ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); 288 StatefulSession sessionBean = sessionHome.create("testBasicSession-create"); 289 String result = sessionBean.method1("CTS-Test"); 290 assertTrue(result.equals("CTS-Test")); 292 293 try 294 { 295 sessionBean.remove(); 296 } 297 catch (Exception ex) 298 { 299 fail("could not remove stateless session bean" + ex); 300 } 301 302 sessionBean = sessionHome.createAlt("testBasicSession-create"); 303 String altName = sessionBean.getTestName(); 304 assertTrue("testName == testBasicSession-createAlt", 305 altName.equals("testBasicSession-createAlt")); 306 307 result = sessionBean.method1("CTS-Test"); 308 assertTrue(result.equals("CTS-Test")); 310 311 try 312 { 313 sessionBean.remove(); 314 } 315 catch (Exception ex) 316 { 317 fail("could not remove stateless session bean" + ex); 318 } 319 } 320 321 323 336 public void testEJBHomeInterface () 337 throws Exception 338 { 339 getLog().debug("+++ testEJBHomeInterface()"); 340 341 Context ctx = new InitialContext (); 343 StatefulSessionHome sessionHome = 344 ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); 345 StatefulSession sessionBean = sessionHome.create("testEJBHomeInterface"); 346 347 javax.ejb.EJBMetaData md = sessionHome.getEJBMetaData(); 349 350 getLog().debug("Verify EJBMetaData from home interface"); 351 assertTrue(md != null); 352 353 getLog().debug("Get Home interface class"); 355 356 java.lang.Class homeInterface = md.getHomeInterfaceClass(); 357 358 getLog().debug("home Interface : " + homeInterface.getName()); 359 assertTrue(homeInterface.getName().equals("org.jboss.test.cts.interfaces.StatefulSessionHome")); 360 getLog().debug("Get Remote Interface class"); 361 362 java.lang.Class remoteInterface = md.getRemoteInterfaceClass(); 363 364 getLog().debug("remote Interface: " + remoteInterface.getName()); 365 assertTrue(remoteInterface.getName().equals("org.jboss.test.cts.interfaces.StatefulSession")); 366 getLog().debug("Verify isSession.."); 367 assertTrue(md.isSession()); 368 369 getLog().debug("Verify is not Stateless session..."); 371 assertTrue(!md.isStatelessSession()); 372 373 try 374 { 375 sessionBean.remove(); 376 } 377 catch (Exception ex) 378 { 379 fail("could not remove stateful session bean" + ex); 380 } 381 } 382 383 392 public void testRemoveSessionObject () 393 throws Exception 394 { 395 getLog().debug("+++ testRemoveSessionObject()"); 396 Context ctx = new InitialContext (); 397 StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); 398 StatefulSession bean = home.create("testRemoveSessionObject"); 399 getLog().debug("OK, bean="+bean); 400 getLog().debug("Call remove using a primary key"); 401 try 402 { 403 home.remove(new AccountPK("pk")); 404 fail("[EJB 1.1, p42, section 5.3.2] Expected 'RemoveException' when remove-ing a session object, got NO exception"); 405 } 406 catch(RemoveException e) 407 { 408 getLog().debug("Remove using a primary key failed as expected"); 409 } 410 } 411 412 415 424 public void testCompareSerializeGetPK () 425 throws Exception 426 { 427 getLog().debug("+++ testCompareSerializeGetPK()"); 428 429 Context ctx = new InitialContext (); 430 StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); 431 StatefulSession bean = home.create("testCompareSerializeGetPK"); 432 433 Handle hn = bean.getHandle(); 435 436 assertTrue(hn != null); 437 438 StatefulSession theOtherBean = 440 ( StatefulSession ) javax.rmi.PortableRemoteObject.narrow( 441 hn.getEJBObject(), StatefulSession.class); 442 443 assertTrue(theOtherBean != null); 444 assertTrue(bean.isIdentical(theOtherBean)); 445 } 446 447 452 public void testSerialization () 453 throws Exception 454 { 455 getLog().debug("+++ testSerialize"); 456 Context ctx = new InitialContext (); 457 StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); 458 StatefulSession bean = home.create("testSerialization"); 459 getLog().debug("Increment bean, count = 3"); 460 461 bean.setCounter(1); 463 bean.incCounter(); 464 bean.incCounter(); 465 466 getLog().debug("Bean == 3?"); 468 assertTrue(bean.getCounter() == 3); 469 getLog().debug("passes.."); 470 471 Handle beanHandle = bean.getHandle(); 473 FileOutputStream out = new FileOutputStream ("abean.ser"); 474 ObjectOutputStream s = new ObjectOutputStream (out); 475 476 s.writeObject(beanHandle); 477 s.flush(); 478 } 479 480 485 public void testUnSerialization () 486 throws Exception 487 { 488 testSerialization(); 490 getLog().debug("+++ testUnSerialize"); 491 492 StatefulSession bean = null; 493 getLog().debug("Resurrect bean from .ser file"); 494 FileInputStream in = new FileInputStream ("abean.ser"); 495 ObjectInputStream s = new ObjectInputStream (in); 496 Handle beanHandle = ( Handle ) s.readObject(); 497 bean = ( StatefulSession ) beanHandle.getEJBObject(); 498 499 getLog().debug("Bean reanimated, still equal '3'? bean = " 501 + bean.getCounter()); 502 assertTrue(bean.getCounter() == 3); 503 getLog().debug("Yup, equal to '3'"); 504 bean.decCounter(); 505 bean.remove(); 506 } 507 508 512 public void testSessionHandleNoDefaultJNDI() 513 throws Exception 514 { 515 getLog().debug("+++ testSessionHandleNoDefaultJNDI()"); 516 517 Properties env = new Properties (); 518 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory"); 519 InitialContext ctx = new InitialContext (env); 520 StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); 521 StatefulSession bean = home.create("testSessionHandleNoDefaultJNDI"); 522 523 Handle beanHandle = bean.getHandle(); 524 ByteArrayOutputStream out = new ByteArrayOutputStream (); 525 ObjectOutputStream oos = new ObjectOutputStream (out); 526 oos.writeObject(beanHandle); 527 oos.flush(); 528 byte[] bytes = out.toByteArray(); 529 530 Properties sysProps = System.getProperties(); 531 Properties newProps = new Properties (sysProps); 532 newProps.setProperty("java.naming.factory.initial", "badFactory"); 533 newProps.setProperty("java.naming.provider.url", "jnp://badhost:12345"); 534 System.setProperties(newProps); 535 try 536 { 537 getLog().debug("Unserialize bean handle..."); 538 ByteArrayInputStream in = new ByteArrayInputStream (bytes); 539 ObjectInputStream ois = new ObjectInputStream (in); 540 beanHandle = (Handle ) ois.readObject(); 541 bean = (StatefulSession) beanHandle.getEJBObject(); 542 bean.method1("Hello"); 543 getLog().debug("Called method1 on handle session bean"); 544 } 545 finally 546 { 547 System.setProperties(sysProps); 548 } 549 } 550 551 555 public void testBMTSessionHandleNoDefaultJNDI() 556 throws Exception 557 { 558 getLog().debug("+++ testBMTSessionHandleNoDefaultJNDI()"); 559 560 Properties env = new Properties (); 561 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory"); 562 InitialContext ctx = new InitialContext (env); 563 StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/BMTStatefulSessionBean"); 564 StatefulSession bean = home.create("testBMTSessionHandleNoDefaultJNDI"); 565 566 Handle beanHandle = bean.getHandle(); 567 ByteArrayOutputStream out = new ByteArrayOutputStream (); 568 ObjectOutputStream oos = new ObjectOutputStream (out); 569 oos.writeObject(beanHandle); 570 oos.flush(); 571 byte[] bytes = out.toByteArray(); 572 573 Properties sysProps = System.getProperties(); 574 Properties newProps = new Properties (sysProps); 575 newProps.setProperty("java.naming.factory.initial", "badFactory"); 576 newProps.setProperty("java.naming.provider.url", "jnp://badhost:12345"); 577 System.setProperties(newProps); 578 try 579 { 580 getLog().debug("Unserialize bean handle..."); 581 ByteArrayInputStream in = new ByteArrayInputStream (bytes); 582 ObjectInputStream ois = new ObjectInputStream (in); 583 beanHandle = (Handle ) ois.readObject(); 584 bean = (StatefulSession) beanHandle.getEJBObject(); 585 bean.method1("Hello"); 586 getLog().debug("Called method1 on handle session bean"); 587 } 588 finally 589 { 590 System.setProperties(sysProps); 591 } 592 } 593 594 598 public void testHomeFromRemoteNoDefaultJNDI() 599 throws Exception 600 { 601 getLog().debug("+++ testHomeFromRemoteNoDefaultJNDI()"); 602 603 Properties sysProps = System.getProperties(); 605 Properties newProps = new Properties (sysProps); 606 newProps.setProperty("java.naming.factory.initial", "badFactory"); 607 newProps.setProperty("java.naming.provider.url", "jnp://badhost:12345"); 608 System.setProperties(newProps); 609 610 Properties env = new Properties (); 612 env.setProperty("java.naming.factory.initial", super.getJndiInitFactory()); 613 env.setProperty("java.naming.provider.url", super.getJndiURL()); 614 try 615 { 616 InitialContext ctx = new InitialContext (env); 617 Object ref = ctx.lookup("ejbcts/StatefulSessionBean"); 618 StatefulSessionHome home = (StatefulSessionHome) 619 PortableRemoteObject.narrow(ref, StatefulSessionHome.class); 620 StatefulSession bean1 = home.create("testHomeFromRemoteNoDefaultJNDI"); 621 StatefulSessionHome home2 = (StatefulSessionHome) bean1.getEJBHome(); 622 StatefulSession bean2 = home2.create("testHomeFromRemoteNoDefaultJNDI"); 623 bean2.remove(); 624 } 625 finally 626 { 627 System.setProperties(sysProps); 628 } 629 } 630 631 637 public void testProbeBeanContext () 638 throws Exception 639 { 640 getLog().debug("+++ testProbeBeanContext"); 641 Context ctx = new InitialContext (); 642 StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); 643 StatefulSession bean = home.create("testProbeBeanContext"); 644 645 getLog().debug("Invoking bean..."); 646 647 BeanContextInfo beanCtxInfo = bean.getBeanContextInfo(); 648 649 assertTrue(beanCtxInfo != null); 650 651 getLog().debug("remote interface: " 652 + beanCtxInfo.remoteInterface); 653 getLog().debug("home interface: " 654 + beanCtxInfo.homeInterface); 655 656 getLog().debug("Testing rollback only setting..."); 657 assertTrue(beanCtxInfo.isRollbackOnly.booleanValue()); 658 getLog().debug( 659 "**************************************************************"); 660 } 661 662 669 public void testLoopback () 670 throws Exception 671 { 672 getLog().debug("+++ testLoopback"); 673 674 Context ctx = new InitialContext (); 676 StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); 677 StatefulSession bean = home.create("testLoopback"); 678 getLog().debug("Calling loopbackTest( )...."); 679 try 680 { 681 bean.loopbackTest(); 682 fail("Was able to call loopbackTest()"); 683 } 684 catch(Exception e) 685 { 686 getLog().debug("The loopbackTest( ) failed as expected"); 687 } 688 } 689 690 public void testUserTx() 691 throws Exception 692 { 693 getLog().debug("+++ testUsrTx"); 694 695 getLog().debug("Obtain home interface"); 696 Context ctx = new InitialContext (); 698 Object ref = ctx.lookup("ejbcts/StatefulSessionBean"); 699 StatefulSessionHome home = ( StatefulSessionHome ) PortableRemoteObject.narrow(ref, 700 StatefulSessionHome.class); 701 StatefulSession bean = home.create("testUserTx"); 702 703 bean.setCounter(100); 704 getLog().debug("Try to instantiate a UserTransaction"); 705 UserTransaction userTx = (UserTransaction )ctx.lookup("UserTransaction"); 706 userTx.begin(); 707 bean.incCounter(); 708 bean.incCounter(); 709 userTx.commit(); 710 int counter = bean.getCounter(); 711 assertTrue("counter == 102", counter == 102); 712 713 bean.setCounter(100); 714 userTx.begin(); 715 bean.incCounter(); 716 bean.incCounter(); 717 userTx.rollback(); 718 counter = bean.getCounter(); 719 assertTrue("counter == 100", counter == 100); 720 721 bean.remove(); 722 } 723 724 729 public void testStrictPooling() throws Exception 730 { 731 log.debug("+++ testStrictPooling"); 732 CountDown done = new CountDown(MAX_SIZE); 733 InitialContext ctx = new InitialContext (); 734 StrictlyPooledSessionHome home = (StrictlyPooledSessionHome) 735 ctx.lookup("ejbcts/StrictlyPooledStatefulBean"); 736 SessionInvoker[] threads = new SessionInvoker[MAX_SIZE]; 737 for(int n = 0; n < MAX_SIZE; n ++) 738 { 739 SessionInvoker t = new SessionInvoker(home, n, done, getLog()); 740 threads[n] = t; 741 t.start(); 742 } 743 boolean ok = done.attempt(1500 * MAX_SIZE); 744 assertTrue("Acquired done, remaining="+done.currentCount(), ok); 745 746 for(int n = 0; n < MAX_SIZE; n ++) 747 { 748 SessionInvoker t = threads[n]; 749 if( t.runEx != null ) 750 { 751 t.runEx.printStackTrace(); 752 log.error("SessionInvoker.runEx != null", t.runEx); 753 fail("SessionInvoker.runEx != null"); 754 } 755 } 756 } 757 758 public void testBadUserTx() throws Exception 759 { 760 getLog().debug("+++ testBadUsrTx"); 761 762 getLog().debug("Obtain home interface"); 763 Context ctx = new InitialContext (); 765 Object ref = ctx.lookup("ejbcts/BMTStatefulSessionBean"); 766 StatefulSessionHome home = (StatefulSessionHome) PortableRemoteObject.narrow(ref, 767 StatefulSessionHome.class); 768 StatefulSession bean = home.create("testBadUserTx"); 769 770 MBeanServerConnection server = getServer(); 771 Long before = (Long ) server.getAttribute(TransactionManagerServiceMBean.OBJECT_NAME, "TransactionCount"); 772 try 773 { 774 bean.testBadUserTx(); 775 fail("Should throw an exception"); 776 } 777 catch (Throwable expected) 778 { 779 log.debug("Got exception", expected); 780 } 781 Long after = (Long ) server.getAttribute(TransactionManagerServiceMBean.OBJECT_NAME, "TransactionCount"); 782 assertEquals("Transaction should no longer be active before=" + before + " after=" + after, before, after); 783 } 784 785 public static Test suite() throws Exception 786 { 787 return getDeploySetup(StatefulSessionUnitTestCase.class, "cts.jar"); 788 } 789 790 } 791 | Popular Tags |