cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From abulat...@apache.org
Subject [cayenne] branch STABLE-4.1 updated: CAY-2586 Char can't be materialized to EnumType in some cases
Date Wed, 26 Jun 2019 11:57:11 GMT
This is an automated email from the ASF dual-hosted git repository.

abulatski pushed a commit to branch STABLE-4.1
in repository https://gitbox.apache.org/repos/asf/cayenne.git


The following commit(s) were added to refs/heads/STABLE-4.1 by this push:
     new f7411f2  CAY-2586 Char can't be materialized to EnumType in some cases
f7411f2 is described below

commit f7411f25e70568c5439c86d9a97f30124e2ec955
Author: Arseni Bulatski <ancarseni@gmail.com>
AuthorDate: Wed Jun 26 14:54:28 2019 +0300

    CAY-2586 Char can't be materialized to EnumType in some cases
---
 RELEASE-NOTES.txt                                  |  1 +
 .../org/apache/cayenne/access/types/EnumType.java  | 10 +--
 .../java/org/apache/cayenne/access/EnumIT.java     | 16 ++++
 .../cayenne/testdo/enum_test/EnumEntity3.java      | 28 +++++++
 .../testdo/enum_test/auto/_EnumEntity3.java        | 87 ++++++++++++++++++++++
 cayenne-server/src/test/resources/cayenne-enum.xml |  2 +-
 cayenne-server/src/test/resources/enum.map.xml     |  9 ++-
 7 files changed, 146 insertions(+), 7 deletions(-)

diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index 286e2a7..e643bfc 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -20,6 +20,7 @@ CAY-2573 DI field injection is triggered when creating sql Driver
 CAY-2580 Cgen: Can't use custom templates for client mode
 CAY-2582 Double insert of manyToMany relationship mapped to Set
 CAY-2584 Crypto: can't use ColumnSelect with encrypted columns
+CAY-2586 Char can't be materialized to EnumType in some cases
 
 ----------------------------------
 Release: 4.1.B2
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/access/types/EnumType.java b/cayenne-server/src/main/java/org/apache/cayenne/access/types/EnumType.java
index 04182b2..c9b9ce9 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/access/types/EnumType.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/access/types/EnumType.java
@@ -19,14 +19,14 @@
 
 package org.apache.cayenne.access.types;
 
-import org.apache.cayenne.ExtendedEnumeration;
-import org.apache.cayenne.dba.TypesMapping;
-
 import java.lang.reflect.Method;
 import java.sql.CallableStatement;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 
+import org.apache.cayenne.ExtendedEnumeration;
+import org.apache.cayenne.dba.TypesMapping;
+
 /**
  * An ExtendedType that handles an enum class. If Enum is mapped to a character column,
  * its name is used as persistent value; if it is mapped to a numeric column, its ordinal
@@ -87,7 +87,7 @@ public class EnumType<T extends Enum<T>> implements ExtendedType<T>
{
             return (rs.wasNull() || index < 0) ? null : values[i];
         } else {
             String string = rs.getString(index);
-            return string != null ? Enum.valueOf(enumClass, string) : null;
+            return string != null ? Enum.valueOf(enumClass, string.trim()) : null;
         }
     }
 
@@ -98,7 +98,7 @@ public class EnumType<T extends Enum<T>> implements ExtendedType<T>
{
             return (rs.wasNull() || index < 0) ? null : values[i];
         } else {
             String string = rs.getString(index);
-            return string != null ? Enum.valueOf(enumClass, string) : null;
+            return string != null ? Enum.valueOf(enumClass, string.trim()) : null;
         }
     }
 
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/access/EnumIT.java b/cayenne-server/src/test/java/org/apache/cayenne/access/EnumIT.java
index 094a4d8..b2889ae 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/access/EnumIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/access/EnumIT.java
@@ -18,10 +18,13 @@
  ****************************************************************/
 package org.apache.cayenne.access;
 
+import java.util.List;
+
 import org.apache.cayenne.Cayenne;
 import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.di.Inject;
 import org.apache.cayenne.query.CapsStrategy;
+import org.apache.cayenne.query.ObjectSelect;
 import org.apache.cayenne.query.SQLTemplate;
 import org.apache.cayenne.query.SelectQuery;
 import org.apache.cayenne.test.jdbc.DBHelper;
@@ -29,6 +32,7 @@ import org.apache.cayenne.test.jdbc.TableHelper;
 import org.apache.cayenne.testdo.enum_test.Enum1;
 import org.apache.cayenne.testdo.enum_test.EnumEntity;
 import org.apache.cayenne.testdo.enum_test.EnumEntity2;
+import org.apache.cayenne.testdo.enum_test.EnumEntity3;
 import org.apache.cayenne.unit.di.server.CayenneProjects;
 import org.apache.cayenne.unit.di.server.ServerCase;
 import org.apache.cayenne.unit.di.server.UseServerRuntime;
@@ -95,4 +99,16 @@ public class EnumIT extends ServerCase {
 
         assertEquals(Enum1.two, test.getEnumAttribute());
     }
+
+    @Test
+    public void testEnumMappedToChar() {
+        EnumEntity3 enumEntity3 = context.newObject(EnumEntity3.class);
+        enumEntity3.setEnumAttribute(Enum1.two);
+        context.commitChanges();
+
+        List<EnumEntity3> enumEntity3s = ObjectSelect.query(EnumEntity3.class)
+                .select(context);
+        assertEquals(1, enumEntity3s.size());
+        assertEquals(Enum1.two, enumEntity3s.get(0).getEnumAttribute());
+    }
 }
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/testdo/enum_test/EnumEntity3.java
b/cayenne-server/src/test/java/org/apache/cayenne/testdo/enum_test/EnumEntity3.java
new file mode 100644
index 0000000..a00b190
--- /dev/null
+++ b/cayenne-server/src/test/java/org/apache/cayenne/testdo/enum_test/EnumEntity3.java
@@ -0,0 +1,28 @@
+/*****************************************************************
+ *   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.cayenne.testdo.enum_test;
+
+import org.apache.cayenne.testdo.enum_test.auto._EnumEntity3;
+
+public class EnumEntity3 extends _EnumEntity3 {
+
+    private static final long serialVersionUID = 1L; 
+
+}
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/testdo/enum_test/auto/_EnumEntity3.java
b/cayenne-server/src/test/java/org/apache/cayenne/testdo/enum_test/auto/_EnumEntity3.java
new file mode 100644
index 0000000..7c65ab7
--- /dev/null
+++ b/cayenne-server/src/test/java/org/apache/cayenne/testdo/enum_test/auto/_EnumEntity3.java
@@ -0,0 +1,87 @@
+package org.apache.cayenne.testdo.enum_test.auto;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import org.apache.cayenne.BaseDataObject;
+import org.apache.cayenne.exp.Property;
+import org.apache.cayenne.testdo.enum_test.Enum1;
+
+/**
+ * Class _EnumEntity3 was generated by Cayenne.
+ * It is probably a good idea to avoid changing this class manually,
+ * since it may be overwritten next time code is regenerated.
+ * If you need to make any customizations, please use subclass.
+ */
+public abstract class _EnumEntity3 extends BaseDataObject {
+
+    private static final long serialVersionUID = 1L; 
+
+    public static final String ID_PK_COLUMN = "ID";
+
+    public static final Property<Enum1> ENUM_ATTRIBUTE = Property.create("enumAttribute",
Enum1.class);
+
+    protected Enum1 enumAttribute;
+
+
+    public void setEnumAttribute(Enum1 enumAttribute) {
+        beforePropertyWrite("enumAttribute", this.enumAttribute, enumAttribute);
+        this.enumAttribute = enumAttribute;
+    }
+
+    public Enum1 getEnumAttribute() {
+        beforePropertyRead("enumAttribute");
+        return this.enumAttribute;
+    }
+
+    @Override
+    public Object readPropertyDirectly(String propName) {
+        if(propName == null) {
+            throw new IllegalArgumentException();
+        }
+
+        switch(propName) {
+            case "enumAttribute":
+                return this.enumAttribute;
+            default:
+                return super.readPropertyDirectly(propName);
+        }
+    }
+
+    @Override
+    public void writePropertyDirectly(String propName, Object val) {
+        if(propName == null) {
+            throw new IllegalArgumentException();
+        }
+
+        switch (propName) {
+            case "enumAttribute":
+                this.enumAttribute = (Enum1)val;
+                break;
+            default:
+                super.writePropertyDirectly(propName, val);
+        }
+    }
+
+    private void writeObject(ObjectOutputStream out) throws IOException {
+        writeSerialized(out);
+    }
+
+    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
+        readSerialized(in);
+    }
+
+    @Override
+    protected void writeState(ObjectOutputStream out) throws IOException {
+        super.writeState(out);
+        out.writeObject(this.enumAttribute);
+    }
+
+    @Override
+    protected void readState(ObjectInputStream in) throws IOException, ClassNotFoundException
{
+        super.readState(in);
+        this.enumAttribute = (Enum1)in.readObject();
+    }
+
+}
diff --git a/cayenne-server/src/test/resources/cayenne-enum.xml b/cayenne-server/src/test/resources/cayenne-enum.xml
index de50894..8fea14b 100644
--- a/cayenne-server/src/test/resources/cayenne-enum.xml
+++ b/cayenne-server/src/test/resources/cayenne-enum.xml
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <domain xmlns="http://cayenne.apache.org/schema/10/domain"
 	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-	 xsi:schemaLocation="http://cayenne.apache.org/schema/10/domain http://cayenne.apache.org/schema/10/domain.xsd"
+	 xsi:schemaLocation="http://cayenne.apache.org/schema/10/domain https://cayenne.apache.org/schema/10/domain.xsd"
 	 project-version="10">
 	<map name="enum"/>
 </domain>
diff --git a/cayenne-server/src/test/resources/enum.map.xml b/cayenne-server/src/test/resources/enum.map.xml
index 56b340b..b4138b3 100644
--- a/cayenne-server/src/test/resources/enum.map.xml
+++ b/cayenne-server/src/test/resources/enum.map.xml
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <data-map xmlns="http://cayenne.apache.org/schema/10/modelMap"
 	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-	 xsi:schemaLocation="http://cayenne.apache.org/schema/10/modelMap http://cayenne.apache.org/schema/10/modelMap.xsd"
+	 xsi:schemaLocation="http://cayenne.apache.org/schema/10/modelMap https://cayenne.apache.org/schema/10/modelMap.xsd"
 	 project-version="10">
 	<property name="defaultPackage" value="org.apache.cayenne.testdo.enum_test"/>
 	<property name="clientSupported" value="true"/>
@@ -15,6 +15,10 @@
 		<db-attribute name="ENUM_ATTRIBUTE" type="VARCHAR" length="250"/>
 		<db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
 	</db-entity>
+	<db-entity name="ENUM_ENTITY_3">
+		<db-attribute name="ENUM_ATTRIBUTE" type="CHAR" length="5"/>
+		<db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
+	</db-entity>
 	<obj-entity name="EnumEntity" className="org.apache.cayenne.testdo.enum_test.EnumEntity"
dbEntityName="ENUM_ENTITY">
 		<obj-attribute name="enumAttribute" type="org.apache.cayenne.testdo.enum_test.Enum1"
db-attribute-path="ENUM_ATTRIBUTE"/>
 	</obj-entity>
@@ -22,4 +26,7 @@
 		<qualifier><![CDATA[enumAttribute = enum:org.apache.cayenne.testdo.enum_test.Enum1.two]]></qualifier>
 		<obj-attribute name="enumAttribute" type="org.apache.cayenne.testdo.enum_test.Enum1"
db-attribute-path="ENUM_ATTRIBUTE"/>
 	</obj-entity>
+	<obj-entity name="EnumEntity3" className="org.apache.cayenne.testdo.enum_test.EnumEntity3"
dbEntityName="ENUM_ENTITY_3">
+		<obj-attribute name="enumAttribute" type="org.apache.cayenne.testdo.enum_test.Enum1"
db-attribute-path="ENUM_ATTRIBUTE"/>
+	</obj-entity>
 </data-map>


Mime
View raw message