From 75999ad5d93b73d6d363364c99caac2c5e104877 Mon Sep 17 00:00:00 2001 From: Maxim Solodovnik Date: Fri, 22 Mar 2019 12:21:32 +0700 Subject: [PATCH] [OPENJPA-2780] reverse customizer adds @Enumerated annotation as expected --- ...nnotationPersistenceMappingSerializer.java | 42 ++++--- .../apache/openjpa/jira2780/Jira2780Enum.java | 26 ++++ .../jira2780/Jira2780ReverseCustomizer.java | 41 +++++++ .../TestJira2780ReverseCustomizer.java | 113 ++++++++++++++++++ .../test/resources/META-INF/persistence.xml | 5 + 5 files changed, 211 insertions(+), 16 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jira2780/Jira2780Enum.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jira2780/Jira2780ReverseCustomizer.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jira2780/TestJira2780ReverseCustomizer.java diff --git a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingSerializer.java b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingSerializer.java index aa8190a12..5043ad49c 100644 --- a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingSerializer.java +++ b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingSerializer.java @@ -267,7 +267,7 @@ public class AnnotationPersistenceMappingSerializer AnnotationBuilder ab) { List cols = null; if (secondaryInfo != null) - cols = (List) secondaryInfo.getSecondaryTableJoinColumns + cols = secondaryInfo.getSecondaryTableJoinColumns (table); boolean print = (cols != null && cols.size() > 0) || @@ -322,11 +322,11 @@ public class AnnotationPersistenceMappingSerializer return true; ValueMappingInfo info = field.getValueInfo(); - List cols = (List) info.getColumns(); + List cols = info.getColumns(); if (cols == null || cols.size() == 0) return false; ValueMappingInfo info2 = field2.getValueInfo(); - List cols2 = (List) info2.getColumns(); + List cols2 = info2.getColumns(); if (cols2 == null || cols2.size() != cols.size()) return true; if (cols.size() != 1) @@ -467,7 +467,7 @@ public class AnnotationPersistenceMappingSerializer * Determine if the field is a lob. */ private boolean isLob(FieldMapping field) { - for (Column col : (List) field.getValueInfo().getColumns()) + for (Column col : field.getValueInfo().getColumns()) if (col.getType() == Types.BLOB || col.getType() == Types.CLOB) return true; return false; @@ -479,14 +479,17 @@ public class AnnotationPersistenceMappingSerializer private TemporalType getTemporal(FieldMapping field) { if (field.getDeclaredTypeCode() != JavaTypes.DATE && field.getDeclaredTypeCode() != JavaTypes.CALENDAR) + { return null; + } DBDictionary dict = ((JDBCConfiguration) getConfiguration()) .getDBDictionaryInstance(); int def = dict.getJDBCType(field.getTypeCode(), false); - for (Column col : (List) field.getValueInfo().getColumns()) { - if (col.getType() == def) + for (Column col : field.getValueInfo().getColumns()) { + if (col.getType() == def) { continue; + } switch (col.getType()) { case Types.DATE: return TemporalType.DATE; @@ -503,10 +506,14 @@ public class AnnotationPersistenceMappingSerializer * Return enum type for the field. */ protected EnumType getEnumType(FieldMapping field) { - if (field.getDeclaredTypeCode() != JavaTypes.OBJECT) + if (field.getDeclaredTypeCode() != JavaTypes.OBJECT + && field.getDeclaredTypeCode() != JavaTypes.ENUM) + { return null; - if (!(field.getHandler() instanceof EnumValueHandler)) + } + if (!(field.getHandler() instanceof EnumValueHandler)) { return null; + } return ((EnumValueHandler) field.getHandler()).getStoreOrdinal() ? EnumType.ORDINAL : EnumType.STRING; } @@ -516,24 +523,27 @@ public class AnnotationPersistenceMappingSerializer */ private void serializeColumns(MappingInfo info, ColType type, String secondary, AnnotationBuilder ab, Object meta) { - List cols = (List) info.getColumns(); - if (cols == null) + List cols = info.getColumns(); + if (cols == null) { return; + } AnnotationBuilder abContainer = ab; if (cols.size() > 1) { - Class grpType = type.getColumnGroupAnnotationType(); + Class grpType = type.getColumnGroupAnnotationType(); if (null != grpType) { AnnotationBuilder abGrp = newAnnotationBuilder(grpType); - if (null == ab) + if (null == ab) { addAnnotation(abGrp, meta); - else + } else { ab.add(null, abGrp); + } abContainer = abGrp; } } - for (Column col : cols) + for (Column col : cols) { serializeColumn(col, type, secondary, info.getUnique() != null, abContainer, meta); + } } /** @@ -637,7 +647,7 @@ public class AnnotationPersistenceMappingSerializer */ private List getQueryResultMappings(ClassMetaData cm) { if (_results == null || _results.isEmpty()) - return (List) Collections.EMPTY_LIST; + return Collections.EMPTY_LIST; List result = null; for (int i = 0; i < _results.size(); i++) { @@ -779,7 +789,7 @@ public class AnnotationPersistenceMappingSerializer protected class MappingSerializationComparator extends SerializationComparator { - + private static final long serialVersionUID = 1L; @Override diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jira2780/Jira2780Enum.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jira2780/Jira2780Enum.java new file mode 100644 index 000000000..14b2e3aaa --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jira2780/Jira2780Enum.java @@ -0,0 +1,26 @@ +/* + * 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. + */ +/* + * Copyright © NORD/LB Norddeutsche Landesbank Girozentrale, Hannover - Alle Rechte vorbehalten - + */ +package org.apache.openjpa.jira2780; + +public enum Jira2780Enum { + A, B, C; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jira2780/Jira2780ReverseCustomizer.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jira2780/Jira2780ReverseCustomizer.java new file mode 100644 index 000000000..c87c23c62 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jira2780/Jira2780ReverseCustomizer.java @@ -0,0 +1,41 @@ +/* + * Copyright © NORD/LB Norddeutsche Landesbank Girozentrale, Hannover - Alle Rechte vorbehalten - + */ +/* + * 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.openjpa.jira2780; + +import org.apache.openjpa.jdbc.meta.FieldMapping; +import org.apache.openjpa.jdbc.meta.PropertiesReverseCustomizer; +import org.apache.openjpa.jdbc.meta.strats.EnumValueHandler; + +public class Jira2780ReverseCustomizer extends PropertiesReverseCustomizer { + @Override + public void customize(FieldMapping field) { + super.customize(field); + if (field.getDeclaredType().isEnum()) { + EnumValueHandler enumValueHandler = new EnumValueHandler(); + enumValueHandler.setStoreOrdinal(false); + field.setHandler(enumValueHandler); + // As a work-around for the error, we can set the type code to + // OBJECT to generate the @Enumerated annotation. + // field.setDeclaredTypeCode(JavaTypes.OBJECT); + } + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jira2780/TestJira2780ReverseCustomizer.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jira2780/TestJira2780ReverseCustomizer.java new file mode 100644 index 000000000..cfeefd944 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jira2780/TestJira2780ReverseCustomizer.java @@ -0,0 +1,113 @@ +/* + * 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.openjpa.jira2780; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Properties; +import java.util.Scanner; + +import javax.persistence.EntityManager; +import javax.persistence.Query; + +import org.apache.openjpa.jdbc.conf.JDBCConfiguration; +import org.apache.openjpa.jdbc.meta.ReverseMappingTool; +import org.apache.openjpa.lib.util.Files; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +/** + * Tests the added useSchemaElement functionality of the + * ReverseMappingTool and CodeGenerator classes. + * + * @author Austin Dorenkamp (ajdorenk) + */ +public class TestJira2780ReverseCustomizer extends SingleEMFTestCase { + + @Override + public void setUp() throws Exception { + super.setUp(); + File f = new File("target/orm.xml"); + + // Make sure to clean up orm.xml from a prior run + if (f.exists()) { + assertTrue(f.delete()); + } + setSupportedDatabases(org.apache.openjpa.jdbc.sql.DerbyDictionary.class); + } + + @Override + public String getPersistenceUnitName(){ + return "rev-mapping-jira2780-pu"; + } + + public void testGettersAndSetters() throws Exception { + + JDBCConfiguration conf = (JDBCConfiguration) emf.getConfiguration(); + + EntityManager em = emf.createEntityManager(); + + em.getTransaction().begin(); + + Query q = em.createNativeQuery("CREATE TABLE JIRA2780.ABC (ID INTEGER PRIMARY KEY, TEST_ENUM VARCHAR(1))"); + try { + q.executeUpdate(); + em.getTransaction().commit(); + } catch (Throwable t) { + em.getTransaction().rollback(); + System.out.println(t.toString()); + } + + final String clsName = "Abc"; + ReverseMappingTool.Flags flags = new ReverseMappingTool.Flags(); + flags.metaDataLevel = "none"; + flags.generateAnnotations = true; + flags.packageName = getClass().getPackage().getName(); + flags.directory = Files.getFile("./target", null); + flags.customizer = new Jira2780ReverseCustomizer(); + Properties customProps = new Properties(); + customProps.put(flags.packageName + "." + clsName + ".testEnum.type" + , Jira2780Enum.class.getName()); + flags.customizer.setConfiguration(customProps); + ReverseMappingTool.run(conf, new String[0], flags, null); + + /* Now that the tool has been run, we will test it by reading the generated files */ + File abc = new File(Files.getPackageFile(flags.directory, flags.packageName, false) + , clsName + ".java"); + String currLine = null, prevLine; + try (Scanner inFile = new Scanner(abc)) { + while (inFile.hasNextLine()) { + prevLine = currLine; + currLine = inFile.nextLine(); + if (currLine.isEmpty() || !currLine.contains("Jira2780Enum testEnum")) { + continue; + } + if (prevLine.contains("@Enumerated(EnumType.STRING)")) { + break; + } else { + fail("@Enumerated annotation has not been injected"); + } + } + } catch (FileNotFoundException e) { + fail(clsName + ".java not generated under ./target by ReverseMappingTool"); + } + + // Delete file to clean up workspace + assertTrue(abc.delete()); + } +} diff --git a/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml b/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml index 7fd9093b0..a3073fa15 100644 --- a/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml +++ b/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml @@ -487,6 +487,11 @@ + + + + + META-INF/pudefaults-orm.xml