db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject svn commit: r522116 - /db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/QueryTest.java
Date Sat, 24 Mar 2007 22:08:49 GMT
Author: arminw
Date: Sat Mar 24 15:08:48 2007
New Revision: 522116

URL: http://svn.apache.org/viewvc?view=rev&rev=522116
Log:
add new tests

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

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/QueryTest.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/QueryTest.java?view=diff&rev=522116&r1=522115&r2=522116
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/QueryTest.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/broker/QueryTest.java Sat Mar
24 15:08:48 2007
@@ -30,6 +30,7 @@
 import org.apache.ojb.broker.accesslayer.sql.SqlGenerator;
 import org.apache.ojb.broker.metadata.ClassDescriptor;
 import org.apache.ojb.broker.metadata.CollectionDescriptor;
+import org.apache.ojb.broker.metadata.FieldDescriptor;
 import org.apache.ojb.broker.platforms.PlatformHsqldbImpl;
 import org.apache.ojb.broker.query.Criteria;
 import org.apache.ojb.broker.query.LikeCriteria;
@@ -37,6 +38,7 @@
 import org.apache.ojb.broker.query.QueryByCriteria;
 import org.apache.ojb.broker.query.QueryFactory;
 import org.apache.ojb.broker.query.ReportQueryByCriteria;
+import org.apache.ojb.broker.util.ObjectModification;
 import org.apache.ojb.junit.PBTestCase;
 
 /**
@@ -66,6 +68,86 @@
     }
 
     /**
+     * Execute an arbitrary sql-string based report-query.
+     */
+    public void testReportQueryByArbitrarySql()
+    {
+        String name = "testReportQueryByArbitrarySql_" + System.currentTimeMillis();
+        Article a1 = new Article();
+        a1.setArticleName(name);
+        Article a2 = new Article();
+        a2.setArticleName(name);
+        broker.beginTransaction();
+        broker.store(a1, ObjectModification.INSERT);
+        broker.store(a2, ObjectModification.INSERT);
+        broker.commitTransaction();
+        ClassDescriptor cld = broker.getClassDescriptor(Article.class);
+        FieldDescriptor fld = cld.getFieldDescriptorByName("articleName");
+
+        String sql = "select * from " + cld.getFullTableName() + " as A1 where A1." + fld.getColumnName()
+ " = '" + name + "'";
+
+        Query query = QueryFactory.newQuery(sql);
+        Iterator it = broker.getReportQueryIteratorByQuery(query);
+        boolean match = false;
+        int counter=0;
+        while(it.hasNext())
+        {
+            Object[] arr =  (Object[]) it.next();
+            //System.out.println("result: " + ArrayUtils.toString(arr));
+            for(int i = 0; i < arr.length; i++)
+            {
+                Object obj = arr[i];
+                if(obj != null && name.equals(obj.toString()))
+                {
+                    match = true;
+                    break;
+                }
+            }
+            assertTrue(match);
+            ++counter;
+        }
+        assertEquals(2, counter);
+    }
+
+    /**
+     * Compare fields of the same object in query.
+     */
+    public void testQueryEqualFields()
+    {
+        String name = "testQueryEqualFields_" + System.currentTimeMillis();
+        Person p1 = new Person();
+        p1.setFirstname("jeff");
+        p1.setLastname(name);
+
+        Person p2 = new Person();
+        p2.setFirstname(name);
+        p2.setLastname(name);
+
+        Person p3 = new Person();
+        p3.setFirstname("tom");
+        p3.setLastname(name);
+
+        broker.beginTransaction();
+        broker.store(p1);
+        broker.store(p2);
+        broker.store(p3);
+        broker.commitTransaction();
+
+        Criteria crit = new Criteria().addEqualToField("firstname", "lastname");
+        QueryByCriteria q = QueryFactory.newQuery(Person.class, crit);
+        q.setDistinct(true);
+        Collection result = broker.getCollectionByQuery(q);
+
+        assertEquals(1, result.size());
+        for(Iterator iterator = result.iterator(); iterator.hasNext();)
+        {
+            Person p = (Person) iterator.next();
+            assertEquals(name, p.getFirstname());
+            assertEquals(name, p.getLastname());
+        }
+    }
+
+    /**
      * Simple ReportQuery returning rows with 3 columns of Person
      */
     public void testReportQueryNullFields()
@@ -393,7 +475,7 @@
      */
     public void testSubQuery2()
     {
-        Collection results = null;
+        Collection results;
         String stamp = "testSubQuery2_" + System.currentTimeMillis();
         int loops = 10;
         // create ProductGroups without article



---------------------------------------------------------------------
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