User: starksm Date: 02/04/18 10:05:02 Modified: src/main/org/jboss/test/cts/test StatefulSessionUnitTestCase.java Log: Add a local entity interface and testing of stateful session passivation of a bean that includes a java:comp/env context and local interface as instance session variables Revision Changes Path 1.4 +134 -323 jbosstest/src/main/org/jboss/test/cts/test/StatefulSessionUnitTestCase.java Index: StatefulSessionUnitTestCase.java =================================================================== RCS file: /cvsroot/jboss/jbosstest/src/main/org/jboss/test/cts/test/StatefulSessionUnitTestCase.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- StatefulSessionUnitTestCase.java 29 Jan 2002 22:00:01 -0000 1.3 +++ StatefulSessionUnitTestCase.java 18 Apr 2002 17:05:02 -0000 1.4 @@ -15,6 +15,7 @@ import java.rmi.ServerException; import javax.ejb.Handle; +import javax.ejb.RemoveException; import javax.naming.InitialContext; import javax.naming.Context; import javax.rmi.PortableRemoteObject; @@ -36,27 +37,51 @@ * @see <related> * @author Author: kimptoc * @author Author: d_jencks converted to JBossTestCase, added logging. - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ */ - public class StatefulSessionUnitTestCase extends JBossTestCase { - static boolean deployed = false; - - /** - * Constructor Main - * - * - * @param name - * - */ - public StatefulSessionUnitTestCase (String name) { super(name); } + /** Create a StatefulSessionBean and then create a local interface to + an entity in the session. This bean is then passivated by creating another + session bean and then activated by invoking a business method. The + purpose is to test that the session is passivated and that the local + interface is restored. + */ + public void testLocalInterfacePassivation() throws Exception + { + Context ctx = new InitialContext(); + getLog().debug("+++ testLocalInterfacePassivation"); + StatefulSessionHome sessionHome = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); + StatefulSession sessionBean = sessionHome.create(); + + getLog().debug("Creating local home"); + AccountPK pk = new AccountPK("123456789"); + sessionBean.createLocalEntity(pk, "jduke"); + + getLog().debug("Creating a second session bean, forcing the first one to be passivated?"); + // The pool size has been set to 1 in the container + // config, so creating another ASession here should + // cause the first one to be passivated. + StatefulSession anotherSession = sessionHome.create(); + getLog().debug("OK"); + + getLog().debug("Checking for complete passivation/activation"); + assertTrue(sessionBean.getWasPassivated() == true); + getLog().debug("OK"); + + // Some other checks + getLog().debug("Checking reactivation of session bean attributes"); + String name = sessionBean.readAndRemoveEntity(); + assertTrue(name.equals("jduke")); + getLog().debug("OK"); + } + /** * EJB 1.1 (Page 40) * "The container is responsible for making the home interfaces @@ -107,7 +132,6 @@ * @throws Exception * */ - public void testEJBHomeInterface () throws Exception { @@ -172,45 +196,27 @@ * method on a session results in the javax.ejb.RemoveException. * */ - public void testRemoveSessionObject () + throws Exception { getLog().debug( "**************************************************************"); getLog().debug(" testRemoveSessionObject()"); - StatefulSession sessionBean = null; - + Context ctx = new InitialContext(); + StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); + StatefulSession bean = home.create(); + getLog().debug("OK"); + getLog().debug("Call remove using a primary key"); try { - Properties props = System.getProperties(); - - getLog().debug("Obtain home interface"); - - // Create a new session object - Context ctx = new InitialContext(props); - StatefulSessionHome home = - ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); - - sessionBean = home.create(); - getLog().debug("OK"); - getLog().debug("Call remove using a primary key"); home.remove(new AccountPK("pk")); - + fail("[EJB 1.1, p42, section 5.3.2] Expected 'RemoveException' when remove-ing a session object, got NO exception"); } - catch (javax.ejb.RemoveException rmEx) + catch(RemoveException e) { - getLog().debug("OK, got a remove exception!" ); - // Expected behavior - return; + getLog().debug("Remove using a primary key failed as expected"); } - catch (Exception ex) - { - getLog().error("Error", ex); - fail("[EJB 1.1, p42, section 5.3.2] Expected 'RemoveException' when remove-ing a session object, detail:" + ex.toString()); - } - - fail("[EJB 1.1, p42, section 5.3.2] Expected 'RemoveException' when remove-ing a session object, got NO exception"); getLog().debug( "**************************************************************"); } @@ -227,65 +233,29 @@ * * */ - public void testCompareSerializeGetPK () + throws Exception { getLog().debug( "**************************************************************"); getLog().debug(" testCompareSerializeGetPK()"); - StatefulSession sessionBean = null; - - try - { - Properties props = System.getProperties(); + Context ctx = new InitialContext(); + StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); + StatefulSession bean = home.create(); + + // Get the bean handle + Handle hn = bean.getHandle(); + + assertTrue(hn != null); + + // "Copy" the bean + StatefulSession theOtherBean = + ( StatefulSession ) javax.rmi.PortableRemoteObject.narrow( + hn.getEJBObject(), StatefulSession.class); - getLog().debug("Obtain home interface"); - - // Create a new session object - Context ctx = new InitialContext(props); - StatefulSessionHome home = - ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); - - sessionBean = home.create(); - } - catch (Exception Ex) - { - fail("Caught an Unknown Exception in lookup"); - } - - try - { - - // Get the bean handle - Handle hn = sessionBean.getHandle(); - - assertTrue(hn != null); - - // "Copy" the bean - StatefulSession theOtherBean = - ( StatefulSession ) javax.rmi.PortableRemoteObject.narrow( - hn.getEJBObject(), StatefulSession.class); - - assertTrue(theOtherBean != null); - assertTrue(sessionBean.isIdentical(theOtherBean)); - } - catch (java.lang.ClassCastException CCEx) - { - fail("Caught ClassCast exception!"); - } - catch (Exception Ex) - { - fail("Caught an Unknown exception copying the beans"); - } - finally - { - try - { - sessionBean.remove(); - } - catch (Exception ex){} - } + assertTrue(theOtherBean != null); + assertTrue(bean.isIdentical(theOtherBean)); getLog().debug( "**************************************************************"); @@ -296,57 +266,35 @@ * EJB 1.1 [5.7] Page 45 * Session bean must be serializable. */ - public void testSerialization () + throws Exception { getLog().debug( "**************************************************************"); getLog().debug(" testSerialize"); - StatefulSession bean = null; - - try - { - Properties props = System.getProperties(); - - getLog().debug("Obtain home interface"); - - // Create a new session object - Context ctx = new InitialContext(props); - StatefulSessionHome home = - ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); - - bean = ( StatefulSession ) home.create(); - - getLog().debug("Increment bean, count = 3"); - - // put on some state... - bean.setCounter(1); - bean.incCounter(); - bean.incCounter(); - - // bean should be=3; - getLog().debug("Bean == 3?"); - assertTrue(bean.getCounter() == 3); - getLog().debug("passes.."); - - // Get handle and serialize - Handle beanHandle = bean.getHandle(); - FileOutputStream out = new FileOutputStream("abean.ser"); - ObjectOutputStream s = new ObjectOutputStream(out); - - s.writeObject(beanHandle); - s.flush(); - } - catch (ClassCastException ccex) - { - fail("caught a class cast exception, testSerialization"); - } - catch (Exception ex) - { - fail("caught an unknown exception, testSerialization"); - } + Context ctx = new InitialContext(); + StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); + StatefulSession bean = home.create(); + getLog().debug("Increment bean, count = 3"); + + // put on some state... + bean.setCounter(1); + bean.incCounter(); + bean.incCounter(); + + // bean should be=3; + getLog().debug("Bean == 3?"); + assertTrue(bean.getCounter() == 3); + getLog().debug("passes.."); + + // Get handle and serialize + Handle beanHandle = bean.getHandle(); + FileOutputStream out = new FileOutputStream("abean.ser"); + ObjectOutputStream s = new ObjectOutputStream(out); + s.writeObject(beanHandle); + s.flush(); getLog().debug( "**************************************************************"); } @@ -356,8 +304,8 @@ * EJB 1.1 [5.7] Page 45 * Part II of the test, makes sure the bean can be resurected and used. */ - public void testUnSerialization () + throws Exception { //We are deploying for each test, so we need to reserialize first. testSerialization(); @@ -367,49 +315,21 @@ StatefulSession bean = null; - try - { - getLog().debug("Resurrect bean from .ser file"); + getLog().debug("Resurrect bean from .ser file"); - FileInputStream in = new FileInputStream("abean.ser"); - ObjectInputStream s = new ObjectInputStream(in); - Handle beanHandle = ( Handle ) s.readObject(); - - bean = ( StatefulSession ) beanHandle.getEJBObject(); - - // Should still equal '3'? - getLog().debug("Bean reanimated, still equal '3'? bean = " - + bean.getCounter()); - assertTrue(bean.getCounter() == 3); - getLog().debug("Yup, equal to '3'"); - bean.decCounter(); - bean.remove(); - } - catch (java.io.StreamCorruptedException scex) - { - fail("caught a StreamCorrupted exception testUnSerialization"); - } - catch (java.io.IOException ioex) - { - getLog().error("Error", ioex); - fail("caught a IOException, testUnSerialization"); - } - catch (java.lang.ClassNotFoundException cnfex) - { - fail("caught a ClassNotFoundException, testUnSerialization"); - } - catch (javax.ejb.RemoveException rmex) - { - fail("caught a RemoveException, testUnSerialization"); - } - finally - { - try - { - bean.remove(); - } - catch (Exception ex){} - } + FileInputStream in = new FileInputStream("abean.ser"); + ObjectInputStream s = new ObjectInputStream(in); + Handle beanHandle = ( Handle ) s.readObject(); + + bean = ( StatefulSession ) beanHandle.getEJBObject(); + + // Should still equal '3'? + getLog().debug("Bean reanimated, still equal '3'? bean = " + + bean.getCounter()); + assertTrue(bean.getCounter() == 3); + getLog().debug("Yup, equal to '3'"); + bean.decCounter(); + bean.remove(); } /** @@ -418,73 +338,28 @@ * EJB 1.1 [6.4.1] Page 51 * */ - public void testProbeBeanContext () + throws Exception { getLog().debug( "**************************************************************"); getLog().debug(" testProbeBeanContext"); + Context ctx = new InitialContext(); + StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); + StatefulSession bean = home.create(); + getLog().debug("Invoking bean..."); + + BeanContextInfo beanCtxInfo = bean.getBeanContextInfo(); + + assertTrue(beanCtxInfo != null); + + getLog().debug("remote interface: " + + beanCtxInfo.remoteInterface); + getLog().debug("home interface: " + + beanCtxInfo.homeInterface); - StatefulSession bean = null; - - try - { - Properties props = System.getProperties(); - - try - { - getLog().debug("Obtain home interface"); - - // Create a new session object - Context ctx = new InitialContext(props); - Object ref = - ctx.lookup("ejbcts/StatefulSessionBean"); - StatefulSessionHome home = - ( StatefulSessionHome ) PortableRemoteObject.narrow(ref, - StatefulSessionHome.class); - - bean = home.create(); - - getLog().debug("Invoking bean..."); - - BeanContextInfo beanCtxInfo = bean.getBeanContextInfo(); - - assertTrue(beanCtxInfo != null); - - getLog().debug("remote interface: " - + beanCtxInfo.remoteInterface); - getLog().debug("home interface: " - + beanCtxInfo.homeInterface); - - getLog().debug("Testing rollback only setting..."); - assertTrue(beanCtxInfo.isRollbackOnly.booleanValue()); - } - catch (javax.naming.NamingException ne) - { - fail("Naming exception failure"); - } - catch (java.rmi.ServerException se) - { - fail("Server exception"); - } - catch (java.rmi.RemoteException re) - { - fail("Remote exception"); - } - finally - { - try - { - bean.remove(); - } - catch (Exception ex){} - } - } - catch (Exception ex) - { - fail("Caught an unknown exception in testProbeBeanContex"); - } - + getLog().debug("Testing rollback only setting..."); + assertTrue(beanCtxInfo.isRollbackOnly.booleanValue()); getLog().debug( "**************************************************************"); } @@ -496,111 +371,48 @@ * in turn calls a method on bean 'A', there should be a * RemoteException should be thrown. */ - public void testLoopback () + throws Exception { getLog().debug( "**************************************************************"); getLog().debug(" testLoopback"); - StatefulSession bean = null; - + // Create a new session object + Context ctx = new InitialContext(); + StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); + StatefulSession bean = home.create(); + getLog().debug("Calling loopbackTest( )...."); try { - Properties props = System.getProperties(); - - getLog().debug("Obtain home interface"); - - // Create a new session object - Context ctx = new InitialContext(props); - StatefulSessionHome home = - ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean"); - - bean = ( StatefulSession ) home.create(); - - getLog().debug("Calling loopbackTest( )...."); bean.loopbackTest(); + fail("Was able to call loopbackTest()"); } - catch (java.rmi.RemoteException rex) - { - getLog().debug("Received RemoteException..."); - } - catch (Exception ex) + catch(Exception e) { - getLog().error("Error", ex); - fail("Test failed in testLoopback, expected RemoteException, got Exception"); - } - finally - { - try - { - bean.remove(); - } - catch (Exception ex){} + getLog().debug("The loopbackTest( ) failed as expected"); } } - /** - * Method testUserTrx - * - * - */ - public void testUserTrx () + throws Exception { getLog().debug( "**************************************************************"); getLog().debug(" testUsrTrx"); - StatefulSession bean = null; - - try - { - Properties props = System.getProperties(); - - try - { - getLog().debug("Obtain home interface"); - - // Create a new session object - Context ctx = new InitialContext(props); - Object ref = - ctx.lookup("ejbcts/StatefulSessionBean"); - StatefulSessionHome home = - ( StatefulSessionHome ) PortableRemoteObject.narrow(ref, - StatefulSessionHome.class); - - bean = home.create(); - - getLog().debug("Try to instantiate a UserTransaction"); + getLog().debug("Obtain home interface"); + // Create a new session object + Context ctx = new InitialContext(); + Object ref = ctx.lookup("ejbcts/StatefulSessionBean"); + StatefulSessionHome home = ( StatefulSessionHome ) PortableRemoteObject.narrow(ref, + StatefulSessionHome.class); + StatefulSession bean = home.create(); + getLog().debug("Try to instantiate a UserTransaction"); javax.transaction.UserTransaction uTrx = (javax.transaction.UserTransaction)ctx.lookup("UserTransaction"); - - bean.remove(); - } - catch (javax.naming.NamingException ne) - { - getLog().error("Error", ne); - fail("Naming excepiton failure"); - } - catch (java.rmi.ServerException se) - { - getLog().error("Error", se); - fail("Server exception"); - } - catch (java.rmi.RemoteException re) - { - getLog().error("Error", re); - fail("Remote exception"); - } - } - catch (Exception ex) - { - getLog().error("Error", ex); - fail("Caught an unknown exception in testProbeBeanContex"); - } - + bean.remove(); getLog().debug( "**************************************************************"); } @@ -612,6 +424,5 @@ } } - /*------ Formatted by Jindent 3.23 Basic 1.0 --- http://www.jindent.de ------*/
_______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-development