From c520553a2accbfd4631e044d866c496fb55ebe3d Mon Sep 17 00:00:00 2001 From: Yubao Liu Date: Wed, 26 Oct 2022 11:36:06 +0800 Subject: [PATCH 1/4] [JDO-709] Enable Element.TYPE for @Convert This reverts commit bddffaba0384d1393b9091461ad9eaba0d9848b8 to be consistent with its javadoc. With this capability, we can avoid as much configuration file as possible, it's very convenient to mark default attribute converter for a type with Java annotation. Reference: https://issues.apache.org/jira/browse/JDO-709?focusedCommentId=17619585&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-17619585 --- api/src/main/java/javax/jdo/annotations/Convert.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/javax/jdo/annotations/Convert.java b/api/src/main/java/javax/jdo/annotations/Convert.java index 72ae3f98b..5db3b4f72 100644 --- a/api/src/main/java/javax/jdo/annotations/Convert.java +++ b/api/src/main/java/javax/jdo/annotations/Convert.java @@ -37,7 +37,7 @@ * @since 3.2 */ @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.METHOD, ElementType.FIELD}) +@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) public @interface Convert { /** From 7594de0b51fe44a467e73e784d5fee18aa90be1d Mon Sep 17 00:00:00 2001 From: Tilmann Date: Sun, 13 Nov 2022 18:29:36 +0100 Subject: [PATCH 2/4] Added TCK test --- .../PointAttributeConverterTest.java | 26 +++++ .../converter/PCRectPointTypeAnnotated.java | 101 ++++++++++++++++++ .../jdo/tck/pc/mylib/ConvertiblePoint.java | 75 +++++++++++++ .../apache/jdo/tck/pc/converter/package.jdo | 5 + .../apache/jdo/tck/pc/converter/package.jdo | 2 + .../jdo/tck/pc/converter/package-standard.orm | 4 + 6 files changed, 213 insertions(+) create mode 100644 tck/src/main/java/org/apache/jdo/tck/pc/converter/PCRectPointTypeAnnotated.java create mode 100644 tck/src/main/java/org/apache/jdo/tck/pc/mylib/ConvertiblePoint.java diff --git a/tck/src/main/java/org/apache/jdo/tck/api/converter/PointAttributeConverterTest.java b/tck/src/main/java/org/apache/jdo/tck/api/converter/PointAttributeConverterTest.java index 6f05acdb6..2dc64a126 100644 --- a/tck/src/main/java/org/apache/jdo/tck/api/converter/PointAttributeConverterTest.java +++ b/tck/src/main/java/org/apache/jdo/tck/api/converter/PointAttributeConverterTest.java @@ -25,6 +25,7 @@ import org.apache.jdo.tck.pc.converter.IPCRect; import org.apache.jdo.tck.pc.converter.PCRect; import org.apache.jdo.tck.pc.converter.PCRectAnnotated; +import org.apache.jdo.tck.pc.converter.PCRectPointTypeAnnotated; import org.apache.jdo.tck.pc.mylib.Point; import org.apache.jdo.tck.util.BatchTestRunner; import org.apache.jdo.tck.util.PointToStringConverter; @@ -111,6 +112,31 @@ public void testPCRectStringAnnotatedQueryWithStringParam() throws Exception { runQueryWithStringParameter(PCRectAnnotated.class); } + /** Test method creating and storing a PCRectStringAnnotated instance. */ + public void testStorePCRectPointTypeAnnotatedInstance() { + runStoreIPCRectInstance(PCRectPointTypeAnnotated.class); + } + + /** Test method reading a PCRectStringAnnotated instance from the datastore. */ + public void testReadPCRectPointTypeAnnotatedInstance() { + runReadIPCRectInstance(PCRectPointTypeAnnotated.class); + } + + /** Test method modifying a PCRectStringAnnotated instance and storing in the datastore. */ + public void testModifyPCRectPointTypeAnnotatedInstance() { + runModifyIPCRectInstance(PCRectPointTypeAnnotated.class); + } + + /** Test method running a PCRectStringAnnotated query with a query parameter of type String. */ + public void testPCRectPointTypeAnnotatedQueryWithPointParam() { + runQueryWithPointParameter(PCRectPointTypeAnnotated.class); + } + + /** Test method running a PCRectStringAnnotated query with a query parameter of type Point. */ + public void testPCRectPointTypeAnnotatedQueryWithStringParam() throws Exception { + runQueryWithStringParameter(PCRectPointTypeAnnotated.class); + } + // Helper methods /** diff --git a/tck/src/main/java/org/apache/jdo/tck/pc/converter/PCRectPointTypeAnnotated.java b/tck/src/main/java/org/apache/jdo/tck/pc/converter/PCRectPointTypeAnnotated.java new file mode 100644 index 000000000..fc5c0d3c6 --- /dev/null +++ b/tck/src/main/java/org/apache/jdo/tck/pc/converter/PCRectPointTypeAnnotated.java @@ -0,0 +1,101 @@ +/* + * 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 + * + * https://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.pc.converter; + +import org.apache.jdo.tck.pc.mylib.ConvertiblePoint; +import org.apache.jdo.tck.pc.mylib.Point; +import org.apache.jdo.tck.util.PointToStringConverter; + +import javax.jdo.annotations.Column; +import javax.jdo.annotations.Convert; +import javax.jdo.annotations.PersistenceCapable; +import java.util.Date; + +/** + * PersistenceCapable class to test JDO AttributeConverter interface. Its fields of type Point are + * converted to strings in the datastore. + * The conversion is declared directly on the type ConvertiblePoint. + */ +@PersistenceCapable(table = "PCRectConv") +public class PCRectPointTypeAnnotated implements IPCRect { + private static long counter = new Date().getTime(); + + private static synchronized long newId() { + return counter++; + } + + @Column(name = "ID") + private long id = newId(); + + @Column(name = "UPPER_LEFT") + private ConvertiblePoint upperLeft; + + @Column(name = "LOWER_RIGHT") + private ConvertiblePoint lowerRight; + + public PCRectPointTypeAnnotated() {} + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public Point getUpperLeft() { + return new Point(upperLeft.getX(), upperLeft.getY()); + } + + public void setUpperLeft(Point upperLeft) { + if (this.upperLeft == null) { + this.upperLeft = new ConvertiblePoint(upperLeft.getX(), upperLeft.getY()); + } else { + this.upperLeft.setX(upperLeft.getX()); + this.upperLeft.setY(upperLeft.getY()); + } + } + + public Point getLowerRight() { + return new Point(lowerRight.getX(), lowerRight.getY()); + } + + public void setLowerRight(Point lowerRight) { + if (this.lowerRight == null) { + this.lowerRight = new ConvertiblePoint(lowerRight.getX(), lowerRight.getY()); + } else { + this.lowerRight.setX(lowerRight.getX()); + this.lowerRight.setY(lowerRight.getY()); + } + } + + public String toString() { + String rc = null; + Object obj = this; + try { + rc = + obj.getClass().getName() + + " ul: " + + getUpperLeft().name() + + " lr: " + + getLowerRight().name(); + } catch (NullPointerException ex) { + rc = "NPE getting PCRectPointTypeAnnotated's values"; + } + return rc; + } +} diff --git a/tck/src/main/java/org/apache/jdo/tck/pc/mylib/ConvertiblePoint.java b/tck/src/main/java/org/apache/jdo/tck/pc/mylib/ConvertiblePoint.java new file mode 100644 index 000000000..d9f54ffe3 --- /dev/null +++ b/tck/src/main/java/org/apache/jdo/tck/pc/mylib/ConvertiblePoint.java @@ -0,0 +1,75 @@ +/* + * 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 + * + * https://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.pc.mylib; + +import org.apache.jdo.tck.util.PointToStringConverter; + +import javax.jdo.annotations.Convert; + +/** + * A simple point class with two fields. The whole class/type is declared convertible. + */ +@Convert(value = PointToStringConverter.class) +public class ConvertiblePoint { + public int x; + public Integer y; + + public ConvertiblePoint() {} + + public ConvertiblePoint(int x, int y) { + this.x = x; + this.y = y; + } + + public ConvertiblePoint(int x, Integer y) { + this.x = x; + this.y = y; + } + + public String toString() { + String rc = null; + try { + rc = "Point(" + name() + ")"; + } catch (NullPointerException ex) { + rc = "NPE getting Point's values"; + } + return rc; + } + + public int getX() { + System.out.println("Hello from Point.getX"); + return x; + } + + public Integer getY() { + System.out.println("Hello from Point.getY"); + return y; + } + + public void setX(int x) { + this.x = x; + } + + public void setY(int y) { + this.y = y; + } + + public String name() { + return "x: " + getX() + ", y: " + getY(); + } +} diff --git a/tck/src/main/resources/jdo/applicationidentity/org/apache/jdo/tck/pc/converter/package.jdo b/tck/src/main/resources/jdo/applicationidentity/org/apache/jdo/tck/pc/converter/package.jdo index b0a627f35..60baeac61 100644 --- a/tck/src/main/resources/jdo/applicationidentity/org/apache/jdo/tck/pc/converter/package.jdo +++ b/tck/src/main/resources/jdo/applicationidentity/org/apache/jdo/tck/pc/converter/package.jdo @@ -33,6 +33,11 @@ + + + + diff --git a/tck/src/main/resources/jdo/datastoreidentity/org/apache/jdo/tck/pc/converter/package.jdo b/tck/src/main/resources/jdo/datastoreidentity/org/apache/jdo/tck/pc/converter/package.jdo index 297e742f7..e33bbf5af 100644 --- a/tck/src/main/resources/jdo/datastoreidentity/org/apache/jdo/tck/pc/converter/package.jdo +++ b/tck/src/main/resources/jdo/datastoreidentity/org/apache/jdo/tck/pc/converter/package.jdo @@ -27,6 +27,7 @@ + @@ -34,6 +35,7 @@ + diff --git a/tck/src/main/resources/orm/datastoreidentity/org/apache/jdo/tck/pc/converter/package-standard.orm b/tck/src/main/resources/orm/datastoreidentity/org/apache/jdo/tck/pc/converter/package-standard.orm index 39f3b3c46..d63ca0960 100644 --- a/tck/src/main/resources/orm/datastoreidentity/org/apache/jdo/tck/pc/converter/package-standard.orm +++ b/tck/src/main/resources/orm/datastoreidentity/org/apache/jdo/tck/pc/converter/package-standard.orm @@ -32,6 +32,10 @@ + + + + From 7234192c0e8f304e8799809ca8bae380e628cb6e Mon Sep 17 00:00:00 2001 From: tzaeschke Date: Wed, 23 Nov 2022 16:23:35 +0100 Subject: [PATCH 3/4] Fixed tests --- .../PointAttributeConverterTest.java | 74 ++++++++------- .../converter/PCRectPointTypeAnnotated.java | 16 +--- .../jdo/tck/pc/mylib/ConvertiblePoint.java | 24 ++--- .../ConvertiblePointToStringConverter.java | 89 +++++++++++++++++++ .../jdo/tck/util/PointConversionCounter.java | 49 ++++++++++ .../jdo/tck/util/PointToStringConverter.java | 27 +----- .../apache/jdo/tck/pc/converter/package.jdo | 2 +- 7 files changed, 193 insertions(+), 88 deletions(-) create mode 100644 tck/src/main/java/org/apache/jdo/tck/util/ConvertiblePointToStringConverter.java create mode 100644 tck/src/main/java/org/apache/jdo/tck/util/PointConversionCounter.java diff --git a/tck/src/main/java/org/apache/jdo/tck/api/converter/PointAttributeConverterTest.java b/tck/src/main/java/org/apache/jdo/tck/api/converter/PointAttributeConverterTest.java index 2dc64a126..34d19e89a 100644 --- a/tck/src/main/java/org/apache/jdo/tck/api/converter/PointAttributeConverterTest.java +++ b/tck/src/main/java/org/apache/jdo/tck/api/converter/PointAttributeConverterTest.java @@ -26,9 +26,10 @@ import org.apache.jdo.tck.pc.converter.PCRect; import org.apache.jdo.tck.pc.converter.PCRectAnnotated; import org.apache.jdo.tck.pc.converter.PCRectPointTypeAnnotated; +import org.apache.jdo.tck.pc.mylib.ConvertiblePoint; import org.apache.jdo.tck.pc.mylib.Point; import org.apache.jdo.tck.util.BatchTestRunner; -import org.apache.jdo.tck.util.PointToStringConverter; +import org.apache.jdo.tck.util.PointConversionCounter; /** * Title:PointAttributeConverterTest
@@ -60,6 +61,7 @@ public static void main(String[] args) { protected void localSetUp() { addTearDownClass(PCRect.class); addTearDownClass(PCRectAnnotated.class); + addTearDownClass(PCRectPointTypeAnnotated.class); } /** Test method creating and storing a PCRectString instance. */ @@ -79,7 +81,7 @@ public void testModifyPCRectStringInstance() { /** Test method running a PCRectString query with a query parameter of type Point. */ public void testPCRectStringQueryWithPointParam() { - runQueryWithPointParameter(PCRect.class); + runQueryWithPointParameter(PCRect.class, false); } /** Test method running a PCRectString query with a query parameter of type String. */ @@ -104,7 +106,7 @@ public void testModifyPCRectStringAnnotatedInstance() { /** Test method running a PCRectStringAnnotated query with a query parameter of type String. */ public void testPCRectStringAnnotatedQueryWithPointParam() { - runQueryWithPointParameter(PCRectAnnotated.class); + runQueryWithPointParameter(PCRectAnnotated.class, false); } /** Test method running a PCRectStringAnnotated query with a query parameter of type Point. */ @@ -129,7 +131,7 @@ public void testModifyPCRectPointTypeAnnotatedInstance() { /** Test method running a PCRectStringAnnotated query with a query parameter of type String. */ public void testPCRectPointTypeAnnotatedQueryWithPointParam() { - runQueryWithPointParameter(PCRectPointTypeAnnotated.class); + runQueryWithPointParameter(PCRectPointTypeAnnotated.class, true); } /** Test method running a PCRectStringAnnotated query with a query parameter of type Point. */ @@ -144,17 +146,17 @@ public void testPCRectPointTypeAnnotatedQueryWithStringParam() throws Exception * convertToDatastore. */ private void runStoreIPCRectInstance(Class pcrectClass) { - int nrOfDbCalls = PointToStringConverter.getNrOfConvertToDatastoreCalls(); - int nrOfAttrCalls = PointToStringConverter.getNrOfConvertToAttributeCalls(); + int nrOfDbCalls = PointConversionCounter.getNrOfConvertToDatastoreCalls(); + int nrOfAttrCalls = PointConversionCounter.getNrOfConvertToAttributeCalls(); // Create a persistent IPCRect instance and store its oid // AttributeConverter method convertToDatastore is called when persisting instance createIPCRectInstances(pcrectClass, 1); // convertToDatastore should be called twice - assertEquals(2, PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls); + assertEquals(2, PointConversionCounter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls); // convertToAttribute should not be called - assertEquals(0, PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls); + assertEquals(0, PointConversionCounter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls); } /** @@ -175,8 +177,8 @@ private void runReadIPCRectInstance(Class pcrectClass) { pm.close(); pm = null; - nrOfDbCalls = PointToStringConverter.getNrOfConvertToDatastoreCalls(); - nrOfAttrCalls = PointToStringConverter.getNrOfConvertToAttributeCalls(); + nrOfDbCalls = PointConversionCounter.getNrOfConvertToDatastoreCalls(); + nrOfAttrCalls = PointConversionCounter.getNrOfConvertToAttributeCalls(); pm = getPM(); pm.currentTransaction().begin(); // Read the IPCRect instance from the datastore, this should call convertToAttribute @@ -186,9 +188,9 @@ private void runReadIPCRectInstance(Class pcrectClass) { pm.currentTransaction().commit(); // convertToDatastore should not be called - assertEquals(0, PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls); + assertEquals(0, PointConversionCounter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls); // convertToAttribute should be called twice - assertEquals(2, PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls); + assertEquals(2, PointConversionCounter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls); // Check the values of the associated Point instances assertEquals(UL_X, ul.getX()); assertEquals(UL_Y, ul.getY() == null ? 0 : ul.getY().intValue()); @@ -215,8 +217,8 @@ private void runModifyIPCRectInstance(Class pcrectClass) pm.close(); pm = null; - nrOfDbCalls = PointToStringConverter.getNrOfConvertToDatastoreCalls(); - nrOfAttrCalls = PointToStringConverter.getNrOfConvertToAttributeCalls(); + nrOfDbCalls = PointConversionCounter.getNrOfConvertToDatastoreCalls(); + nrOfAttrCalls = PointConversionCounter.getNrOfConvertToAttributeCalls(); pm = getPM(); tx = pm.currentTransaction(); tx.begin(); @@ -232,9 +234,9 @@ private void runModifyIPCRectInstance(Class pcrectClass) tx.commit(); // convertToDatastore should be called twice - assertEquals(2, PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls); + assertEquals(2, PointConversionCounter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls); // convertToAttribute should be called twice - assertEquals(2, PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls); + assertEquals(2, PointConversionCounter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls); } /** @@ -243,29 +245,33 @@ private void runModifyIPCRectInstance(Class pcrectClass) * * @throws Exception */ - private void runQueryWithPointParameter(Class pcrectClass) { + private void runQueryWithPointParameter(Class pcrectClass, boolean useConvertiblePoint) { int nrOfDbCalls; int nrOfAttrCalls; - nrOfDbCalls = PointToStringConverter.getNrOfConvertToDatastoreCalls(); - nrOfAttrCalls = PointToStringConverter.getNrOfConvertToAttributeCalls(); + nrOfDbCalls = PointConversionCounter.getNrOfConvertToDatastoreCalls(); + nrOfAttrCalls = PointConversionCounter.getNrOfConvertToAttributeCalls(); createIPCRectInstances(pcrectClass, 5); // convertToDatastore should be called twice per instance = 10 times - assertEquals(10, PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls); + assertEquals(10, PointConversionCounter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls); // convertToAttribute should not be called - assertEquals(0, PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls); + assertEquals(0, PointConversionCounter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls); // Cleanup the 2nd-level cache and close the pm to make sure PCRect instances are not cached pm.getPersistenceManagerFactory().getDataStoreCache().evictAll(false, pcrectClass); pm.close(); pm = null; - nrOfDbCalls = PointToStringConverter.getNrOfConvertToDatastoreCalls(); - nrOfAttrCalls = PointToStringConverter.getNrOfConvertToAttributeCalls(); + nrOfDbCalls = PointConversionCounter.getNrOfConvertToDatastoreCalls(); + nrOfAttrCalls = PointConversionCounter.getNrOfConvertToAttributeCalls(); pm = getPM(); pm.currentTransaction().begin(); try (Query q = pm.newQuery(pcrectClass, "this.upperLeft == :point")) { - q.setParameters(new Point(UL_X + 1, UL_Y + 1)); + if (useConvertiblePoint) { + q.setParameters(new ConvertiblePoint(UL_X + 1, UL_Y + 1)); + } else { + q.setParameters(new Point(UL_X + 1, UL_Y + 1)); + } // AttributeConverter method convertToAttribute is called when loading instance from the // datastore List res = q.executeList(); @@ -286,9 +292,9 @@ private void runQueryWithPointParameter(Class pcrectClass } // convertToDatastore should be called to handle the query parameter - assertTrue(PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls >= 1); + assertTrue(PointConversionCounter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls >= 1); // convertToAttribute should be called at least twice - assertTrue(PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls >= 2); + assertTrue(PointConversionCounter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls >= 2); } /** @@ -302,21 +308,21 @@ private void runQueryWithStringParameter(Class pcrectClas int nrOfDbCalls; int nrOfAttrCalls; - nrOfDbCalls = PointToStringConverter.getNrOfConvertToDatastoreCalls(); - nrOfAttrCalls = PointToStringConverter.getNrOfConvertToAttributeCalls(); + nrOfDbCalls = PointConversionCounter.getNrOfConvertToDatastoreCalls(); + nrOfAttrCalls = PointConversionCounter.getNrOfConvertToAttributeCalls(); createIPCRectInstances(pcrectClass, 5); // convertToDatastore should be called twice per instance = 10 times - assertEquals(10, PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls); + assertEquals(10, PointConversionCounter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls); // convertToAttribute should not be called - assertEquals(0, PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls); + assertEquals(0, PointConversionCounter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls); // Cleanup the 2nd-level cache and close the pm to make sure PCRect instances are not cached pm.getPersistenceManagerFactory().getDataStoreCache().evictAll(false, pcrectClass); pm.close(); pm = null; - nrOfDbCalls = PointToStringConverter.getNrOfConvertToDatastoreCalls(); - nrOfAttrCalls = PointToStringConverter.getNrOfConvertToAttributeCalls(); + nrOfDbCalls = PointConversionCounter.getNrOfConvertToDatastoreCalls(); + nrOfAttrCalls = PointConversionCounter.getNrOfConvertToAttributeCalls(); pm = getPM(); pm.currentTransaction().begin(); try (Query q = pm.newQuery(pcrectClass, "this.upperLeft == str")) { @@ -340,9 +346,9 @@ private void runQueryWithStringParameter(Class pcrectClas } // convertToDatastore should not be called - assertTrue(PointToStringConverter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls == 0); + assertTrue(PointConversionCounter.getNrOfConvertToDatastoreCalls() - nrOfDbCalls == 0); // convertToAttribute should be called at least twice - assertTrue(PointToStringConverter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls >= 2); + assertTrue(PointConversionCounter.getNrOfConvertToAttributeCalls() - nrOfAttrCalls >= 2); } /** diff --git a/tck/src/main/java/org/apache/jdo/tck/pc/converter/PCRectPointTypeAnnotated.java b/tck/src/main/java/org/apache/jdo/tck/pc/converter/PCRectPointTypeAnnotated.java index fc5c0d3c6..6bf27f6a5 100644 --- a/tck/src/main/java/org/apache/jdo/tck/pc/converter/PCRectPointTypeAnnotated.java +++ b/tck/src/main/java/org/apache/jdo/tck/pc/converter/PCRectPointTypeAnnotated.java @@ -18,7 +18,7 @@ import org.apache.jdo.tck.pc.mylib.ConvertiblePoint; import org.apache.jdo.tck.pc.mylib.Point; -import org.apache.jdo.tck.util.PointToStringConverter; +import org.apache.jdo.tck.util.ConvertiblePointToStringConverter; import javax.jdo.annotations.Column; import javax.jdo.annotations.Convert; @@ -62,12 +62,7 @@ public Point getUpperLeft() { } public void setUpperLeft(Point upperLeft) { - if (this.upperLeft == null) { - this.upperLeft = new ConvertiblePoint(upperLeft.getX(), upperLeft.getY()); - } else { - this.upperLeft.setX(upperLeft.getX()); - this.upperLeft.setY(upperLeft.getY()); - } + this.upperLeft = new ConvertiblePoint(upperLeft.getX(), upperLeft.getY()); } public Point getLowerRight() { @@ -75,12 +70,7 @@ public Point getLowerRight() { } public void setLowerRight(Point lowerRight) { - if (this.lowerRight == null) { - this.lowerRight = new ConvertiblePoint(lowerRight.getX(), lowerRight.getY()); - } else { - this.lowerRight.setX(lowerRight.getX()); - this.lowerRight.setY(lowerRight.getY()); - } + this.lowerRight = new ConvertiblePoint(lowerRight.getX(), lowerRight.getY()); } public String toString() { diff --git a/tck/src/main/java/org/apache/jdo/tck/pc/mylib/ConvertiblePoint.java b/tck/src/main/java/org/apache/jdo/tck/pc/mylib/ConvertiblePoint.java index d9f54ffe3..39cf9656a 100644 --- a/tck/src/main/java/org/apache/jdo/tck/pc/mylib/ConvertiblePoint.java +++ b/tck/src/main/java/org/apache/jdo/tck/pc/mylib/ConvertiblePoint.java @@ -17,14 +17,14 @@ package org.apache.jdo.tck.pc.mylib; -import org.apache.jdo.tck.util.PointToStringConverter; +import org.apache.jdo.tck.util.ConvertiblePointToStringConverter; import javax.jdo.annotations.Convert; /** * A simple point class with two fields. The whole class/type is declared convertible. */ -@Convert(value = PointToStringConverter.class) +@Convert(value = ConvertiblePointToStringConverter.class) public class ConvertiblePoint { public int x; public Integer y; @@ -33,7 +33,7 @@ public ConvertiblePoint() {} public ConvertiblePoint(int x, int y) { this.x = x; - this.y = y; + this.y = Integer.valueOf(y); } public ConvertiblePoint(int x, Integer y) { @@ -44,32 +44,24 @@ public ConvertiblePoint(int x, Integer y) { public String toString() { String rc = null; try { - rc = "Point(" + name() + ")"; + rc = "ConvertiblePoint(" + name() + ")"; } catch (NullPointerException ex) { - rc = "NPE getting Point's values"; + rc = "NPE getting ConvertiblePoint's values"; } return rc; } public int getX() { - System.out.println("Hello from Point.getX"); + System.out.println("Hello from ConvertiblePoint.getX"); return x; } public Integer getY() { - System.out.println("Hello from Point.getY"); + System.out.println("Hello from ConvertiblePoint.getY"); return y; } - public void setX(int x) { - this.x = x; - } - - public void setY(int y) { - this.y = y; - } - public String name() { - return "x: " + getX() + ", y: " + getY(); + return "x: " + getX() + ", y: " + getY().intValue(); } } diff --git a/tck/src/main/java/org/apache/jdo/tck/util/ConvertiblePointToStringConverter.java b/tck/src/main/java/org/apache/jdo/tck/util/ConvertiblePointToStringConverter.java new file mode 100644 index 000000000..d2a3e86f9 --- /dev/null +++ b/tck/src/main/java/org/apache/jdo/tck/util/ConvertiblePointToStringConverter.java @@ -0,0 +1,89 @@ +/* + * 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.ConvertiblePoint; + +import javax.jdo.AttributeConverter; + +/** AttributeConverter implementation mapping a Point instance to a string of the form x:y. */ +public class ConvertiblePointToStringConverter extends PointConversionCounter implements AttributeConverter { + + // Character to separate x and y value of the Point instance. + private static final String SEPARATOR = ":"; + + private final 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(ConvertiblePoint attributeValue) { + PointConversionCounter.incNrOfConvertToDatastoreCalls(); + String datastoreValue = null; + if (attributeValue != null) { + datastoreValue = + attributeValue.getX() + + SEPARATOR + + (attributeValue.getY() == null ? Integer.valueOf(0) : attributeValue.getY()); + } + if (logger.isDebugEnabled()) { + logger.debug( + "ConvertiblePointToStringConverter.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 ConvertiblePoint convertToAttribute(String datastoreValue) { + PointConversionCounter.incNrOfConvertToAttributeCalls(); + ConvertiblePoint 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 ConvertiblePoint(x == null ? 0 : x.intValue(), y); + } + } + if (logger.isDebugEnabled()) { + logger.debug( + "ConvertiblePointToStringConverter.convertToAttribute " + + "datastoreValue=" + + datastoreValue + + " attributeValue=" + + attributeValue); + } + return attributeValue; + } +} diff --git a/tck/src/main/java/org/apache/jdo/tck/util/PointConversionCounter.java b/tck/src/main/java/org/apache/jdo/tck/util/PointConversionCounter.java new file mode 100644 index 000000000..224fa8aa5 --- /dev/null +++ b/tck/src/main/java/org/apache/jdo/tck/util/PointConversionCounter.java @@ -0,0 +1,49 @@ +/* + * 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; + +public class PointConversionCounter { + + private static int nrOfConvertToDatastoreCalls = 0; + private static int nrOfConvertToAttributeCalls = 0; + + public static void incNrOfConvertToDatastoreCalls() { + nrOfConvertToDatastoreCalls++; + } + + public static void incNrOfConvertToAttributeCalls() { + nrOfConvertToAttributeCalls++; + } + + /** + * 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/java/org/apache/jdo/tck/util/PointToStringConverter.java b/tck/src/main/java/org/apache/jdo/tck/util/PointToStringConverter.java index 80524aa2f..b4af6f066 100644 --- a/tck/src/main/java/org/apache/jdo/tck/util/PointToStringConverter.java +++ b/tck/src/main/java/org/apache/jdo/tck/util/PointToStringConverter.java @@ -22,10 +22,7 @@ import org.apache.jdo.tck.pc.mylib.Point; /** AttributeConverter implementation mapping a Point instance to a string of the form x:y. */ -public class PointToStringConverter implements AttributeConverter { - - private static int nrOfConvertToDatastoreCalls = 0; - private static int nrOfConvertToAttributeCalls = 0; +public class PointToStringConverter extends PointConversionCounter implements AttributeConverter { // Character to separate x and y value of the Point instance. private static final String SEPARATOR = ":"; @@ -40,7 +37,7 @@ public class PointToStringConverter implements AttributeConverter */ @Override public String convertToDatastore(Point attributeValue) { - nrOfConvertToDatastoreCalls++; + incNrOfConvertToDatastoreCalls(); String datastoreValue = null; if (attributeValue != null) { datastoreValue = @@ -68,7 +65,7 @@ public String convertToDatastore(Point attributeValue) { */ @Override public Point convertToAttribute(String datastoreValue) { - nrOfConvertToAttributeCalls++; + incNrOfConvertToAttributeCalls(); Point attributeValue = null; if (datastoreValue != null) { String[] parts = datastoreValue.split(SEPARATOR); @@ -88,22 +85,4 @@ public Point convertToAttribute(String datastoreValue) { } 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/datastoreidentity/org/apache/jdo/tck/pc/converter/package.jdo b/tck/src/main/resources/jdo/datastoreidentity/org/apache/jdo/tck/pc/converter/package.jdo index e33bbf5af..6be2c08c8 100644 --- a/tck/src/main/resources/jdo/datastoreidentity/org/apache/jdo/tck/pc/converter/package.jdo +++ b/tck/src/main/resources/jdo/datastoreidentity/org/apache/jdo/tck/pc/converter/package.jdo @@ -27,6 +27,7 @@
+ @@ -35,7 +36,6 @@ - From beb21bff7ec5b64919cee08c1e7ba919db1bf927 Mon Sep 17 00:00:00 2001 From: Tilmann Date: Wed, 23 Nov 2022 21:47:27 +0100 Subject: [PATCH 4/4] fixed formatting --- .../api/converter/PointAttributeConverterTest.java | 3 ++- .../tck/pc/converter/PCRectPointTypeAnnotated.java | 13 +++++-------- .../apache/jdo/tck/pc/mylib/ConvertiblePoint.java | 7 ++----- .../tck/util/ConvertiblePointToStringConverter.java | 6 +++--- .../apache/jdo/tck/util/PointConversionCounter.java | 2 +- .../apache/jdo/tck/util/PointToStringConverter.java | 3 ++- 6 files changed, 15 insertions(+), 19 deletions(-) diff --git a/tck/src/main/java/org/apache/jdo/tck/api/converter/PointAttributeConverterTest.java b/tck/src/main/java/org/apache/jdo/tck/api/converter/PointAttributeConverterTest.java index 34d19e89a..c5a833610 100644 --- a/tck/src/main/java/org/apache/jdo/tck/api/converter/PointAttributeConverterTest.java +++ b/tck/src/main/java/org/apache/jdo/tck/api/converter/PointAttributeConverterTest.java @@ -245,7 +245,8 @@ private void runModifyIPCRectInstance(Class pcrectClass) * * @throws Exception */ - private void runQueryWithPointParameter(Class pcrectClass, boolean useConvertiblePoint) { + private void runQueryWithPointParameter( + Class pcrectClass, boolean useConvertiblePoint) { int nrOfDbCalls; int nrOfAttrCalls; diff --git a/tck/src/main/java/org/apache/jdo/tck/pc/converter/PCRectPointTypeAnnotated.java b/tck/src/main/java/org/apache/jdo/tck/pc/converter/PCRectPointTypeAnnotated.java index 6bf27f6a5..ee0754acd 100644 --- a/tck/src/main/java/org/apache/jdo/tck/pc/converter/PCRectPointTypeAnnotated.java +++ b/tck/src/main/java/org/apache/jdo/tck/pc/converter/PCRectPointTypeAnnotated.java @@ -16,19 +16,16 @@ */ package org.apache.jdo.tck.pc.converter; -import org.apache.jdo.tck.pc.mylib.ConvertiblePoint; -import org.apache.jdo.tck.pc.mylib.Point; -import org.apache.jdo.tck.util.ConvertiblePointToStringConverter; - +import java.util.Date; import javax.jdo.annotations.Column; -import javax.jdo.annotations.Convert; import javax.jdo.annotations.PersistenceCapable; -import java.util.Date; +import org.apache.jdo.tck.pc.mylib.ConvertiblePoint; +import org.apache.jdo.tck.pc.mylib.Point; /** * PersistenceCapable class to test JDO AttributeConverter interface. Its fields of type Point are - * converted to strings in the datastore. - * The conversion is declared directly on the type ConvertiblePoint. + * converted to strings in the datastore. The conversion is declared directly on the type + * ConvertiblePoint. */ @PersistenceCapable(table = "PCRectConv") public class PCRectPointTypeAnnotated implements IPCRect { diff --git a/tck/src/main/java/org/apache/jdo/tck/pc/mylib/ConvertiblePoint.java b/tck/src/main/java/org/apache/jdo/tck/pc/mylib/ConvertiblePoint.java index 39cf9656a..b26b30ad6 100644 --- a/tck/src/main/java/org/apache/jdo/tck/pc/mylib/ConvertiblePoint.java +++ b/tck/src/main/java/org/apache/jdo/tck/pc/mylib/ConvertiblePoint.java @@ -17,13 +17,10 @@ package org.apache.jdo.tck.pc.mylib; -import org.apache.jdo.tck.util.ConvertiblePointToStringConverter; - import javax.jdo.annotations.Convert; +import org.apache.jdo.tck.util.ConvertiblePointToStringConverter; -/** - * A simple point class with two fields. The whole class/type is declared convertible. - */ +/** A simple point class with two fields. The whole class/type is declared convertible. */ @Convert(value = ConvertiblePointToStringConverter.class) public class ConvertiblePoint { public int x; diff --git a/tck/src/main/java/org/apache/jdo/tck/util/ConvertiblePointToStringConverter.java b/tck/src/main/java/org/apache/jdo/tck/util/ConvertiblePointToStringConverter.java index d2a3e86f9..07f12091d 100644 --- a/tck/src/main/java/org/apache/jdo/tck/util/ConvertiblePointToStringConverter.java +++ b/tck/src/main/java/org/apache/jdo/tck/util/ConvertiblePointToStringConverter.java @@ -16,14 +16,14 @@ */ package org.apache.jdo.tck.util; +import javax.jdo.AttributeConverter; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.jdo.tck.pc.mylib.ConvertiblePoint; -import javax.jdo.AttributeConverter; - /** AttributeConverter implementation mapping a Point instance to a string of the form x:y. */ -public class ConvertiblePointToStringConverter extends PointConversionCounter implements AttributeConverter { +public class ConvertiblePointToStringConverter extends PointConversionCounter + implements AttributeConverter { // Character to separate x and y value of the Point instance. private static final String SEPARATOR = ":"; diff --git a/tck/src/main/java/org/apache/jdo/tck/util/PointConversionCounter.java b/tck/src/main/java/org/apache/jdo/tck/util/PointConversionCounter.java index 224fa8aa5..682bbb98c 100644 --- a/tck/src/main/java/org/apache/jdo/tck/util/PointConversionCounter.java +++ b/tck/src/main/java/org/apache/jdo/tck/util/PointConversionCounter.java @@ -29,7 +29,7 @@ public static void incNrOfConvertToAttributeCalls() { nrOfConvertToAttributeCalls++; } - /** + /** * Method returning the current number of convertToDatastore method calls. * * @return number of convertToDatastore method calls 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 index b4af6f066..38fd0ef83 100644 --- a/tck/src/main/java/org/apache/jdo/tck/util/PointToStringConverter.java +++ b/tck/src/main/java/org/apache/jdo/tck/util/PointToStringConverter.java @@ -22,7 +22,8 @@ import org.apache.jdo.tck.pc.mylib.Point; /** AttributeConverter implementation mapping a Point instance to a string of the form x:y. */ -public class PointToStringConverter extends PointConversionCounter implements AttributeConverter { +public class PointToStringConverter extends PointConversionCounter + implements AttributeConverter { // Character to separate x and y value of the Point instance. private static final String SEPARATOR = ":";