db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker QueryTest.java
Date Fri, 27 Aug 2004 19:57:07 GMT
brj         2004/08/27 12:57:07

  Modified:    src/test/org/apache/ojb/broker QueryTest.java
  Log:
  modified testcases for subqueries using extents
  added testcases for queryByExample
  
  Revision  Changes    Path
  1.72      +96 -8     db-ojb/src/test/org/apache/ojb/broker/QueryTest.java
  
  Index: QueryTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/QueryTest.java,v
  retrieving revision 1.71
  retrieving revision 1.72
  diff -u -r1.71 -r1.72
  --- QueryTest.java	23 Aug 2004 18:24:47 -0000	1.71
  +++ QueryTest.java	27 Aug 2004 19:57:07 -0000	1.72
  @@ -14,6 +14,7 @@
   import org.apache.ojb.broker.query.LikeCriteria;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryByCriteria;
  +import org.apache.ojb.broker.query.QueryByExample;
   import org.apache.ojb.broker.query.QueryFactory;
   import org.apache.ojb.broker.query.ReportQueryByCriteria;
   import org.apache.ojb.broker.platforms.PlatformHsqldbImpl;
  @@ -52,7 +53,7 @@
           broker.commitTransaction();
           broker.clearCache();
   
  - 	    QueryByCriteria query = new QueryByCriteria(group);
  +        QueryByExample query = new QueryByExample(group);
    	    ObjectRepository.Group newGroup = ( ObjectRepository.Group ) broker.getObjectByQuery(query);
           assertNull(newGroup);
   
  @@ -103,6 +104,38 @@
       }
   
       /**
  +     * Criteria by example
  +     */
  +    public void testByExample1()
  +    {
  +        Person p = new Person();
  +        p.setId(1);
  +        p.setFirstname("tom");
  +
  +        Query q = QueryFactory.newQueryByExample(p);
  +        Collection results = broker.getCollectionByQuery(q);
  +
  +        // 1 person
  +        assertEquals(results.size(), 1);
  +    }
  +
  +    /**
  +     * Criteria by example, ignoring 0
  +     */
  +    public void testByExample2()
  +    {
  +        Article a = new Article();
  +        a.setArticleId(200);
  +
  +        QueryByExample q = QueryFactory.newQueryByExample(a);
  +        q.setIgnoreZero(true);
  +        Collection results = broker.getCollectionByQuery(q);
  +
  +        // 1 cd
  +        assertEquals(results.size(), 1);
  +    }
  +    
  +    /**
   	 * test EqualTo Criteria
   	 */
       public void testEqualCriteria()
  @@ -730,10 +763,13 @@
   
       }
   
  -    public void testReportQueryAgainstAbstractClasses()
  +    /**
  +     * Concrete Class in SubQuery
  +     */
  +    public void testSubQueryAgainstConcreteClass()
       {
  -        String name = "testReportQueryAgainstAbstractClasses_"+System.currentTimeMillis();
  -        Article article = new Article();
  +        String name = "testSubQueryAgainstConcreteClass_"+System.currentTimeMillis();
  +        Article article = new BookArticle();
           article.setArticleName(name);
           ProductGroup group = new ProductGroup();
           group.setGroupName(name);
  @@ -748,11 +784,12 @@
           Criteria critSub = new Criteria();
   
           critSub.addEqualTo("articleName", name);
  -        ReportQueryByCriteria querySub = QueryFactory.newReportQuery(AbstractArticle.class,
critSub);
  +        ReportQueryByCriteria querySub = QueryFactory.newReportQuery(BookArticle.class,
critSub);
           querySub.setAttributes(new String[] { "productGroupId"});
   
  -        critMain.addIn("productGroup.groupId", querySub);
           ReportQueryByCriteria queryMain = QueryFactory.newReportQuery(AbstractProductGroup.class,
critMain);
  +        queryMain.setAttributes(new String[] { "groupId", "groupName"});
  +        critMain.addIn("groupId", querySub);
   
           Iterator iter = broker.getReportQueryIteratorByQuery(queryMain);
           int result = 0;
  @@ -770,6 +807,57 @@
       }
   
       /**
  +     * Class with extents in SubQuery.
  +     * SubQueries are NOT extent aware ! so we have to use two queries.
  +     */
  +    public void testSubQueryAgainstExtents()
  +    {
  +        String name = "testSubQueryAgainstExtents_"+System.currentTimeMillis();
  +        Article article = new BookArticle();
  +        article.setArticleName(name);
  +        ProductGroup group = new ProductGroup();
  +        group.setGroupName(name);
  +        article.setProductGroup(group);
  +    
  +        broker.beginTransaction();
  +        broker.store(group);
  +        broker.store(article);
  +        broker.commitTransaction();
  +    
  +        Criteria critMain = new Criteria();
  +        Criteria critSub = new Criteria();
  +    
  +        critSub.addEqualTo("articleName", name);
  +        ReportQueryByCriteria querySub = QueryFactory.newReportQuery(Article.class, critSub);
  +        querySub.setAttributes(new String[] { "productGroupId"});
  +        Iterator subIter = broker.getReportQueryIteratorByQuery(querySub);
  +        Collection subIds = new ArrayList();
  +        while (subIter.hasNext())
  +        {
  +            Object[] id = (Object[])subIter.next();
  +            subIds.add(id[0]);
  +        }
  +            
  +        ReportQueryByCriteria queryMain = QueryFactory.newReportQuery(AbstractProductGroup.class,
critMain);
  +        queryMain.setAttributes(new String[] { "groupId", "groupName"});
  +        critMain.addIn("groupId", subIds);
  +    
  +        Iterator iter = broker.getReportQueryIteratorByQuery(queryMain);
  +        int result = 0;
  +        assertNotNull(iter);
  +        while (iter.hasNext())
  +        {
  +            iter.next();
  +            ++result;
  +        }
  +        assertEquals(1, result);
  +    
  +        // compare with count
  +        int count = broker.getCount(queryMain);
  +        assertEquals(result, count);
  +    }
  +
  +    /**
        * ReportQuery with pathExpression in columns
        */
       public void testReportQueryPathExpression()
  @@ -1323,7 +1411,6 @@
           {
               Object obj = iter.next();
               list.add(obj);
  -            int i = ((Integer)((Object[]) obj)[2]).intValue();
   
               /*
               arminw:
  @@ -1337,7 +1424,8 @@
   //                        + "  " + ((Object[]) obj)[1]
   //                        + "  " + ((Object[]) obj)[2]);
   
  -                assertTrue(i<6 & i>0);
  +                int i = ((Integer)((Object[]) obj)[2]).intValue();
  +                assertTrue("i=" + i, i<6 & i>0);
               }
           }
       }
  
  
  

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


Mime
View raw message