You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jdo-commits@db.apache.org by mb...@apache.org on 2021/04/28 18:45:30 UTC

[db-jdo] branch JDO-709 updated: JDO-709: Added Query Test case

This is an automated email from the ASF dual-hosted git repository.

mbo pushed a commit to branch JDO-709
in repository https://gitbox.apache.org/repos/asf/db-jdo.git


The following commit(s) were added to refs/heads/JDO-709 by this push:
     new 0678884  JDO-709: Added Query Test case
0678884 is described below

commit 0678884e5dec68c00135e7fdc497ea2438f93c2b
Author: Michael Bouschen <mb...@apache.org>
AuthorDate: Wed Apr 28 20:45:20 2021 +0200

    JDO-709: Added Query Test case
---
 .../tck/api/converter/AttributeConverterTest.java  | 124 ++++++++++++++++-----
 .../java/org/apache/jdo/tck/pc/mylib/PCRect2.java  |  12 ++
 tck/src/main/resources/conf/jdori-log4j2.xml       |   2 +-
 3 files changed, 110 insertions(+), 28 deletions(-)

diff --git a/tck/src/main/java/org/apache/jdo/tck/api/converter/AttributeConverterTest.java b/tck/src/main/java/org/apache/jdo/tck/api/converter/AttributeConverterTest.java
index b1ca78b..969d442 100644
--- a/tck/src/main/java/org/apache/jdo/tck/api/converter/AttributeConverterTest.java
+++ b/tck/src/main/java/org/apache/jdo/tck/api/converter/AttributeConverterTest.java
@@ -21,8 +21,9 @@ import org.apache.jdo.tck.pc.mylib.PCRect2;
 import org.apache.jdo.tck.pc.mylib.Point;
 import org.apache.jdo.tck.util.BatchTestRunner;
 
-import javax.jdo.PersistenceManager;
+import javax.jdo.Query;
 import javax.jdo.Transaction;
+import java.util.List;
 
 /**
  *<B>Title:</B>AttributeConverterTest
@@ -35,10 +36,10 @@ import javax.jdo.Transaction;
  */
 public class AttributeConverterTest extends JDO_Test {
 
-    private static int UL_X = 1;
-    private static int UL_Y = 10;
-    private static int LR_X = 9;
-    private static int LR_Y = 2;
+    private static final int UL_X = 1;
+    private static final int UL_Y = 10;
+    private static final int LR_X = 10;
+    private static final int LR_Y = 1;
 
     /**
      * The <code>main</code> is called when the class
@@ -60,48 +61,117 @@ public class AttributeConverterTest extends JDO_Test {
     /**
      * Test method creating a PCRect2 instance and reading it back from the datastore.
      * A PCRect2 instance refers two Point instances, that are stored as strings in the datastore.
-     * A Point instance is converted using an AttribueConverter.
+     * A Point instance is converted using an AttributeConverter.
      */
     public void testCreatePCRect2Instance() {
-        pm = getPM();
+        Transaction tx;
+        PCRect2 rect;
+        Object oid;
+        int nrOfDbCalls;
+        int nrOfAttrCalls;
 
-        // Create a persistent PCRect2 instance
-        Object oid = createPCRect2Instance(pm);
+        // Test: AttributeConverter method convertToDatastore is called when persisting instance
+        nrOfDbCalls = PCRect2.PointToStringConverter.getNrOfConvertToDatastoreCalls();
+        nrOfAttrCalls = PCRect2.PointToStringConverter.getNrOfConvertToAttributeCalls();
+        pm = getPM();
+        tx = pm.currentTransaction();
+        tx.begin();
+        // Create a persistent PCRect2 instance and store its oid
+        rect = new PCRect2(new Point(UL_X, UL_Y), new Point(LR_X, LR_Y));
+        pm.makePersistent(rect);
+        oid = pm.getObjectId(rect);
+        tx.commit();
+        // convertToDatastore should be called twice
+        assertEquals(2, PCRect2.PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls);
+        // convertToAttribute should not be called
+        assertEquals(0, PCRect2.PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls);
 
-        // cleanup the 2nd-level cache and close the pm
-        // to make sure the PCRect instance is not cached and has to be reloaded from the datastore
-        pm.getPersistenceManagerFactory().getDataStoreCache().evict(oid);
-        if (pm != null) {
-            pm.close();
-            pm = null;
-        }
+        // Cleanup the 2nd-level cache and close the pm to make sure PCRect instances are not cached
+        pm.getPersistenceManagerFactory().getDataStoreCache().evictAll(false, PCRect2.class);
+        pm.close();
+        pm = null;
 
+        // Test: AttributeConverter method convertToAttribute is called when loading instance from the datastore
+        nrOfDbCalls = PCRect2.PointToStringConverter.getNrOfConvertToDatastoreCalls();
+        nrOfAttrCalls = PCRect2.PointToStringConverter.getNrOfConvertToAttributeCalls();
         pm = getPM();
-        Transaction tx = pm.currentTransaction();
+        tx = pm.currentTransaction();
         tx.begin();
         // Read the PCRect2 instance from the datastore
-        PCRect2 rect = (PCRect2)pm.getObjectById(oid);
+        rect = (PCRect2)pm.getObjectById(oid);
         Point ul = rect.getUpperLeft();
         Point lr = rect.getLowerRight();
         tx.commit();
+        // convertToDatastore should not be called
+        assertEquals(0, PCRect2.PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls);
+        // convertToAttribute should be called twice
+        assertEquals(2, PCRect2.PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls);
 
         // Check the coordinates of the associated Point instances
         assertEquals(UL_X, ul.getX());
-        assertEquals(UL_Y, ul.getY() == null ? 0 : ul.getY().intValue());
+        assertEquals(Integer.valueOf(UL_Y), ul.getY());
         assertEquals(LR_X, lr.getX());
         assertEquals(LR_Y, lr.getY() == null ? 0 : lr.getY().intValue());
     }
 
-    private Object createPCRect2Instance(PersistenceManager pm) {
-        Point ul = new Point(UL_X, UL_Y);
-        Point lr = new Point(LR_X, LR_Y);
-        PCRect2 rect = new PCRect2(ul, lr);
-        Transaction tx = pm.currentTransaction();
+    /**
+     * Test method running a query with a Point parameter.
+     * This parameter value is converted using the AttributeConverter.
+     * @throws Exception
+     */
+    public void testQueryPCRect2Instance() throws Exception {
+        Transaction tx;
+        int nrOfDbCalls;
+        int nrOfAttrCalls;
+
+        // Test: AttributeConverter method convertToDatastore is called when persisting instances
+        nrOfDbCalls = PCRect2.PointToStringConverter.getNrOfConvertToDatastoreCalls();
+        nrOfAttrCalls = PCRect2.PointToStringConverter.getNrOfConvertToAttributeCalls();
+        pm = getPM();
+        tx = pm.currentTransaction();
         tx.begin();
-        pm.makePersistent(rect);
-        Object oid = pm.getObjectId(rect);
+        // create 5 persistent PCRect2 instances
+        for (int i = 0; i < 5; i++) {
+            pm.makePersistent(new PCRect2(new Point(UL_X + i, UL_Y + i), new Point(LR_X + i, LR_Y + i)));
+        }
         tx.commit();
-        return oid;
+        // convertToDatastore should be called twice per instance = 10 times
+        assertEquals(10, PCRect2.PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls);
+        // convertToAttribute should not be called
+        assertEquals(0, PCRect2.PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls);
+
+        // Cleanup the 2nd-level cache and close the pm to make sure PCRect instances are not cached
+        pm.getPersistenceManagerFactory().getDataStoreCache().evictAll(false, PCRect2.class);
+        pm.close();
+        pm = null;
+
+        // Test: AttributeConverter method convertToAttribute is called when loading instance from the datastore
+        nrOfDbCalls = PCRect2.PointToStringConverter.getNrOfConvertToDatastoreCalls();
+        nrOfAttrCalls = PCRect2.PointToStringConverter.getNrOfConvertToAttributeCalls();
+        pm = getPM();
+        tx = pm.currentTransaction();
+        tx.begin();
+        try (Query<PCRect2> q = pm.newQuery(PCRect2.class, "this.upperLeft == :point")) {
+            q.setParameters(new Point(UL_X+1, UL_Y+1));
+            List<PCRect2> res = q.executeList();
+            assertEquals(1, res.size());
+            PCRect2 rect = res.get(0);
+            Point ul = rect.getUpperLeft();
+            Point lr = rect.getLowerRight();
+
+            // Check the coordinates of the associated Point instances
+            assertEquals(UL_X+1, ul.getX());
+            assertEquals(UL_Y+1, ul.getY() == null ? 0 : ul.getY().intValue());
+            assertEquals(LR_X+1, lr.getX());
+            assertEquals(LR_Y+1, lr.getY() == null ? 0 : lr.getY().intValue());
+        } finally {
+            tx.commit();
+        }
+
+        // convertToDatastore should not be called to handle the query parameter
+        assertTrue(PCRect2.PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls >= 1);
+        // convertToAttribute should be called at least twice
+        assertTrue(PCRect2.PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls >= 2);
     }
 
 }
diff --git a/tck/src/main/java/org/apache/jdo/tck/pc/mylib/PCRect2.java b/tck/src/main/java/org/apache/jdo/tck/pc/mylib/PCRect2.java
index b6a21cf..772e30b 100644
--- a/tck/src/main/java/org/apache/jdo/tck/pc/mylib/PCRect2.java
+++ b/tck/src/main/java/org/apache/jdo/tck/pc/mylib/PCRect2.java
@@ -120,6 +120,9 @@ public class PCRect2 {
      */
     public static class PointToStringConverter implements AttributeConverter<Point, String> {
 
+        private static int nrOfConvertToDatastoreCalls = 0;
+        private static int nrOfConvertToAttributeCalls = 0;
+
         // Character to separate x and y value of teh Pont instance.
         private static final String SEPARATOR = ":";
 
@@ -132,6 +135,7 @@ public class PCRect2 {
          */
         @Override
         public String convertToDatastore(Point attributeValue) {
+            nrOfConvertToDatastoreCalls++;
             String datastoreValue = null;
             if (attributeValue != null) {
                 StringBuilder builder = new StringBuilder();
@@ -154,6 +158,7 @@ public class PCRect2 {
          */
         @Override
         public Point convertToAttribute(String datastoreValue) {
+            nrOfConvertToAttributeCalls++;
             Point attributeValue = null;
             if (datastoreValue != null) {
                 String[] parts = datastoreValue.split(SEPARATOR);
@@ -169,5 +174,12 @@ public class PCRect2 {
             }
             return attributeValue;
         }
+
+        public static int getNrOfConvertToDatastoreCalls() {
+            return nrOfConvertToDatastoreCalls;
+        }
+        public static int getNrOfConvertToAttributeCalls() {
+            return nrOfConvertToAttributeCalls;
+        }
     }
 }
\ No newline at end of file
diff --git a/tck/src/main/resources/conf/jdori-log4j2.xml b/tck/src/main/resources/conf/jdori-log4j2.xml
index 610d124..52fc901 100644
--- a/tck/src/main/resources/conf/jdori-log4j2.xml
+++ b/tck/src/main/resources/conf/jdori-log4j2.xml
@@ -38,7 +38,7 @@
     
     <!-- TCK Loggers -->
 
-    <Logger name="org.apache.jdo.tck" level="info" additivity="false">
+    <Logger name="org.apache.jdo.tck" level="debug" additivity="false">
       <AppenderRef ref="TCK"/>
     </Logger>
     <Logger name="org.springframework" level="error" additivity="false">