User: osh     
  Date: 01/03/06 12:41:07

  Modified:    src/main/org/jboss/test/cts/test AllJUnitTests.java
  Added:       src/main/org/jboss/test/cts/test BmpTest.java
  Removed:     src/main/org/jboss/test/cts/test bmpTest.java
  Log:
  CTS BMP test and test bean changes.
  
  Revision  Changes    Path
  1.11      +9 -9      jbosstest/src/main/org/jboss/test/cts/test/AllJUnitTests.java
  
  Index: AllJUnitTests.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jbosstest/src/main/org/jboss/test/cts/test/AllJUnitTests.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- AllJUnitTests.java        2001/03/02 03:11:28     1.10
  +++ AllJUnitTests.java        2001/03/06 20:41:06     1.11
  @@ -68,19 +68,19 @@
   
         suite.addTest(new StatelessSessionTest("testBasicStatelessSession"));
   
  -      suite.addTest(new bmpTest("testProbeContainerCallbacks"));
  +      suite.addTest(new BmpTest("testProbeContainerCallbacks"));
   
  -      suite.addTest(new bmpTest("testEjbCreate"));
  -      suite.addTest(new bmpTest("testEjbFinder"));
  -      suite.addTest(new bmpTest("testEjbRemove"));
  -      suite.addTest(new bmpTest("testEjbLifeCycle"));
  +      suite.addTest(new BmpTest("testEjbCreate"));
  +      suite.addTest(new BmpTest("testEjbFinder"));
  +      suite.addTest(new BmpTest("testEjbRemove"));
  +      suite.addTest(new BmpTest("testEjbLifeCycle"));
   
  -      suite.addTest(new bmpTest("testPrimaryKeyObjectIdentity"));
  -      suite.addTest(new bmpTest("testEjbRemoteIF"));
  -      suite.addTest(new bmpTest("testEntityHandle"));
  +      suite.addTest(new BmpTest("testPrimaryKeyObjectIdentity"));
  +      suite.addTest(new BmpTest("testEjbRemoteIF"));
  +      suite.addTest(new BmpTest("testEntityHandle"));
    
         // 03/01/2001
  -      suite.addTest(new bmpTest("testContainerObjects"));
  +      suite.addTest(new BmpTest("testContainerObjects"));
   
         return suite;
      }
  
  
  
  1.1                  jbosstest/src/main/org/jboss/test/cts/test/BmpTest.java
  
  Index: BmpTest.java
  ===================================================================
  package org.jboss.test.cts.test;
  
  
  import java.io.ByteArrayInputStream;
  import java.io.ObjectInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.ObjectOutputStream;
  
  import java.util.Collection;
  import java.util.Iterator;
  
  import javax.ejb.Handle;
  import javax.ejb.HomeHandle;
  import javax.ejb.EJBMetaData;
  
  import javax.naming.InitialContext;
  
  import javax.rmi.PortableRemoteObject;
  
  import org.jboss.test.cts.jms.ContainerMBox;
  import org.jboss.test.cts.interfaces.CtsBmpHome;
  import org.jboss.test.cts.interfaces.CtsBmp;
  import org.jboss.test.cts.keys.AccountPK;
  
  
  /**
   *  Class BmpTest
   *
   *  @author $Author: osh $
   *  @version $Revision: 1.1 $
   */
  
  public class BmpTest
     extends junit.framework.TestCase
  {
     private ContainerMBox mbx = null; 
     public static final String BEAN_NAME = "GuysName";
     public static final String BEAN_OTHER_NAME = "OtherGuysName";
     public static final String BEAN_PK_007 = "007";
  
     /**
      * Constructor BmpTest
      *
      * @param name
      *
      */
     public BmpTest(String name)
     {
        super(name);
     }
  
     /**
      *  Return the bean home interface.
      */
     private CtsBmpHome getHome()
        throws Exception
     {
        return (CtsBmpHome)new InitialContext().lookup("ejbcts/BMPBean");
     }
  
     /**
      *  Create a bean instance.
      */
     private CtsBmp doEjbCreate(AccountPK pk, String name)
        throws Exception
     {
        return getHome().create(pk, name);
     }
  
     /**
      * Method testEjbCreate
      * EJB 1.1 [8.3.1] p. 89
      * An entity bean's home interface can define zero or more create(...)
      * methods. 
      *
      * @throws Exception
      *
      */
     public void testEjbCreate()
        throws Exception
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testEjbCreate()");
  
        CtsBmp bean = null;
  
        try {
           System.out.println("create bean, name=" + BEAN_NAME);
  
           doEjbCreate(new AccountPK(BEAN_PK_007), BEAN_NAME);
        } catch (Exception ex) {
           ex.printStackTrace();
           fail("testEjbCreate has failed!");
        }
  
        System.out.println(
           "**************************************************************");
     }
  
    /**
      * Method testEjbFinder
      * EJB 1.1 [8.3.2] p. 90
      * An entity bean's home interface defines one or more finder methods,
      * one for each way to find and entity object or collection of entity objects
      * within the home.
      *
      * Test stategy: Create a bean. Use the bean that has been previously
      *               created, and call the finder method.  Make sure that
      *               a result set is returned and that the bean returned
      *               has the same name associated with it as the bean that
      *               was previously created.
      * 
      * @throws Exception
      *
      */
     public void testEjbFinder()
        throws Exception
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testEjbFinder()");
  
        CtsBmp bean = null;
  
        try {
           CtsBmpHome home = getHome();
  
           // First create a bean instance to find
           System.out.println("Create bean, name=" + BEAN_NAME);
           doEjbCreate(new AccountPK(BEAN_PK_007), BEAN_NAME);
  
           System.out.println("Find bean, name=" + BEAN_NAME);
  
           Collection clct = home.findByPersonsName(BEAN_NAME);
           System.out.println("Verify result set not empty");
           assert(!clct.isEmpty());
           System.out.println("OK");
           System.out.println("Bean result set:");
           for(Iterator itr=clct.iterator(); itr.hasNext();)
         {
               bean = (CtsBmp)itr.next();
               System.out.println("Name from Bean=" + bean.getPersonsName());
               System.out.println("Verify bean name equals: " + BEAN_NAME);
               assert(bean.getPersonsName().trim().equals(BEAN_NAME));
               System.out.println("OK");              
         }
        } catch (Exception ex) {
           ex.printStackTrace();
           fail("testEjbFinder has failed!");
        }
  
        System.out.println(
           "**************************************************************");
     }
  
     /**
      * Method testEjbRemove
      * EJB 1.1 [8.3.3] p. 90
      * 
      * Test Strategy:
      * 1) Create a bean to remove.
      * 2) Attempt a simple remove using the remote interface.
      * 3) Create a bean to remove.
      * 4) Attempt a simple remove using the home interface and primary key.
      * 5) Create a bean to remove.
      * 6) Try to remove the instance using its handle.
      * 7) Try to access the instance. This should result in a
      *    java.rmi.NoSuchObjectException
      *
      * @throws Exception
      */
     public void testEjbRemove()
        throws Exception
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testEjbRemove()");
  
        CtsBmp bean = null;
  
        try {
           CtsBmpHome home = getHome();
           AccountPK pk = new AccountPK(BEAN_PK_007);
  
           System.out.print("Create a bean...");
           bean = doEjbCreate(pk, BEAN_NAME);
           System.out.println("OK");
  
           System.out.print("Delete with bean.remove()...");
           bean.remove();
           System.out.println("OK");
  
           System.out.print("Recreate the bean...");
           bean = doEjbCreate(pk, BEAN_NAME);
           System.out.println("OK");
  
           System.out.print("Remove the bean using primary key...");
           home.remove(pk);
           System.out.println("OK");
  
           System.out.print("Reconstitute the bean...");
           bean = doEjbCreate(pk, BEAN_NAME);
           System.out.println("OK");
  
           System.out.print("Get Handle object...");
           Handle hn = bean.getHandle( );
           System.out.println("OK");
  
           System.out.print("Remove the bean using the handle...");
           home.remove(hn);
           System.out.println("OK");
  
           System.out.print("Bean remove, try to use.. " +
                            "Should get 'java.rmi.NoSuchObjectException'..." );
           try {
             bean.getPersonsName();
           } catch(java.rmi.NoSuchObjectException nsoex) {
             System.out.println("OK");
           } catch(Exception ex) {
               System.err.println("FAILED");
               ex.printStackTrace();
             fail("Got Exception: expecting java.rmi.NoSuchObjectException"  );
           }
        } catch (Exception ex) {
           ex.printStackTrace();
           fail("testEjbRemove has failed!");
        }
  
        System.out.println(
           "**************************************************************");
     }
  
     /**
      * Method testEjbLifeCycle
      * EJB 1.1 [8.4] p. 92
      * 
      * A client can get a reference to an existing entity objects
      * remote interface in any of the following ways:
      * - Receive the reference as a parameter in a method call.
      * - Find the entity object using a finder method defined in the EB home i/f.
      * - Obtain the reference from the entity objects' handle.
      *
      * @throws Exception
      *
      */
     public void testEjbLifeCycle()
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testEjbLifeCycle()");
  
        CtsBmp bean = null;
  
        try {
           CtsBmpHome home = getHome();
           AccountPK pk = new AccountPK(BEAN_PK_007);
  
           System.out.print("Create a bean...");
           doEjbCreate(pk, BEAN_NAME);
           System.out.println("OK");
  
           System.out.print("Use a finder method to retrieve the bean...");
           bean = home.findByPrimaryKey( pk );
           System.out.println("OK");
  
           System.out.print("Assert it is the same bean as passed to a method..." );
           // Send to a method as a reference, make sure it is usable by the method
           assert( this.gotRefOkay(bean, BEAN_NAME) );
           System.out.println("OK");
  
           // Execute a business method
           System.out.print("Calling setter as a business method...");
           bean.setPersonsName(BEAN_OTHER_NAME);
           System.out.println("OK");
  
           // Get the home interface
           System.out.print("Get the HOME interface...");
           home = (CtsBmpHome)bean.getEJBHome();
           System.out.println("OK");
  
           // Get the primary key
           System.out.print("Get the bean's Primary Key...");
           pk = (AccountPK)bean.getPrimaryKey();
           System.out.println("OK");
  
           System.out.print("Get the bean's handle...");
           Handle hn = bean.getHandle();
           System.out.println("OK");
  
           // Remove
           System.out.print("Remove the bean...");
           bean.remove();
           System.out.println("OK");
        } catch (Exception ex) {
           ex.printStackTrace();
           fail("testEjbCreate has failed!");
        }
  
        System.out.println(
           "**************************************************************");
     }
  
     /**
      * Method testPrimaryKeyObjectIdentity
      * EJB 1.1 [8.5] p. 92-93
      * 
      * Every entity object has a unique identity within its home.   If
      * two entity objects have the same home and the same primary key
      * they are considered identitcal.
      *
      * getPrimaryKey() always returns the same value when called one the
      * same entity object.
      * 
      * A client can test whether two entity object references refer to the 
      * same entity object by using the isIdentical(EBJObject) method. 
      * Alternatively, if a client obtains two entity object references from
      * the same home, it can determin if they refer to the same entity by comparing 
      * their primary keys using the 'equals' method.
      * 
      * @throws Exception
      *
      */
     public void testPrimaryKeyObjectIdentity()
     {
        System.out.println(
            "**************************************************************");
        System.out.println("     testPrimaryKeyObjectIdentity()");
  
        CtsBmp bean = null;
        CtsBmp anotherBean = null;
        CtsBmp differentBean = null;
  
        try {
           CtsBmpHome home = getHome();
           AccountPK pk = new AccountPK(BEAN_PK_007);
  
           System.out.print("Create a bean...");
           bean = doEjbCreate(pk, BEAN_NAME);
           System.out.println("OK");
  
           System.out.print("Now query based on the 'PersonsName': " +
                            BEAN_NAME + "...");
           Collection clct = home.findByPersonsName(BEAN_NAME);
           System.out.println("OK");
  
           System.out.print("Verify result set not empty...");
           assert(!clct.isEmpty());
           System.out.println("OK");
  
           System.out.println("Bean result set:");
           for (Iterator itr=clct.iterator(); itr.hasNext();)
           {
              anotherBean = (CtsBmp)itr.next();
              System.out.println("Use 'isIdentical()' to compare beans");
              assert(anotherBean.isIdentical(bean));
              System.out.println( "beans match..OK" );
           }
  
           System.out.print("Make a bean that doesn't match..");
           AccountPK anotherPK = new AccountPK("123");
           differentBean = doEjbCreate(anotherPK, "SomeOtherGuy");
           System.out.println("OK");
  
           System.out.print("Use 'isIdentical()' to verify different beans...");
           assert(!differentBean.isIdentical(bean));
           System.out.println("OK...beans are different!");
  
           System.out.print("Test the Primary Keys...");
           AccountPK beansPK = (AccountPK)bean.getPrimaryKey();
           AccountPK anotherBeansPK = (AccountPK)anotherBean.getPrimaryKey();
           assert(beansPK.equals(anotherBeansPK));
           System.out.println("OK...they're the same");
  
           System.out.print("Compare different keys...");
           assert(!beansPK.equals(anotherPK));
           System.out.println("OK...they're different");
  
           System.out.println(
             "**************************************************************");
  
        } catch(Exception ex) {
           ex.printStackTrace();
           fail("Caught an unknown exception: " + ex.toString() );
        }
     }
  
     /**
      * Method testEjbRemoteIF
      * EJB 1.1 [8.6] p. 93-94
      *
      * The javax.ejb.EJBObject I/F defines the methods that allow the client
      * to perform the following:
      * - Obtain the home interface for the entity object
      * - Remove the entity object
      * - Obtain the entity object's handle
      * - Obtain the entity object's primary key 
      * 
      * @throws Exception
      *
      */
     public void testEjbRemoteIF()
     {
       System.out.println(
           "**************************************************************");
       System.out.println("     testEjbRemoteIF ()");
  
        CtsBmp bean = null;
  
        try {
           CtsBmpHome home = getHome();
           AccountPK pk = new AccountPK(BEAN_PK_007);
  
           System.out.print("Create a bean...");
           bean = doEjbCreate(pk, BEAN_NAME);
           System.out.println("OK");
  
           System.out.print("Obtain the HOME interface...");
           home = (CtsBmpHome)bean.getEJBHome();
           assert(home != null);
           System.out.println("OK");
  
           System.out.print("Obtain the HANDLE...");
           Handle han = bean.getHandle();
           assert(han != null);
           System.out.println("OK");
  
           System.out.print("Obtain the primary key...");
           pk = (AccountPK)bean.getPrimaryKey();
           assert(pk != null);
           System.out.println("OK");
  
           System.out.println("Remove the entity bean");
           bean.remove();
           System.out.println("OK");
        } catch(Exception ex) {
           ex.printStackTrace();
           fail("Caught an unknown exception" + ex.toString());
        }
  
        System.out.println(
           "**************************************************************");
     }
  
     /**
      * Method testEntityHandle
      * EJB 1.1 [8.7] p. 93-94
      *
      * - Client can get handle to remote interface
      * - Use javax.rmi.PortableRemoteObject.narrow(...) to convert the 
      *   result of the getEJBObject().
      * - An entity handle is typically implemented to be usable over a 
      *   long period of time it must be usable at least across a server
      *   restart.
      * 
      * @throws Exception
      *
      */
     public void testEntityHandle()
     {
       System.out.println(
           "**************************************************************");
       System.out.println("     testEntityHandle()"); 
  
        CtsBmp bean = null;
  
        try {
           CtsBmpHome home = getHome();
           AccountPK pk = new AccountPK(BEAN_PK_007);
  
           System.out.print("Create a bean...");
           bean = doEjbCreate(pk, BEAN_NAME);
           System.out.println("OK");
  
           System.out.print("Get a Handle reference and serialize it...");
           Handle beanHandle = bean.getHandle();
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           ObjectOutputStream sOut = new ObjectOutputStream(out);
           sOut.writeObject(beanHandle);
           sOut.flush();
           byte[] bytes = out.toByteArray();
           System.out.println("OK");
   
           System.out.print("Unserialize bean handle...");
           ByteArrayInputStream in = new ByteArrayInputStream(bytes);
           ObjectInputStream sIn = new ObjectInputStream(in);
           beanHandle = (Handle)sIn.readObject();
           System.out.println("OK");
  
           System.out.print("Use PortableRemoteObject to narrow result...");
           bean = (CtsBmp)PortableRemoteObject.narrow(beanHandle.getEJBObject(),
                                                      CtsBmp.class);
           System.out.println("OK");
  
           System.out.print("Check that new reference works...");
           assert(bean.getPersonsName().trim().equals(BEAN_NAME));
           System.out.println("OK");
        } catch(Exception ex) {
           ex.printStackTrace();
           fail("Caught an unknown exeption: " + ex.toString());
        }
  
       System.out.println(
           "**************************************************************");
  
     }
  
     /**
      *  Method testProbeContainerCallbacks
      */
     public void testProbeContainerCallbacks()
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testProbeContainerCallbacks()");
  
        CtsBmp bean = null;
  
        try {
           CtsBmpHome home = getHome();
           AccountPK pk = new AccountPK(BEAN_PK_007);
  
           mbx.clearMessages();
  
           System.out.print("Create a bean...");
           bean = doEjbCreate(pk, BEAN_NAME);
           System.out.println("OK");
  
           System.out.print("Check for set entity context, create " +
                            "and post create messages...");
           Thread.sleep(2500);
           // OSH: We cannot be sure that the context will be set:
           // If the container elects a pooled instance to use for the
           // new object, setEntityContext() may have been called before
           // we cleared the message box.
           //assert(mbx.messageReceived(ContainerMBox.SET_ENTITY_CONTEXT_MSG));
           assert(mbx.messageReceived(ContainerMBox.EJB_CREATE_MSG));
           assert(mbx.messageReceived(ContainerMBox.EJB_POST_CREATE_MSG));
           System.out.println("OK");
  
           // Execute a business method
           System.out.print("Calling setter as a business method...");
           bean.setPersonsName(BEAN_OTHER_NAME);
           System.out.println("OK");
  
           // Remove
           System.out.print("Remove the bean...");
           bean.remove();
           Thread.sleep(3000);
           assert(mbx.messageReceived(ContainerMBox.EJB_STORE_MSG));
           assert(mbx.messageReceived(ContainerMBox.EJB_REMOVE_MSG));
           System.out.println("OK");
        } catch (Exception ex) {
           ex.printStackTrace();
           fail("testEjbCreate has failed!");
        }
  
        System.out.println(
           "**************************************************************");
     }
  
     /**
      * Method testContainerObjects
      * EJB 1.1 [9.3] p. 127-129
      * Container must implement:
      *  - Entity EJBHome class
      *  - Entity EJBObject class
      *  - Handle class
      *  - HomeHandle class
      *  - Meta-data class
      */
     public void testContainerObjects()
     {
        System.out.println(
           "**************************************************************");
        System.out.println("     testContainerObjects()");
  
        CtsBmp bean = null;
  
        try {
           CtsBmpHome home = getHome();
           AccountPK pk = new AccountPK(BEAN_PK_007);
  
           mbx.clearMessages();
  
           System.out.print("Create a bean...");
           bean = doEjbCreate(pk, BEAN_NAME);
           System.out.println("OK");
  
           System.out.print("Get HomeHandle..." );
           HomeHandle homeHan = home.getHomeHandle();
           assert(homeHan != null);   
           System.out.println("OK");
  
           System.out.print("Get another home from the HomeHandle...");
           CtsBmpHome anotherHome = (CtsBmpHome)homeHan.getEJBHome();
           assert(anotherHome != null);
           System.out.println("OK");
  
           System.out.print("Get the Meta-data object...");
           EJBMetaData md = anotherHome.getEJBMetaData();
           assert(md != null);
           System.out.println("OK");
  
           System.out.println("Probe the Meta-data object:");
           String homeInterface = md.getHomeInterfaceClass().getName();
           String primaryKey = md.getPrimaryKeyClass().getName();
           String remoteInterface = md.getRemoteInterfaceClass().getName();
           System.out.println("  Home Interface  : " + homeInterface);
           System.out.println("  PrimaryKey      : " + primaryKey);
           System.out.println("  Remote Interface: " + remoteInterface);
           assert(homeInterface.equals("org.jboss.test.cts.interfaces.CtsBmpHome"));
           assert(primaryKey.equals("org.jboss.test.cts.keys.AccountPK"));
           assert(remoteInterface.equals("org.jboss.test.cts.interfaces.CtsBmp"));
           System.out.println("Meta-data OK");
  
           System.out.print("Check isSession()==false ...");
           assert(!md.isSession());
           System.out.println("OK");
  
           System.out.print("Check isStatelessSession()==false ...");
           assert(!md.isStatelessSession());
           System.out.println("OK");
  
           System.out.print("Test EJBHome.remove(PrimaryKey)");
           anotherHome.remove(pk);
           System.out.println("OK");
  
        } catch (Exception ex) {
           ex.printStackTrace();
           fail("testEjbCreate has failed!");
        }
  
        System.out.println(
           "**************************************************************");
     }
  
    
     // Used to test passing a Entity bean as a parameter.
     // OSH: ??? This just calls a method on the bean ???
     private boolean gotRefOkay(CtsBmp bean, String expectedName)
     {
        boolean retVal = false;
  
        try {
         System.out.println(expectedName + "==" + bean.getPersonsName()+"?");
           retVal =  (bean.getPersonsName().equals(expectedName));
        } catch(Exception ex) {
           System.err.println("Unknown Exception : " + ex.toString() );
        } 
  
        return retVal;
     }
  
     protected void setUp()
        throws Exception
     {
        System.out.println("Build Container MBX for BMP");
        mbx = new ContainerMBox();  
  
        System.out.println("Initialize to empty BMP table.");
        CtsBmpHome home = getHome();
        Collection clct = home.findAll();
        if (clct.size() != 0) {
           System.out.println("Removing " + clct.size() + " old beans.");
           for (Iterator itr=clct.iterator(); itr.hasNext();) {
              CtsBmp bean = (CtsBmp)itr.next();
              bean.remove();
         }
           System.out.println("Removal done.");              
        }
     }
  
     protected void tearDown()
        throws Exception
     {
        mbx.close();
     }
  }
  
  
  

Reply via email to