Author: arminw
Date: Mon Apr  2 11:15:05 2007
New Revision: 524865

URL: http://svn.apache.org/viewvc?view=rev&rev=524865
Log:
fix tests, add new test

Modified:
    db/ojb/trunk/src/test/org/apache/ojb/broker/QueryTest.java

Modified: db/ojb/trunk/src/test/org/apache/ojb/broker/QueryTest.java
URL: 
http://svn.apache.org/viewvc/db/ojb/trunk/src/test/org/apache/ojb/broker/QueryTest.java?view=diff&rev=524865&r1=524864&r2=524865
==============================================================================
--- db/ojb/trunk/src/test/org/apache/ojb/broker/QueryTest.java (original)
+++ db/ojb/trunk/src/test/org/apache/ojb/broker/QueryTest.java Mon Apr  2 
11:15:05 2007
@@ -80,7 +80,7 @@
         ClassDescriptor cld = broker.getClassDescriptor(Article.class);
         FieldDescriptor fld = cld.getFieldDescriptorByName("articleName");
 
-        String sql = "select * from " + cld.getFullTableName() + " as A1 where 
A1." + fld.getColumnName() + " = '" + name + "'";
+        String sql = "select * from " + cld.getFullTableName() + " A1 where 
A1." + fld.getColumnName() + " = '" + name + "'";
 
         Query query = QueryFactory.newQuery(sql);
         Iterator it = broker.getReportQueryIteratorByQuery(query);
@@ -508,7 +508,7 @@
         Criteria crit;
         Query q;
         Collection results;
-        
+
         // sql only
         crit = new Criteria();
         crit.addSql("upper(firstname) = 'TOM' and id = 1");
@@ -516,8 +516,8 @@
 
         results = broker.getCollectionByQuery(q);
         int size1 = results.size();
-        
-        // sql plus attribute 
+
+        // sql plus attribute
         crit = new Criteria();
         crit.addSql("upper(firstname) = 'TOM'");
         crit.addEqualTo("id", new Integer(1));
@@ -525,8 +525,8 @@
 
         results = broker.getCollectionByQuery(q);
         int size2 = results.size();
-        
-        // attribute plus sql 
+
+        // attribute plus sql
         crit = new Criteria();
         crit.addEqualTo("upper(firstname)", "TOM");
         crit.addSql("id = 1");
@@ -553,7 +553,7 @@
         Collection results = broker.getCollectionByQuery(q);
         assertNotNull(results);
         assertTrue(results.size() > 0);
-        
+
         // compare with count
         int count = broker.getCount(q);
         assertEquals(results.size(), count);
@@ -586,6 +586,7 @@
         list_projects.add(p2);
         p.setProjects(list_projects);
 
+        // another Project but without associated Person objects
         Project p3 = new Project();
         p3.setTitle(name);
         ArrayList list_3 = new ArrayList();
@@ -658,7 +659,7 @@
      */
     public void testSubQuery2()
     {
-        Collection results = null;
+        Collection results;
         String stamp = "testSubQuery2_" + System.currentTimeMillis();
         int loops = 10;
         // create ProductGroups without article
@@ -674,15 +675,15 @@
 
         ReportQueryByCriteria subQuery;
         Criteria subCrit = new Criteria();
-        Criteria subCrit2 = new Criteria();
-        Criteria crit = new Criteria();
+        // we want to execute a "not in" query, so we have to guaratee none 
null
+        // results in the sub-query, else the whole sub-result will be false
+        subCrit.addNotNull("productGroupId");
 
-        subCrit2.addNotNull("productGroupId");
-        subCrit.addAndCriteria(subCrit2);
         subQuery = ojb.getQueryFactory().newReportQuery(Article.class, 
subCrit);
         subQuery.setAttributes(new String[]{"productGroupId"});
         subQuery.setDistinct(true);
 
+        Criteria crit = new Criteria();
         crit.addEqualTo("groupName", "test group " + stamp);
         crit.addNotIn("groupId", subQuery);
         Query q = ojb.getQueryFactory().newQuery(ProductGroup.class, crit);
@@ -691,7 +692,50 @@
         assertNotNull(results);
         // System.out.println("*** Found " + results.size() + " empty 
ProductGroups ***");
         assertEquals("Result of the query with sub-query does not match", 
loops, results.size());
+    }
+
+    /**
+     * test Subquery get all product groups without articles, with orderby in 
sub-query
+     * <p/>
+     * test may fail if db does not support sub queries
+     */
+    public void testSubQuery2a()
+    {
+        Collection results;
+        String stamp = "testSubQuery2a_" + System.currentTimeMillis();
+        int loops = 10;
+        // create ProductGroups without article
+        broker.beginTransaction();
+        for(int i = 0; i < loops; i++)
+        {
+            ProductGroup pg = new ProductGroup();
+            pg.setGroupName("test group " + stamp);
+            pg.setDescription("build by QueryTest#testSubQuery2");
+            broker.store(pg);
+        }
+        broker.commitTransaction();
+
+        ReportQueryByCriteria subQuery;
+        Criteria subCrit = new Criteria();
+        // we want to execute a "not in" query, so we have to guaratee none 
null
+        // results in the sub-query, else the whole sub-result will be false
+        subCrit.addNotNull("productGroupId");
+
+        subQuery = QueryFactory.newReportQuery(Article.class, subCrit);
+        subQuery.setAttributes(new String[]{"productGroupId"});
+        subQuery.setDistinct(true);
+        // this doesn't make sense in this test, it's only added to check
+        // orderby clause in sub-query
+        subQuery.addOrderBy("productGroupId", true);
 
+        Criteria crit = new Criteria();
+        crit.addEqualTo("groupName", "test group " + stamp);
+        crit.addNotIn("groupId", subQuery);
+        Query q = QueryFactory.newQuery(ProductGroup.class, crit);
+
+        results = broker.getCollectionByQuery(q);
+        assertNotNull(results);
+        assertEquals("Result of the query with sub-query does not match", 
loops, results.size());
     }
 
     /**
@@ -2111,8 +2155,6 @@
      */
     public void testReportPathExpressionForExtents1()
     {
-        ArrayList list = new ArrayList();
-
         Criteria crit = new Criteria();
         crit.addGreaterOrEqualThan("allArticlesInGroup.articleId", new 
Integer(1));
         crit.addLessOrEqualThan("allArticlesInGroup.articleId", new 
Integer(5));
@@ -2124,18 +2166,11 @@
         while (iter.hasNext())
         {
             Object row;
-                        Object[] columns;
-
-                        assertNotNull("Invalid ReportQueryIterator, hasNext() 
is true but next() is null",
-                                row = iter.next());
-                        assertTrue("ReportQuery result row is not Object[]",
-                                row instanceof Object[]);
-                        columns = (Object[]) row;
-                        list.add(columns);
-
-                        assertTrue("ReportQuery result row does not contain 
all expected columns",
-                                columns.length == 3);
-
+            Object[] columns;
+            assertNotNull("Invalid ReportQueryIterator, hasNext() is true but 
next() is null", row = iter.next());
+            assertTrue("ReportQuery result row is not Object[]", row 
instanceof Object[]);
+            columns = (Object[]) row;
+            assertTrue("ReportQuery result row does not contain all expected 
columns", columns.length == 3);
             /*
             arminw:
             think hsql returns the wrong result or interpret the query in wrong
@@ -2147,18 +2182,21 @@
 //                System.out.println("### " + ((Object[]) obj)[0]
 //                        + "  " + ((Object[]) obj)[1]
 //                        + "  " + ((Object[]) obj)[2]);
-                    Object articleId = columns[2];
-                    int i = -1;
-                    if (articleId instanceof Integer) {
-                        i = ((Integer) articleId).intValue();
-                    } else if (articleId instanceof BigDecimal) {
-                        i = ((BigDecimal) articleId).intValue();
-                    } else {
-                        assertTrue("TODO: Your platforms resulting class for 
INTEGER (" +
-                                    articleId.getClass().getName() +
-                                    ") is not yet supported in testcase.", 
false);
-                    }
-
+                Object articleId = columns[2];
+                int i = -1;
+                if (articleId instanceof Integer)
+                {
+                    i = ((Integer) articleId).intValue();
+                }
+                else if (articleId instanceof BigDecimal)
+                {
+                    i = ((BigDecimal) articleId).intValue();
+                }
+                else
+                {
+                    assertTrue("TODO: Your platforms resulting class for 
INTEGER ("
+                            + articleId.getClass().getName() + ") is not yet 
supported in testcase.", false);
+                }
                 assertTrue("i=" + i, i < 6 & i > 0);
             }
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to