User: peter   
  Date: 01/01/04 17:27:18

  Added:       src/main/org/jboss/test/cts/test AllJUnitTests.java
                        StatefulSessionTest.java
  Log:
  Added two files to support testing.
  
  Revision  Changes    Path
  1.1                  jbosstest/src/main/org/jboss/test/cts/test/AllJUnitTests.java
  
  Index: AllJUnitTests.java
  ===================================================================
  package org.jboss.test.cts.test;
  
  
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  
  /**
   * Class AllJUnitTests
   *
   *
   * @author
   * @version %I%, %G%
   */
  
  public class AllJUnitTests
     extends TestCase
  {
  
     /**
      * Constructor AllJUnitTests
      *
      *
      * @param name
      *
      */
  
     public AllJUnitTests (String name)
     {
        super(name);
     }
  
     /**
      * Method suite
      *
      *
      * @return
      *
      */
  
     public static Test suite ()
     {
        TestSuite suite = new TestSuite();
  
        try
        {
           System.out.println("Deploying");
           new org.jboss.jmx.client.Deployer().deploy("../deploy/cts.jar");
        }
        catch (Exception ex)
        {
           ex.printStackTrace();
           System.exit(0);
        }
  
        //suite.addTest(new StatefulSessionTest("testBasicSession"));
        //suite.addTest(new StatefulSessionTest("testEJBHomeInterface"));
        // Broken 12/05/00
        //suite.addTest(new StatefulSessionTest("testRemoveSessionObject"));
        //suite.addTest(new StatefulSessionTest("testSerialization"));
        //suite.addTest(new StatefulSessionTest("testUnSerialization"));
        //suite.addTest(new StatefulSessionTest("testCompareSerializeGetPK"));
        //suite.addTest(new StatefulSessionTest("testProbeBeanContext"));
        suite.addTest(new StatefulSessionTest("testLoopback"));
  
        return suite;
     }
  }
  
  
  /*------ Formatted by Jindent 3.23 Basic 1.0 --- http://www.jindent.de ------*/
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/cts/test/StatefulSessionTest.java
  
  Index: StatefulSessionTest.java
  ===================================================================
  package org.jboss.test.cts.test;
  
  
  
  import java.io.*;
  import java.util.*;
  import java.lang.reflect.*;
  import javax.ejb.*;
  import javax.naming.*;
  import javax.management.*;
  import org.jboss.test.cts.interfaces.*;
  import org.jboss.test.util.ejb.*;
  
  
  /**
   *
   *   @see <related>
   *   @author $Author: peter $
   *   @version $Revision: 1.1 $
   */
  
  public class StatefulSessionTest
     extends junit.framework.TestCase
  {
     private static int listenPort = 5755;
  
     class DummyPK
        implements Serializable
     {
        public String key;
  
        /**
         * Constructor DummyPK
         *
         *
         * @param key
         *
         */
  
        public DummyPK (String key)
        {
           this.key = key;
        }
     }
  
     static boolean deployed = false;
  
     /**
      * Constructor Main
      *
      *
      * @param name
      *
      */
  
     public StatefulSessionTest (String name)
     {
        super(name);
     }
  
     /**
      * EJB 1.1 (Page 40)
      *  "The container is responsible for making the home interfaces
      *   of its deployed enterprise of its deployed enterprise beans
      *   available to the client through JNDI API extension.
      */
  
     public void testBasicSession ()
        throws Exception
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testBasicSession()");
  
        Context             ctx         = new InitialContext();
        StatefulSessionHome home        =
           ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
        StatefulSession     sessionBean = home.create();
        String              result      = sessionBean.method1("CTS-Test");
  
        // Test response
        assert(result.equals("CTS-Test"));
        sessionBean.remove();
        System.out.println(
           "**************************************************************");
     }
  
     //
  
     /**
      * Method testEJBHomeInterface
      *  EJB 1.1 (Page 42)
      *  "The home interface allows a client to do the following:"
      *   - Create a new session object
      *   - Remove session object
      *   - Get the javax.ejb.EJBMetaData interface for the
      *     session bean.
      *   - Obtain a handle for the home interface
      *
      * @throws Exception
      *
      */
  
     public void testEJBHomeInterface ()
        throws Exception
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testEJBHomeInterface()");
  
        // Create a new session object   
        Context             ctx         = new InitialContext();
        StatefulSessionHome home        =
           ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
        StatefulSession     sessionBean = home.create();
  
        // Get the EJBMetaData
        javax.ejb.EJBMetaData md = home.getEJBMetaData();
  
        System.out.println("Verify EJBMetaData from home interface");
        assert(md != null);
  
        // Get the EJBMetaData constructs
        System.out.println("Get Home interface class");
  
        java.lang.Class homeInterface = md.getHomeInterfaceClass();
  
        System.out.println("home Interface : " + homeInterface.getName());
        assert(
           homeInterface.getName().equals(
              "org.jboss.test.cts.interfaces.StatefulSessionHome"));
        System.out.println("Get Remote Interface class");
  
        java.lang.Class remoteInterface = md.getRemoteInterfaceClass();
  
        System.out.println("remote Interface: " + remoteInterface.getName());
        assert(
           remoteInterface.getName().equals(
              "org.jboss.test.cts.interfaces.StatefulSession"));
        System.out.println("Verify isSession..");
        assert(md.isSession());
  
        // EJB 1.1 only
        System.out.println("Verify is not Stateless session...");
        assert(!md.isStatelessSession());
        System.out.println(
           "**************************************************************");
     }
  
     /**
      * Method testRemoveSessionObject
      * EJB 1.1 (Page 42)
      * Removing a Session Object.
      * Because session objects do not have primary keys that are
      * accessible to clients, invoking the javax.ejb.Home.remove( Object primaryKey )
      * method on a session results in the javax.ejb.RemoveException.
      *
      */
  
     public void testRemoveSessionObject ()
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testRemoveSessionObject()");
  
        try
        {
           Properties props = System.getProperties();
  
           System.out.println("Obtain home interface");
  
           // Create a new session object   
           Context             ctx         = new InitialContext(props);
           StatefulSessionHome home        =
              ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
           StatefulSession     sessionBean = home.create();
  
           home.remove(new DummyPK("pk"));
        }
        catch (javax.ejb.RemoveException rmEx)
        {
  
           // Expected behavior
           return;
        }
        catch (Exception ex)
        {
           ex.printStackTrace();
           fail("Expected javax.ejb.RemoveException, got Unknown Exception");
        }
  
        fail("Expected javax.ejb.RemoveException, got NO exception");
        System.out.println(
           "**************************************************************");
     }
  
     //-------------------------------------------------------------------------
     //-------------------------------------------------------------------------
  
     /**
      * Method testCompareSerializeGetPK
      * EJB 1.1 [5.5] Page 43
      * EJBOjbect.getPrimaryKey results in a RemoteException
      * Get a serializable handle
      * Compare on bean to another for equality
      *
      *
      */
  
     public void testCompareSerializeGetPK ()
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testCompareSerializeGetPK()");
  
        StatefulSession sessionBean = null;
  
        try
        {
           Properties props = System.getProperties();
  
           System.out.println("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();
  
           assert(hn != null);
  
           // "Copy" the bean
           StatefulSession theOtherBean =
              ( StatefulSession ) javax.rmi.PortableRemoteObject.narrow(
                 hn.getEJBObject(), StatefulSession.class);
  
           assert(theOtherBean != null);
           assert(sessionBean.isIdentical(theOtherBean));
        }
        catch (java.lang.ClassCastException CCEx)
        {
           fail("Caught ClassCast exception!");
        }
        catch (Exception Ex)
        {
           fail("Caught an Unknown exception copying the beans");
        }
  
        System.out.println(
           "**************************************************************");
     }
  
     /**
      * Method testSerialization
      * EJB 1.1 [5.7] Page 45
      * Session bean must be serializable.
      */
  
     public void testSerialization ()
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testSerialize");
  
        try
        {
           Properties props = System.getProperties();
  
           System.out.println("Obtain home interface");
  
           // Create a new session object   
           Context             ctx  = new InitialContext(props);
           StatefulSessionHome home =
              ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
           StatefulSession     bean = ( StatefulSession ) home.create();
  
           System.out.println("Increment bean, count = 3");
  
           // put on some state...
           bean.setCounter(1);
           bean.incCounter();
           bean.incCounter();
  
           // bean should be=3;
           System.out.println("Bean == 3?");
           assert(bean.getCounter() == 3);
           System.out.println("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");
        }
  
        System.out.println(
           "**************************************************************");
     }
  
     /**
      * Method testProbeBeanContext
      *
      * EJB 1.1 [6.4.1] Page 51
      *
      */
  
     public void testProbeBeanContext ()
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testProbeBeanContext");
  
        try
        {
           Properties props = System.getProperties();
  
           System.out.println("Obtain home interface");
  
           // Create a new session object   
           Context             ctx         = new InitialContext(props);
           StatefulSessionHome home        =
              ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
           StatefulSession     bean        = ( StatefulSession ) home.create();
           BeanContextInfo     beanCtxInfo = bean.getBeanContextInfo();
  
           assert(beanCtxInfo != null);
           System.out.println("remote interface: "
                              + beanCtxInfo.remoteInterface);
           System.out.println("home interface:   " + beanCtxInfo.homeInterface);
  
           //System.out.println("principleName:    " + beanCtxInfo.principleName);
           System.out.println("Testing rollback only setting...");
           assert(beanCtxInfo.isRollbackOnly.booleanValue());
        }
        catch (Exception ex)
        {
           ex.printStackTrace();
           fail("Caught an unknown exception in testProbeBeanContex");
        }
  
        System.out.println(
           "**************************************************************");
     }
  
     /**
      * Method testUnSerialization
      * EJB 1.1 [5.7] Page 45
      * Part II of the test, makes sure the bean can be resurected and used.
      */
  
     public void testUnSerialization ()
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testUnSerialize");
  
        try
        {
           System.out.println("Resurrect bean from .ser file");
  
           FileInputStream   in         = new FileInputStream("abean.ser");
           ObjectInputStream s          = new ObjectInputStream(in);
           Handle            beanHandle = ( Handle ) s.readObject();
           StatefulSession   bean       =
              ( StatefulSession ) beanHandle.getEJBObject();
  
           // Should still equal '3'?
           System.out.println("Bean reanimated, still equal '3'?");
           assert(bean.getCounter() == 3);
           System.out.println("Yup, equal to '3'");
           bean.decCounter();
           bean.remove();
        }
        catch (java.io.StreamCorruptedException scex)
        {
           fail("caught a StramCorrupted exception testUnSerialization");
        }
        catch (java.io.IOException 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");
        }
     }
  
     /**
      * Method testLoopback
      * EJB 1.1 [6.5.6]
      * A client call to bean 'A' which calls bean 'B' and bean 'B' 
      * in turn calls a method on bean 'A', there should be a 
      * RemoteException should be thrown.
      */
  
     public void testLoopback ()
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testLoopback");
  
        try
        {
           Properties props = System.getProperties();
  
           System.out.println("Obtain home interface");
  
           // Create a new session object   
           Context             ctx  = new InitialContext(props);
           StatefulSessionHome home =
              ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
           StatefulSession     bean = ( StatefulSession ) home.create();
  
           bean.loopbackTest();
        }
        catch (java.rmi.RemoteException rex)
        {
           System.out.println("Received RemoteException...");
        }
        catch (Exception ex)
        {
           System.out.println();
           fail("Test failed in testLoopback, expected RemoteException, got 
Exception");
        }
     }
  
     protected void setUp ()
        throws Exception
     {
        if (deployed) return;
  
        deployed = true;
     }
  }
  
  
  /*------ Formatted by Jindent 3.23 Basic 1.0 --- http://www.jindent.de ------*/
  
  
  

Reply via email to