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/05/01 22:24:00 UTC
[db-jdo] branch JDO-709 updated: JDO-709: Improved
AttributeConverter Test
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 4b167e0 JDO-709: Improved AttributeConverter Test
4b167e0 is described below
commit 4b167e01b86627e570bc94118d0bcd4bc5c15639
Author: Michael Bouschen <mb...@apache.org>
AuthorDate: Sun May 2 00:23:50 2021 +0200
JDO-709: Improved AttributeConverter Test
- Added separate test cases for storing and reading a PCRectString instance
- Added new test case for modifying a PCRectString instance
- AttributeConverter implementation moved from static nested class of PCRectString to util package
- Added helper method to create PCRectString instances to TCK test class
- Removed author tag
---
.../tck/api/converter/AttributeConverterTest.java | 161 ++++++++++++++-------
.../org/apache/jdo/tck/pc/mylib/PCRectString.java | 87 +----------
.../jdo/tck/util/PointToStringConverter.java | 100 +++++++++++++
.../org/apache/jdo/tck/pc/mylib/package.jdo | 4 +-
.../org/apache/jdo/tck/pc/mylib/package.jdo | 4 +-
5 files changed, 219 insertions(+), 137 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 4339d37..adc3ea2 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
@@ -20,7 +20,9 @@ import org.apache.jdo.tck.JDO_Test;
import org.apache.jdo.tck.pc.mylib.PCRectString;
import org.apache.jdo.tck.pc.mylib.Point;
import org.apache.jdo.tck.util.BatchTestRunner;
+import org.apache.jdo.tck.util.PointToStringConverter;
+import javax.jdo.JDOHelper;
import javax.jdo.Query;
import javax.jdo.Transaction;
import java.util.List;
@@ -33,6 +35,8 @@ import java.util.List;
*<B>Assertion ID:</B> [not identified]
*<BR>
*<B>Assertion Description: </B>
+ * A PCRectString instance refers two Point instances, that are stored as strings in the datastore.
+ * A Point instance is converted using an AttributeConverter.
*/
public class AttributeConverterTest extends JDO_Test {
@@ -59,100 +63,124 @@ public class AttributeConverterTest extends JDO_Test {
}
/**
- * Test method creating a PCRectString instance and reading it back from the datastore.
- * A PCRectString instance refers two Point instances, that are stored as strings in the datastore.
- * A Point instance is converted using an AttributeConverter.
+ * Test method creating a PCRectString instance.
+ * It should call AttributeConverter method convertToDatastore.
*/
- public void testCreatePCRectStringInstance() {
- Transaction tx;
+ public void testStorePCRectStringInstance() {
+ int nrOfDbCalls = PointToStringConverter.getNrOfConvertToDatastoreCalls();
+ int nrOfAttrCalls = PointToStringConverter.getNrOfConvertToAttributeCalls();
+
+ // Create a persistent PCRectString instance and store its oid
+ // AttributeConverter method convertToDatastore is called when persisting instance
+ createPCRectStringInstances(1);
+
+ // convertToDatastore should be called twice
+ assertEquals(2, PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls);
+ // convertToAttribute should not be called
+ assertEquals(0, PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls);
+ }
+
+ /**
+ * Test method reading a PCRectString instance from the datastore.
+ * It should call AttributeConverter method convertToAttribute.
+ */
+ public void testReadPCRectStringInstance() {
PCRectString rect;
Object oid;
int nrOfDbCalls;
int nrOfAttrCalls;
- // Test: AttributeConverter method convertToDatastore is called when persisting instance
- nrOfDbCalls = PCRectString.PointToStringConverter.getNrOfConvertToDatastoreCalls();
- nrOfAttrCalls = PCRectString.PointToStringConverter.getNrOfConvertToAttributeCalls();
- pm = getPM();
- tx = pm.currentTransaction();
- tx.begin();
// Create a persistent PCRectString instance and store its oid
- rect = new PCRectString(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, PCRectString.PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls);
- // convertToAttribute should not be called
- assertEquals(0, PCRectString.PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls);
+ oid = createPCRectStringInstances(1);
// Cleanup the 2nd-level cache and close the pm to make sure PCRect instances are not cached
pm.getPersistenceManagerFactory().getDataStoreCache().evictAll(false, PCRectString.class);
pm.close();
pm = null;
- // Test: AttributeConverter method convertToAttribute is called when loading instance from the datastore
- nrOfDbCalls = PCRectString.PointToStringConverter.getNrOfConvertToDatastoreCalls();
- nrOfAttrCalls = PCRectString.PointToStringConverter.getNrOfConvertToAttributeCalls();
+ nrOfDbCalls = PointToStringConverter.getNrOfConvertToDatastoreCalls();
+ nrOfAttrCalls = PointToStringConverter.getNrOfConvertToAttributeCalls();
pm = getPM();
- tx = pm.currentTransaction();
- tx.begin();
- // Read the PCRectString instance from the datastore
+ pm.currentTransaction().begin();
+ // Read the PCRectString instance from the datastore, this should call convertToAttribute
rect = (PCRectString)pm.getObjectById(oid);
Point ul = rect.getUpperLeft();
Point lr = rect.getLowerRight();
- tx.commit();
+ pm.currentTransaction().commit();
+
// convertToDatastore should not be called
- assertEquals(0, PCRectString.PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls);
+ assertEquals(0, PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls);
// convertToAttribute should be called twice
- assertEquals(2, PCRectString.PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls);
-
- // Check the coordinates of the associated Point instances
+ assertEquals(2, PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls);
+ // Check the values of the associated Point instances
assertEquals(UL_X, ul.getX());
- assertEquals(Integer.valueOf(UL_Y), ul.getY());
+ assertEquals(UL_Y, ul.getY() == null ? 0 : ul.getY().intValue());
assertEquals(LR_X, lr.getX());
assertEquals(LR_Y, lr.getY() == null ? 0 : lr.getY().intValue());
}
/**
- * Test method running a query with a Point parameter.
- * This parameter value is converted using the AttributeConverter.
- * @throws Exception
+ * Test method modifying a PCRectString instance.
+ * It should call AttributeConverter method convertToDatastore.
*/
- public void testQueryPCRectStringInstance() throws Exception {
+ public void testModifyPCRectStringInstance() {
Transaction tx;
+ PCRectString rect;
+ Object oid;
int nrOfDbCalls;
int nrOfAttrCalls;
- // Test: AttributeConverter method convertToDatastore is called when persisting instances
- nrOfDbCalls = PCRectString.PointToStringConverter.getNrOfConvertToDatastoreCalls();
- nrOfAttrCalls = PCRectString.PointToStringConverter.getNrOfConvertToAttributeCalls();
+ // Create a persistent PCRectString instance and store its oid
+ oid = createPCRectStringInstances(1);
+
+ nrOfDbCalls = PointToStringConverter.getNrOfConvertToDatastoreCalls();
+ nrOfAttrCalls = PointToStringConverter.getNrOfConvertToAttributeCalls();
pm = getPM();
tx = pm.currentTransaction();
tx.begin();
- // create 5 persistent PCRectString instances
- for (int i = 0; i < 5; i++) {
- pm.makePersistent(new PCRectString(new Point(UL_X + i, UL_Y + i), new Point(LR_X + i, LR_Y + i)));
- }
+ rect = (PCRectString)pm.getObjectById(oid);
+ // Update PCRectString instance, this should call convertToDatastore
+ rect.setUpperLeft(new Point(UL_X + 1, UL_Y + 1));
+ rect.setLowerRight(new Point(LR_X + 1, LR_Y + 1));
+ // PCRectString instance should be dirty
+ assertTrue(JDOHelper.isDirty(rect));
tx.commit();
+
+ // convertToDatastore should be called twice
+ assertEquals(2, PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls);
+ // convertToAttribute should not be called
+ assertEquals(0, PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls);
+ }
+
+ /**
+ * Test method running a query with a Point parameter.
+ * This parameter value is converted using the AttributeConverter.
+ * @throws Exception
+ */
+ public void testQueryPCRectStringInstance() throws Exception {
+ int nrOfDbCalls;
+ int nrOfAttrCalls;
+
+ nrOfDbCalls = PointToStringConverter.getNrOfConvertToDatastoreCalls();
+ nrOfAttrCalls = PointToStringConverter.getNrOfConvertToAttributeCalls();
+ createPCRectStringInstances(5);
// convertToDatastore should be called twice per instance = 10 times
- assertEquals(10, PCRectString.PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls);
+ assertEquals(10, PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls);
// convertToAttribute should not be called
- assertEquals(0, PCRectString.PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls);
+ assertEquals(0, 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, PCRectString.class);
pm.close();
pm = null;
- // Test: AttributeConverter method convertToAttribute is called when loading instance from the datastore
- nrOfDbCalls = PCRectString.PointToStringConverter.getNrOfConvertToDatastoreCalls();
- nrOfAttrCalls = PCRectString.PointToStringConverter.getNrOfConvertToAttributeCalls();
+ nrOfDbCalls = PointToStringConverter.getNrOfConvertToDatastoreCalls();
+ nrOfAttrCalls = PointToStringConverter.getNrOfConvertToAttributeCalls();
pm = getPM();
- tx = pm.currentTransaction();
- tx.begin();
+ pm.currentTransaction().begin();
try (Query<PCRectString> q = pm.newQuery(PCRectString.class, "this.upperLeft == :point")) {
- q.setParameters(new Point(UL_X+1, UL_Y+1));
+ q.setParameters(new Point(UL_X + 1, UL_Y + 1));
+ // AttributeConverter method convertToAttribute is called when loading instance from the datastore
List<PCRectString> res = q.executeList();
assertEquals(1, res.size());
PCRectString rect = res.get(0);
@@ -165,13 +193,38 @@ public class AttributeConverterTest extends JDO_Test {
assertEquals(LR_X+1, lr.getX());
assertEquals(LR_Y+1, lr.getY() == null ? 0 : lr.getY().intValue());
} finally {
- tx.commit();
+ pm.currentTransaction().commit();
}
- // convertToDatastore should not be called to handle the query parameter
- assertTrue(PCRectString.PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls >= 1);
+ // convertToDatastore should be called to handle the query parameter
+ assertTrue(PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls >= 1);
// convertToAttribute should be called at least twice
- assertTrue(PCRectString.PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls >= 2);
+ assertTrue(PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls >= 2);
+ }
+
+ /**
+ * Helper method to create PCRectString instances.
+ * @param nrOfObjects number of PCRectString instances to be created
+ * @return ObjectId of the first PCRectString instance
+ */
+ private Object createPCRectStringInstances(int nrOfObjects) {
+ PCRectString rect;
+ Object oid;
+
+ if (nrOfObjects < 1) {
+ return null;
+ }
+
+ pm = getPM();
+ pm.currentTransaction().begin();
+ rect = new PCRectString(new Point(UL_X, UL_Y), new Point(LR_X, LR_Y));
+ pm.makePersistent(rect);
+ oid = pm.getObjectId(rect);
+ for (int i = 1; i < nrOfObjects; i++) {
+ pm.makePersistent(new PCRectString(new Point(UL_X + i, UL_Y + i), new Point(LR_X + i, LR_Y + i)));
+ }
+ pm.currentTransaction().commit();
+ return oid;
}
}
diff --git a/tck/src/main/java/org/apache/jdo/tck/pc/mylib/PCRectString.java b/tck/src/main/java/org/apache/jdo/tck/pc/mylib/PCRectString.java
index 58fec35..29761ce 100644
--- a/tck/src/main/java/org/apache/jdo/tck/pc/mylib/PCRectString.java
+++ b/tck/src/main/java/org/apache/jdo/tck/pc/mylib/PCRectString.java
@@ -16,17 +16,11 @@
*/
package org.apache.jdo.tck.pc.mylib;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import javax.jdo.AttributeConverter;
import java.util.Date;
/**
* PersistenceCapable class to test JDO AttributeConverter interface.
- * It's fields of type Point are converted to strings in the datastore.
- *
- * @author Michael Bouschen
+ * Its fields of type Point are converted to strings in the datastore.
*/
public class PCRectString {
private static long counter = new Date().getTime();
@@ -39,8 +33,7 @@ public class PCRectString {
private Point upperLeft;
private Point lowerRight;
- public PCRectString() {
- }
+ public PCRectString() {}
public PCRectString(Point ul, Point lr) {
upperLeft = ul;
@@ -50,15 +43,19 @@ public class PCRectString {
public Point getUpperLeft() {
return upperLeft;
}
+ public void setUpperLeft(Point upperLeft) {
+ this.upperLeft = upperLeft;
+ }
public Point getLowerRight() {
return lowerRight;
}
-
+ public void setLowerRight(Point lowerRight) {
+ this.lowerRight = lowerRight;
+ }
public long getId() {
return id;
}
-
public void setId(long id) {
this.id = id;
}
@@ -75,72 +72,4 @@ public class PCRectString {
}
return rc;
}
-
- /**
- * AttributeConverter implementation mapping a Point instance to a string of the form x:y.
- */
- 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 the Point instance.
- private static final String SEPARATOR = ":";
-
- private Log logger = LogFactory.getFactory().getInstance("org.apache.jdo.tck");
-
- /**
- * Converts the given Point attribute value to its string representation in the datastore.
- * @param attributeValue the attribute value of type Point to be converted
- * @return the string representation of the Point instance
- */
- @Override
- public String convertToDatastore(Point attributeValue) {
- nrOfConvertToDatastoreCalls++;
- String datastoreValue = null;
- if (attributeValue != null) {
- StringBuilder builder = new StringBuilder();
- builder.append(attributeValue.getX());
- builder.append(SEPARATOR);
- builder.append(attributeValue.getY() == null ? Integer.valueOf(0) : attributeValue.getY());
- datastoreValue = builder.toString();
- }
- if (logger.isDebugEnabled()) {
- logger.debug("PointToStringConverter.convertToDatastore " +
- "attributeValue=" + attributeValue + " datastoreValue=" + datastoreValue);
- }
- return datastoreValue;
- }
-
- /**
- * Converts the given string datastore value to its representation as a persistent attribute of type Point.
- * @param datastoreValue the string value in the datastore
- * @return the attribute value as Point instance
- */
- @Override
- public Point convertToAttribute(String datastoreValue) {
- nrOfConvertToAttributeCalls++;
- Point attributeValue = null;
- if (datastoreValue != null) {
- String[] parts = datastoreValue.split(SEPARATOR);
- if (parts.length == 2) {
- Integer x = Integer.valueOf(parts[0]);
- Integer y = Integer.valueOf(parts[1]);
- attributeValue = new Point(x == null ? 0 : x.intValue(), y);
- }
- }
- if (logger.isDebugEnabled()) {
- logger.debug("PointToStringConverter.convertToAttribute " +
- "datastoreValue=" + datastoreValue + " attributeValue=" + attributeValue);
- }
- return attributeValue;
- }
-
- public static int getNrOfConvertToDatastoreCalls() {
- return nrOfConvertToDatastoreCalls;
- }
- public static int getNrOfConvertToAttributeCalls() {
- return nrOfConvertToAttributeCalls;
- }
- }
}
diff --git a/tck/src/main/java/org/apache/jdo/tck/util/PointToStringConverter.java b/tck/src/main/java/org/apache/jdo/tck/util/PointToStringConverter.java
new file mode 100644
index 0000000..eeda26f
--- /dev/null
+++ b/tck/src/main/java/org/apache/jdo/tck/util/PointToStringConverter.java
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jdo.tck.util;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jdo.tck.pc.mylib.Point;
+
+import javax.jdo.AttributeConverter;
+
+/**
+ * AttributeConverter implementation mapping a Point instance to a string of the form x:y.
+ */
+public class PointToStringConverter implements AttributeConverter<Point, String> {
+
+ private static int nrOfConvertToDatastoreCalls = 0;
+ private static int nrOfConvertToAttributeCalls = 0;
+
+ // Character to separate x and y value of the Point instance.
+ private static final String SEPARATOR = ":";
+
+ private Log logger = LogFactory.getFactory().getInstance("org.apache.jdo.tck");
+
+ /**
+ * Converts the given Point attribute value to its string representation in the datastore.
+ * @param attributeValue the attribute value of type Point to be converted
+ * @return the string representation of the Point instance
+ */
+ @Override
+ public String convertToDatastore(Point attributeValue) {
+ nrOfConvertToDatastoreCalls++;
+ String datastoreValue = null;
+ if (attributeValue != null) {
+ StringBuilder builder = new StringBuilder();
+ builder.append(attributeValue.getX());
+ builder.append(SEPARATOR);
+ builder.append(attributeValue.getY() == null ? Integer.valueOf(0) : attributeValue.getY());
+ datastoreValue = builder.toString();
+ }
+ if (logger.isDebugEnabled()) {
+ logger.debug("PointToStringConverter.convertToDatastore " +
+ "attributeValue=" + attributeValue + " datastoreValue=" + datastoreValue);
+ }
+ return datastoreValue;
+ }
+
+ /**
+ * Converts the given string datastore value to its representation as a persistent attribute of type Point.
+ * @param datastoreValue the string value in the datastore
+ * @return the attribute value as Point instance
+ */
+ @Override
+ public Point convertToAttribute(String datastoreValue) {
+ nrOfConvertToAttributeCalls++;
+ Point attributeValue = null;
+ if (datastoreValue != null) {
+ String[] parts = datastoreValue.split(SEPARATOR);
+ if (parts.length == 2) {
+ Integer x = Integer.valueOf(parts[0]);
+ Integer y = Integer.valueOf(parts[1]);
+ attributeValue = new Point(x == null ? 0 : x.intValue(), y);
+ }
+ }
+ if (logger.isDebugEnabled()) {
+ logger.debug("PointToStringConverter.convertToAttribute " +
+ "datastoreValue=" + datastoreValue + " attributeValue=" + attributeValue);
+ }
+ return attributeValue;
+ }
+
+ /**
+ * Method returning the current number of convertToDatastore method calls.
+ * @return number of convertToDatastore method calls
+ */
+ public static int getNrOfConvertToDatastoreCalls() {
+ return nrOfConvertToDatastoreCalls;
+ }
+
+ /**
+ * Method returning the current number of convertToAttribute method calls.
+ * @return number of convertToAttribute method calls
+ */
+ public static int getNrOfConvertToAttributeCalls() {
+ return nrOfConvertToAttributeCalls;
+ }
+}
diff --git a/tck/src/main/resources/jdo/applicationidentity/org/apache/jdo/tck/pc/mylib/package.jdo b/tck/src/main/resources/jdo/applicationidentity/org/apache/jdo/tck/pc/mylib/package.jdo
index f1e58d4..fe64045 100644
--- a/tck/src/main/resources/jdo/applicationidentity/org/apache/jdo/tck/pc/mylib/package.jdo
+++ b/tck/src/main/resources/jdo/applicationidentity/org/apache/jdo/tck/pc/mylib/package.jdo
@@ -55,8 +55,8 @@
<class name="PCRectString"
identity-type="application" objectid-class="javax.jdo.identity.LongIdentity">
<field name="id" primary-key="true"/>
- <field name="upperLeft" converter="org.apache.jdo.tck.pc.mylib.PCRectString$PointToStringConverter"/>
- <field name="lowerRight" converter="org.apache.jdo.tck.pc.mylib.PCRectString$PointToStringConverter"/>
+ <field name="upperLeft" converter="org.apache.jdo.tck.util.PointToStringConverter"/>
+ <field name="lowerRight" converter="org.apache.jdo.tck.util.PointToStringConverter"/>
</class>
<class name="PrimitiveTypes"
diff --git a/tck/src/main/resources/jdo/datastoreidentity/org/apache/jdo/tck/pc/mylib/package.jdo b/tck/src/main/resources/jdo/datastoreidentity/org/apache/jdo/tck/pc/mylib/package.jdo
index 9ed2a93..fdf9561 100644
--- a/tck/src/main/resources/jdo/datastoreidentity/org/apache/jdo/tck/pc/mylib/package.jdo
+++ b/tck/src/main/resources/jdo/datastoreidentity/org/apache/jdo/tck/pc/mylib/package.jdo
@@ -44,8 +44,8 @@
</class>
<class name="PCRectString" identity-type="datastore">
- <field name="upperLeft" converter="org.apache.jdo.tck.pc.mylib.PCRectString$PointToStringConverter"/>
- <field name="lowerRight" converter="org.apache.jdo.tck.pc.mylib.PCRectString$PointToStringConverter"/>
+ <field name="upperLeft" converter="org.apache.jdo.tck.util.PointToStringConverter"/>
+ <field name="lowerRight" converter="org.apache.jdo.tck.util.PointToStringConverter"/>
</class>
<class name="PrimitiveTypes" identity-type="datastore"/>