From eebcf6142cd1606c110006e6dbe7c45e3cefd2b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Tue, 12 Mar 2019 11:04:30 +0100 Subject: [PATCH] HHH-13266 Allow to override the SQL type mappings in AbstractJavaTimeTypeTest --- .../test/type/AbstractJavaTimeTypeTest.java | 70 +++++++++++++++++-- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/AbstractJavaTimeTypeTest.java b/hibernate-core/src/test/java/org/hibernate/test/type/AbstractJavaTimeTypeTest.java index 32b980d145..32ce3d6eac 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/AbstractJavaTimeTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/type/AbstractJavaTimeTypeTest.java @@ -19,8 +19,12 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Configuration; import org.hibernate.dialect.Dialect; +import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.PostgreSQL81Dialect; +import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; @@ -71,6 +75,14 @@ abstract class AbstractJavaTimeTypeTest extends BaseCoreFunctionalTestCase return new Class[] { getEntityType() }; } + @Override + protected void configure(Configuration configuration) { + super.configure( configuration ); + if ( env.remappingDialectClass != null ) { + configuration.setProperty( AvailableSettings.DIALECT, env.remappingDialectClass.getName() ); + } + } + protected abstract Class getEntityType(); protected abstract E createEntityForHibernateWrite(int id); @@ -195,14 +207,30 @@ abstract class AbstractJavaTimeTypeTest extends BaseCoreFunctionalTestCase } } + protected final Class getRemappingDialectClass() { + return env.remappingDialectClass; + } + protected static abstract class AbstractParametersBuilder> { private final Dialect dialect; private final List result = new ArrayList<>(); + private final List> remappingDialectClasses = new ArrayList<>(); + protected AbstractParametersBuilder() { dialect = determineDialect(); + remappingDialectClasses.add( null ); // Always test without remapping + } + + @SafeVarargs + public final S alsoTestRemappingsWithH2(Class ... dialectClasses) { + if ( dialect instanceof H2Dialect ) { + // Only test remappings with H2 + Collections.addAll( remappingDialectClasses, dialectClasses ); + } + return thisAsS(); } protected final boolean isNanosecondPrecisionSupported() { @@ -211,10 +239,12 @@ abstract class AbstractJavaTimeTypeTest extends BaseCoreFunctionalTestCase } protected final S add(ZoneId defaultJvmTimeZone, Object ... subClassParameters) { - List parameters = new ArrayList<>(); - parameters.add( new EnvironmentParameters( defaultJvmTimeZone ) ); - Collections.addAll( parameters, subClassParameters ); - result.add( parameters.toArray() ); + for ( Class remappingDialectClass : remappingDialectClasses ) { + List parameters = new ArrayList<>(); + parameters.add( new EnvironmentParameters( defaultJvmTimeZone, remappingDialectClass ) ); + Collections.addAll( parameters, subClassParameters ); + result.add( parameters.toArray() ); + } return thisAsS(); } @@ -236,13 +266,41 @@ abstract class AbstractJavaTimeTypeTest extends BaseCoreFunctionalTestCase */ private final ZoneId defaultJvmTimeZone; - private EnvironmentParameters(ZoneId defaultJvmTimeZone) { + private final Class remappingDialectClass; + + private EnvironmentParameters(ZoneId defaultJvmTimeZone, + Class remappingDialectClass) { this.defaultJvmTimeZone = defaultJvmTimeZone; + this.remappingDialectClass = remappingDialectClass; } @Override public String toString() { - return String.format( "[JVM TZ: %s]", defaultJvmTimeZone ); + return String.format( + "[JVM TZ: %s, remapping dialect: %s]", + defaultJvmTimeZone, + remappingDialectClass == null ? null : remappingDialectClass.getSimpleName() + ); + } + } + + protected static class AbstractRemappingH2Dialect extends H2Dialect { + private final int overriddenSqlTypeCode; + private final SqlTypeDescriptor overriddenSqlTypeDescriptor; + + public AbstractRemappingH2Dialect(int overriddenSqlTypeCode, SqlTypeDescriptor overriddenSqlTypeDescriptor) { + this.overriddenSqlTypeCode = overriddenSqlTypeCode; + this.overriddenSqlTypeDescriptor = overriddenSqlTypeDescriptor; + } + + @Override + protected SqlTypeDescriptor getSqlTypeDescriptorOverride(int sqlCode) { + if ( overriddenSqlTypeCode == sqlCode ) { + return overriddenSqlTypeDescriptor; + } + else { + return super.getSqlTypeDescriptorOverride( sqlCode ); + } } }