db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/ejb/org/apache/ojb/ejb PBBaseBeanImpl.java CommonTestClient.java ArticleManagerPBBean.java ArticleManagerODMGBean.java
Date Tue, 03 Jun 2003 14:54:44 GMT
arminw      2003/06/03 07:54:44

  Modified:    src/ejb/org/apache/ojb/ejb PBBaseBeanImpl.java
                        CommonTestClient.java ArticleManagerPBBean.java
                        ArticleManagerODMGBean.java
  Log:
  add new test using odmg-api
  and reference collection
  
  Revision  Changes    Path
  1.6       +23 -10    db-ojb/src/ejb/org/apache/ojb/ejb/PBBaseBeanImpl.java
  
  Index: PBBaseBeanImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/ejb/org/apache/ojb/ejb/PBBaseBeanImpl.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- PBBaseBeanImpl.java	26 Apr 2003 23:18:26 -0000	1.5
  +++ PBBaseBeanImpl.java	3 Jun 2003 14:54:43 -0000	1.6
  @@ -57,10 +57,10 @@
   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.core.PBFactoryIF;
   import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
  +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;
   
  @@ -160,8 +160,15 @@
       public int getCount(Class target)
       {
           PersistenceBroker broker = getBroker();
  -        int result = broker.getCount(new QueryByCriteria(target, null));
  -        broker.close();
  +        int result;
  +        try
  +        {
  +            result = broker.getCount(new QueryByCriteria(target, null));
  +        }
  +        finally
  +        {
  +            if (broker != null) broker.close();
  +        }
           return result;
       }
   
  @@ -171,9 +178,16 @@
       public Collection getAllObjects(Class target)
       {
           PersistenceBroker broker = getBroker();
  -        Query q = new QueryByCriteria(target, null);
  -        Collection result = broker.getCollectionByQuery(q);
  -        broker.close();
  +        Collection result;
  +        try
  +        {
  +            Query q = new QueryByCriteria(target, null);
  +            result = broker.getCollectionByQuery(q);
  +        }
  +        finally
  +        {
  +            if (broker != null) broker.close();
  +        }
           return result;
       }
   
  @@ -182,10 +196,9 @@
        */
       public Object storeObject(Object object)
       {
  -        PersistenceBroker broker = null;
  +        PersistenceBroker broker = getBroker();
           try
           {
  -            broker = getBroker();
               broker.store(object);
           }
           finally
  
  
  
  1.2       +47 -9     db-ojb/src/ejb/org/apache/ojb/ejb/CommonTestClient.java
  
  Index: CommonTestClient.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/ejb/org/apache/ojb/ejb/CommonTestClient.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CommonTestClient.java	3 Jun 2003 00:05:37 -0000	1.1
  +++ CommonTestClient.java	3 Jun 2003 14:54:43 -0000	1.2
  @@ -69,7 +69,8 @@
    */
   public class CommonTestClient extends TestCase
   {
  -    ArticleManagerPBRemote articleBean;
  +    ArticleManagerPBRemote pbArticleBean;
  +    ArticleManagerODMGRemote odmgArticleBean;
   
       public CommonTestClient(String s)
       {
  @@ -87,13 +88,13 @@
           junit.textui.TestRunner.main(arr);
       }
   
  -    public void testCollectionRetrieve() throws Exception
  +    public void testPBCollectionRetrieve() throws Exception
       {
           long timestamp = System.currentTimeMillis();
           String name = "collection_test_" + timestamp;
           CategoryVO cat = VOHelper.createNewCategory("category_" + timestamp);
           // store new category
  -        cat = articleBean.storeCategory(cat);
  +        cat = pbArticleBean.storeCategory(cat);
           ArrayList articles = new ArrayList();
           for (int i = 0; i < 5; i++)
           {
  @@ -101,18 +102,52 @@
               // set category
               art.setCategory(cat);
               // store article
  -            art = articleBean.storeArticle(art);
  +            art = pbArticleBean.storeArticle(art);
               articles.add(art);
           }
           // set article collection
           cat.setAssignedArticles(articles);
           // persist updated category
  -        cat = articleBean.storeCategory(cat);
  +        cat = pbArticleBean.storeCategory(cat);
           assertNotNull(cat.getObjId());
           assertNotNull(cat.getAssignedArticles());
           assertEquals("Wrong number of referenced articles found", 5, cat.getAssignedArticles().size());
   
  -        Collection result = articleBean.getCategoryByName("category_" + timestamp);
  +        Collection result = pbArticleBean.getCategoryByName("category_" + timestamp);
  +        assertNotNull(result);
  +        assertEquals(1, result.size());
  +        cat = (CategoryVO) result.iterator().next();
  +        Collection articlesCol = cat.getAssignedArticles();
  +        assertNotNull(articlesCol);
  +        assertEquals("Wrong number of referenced articles found", 5, articlesCol.size());
  +    }
  +
  +    public void testODMGCollectionRetrieve() throws Exception
  +    {
  +        long timestamp = System.currentTimeMillis();
  +        String name = "collection_test_" + timestamp;
  +        CategoryVO cat = VOHelper.createNewCategory("category_" + timestamp);
  +        // store new category
  +        cat = odmgArticleBean.storeCategory(cat);
  +        ArrayList articles = new ArrayList();
  +        for (int i = 0; i < 5; i++)
  +        {
  +            ArticleVO art = VOHelper.createNewArticle(name, 1);
  +            // set category
  +            art.setCategory(cat);
  +            // store article
  +            art = odmgArticleBean.storeArticle(art);
  +            articles.add(art);
  +        }
  +        // set article collection
  +        cat.setAssignedArticles(articles);
  +        // persist updated category
  +        cat = odmgArticleBean.storeCategory(cat);
  +        assertNotNull(cat.getObjId());
  +        assertNotNull(cat.getAssignedArticles());
  +        assertEquals("Wrong number of referenced articles found", 5, cat.getAssignedArticles().size());
  +
  +        Collection result = odmgArticleBean.getCategoryByName("category_" + timestamp);
           assertNotNull(result);
           assertEquals(1, result.size());
           cat = (CategoryVO) result.iterator().next();
  @@ -133,8 +168,11 @@
           {
               Object object = PortableRemoteObject.narrow(
                       ContextHelper.getContext().lookup(ArticleManagerPBHome.JNDI_NAME),
EJBHome.class);
  -            articleBean = (ArticleManagerPBRemote) ((ArticleManagerPBHome) object).create();
  -            System.out.println("Bean found: " + articleBean);
  +            pbArticleBean = (ArticleManagerPBRemote) ((ArticleManagerPBHome) object).create();
  +
  +            object = PortableRemoteObject.narrow(
  +                    ContextHelper.getContext().lookup(ArticleManagerODMGHome.JNDI_NAME),
EJBHome.class);
  +            odmgArticleBean = (ArticleManagerODMGRemote) ((ArticleManagerODMGHome) object).create();
           }
           catch (Exception e)
           {
  
  
  
  1.7       +29 -14    db-ojb/src/ejb/org/apache/ojb/ejb/ArticleManagerPBBean.java
  
  Index: ArticleManagerPBBean.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/ejb/org/apache/ojb/ejb/ArticleManagerPBBean.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ArticleManagerPBBean.java	3 Jun 2003 00:05:37 -0000	1.6
  +++ ArticleManagerPBBean.java	3 Jun 2003 14:54:43 -0000	1.7
  @@ -109,14 +109,6 @@
       /**
        * @ejb:interface-method
        */
  -    public CategoryVO storeCategory(CategoryVO category)
  -    {
  -        return (CategoryVO) this.storeObject(category);
  -    }
  -
  -    /**
  -     * @ejb:interface-method
  -     */
       public Collection storeArticles(Collection articles)
       {
           return this.storeObjects(articles);
  @@ -172,12 +164,35 @@
       public Collection getArticles(String articleName)
       {
           PersistenceBroker broker = getBroker();
  -        Criteria criteria = new Criteria();
  -        if (articleName != null) criteria.addEqualTo("articleName", articleName);
  -        Query q = new QueryByCriteria(ArticleVO.class, criteria);
  -        Collection result = broker.getCollectionByQuery(q);
  -        broker.close();
  +        Collection result;
  +        try
  +        {
  +            Criteria criteria = new Criteria();
  +            if (articleName != null) criteria.addEqualTo("articleName", articleName);
  +            Query q = new QueryByCriteria(ArticleVO.class, criteria);
  +            result = broker.getCollectionByQuery(q);
  +        }
  +        finally
  +        {
  +            if (broker != null) broker.close();
  +        }
           return result;
  +    }
  +
  +    /**
  +     * @ejb:interface-method
  +     */
  +    public CategoryVO storeCategory(CategoryVO category)
  +    {
  +        return (CategoryVO) this.storeObject(category);
  +    }
  +
  +    /**
  +     * @ejb:interface-method
  +     */
  +    public void deleteCategory(CategoryVO category)
  +    {
  +        this.deleteObject(category);
       }
   
       /**
  
  
  
  1.6       +47 -3     db-ojb/src/ejb/org/apache/ojb/ejb/ArticleManagerODMGBean.java
  
  Index: ArticleManagerODMGBean.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/ejb/org/apache/ojb/ejb/ArticleManagerODMGBean.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ArticleManagerODMGBean.java	10 Jan 2003 16:52:22 -0000	1.5
  +++ ArticleManagerODMGBean.java	3 Jun 2003 14:54:43 -0000	1.6
  @@ -168,9 +168,53 @@
           try
           {
               StringBuffer buf = new StringBuffer("select allObjects from " + ArticleVO.class.getName());
  -            buf.append(" where articleId not null");
  -            if (articleName != null) buf.append(" and articleName = " + articleName);
  +            // buf.append(" where articleId not null");
  +            if (articleName != null)
  +                buf.append(" where articleName = $1");
  +            else
  +                buf.append(" where articleName is null");
               query.create(buf.toString());
  +            if (articleName != null) query.bind(articleName);
  +            return (Collection) query.execute();
  +        }
  +        catch (Exception e)
  +        {
  +            log.error("OQLQuery failed", e);
  +            throw new OJBRuntimeException("OQLQuery failed", e);
  +        }
  +    }
  +
  +    /**
  +     * @ejb:interface-method
  +     */
  +    public CategoryVO storeCategory(CategoryVO category)
  +    {
  +        return (CategoryVO) this.storeObject(category);
  +    }
  +
  +    /**
  +     * @ejb:interface-method
  +     */
  +    public void deleteCategory(CategoryVO category)
  +    {
  +        this.deleteObject(category);
  +    }
  +
  +    /**
  +     * @ejb:interface-method
  +     */
  +    public Collection getCategoryByName(String categoryName)
  +    {
  +        OQLQuery query = getImplementation().newOQLQuery();
  +        try
  +        {
  +            StringBuffer buf = new StringBuffer("select allObjects from " + CategoryVO.class.getName());
  +            if (categoryName != null)
  +                buf.append(" where categoryName = $1");
  +            else
  +                buf.append(" where categoryName is null");
  +            query.create(buf.toString());
  +            if (categoryName != null) query.bind(categoryName);
               return (Collection) query.execute();
           }
           catch (Exception e)
  
  
  

Mime
View raw message