From 11da1ec25a8848045560b2c425327caed392b2fb Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Mon, 27 May 2024 10:59:29 +0800 Subject: [PATCH] Introduce AbstractNamedEnumTest --- ...umTest.java => AbstractNamedEnumTest.java} | 75 ++++++---------- .../orm/test/type/OracleNamedEnumTest.java | 73 +++++++++++++++ .../orm/test/type/PostgresEnumTest.java | 90 ------------------- .../orm/test/type/PostgresNamedEnumTest.java | 26 ++++++ 4 files changed, 124 insertions(+), 140 deletions(-) rename hibernate-core/src/test/java/org/hibernate/orm/test/type/{OracleEnumTest.java => AbstractNamedEnumTest.java} (68%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/type/OracleNamedEnumTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/type/PostgresEnumTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/type/PostgresNamedEnumTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/OracleEnumTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/AbstractNamedEnumTest.java similarity index 68% rename from hibernate-core/src/test/java/org/hibernate/orm/test/type/OracleEnumTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/type/AbstractNamedEnumTest.java index e1b3f7e14d..d6156bc831 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/type/OracleEnumTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/AbstractNamedEnumTest.java @@ -6,6 +6,16 @@ */ package org.hibernate.orm.test.type; +import java.sql.ResultSet; + +import org.hibernate.annotations.JdbcTypeCode; +import org.hibernate.type.SqlTypes; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -13,25 +23,16 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; -import org.hibernate.annotations.JdbcTypeCode; -import org.hibernate.dialect.OracleDialect; -import org.hibernate.testing.orm.junit.DomainModel; -import org.hibernate.testing.orm.junit.RequiresDialect; -import org.hibernate.testing.orm.junit.SessionFactory; -import org.hibernate.testing.orm.junit.SessionFactoryScope; -import org.hibernate.type.SqlTypes; -import org.junit.jupiter.api.Test; - -import java.sql.ResultSet; -import java.sql.Statement; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; +/** + * @author Yanming Zhou + */ @SessionFactory -@DomainModel(annotatedClasses = {OracleEnumTest.Timeslot.class, OracleEnumTest.Activity.class, OracleEnumTest.Weather.class, OracleEnumTest.Sky.class}) -@RequiresDialect(value = OracleDialect.class, majorVersion = 23) -public class OracleEnumTest { +@DomainModel(annotatedClasses = { AbstractNamedEnumTest.Timeslot.class, AbstractNamedEnumTest.Activity.class, AbstractNamedEnumTest.Weather.class, AbstractNamedEnumTest.Sky.class}) +public abstract class AbstractNamedEnumTest { @Test public void testNamedEnum(SessionFactoryScope scope) { Timeslot timeslot = new Timeslot(); @@ -60,49 +61,17 @@ public class OracleEnumTest { boolean namedEnumFound = false; boolean namedOrdinalEnumFound = false; - try(Statement stmt = c.createStatement()) { - try(ResultSet typeInfo = stmt.executeQuery("select name, decode(instr(data_display,'WHEN '''),0,'NUMBER','VARCHAR2') from user_domains where type='ENUMERATED'")) { - while (typeInfo.next()) { - String name = typeInfo.getString(1); - String baseType = typeInfo.getString(2); - if (name.equalsIgnoreCase("ActivityType") && baseType.equals("VARCHAR2")) { - namedEnumFound = true; - continue; - } - if (name.equalsIgnoreCase("SkyType") && baseType.equals("NUMBER")) { - namedOrdinalEnumFound = true; - continue; - } - } - } - } - - if (!namedEnumFound) { - fail("named enum type not exported"); - } - if (!namedOrdinalEnumFound) { - fail("named ordinal enum type not exported"); - } - } - ); - }); - scope.inSession( s -> { - s.doWork( - c -> { - boolean namedEnumFound = false; - boolean namedOrdinalEnumFound = false; - - ResultSet tableInfo = c.getMetaData().getColumns(null, null, "ACTIVITY", "ACTIVITYTYPE" ); + ResultSet tableInfo = c.getMetaData().getColumns(null, null, normalizeNameForQueryingMetadata("Activity"), normalizeNameForQueryingMetadata("ActivityType")); while ( tableInfo.next() ) { String type = tableInfo.getString(6); - assertEquals( "VARCHAR2", type ); + assertEquals( getDataTypeForNamedEnum( "ActivityType"), type ); namedEnumFound = true; break; } - tableInfo = c.getMetaData().getColumns(null, null, "SKY", "SKYTYPE" ); + tableInfo = c.getMetaData().getColumns(null, null, normalizeNameForQueryingMetadata("Sky"), normalizeNameForQueryingMetadata("SkyType")); while ( tableInfo.next() ) { String type = tableInfo.getString(6); - assertEquals( "NUMBER", type ); + assertEquals( getDataTypeForNamedOrdinalEnum("SkyType"), type ); namedOrdinalEnumFound = true; break; } @@ -118,6 +87,12 @@ public class OracleEnumTest { }); } + protected abstract String normalizeNameForQueryingMetadata(String name); + + protected abstract String getDataTypeForNamedEnum(String namedEnum); + + protected abstract String getDataTypeForNamedOrdinalEnum(String namedEnum); + public enum ActivityType {Work, Play, Sleep } @Entity(name = "Activity") diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/OracleNamedEnumTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/OracleNamedEnumTest.java new file mode 100644 index 0000000000..657e29d8c0 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/OracleNamedEnumTest.java @@ -0,0 +1,73 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.type; + +import org.hibernate.dialect.OracleDialect; +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.Locale; + +import static org.junit.jupiter.api.Assertions.fail; + +@RequiresDialect(value = OracleDialect.class, majorVersion = 23) +public class OracleNamedEnumTest extends AbstractNamedEnumTest { + + @Override + protected String normalizeNameForQueryingMetadata(String name) { + return name.toUpperCase(Locale.ROOT); + } + + @Override + protected String getDataTypeForNamedEnum(String namedEnum) { + return "VARCHAR2"; + } + + @Override + protected String getDataTypeForNamedOrdinalEnum(String namedEnum) { + return "NUMBER"; + } + + @Test public void testUserDomains(SessionFactoryScope scope) { + scope.inSession( s -> { + s.doWork( + c -> { + boolean namedEnumFound = false; + boolean namedOrdinalEnumFound = false; + + try(Statement stmt = c.createStatement()) { + try(ResultSet typeInfo = stmt.executeQuery("select name, decode(instr(data_display,'WHEN '''),0,'NUMBER','VARCHAR2') from user_domains where type='ENUMERATED'")) { + while (typeInfo.next()) { + String name = typeInfo.getString(1); + String baseType = typeInfo.getString(2); + if (name.equalsIgnoreCase("ActivityType") && baseType.equals( + getDataTypeForNamedEnum("ActivityType"))) { + namedEnumFound = true; + continue; + } + if (name.equalsIgnoreCase("SkyType") && baseType.equals( + getDataTypeForNamedOrdinalEnum("SkyType"))) { + namedOrdinalEnumFound = true; + } + } + } + } + + if (!namedEnumFound) { + fail("named enum type not exported"); + } + if (!namedOrdinalEnumFound) { + fail("named ordinal enum type not exported"); + } + } + ); + }); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/PostgresEnumTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/PostgresEnumTest.java deleted file mode 100644 index ea3b1b4109..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/type/PostgresEnumTest.java +++ /dev/null @@ -1,90 +0,0 @@ -package org.hibernate.orm.test.type; - -import jakarta.persistence.CascadeType; -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.ManyToOne; -import org.hibernate.annotations.JdbcTypeCode; -import org.hibernate.dialect.PostgreSQLDialect; -import org.hibernate.testing.orm.junit.DomainModel; -import org.hibernate.testing.orm.junit.RequiresDialect; -import org.hibernate.testing.orm.junit.SessionFactory; -import org.hibernate.testing.orm.junit.SessionFactoryScope; -import org.hibernate.type.SqlTypes; -import org.junit.jupiter.api.Test; - -import java.sql.ResultSet; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; - -@SessionFactory -@DomainModel(annotatedClasses = {PostgresEnumTest.Timeslot.class, PostgresEnumTest.Activity.class}) -@RequiresDialect(PostgreSQLDialect.class) -public class PostgresEnumTest { - - @Test public void test(SessionFactoryScope scope) { - Timeslot timeslot = new Timeslot(); - Activity activity = new Activity(); - activity.activityType = ActivityType.Play; - timeslot.activity = activity; - scope.inTransaction( s -> s.persist( timeslot ) ); - Timeslot ts = scope.fromTransaction( s-> s.createQuery("from Timeslot where activity.activityType = Play", Timeslot.class ).getSingleResult() ); - assertEquals( ts.activity.activityType, ActivityType.Play ); - } - - @Test public void testSchema(SessionFactoryScope scope) { - scope.inSession( s -> { - s.doWork( - c -> { - ResultSet typeInfo = c.getMetaData().getTypeInfo(); - while ( typeInfo.next() ) { - String name = typeInfo.getString(1); - if ( name.equalsIgnoreCase("ActivityType") ) { - return; - } - } - fail("named enum type not exported"); - } - ); - }); - scope.inSession( s -> { - s.doWork( - c -> { - ResultSet tableInfo = c.getMetaData().getColumns(null, null, "activity", "activitytype" ); - while ( tableInfo.next() ) { - String type = tableInfo.getString(6); - assertEquals( "activitytype", type ); - return; - } - fail("named enum column not exported"); - } - ); - }); - } - - public enum ActivityType {Work, Play, Sleep } - - @Entity(name = "Activity") - public static class Activity { - - @Id - @JdbcTypeCode(SqlTypes.NAMED_ENUM) - ActivityType activityType; - - } - - @Entity(name = "Timeslot") - public static class Timeslot { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column(name = "id") - private int id; - - @ManyToOne(cascade = CascadeType.PERSIST) - Activity activity; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/PostgresNamedEnumTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/PostgresNamedEnumTest.java new file mode 100644 index 0000000000..41a4ddc661 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/PostgresNamedEnumTest.java @@ -0,0 +1,26 @@ +package org.hibernate.orm.test.type; + +import java.util.Locale; + +import org.hibernate.dialect.PostgreSQLDialect; +import org.hibernate.testing.orm.junit.RequiresDialect; + +@RequiresDialect(PostgreSQLDialect.class) +public class PostgresNamedEnumTest extends AbstractNamedEnumTest { + + @Override + protected String normalizeNameForQueryingMetadata(String name) { + return name.toLowerCase(Locale.ROOT); + } + + @Override + protected String getDataTypeForNamedEnum(String namedEnum) { + return namedEnum.toLowerCase(Locale.ROOT); + } + + @Override + protected String getDataTypeForNamedOrdinalEnum(String namedEnum) { + return namedEnum.toLowerCase(Locale.ROOT); + } + +}