You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2003/12/31 12:44:38 UTC

cvs commit: db-ojb/src/ejb/org/apache/ojb/ejb PBSessionBean.java PBClient.java ODMGSessionBean.java ODMGClient.java

arminw      2003/12/31 03:44:38

  Modified:    src/ejb/org/apache/ojb/ejb PBSessionBean.java PBClient.java
                        ODMGSessionBean.java ODMGClient.java
  Log:
  fix bean test cases
  add new tests
  
  Revision  Changes    Path
  1.11      +61 -12    db-ojb/src/ejb/org/apache/ojb/ejb/PBSessionBean.java
  
  Index: PBSessionBean.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/ejb/org/apache/ojb/ejb/PBSessionBean.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- PBSessionBean.java	26 Apr 2003 23:18:26 -0000	1.10
  +++ PBSessionBean.java	31 Dec 2003 11:44:38 -0000	1.11
  @@ -55,17 +55,6 @@
    */
   
   
  -import org.apache.ojb.broker.PBKey;
  -import org.apache.ojb.broker.PersistenceBroker;
  -import org.apache.ojb.broker.PersistenceBrokerException;
  -import org.apache.ojb.broker.query.Query;
  -import org.apache.ojb.broker.query.QueryByCriteria;
  -import org.apache.ojb.broker.query.Criteria;
  -import org.apache.ojb.broker.core.PBFactoryIF;
  -import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
  -import org.apache.ojb.broker.util.logging.Logger;
  -import org.apache.ojb.broker.util.logging.LoggerFactory;
  -
   import javax.ejb.EJBException;
   import javax.ejb.SessionBean;
   import javax.ejb.SessionContext;
  @@ -76,6 +65,16 @@
   import java.util.Iterator;
   import java.util.List;
   
  +import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.PersistenceBrokerException;
  +import org.apache.ojb.broker.core.PBFactoryIF;
  +import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
  +import org.apache.ojb.broker.query.Criteria;
  +import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryByCriteria;
  +import org.apache.ojb.broker.util.logging.Logger;
  +import org.apache.ojb.broker.util.logging.LoggerFactory;
  +
   /**
    * This is an session bean implementation using PB-api.
    * <br>
  @@ -214,6 +213,19 @@
       /**
        * @ejb:interface-method
        */
  +    public Iterator iterateAllObjects(Class target)
  +    {
  +        if(log.isDebugEnabled()) log.debug("getAllObjects was called");
  +        PersistenceBroker broker = getBroker();
  +        Query q = new QueryByCriteria(target, null);
  +        Iterator result = broker.getIteratorByQuery(q);
  +        broker.close();
  +        return result;
  +    }
  +
  +    /**
  +     * @ejb:interface-method
  +     */
       public int getArticleCount()
       {
           if(log.isDebugEnabled()) log.debug("getArticleCount was called");
  @@ -309,5 +321,42 @@
           if(log.isDebugEnabled()) log.debug("rollbackTest was called");
           storeObjects(objects);
           throw new EJBException("## Testing of rollback behaviour ##");
  +    }
  +
  +    /**
  +     * @ejb:interface-method
  +     */
  +    public void rollbackOnlyTest(List objects)
  +    {
  +        if(log.isDebugEnabled()) log.debug("rollbackTest was called");
  +        storeObjects(objects);
  +        ctx.setRollbackOnly();
  +        throw new EJBException("Set rollback only");
  +    }
  +
  +    /**
  +     * @ejb:interface-method
  +     */
  +    public void iteratorRollbackOnlyTest(List objects)
  +    {
  +        if(log.isDebugEnabled()) log.debug("rollbackTest was called");
  +        PersistenceBroker broker = null;
  +        try
  +        {
  +            storeObjects(objects);
  +            Class searchClass = objects.get(0).getClass();
  +            Query q = new QueryByCriteria(searchClass, new Criteria());
  +            broker = getBroker();
  +            // we get the iterator and step into the first found object
  +            Iterator it = broker.getIteratorByQuery(q);
  +            it.next();
  +            // now the iterator resources may not release, see what's going on
  +            ctx.setRollbackOnly();
  +            throw new EJBException("Set rollback only");
  +        }
  +        finally
  +        {
  +            if(broker != null) broker.close();
  +        }
       }
   }
  
  
  
  1.10      +45 -9     db-ojb/src/ejb/org/apache/ojb/ejb/PBClient.java
  
  Index: PBClient.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/ejb/org/apache/ojb/ejb/PBClient.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- PBClient.java	14 Mar 2003 01:17:27 -0000	1.9
  +++ PBClient.java	31 Dec 2003 11:44:38 -0000	1.10
  @@ -54,19 +54,16 @@
    * <http://www.apache.org/>.
    */
   
  -import junit.framework.TestCase;
  -
   import javax.ejb.EJBHome;
   import javax.naming.Context;
  -import javax.naming.InitialContext;
  -import javax.naming.NamingException;
   import javax.rmi.PortableRemoteObject;
   import java.rmi.RemoteException;
   import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Iterator;
   import java.util.List;
  -import java.util.Properties;
  +
  +import junit.framework.TestCase;
   
   /**
    * Test client using the {@link org.apache.ojb.ejb.PBSessionBean}.
  @@ -123,9 +120,8 @@
           Context ctx = ContextHelper.getContext();
           try
           {
  -//            System.err.println("## "+ctx.getEnvironment());
               Object object = PortableRemoteObject.narrow(ctx.lookup(PBSessionHome.JNDI_NAME), EJBHome.class);
  -            bean = (PBSessionRemote) ((PBSessionHome) object).create();
  +            bean = ((PBSessionHome) object).create();
           }
           catch (Exception e)
           {
  @@ -166,7 +162,47 @@
           try
           {
               bean.rollbackTest(persons);
  -            assertTrue("Rollback (remote) exception expected", false);
  +            fail("Rollback (remote) exception expected");
  +        }
  +        catch (RemoteException e)
  +        {
  +            // this should happend
  +            assertTrue(true);
  +        }
  +
  +        int personsAfterFailedStore = bean.getPersonCount();
  +        assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore);
  +    }
  +
  +    public void testSetRollbackOnly() throws RemoteException
  +    {
  +        int personsBefore = bean.getPersonCount();
  +
  +        List persons = VOHelper.createNewPersonList(5);
  +        try
  +        {
  +            bean.rollbackOnlyTest(persons);
  +            fail("Rollback (remote) exception expected");
  +        }
  +        catch (RemoteException e)
  +        {
  +            // this should happend
  +            assertTrue(true);
  +        }
  +
  +        int personsAfterFailedStore = bean.getPersonCount();
  +        assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore);
  +    }
  +
  +    public void testIteratorRollbackOnly() throws RemoteException
  +    {
  +        int personsBefore = bean.getPersonCount();
  +
  +        List persons = VOHelper.createNewPersonList(5);
  +        try
  +        {
  +            bean.iteratorRollbackOnlyTest(persons);
  +            fail("Rollback (remote) exception expected");
           }
           catch (RemoteException e)
           {
  
  
  
  1.12      +60 -28    db-ojb/src/ejb/org/apache/ojb/ejb/ODMGSessionBean.java
  
  Index: ODMGSessionBean.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/ejb/org/apache/ojb/ejb/ODMGSessionBean.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- ODMGSessionBean.java	14 Mar 2003 00:56:25 -0000	1.11
  +++ ODMGSessionBean.java	31 Dec 2003 11:44:38 -0000	1.12
  @@ -55,10 +55,25 @@
    */
   
   
  +import javax.ejb.EJBException;
  +import javax.ejb.SessionBean;
  +import javax.ejb.SessionContext;
  +import javax.naming.Context;
  +import javax.naming.InitialContext;
  +import javax.naming.NamingException;
  +import java.util.Collection;
  +import java.util.Iterator;
  +import java.util.List;
  +
   import org.apache.ojb.broker.OJBRuntimeException;
  +import org.apache.ojb.broker.PersistenceBroker;
  +import org.apache.ojb.broker.query.Criteria;
  +import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryByCriteria;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   import org.apache.ojb.jboss.ODMGFactory;
  +import org.apache.ojb.odmg.TransactionExt;
   import org.odmg.DList;
   import org.odmg.Database;
   import org.odmg.Implementation;
  @@ -66,18 +81,6 @@
   import org.odmg.OQLQuery;
   import org.odmg.QueryException;
   import org.odmg.Transaction;
  -import org.odmg.QueryParameterCountInvalidException;
  -import org.odmg.QueryParameterTypeInvalidException;
  -
  -import javax.ejb.EJBException;
  -import javax.ejb.SessionBean;
  -import javax.ejb.SessionContext;
  -import javax.naming.Context;
  -import javax.naming.InitialContext;
  -import javax.naming.NamingException;
  -import java.util.Collection;
  -import java.util.Iterator;
  -import java.util.List;
   
   /**
    * This is an session bean implementation using odmg implementation.
  @@ -183,22 +186,14 @@
        */
       public List storeObjects(List objects)
       {
  -        try
  -        {
  -            if(log.isDebugEnabled()) log.debug("storeObjects");
  +        if(log.isDebugEnabled()) log.debug("storeObjects");
   
  -            /* One possibility of storing objects is to use the current transaction
  -             associated with the container */
  -            Transaction tx = odmg.currentTransaction();
  -            for (Iterator iterator = objects.iterator(); iterator.hasNext();)
  -            {
  -                tx.lock(iterator.next(), Transaction.WRITE);
  -            }
  -        }
  -        catch (Throwable t)
  +        /* One possibility of storing objects is to use the current transaction
  +         associated with the container */
  +        Transaction tx = odmg.currentTransaction();
  +        for (Iterator iterator = objects.iterator(); iterator.hasNext();)
           {
  -            t.printStackTrace();
  -            System.out.println(t.getMessage());
  +            tx.lock(iterator.next(), Transaction.WRITE);
           }
           return objects;
       }
  @@ -336,6 +331,43 @@
       /**
        * @ejb:interface-method
        */
  +    public void rollbackTest3(List objects)
  +    {
  +        if(log.isDebugEnabled()) log.debug("rollbackTest3 method was called");
  +        storeObjects(objects);
  +        ctx.setRollbackOnly();
  +        throw new EJBException("Set rollback only");
  +    }
  +
  +    /**
  +     * @ejb:interface-method
  +     */
  +    public void rollbackTest4(List objects)
  +    {
  +        // now we mix up different api's and use PB-api too
  +        if(log.isDebugEnabled()) log.debug("rollbackTest4");
  +        /*
  +        store list of objects, then get these objects with Iterator, start
  +        iteration, then break
  +        */
  +        storeObjects(objects);
  +        TransactionExt tx = ((TransactionExt) odmg.currentTransaction());
  +        // force writing to DB
  +        tx.flush();
  +        Class searchClass = objects.get(0).getClass();
  +        PersistenceBroker broker = tx.getBroker();
  +        Query q = new QueryByCriteria(searchClass, new Criteria());
  +        // we get the iterator and step into the first found object
  +        Iterator it = broker.getIteratorByQuery(q);
  +        it.next();
  +        // now the iterator resources may not release, see what's going on
  +        ctx.setRollbackOnly();
  +        throw new EJBException("Set rollback only");
  +    }
  +
  +    /**
  +     * @ejb:interface-method
  +     */
       public Collection getAllObjects(Class target)
       {
           if(log.isDebugEnabled()) log.debug("getAllObjects was called");
  @@ -356,7 +388,7 @@
       {
           try
           {
  -            db.close();
  +            if(odmg.currentTransaction() == null) db.close();
           }
           catch (ODMGException e)
           {
  
  
  
  1.11      +76 -53    db-ojb/src/ejb/org/apache/ojb/ejb/ODMGClient.java
  
  Index: ODMGClient.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/ejb/org/apache/ojb/ejb/ODMGClient.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ODMGClient.java	14 Mar 2003 01:17:27 -0000	1.10
  +++ ODMGClient.java	31 Dec 2003 11:44:38 -0000	1.11
  @@ -54,19 +54,17 @@
    * <http://www.apache.org/>.
    */
   
  -import junit.framework.TestCase;
  -
   import javax.ejb.EJBHome;
   import javax.naming.Context;
  -import javax.naming.InitialContext;
  -import javax.naming.NamingException;
   import javax.rmi.PortableRemoteObject;
  +import javax.transaction.UserTransaction;
   import java.rmi.RemoteException;
   import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Iterator;
   import java.util.List;
  -import java.util.Properties;
  +
  +import junit.framework.TestCase;
   
   /**
    * Test client using the {@link org.apache.ojb.ejb.ODMGSessionBean}.
  @@ -93,8 +91,6 @@
       {
           loops = args.length > 0 ? new Integer(args[0]).intValue(): 500;
           junit.textui.TestRunner.main(new String[] {ODMGClient.class.getName()});
  -//        ODMGClient client = new ODMGClient();
  -//        client.start();
       }
   
       protected void setUp() throws Exception
  @@ -108,24 +104,6 @@
           super.tearDown();
       }
   
  -//    // only used when running outside junit
  -//    public void start()
  -//    {
  -//        init();
  -//        try
  -//        {
  -//            testInsertDelete();
  -//            testRollback_2();
  -//            testRollback_1();
  -//            testServerSideMethods();
  -//            //testStress();
  -//        }
  -//        catch (Exception e)
  -//        {
  -//            e.printStackTrace();
  -//        }
  -//    }
  -
       protected void init()
       {
           Context ctx = ContextHelper.getContext();
  @@ -167,31 +145,34 @@
           assertEquals("Deleting of persons failed", personsAfterStore - 5, personsAfterDelete);
       }
   
  -//    public void testRollback3() throws Exception
  -//    {
  -//        int articlesBefore = sampleBean.getArticleCount();
  -//        int personsBefore = sampleBean.getPersonCount();
  -//        try
  -//        {
  -//            UserTransaction tx = (UserTransaction) ctx.lookup("UserTransaction");
  -//            tx.begin();
  -//            List articles = VOHelper.createNewArticleList(10);
  -//            List persons = VOHelper.createNewPersonList(5);
  -//            articles = sampleBean.storeObjects(articles);
  -//            persons = sampleBean.storeObjects(persons);
  -//            tx.rollback();
  -//        }
  -//        catch (RemoteException e)
  -//        {
  -//            // we should catch
  -//            //e.printStackTrace();
  -//        }
  -//
  -//        int articlesAfterStore = sampleBean.getArticleCount();
  -//        int personsAfterStore = sampleBean.getPersonCount();
  -//        assertEquals("Storing of articles failed", articlesBefore+10, articlesAfterStore);
  -//        assertEquals("Storing of persons faile", personsBefore+5, personsAfterStore);
  -//    }
  +    /*
  +    TODO: Make this work
  +    */
  +    public void YYYtestRollbackRemoteUserTransaction() throws Exception
  +    {
  +        int articlesBefore = sampleBean.getArticleCount();
  +        int personsBefore = sampleBean.getPersonCount();
  +        try
  +        {
  +            UserTransaction tx = (UserTransaction) ContextHelper.getContext().lookup("UserTransaction");
  +            tx.begin();
  +            List articles = VOHelper.createNewArticleList(10);
  +            List persons = VOHelper.createNewPersonList(5);
  +            articles = sampleBean.storeObjects(articles);
  +            persons = sampleBean.storeObjects(persons);
  +            tx.rollback();
  +        }
  +        catch (RemoteException e)
  +        {
  +            // we should catch
  +            e.printStackTrace();
  +        }
  +
  +        int articlesAfterStore = sampleBean.getArticleCount();
  +        int personsAfterStore = sampleBean.getPersonCount();
  +        assertEquals("Storing of articles failed", articlesBefore+10, articlesAfterStore);
  +        assertEquals("Storing of persons faile", personsBefore+5, personsAfterStore);
  +    }
   
       public void testRollback_1() throws RemoteException
       {
  @@ -202,7 +183,7 @@
           try
           {
               sampleBean.rollbackTest(persons);
  -            assertTrue("Rollback exception expected", false);
  +            fail("RemoteException expected");
           }
           catch (RemoteException e)
           {
  @@ -223,7 +204,49 @@
           try
           {
               sampleBean.rollbackTest2(persons);
  -            assertTrue("Rollback exception expected", false);
  +            fail("RemoteException expected");
  +        }
  +        catch (RemoteException e)
  +        {
  +            // we should catch
  +            //e.printStackTrace();
  +        }
  +
  +        int personsAfterFailedStore = sampleBean.getPersonCount();
  +        assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore);
  +    }
  +
  +    public void testRollback_3() throws RemoteException
  +    {
  +        System.out.println("## testRollback_3");
  +        int personsBefore = sampleBean.getPersonCount();
  +
  +        List persons = VOHelper.createNewPersonList(5);
  +        try
  +        {
  +            sampleBean.rollbackTest3(persons);
  +            fail("RemoteException expected");
  +        }
  +        catch (RemoteException e)
  +        {
  +            // we should catch
  +            //e.printStackTrace();
  +        }
  +
  +        int personsAfterFailedStore = sampleBean.getPersonCount();
  +        assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore);
  +    }
  +
  +    public void testRollback_4() throws RemoteException
  +    {
  +        System.out.println("## testRollback_4");
  +        int personsBefore = sampleBean.getPersonCount();
  +
  +        List persons = VOHelper.createNewPersonList(5);
  +        try
  +        {
  +            sampleBean.rollbackTest4(persons);
  +            fail("RemoteException expected");
           }
           catch (RemoteException e)
           {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org