From 6b5eb3f319330e79131d93941717fc1e57ed08b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Mon, 27 Nov 2023 10:41:40 +0100 Subject: [PATCH] HHH-17466 Implement passthrough wrapping/unwrapping in LocaleJavaType --- .../type/descriptor/java/LocaleJavaType.java | 6 ++ .../type/java/AbstractDescriptorTest.java | 68 ++++++++++++++++++- .../mapping/type/java/BlobDescriptorTest.java | 13 ++++ .../java/LocaleJavaTypeDescriptorTest.java | 28 +++++++- 4 files changed, 111 insertions(+), 4 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocaleJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocaleJavaType.java index 711f9200fe..9927ffcf92 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocaleJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/LocaleJavaType.java @@ -91,6 +91,9 @@ public class LocaleJavaType extends AbstractClassJavaType { if ( value == null ) { return null; } + if ( Locale.class.isAssignableFrom( type ) ) { + return (X) value; + } if ( String.class.isAssignableFrom( type ) ) { return (X) value.toString(); } @@ -101,6 +104,9 @@ public class LocaleJavaType extends AbstractClassJavaType { if ( value == null ) { return null; } + if ( value instanceof Locale ) { + return (Locale) value; + } if (value instanceof CharSequence) { return fromString( (CharSequence) value ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/AbstractDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/AbstractDescriptorTest.java index 30a559a87a..b512eb46d8 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/AbstractDescriptorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/AbstractDescriptorTest.java @@ -8,15 +8,24 @@ package org.hibernate.orm.test.mapping.type.java; import java.io.Serializable; import java.sql.Blob; import java.sql.Clob; +import java.util.TimeZone; +import org.hibernate.engine.jdbc.LobCreator; +import org.hibernate.engine.jdbc.NonContextualLobCreator; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.engine.spi.SharedSessionContractImplementor; +import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.java.JavaType; +import org.hibernate.type.descriptor.jdbc.JdbcType; import org.junit.Before; import org.junit.Test; import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.hibernate.testing.orm.junit.JiraKey; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; /** @@ -37,6 +46,40 @@ public abstract class AbstractDescriptorTest extends BaseUnitTestCase { private final JavaType typeDescriptor; + protected final WrapperOptions wrapperOptions = new WrapperOptions() { + @Override + public SharedSessionContractImplementor getSession() { + return null; + } + + @Override + public SessionFactoryImplementor getSessionFactory() { + return null; + } + + public boolean useStreamForLobBinding() { + return false; + } + + @Override + public int getPreferredSqlTypeCodeForBoolean() { + return 0; + } + + public LobCreator getLobCreator() { + return NonContextualLobCreator.INSTANCE; + } + + public JdbcType remapSqlTypeDescriptor(JdbcType sqlTypeDescriptor) { + return sqlTypeDescriptor; + } + + @Override + public TimeZone getJdbcTimeZone() { + return null; + } + }; + public AbstractDescriptorTest(JavaType typeDescriptor) { this.typeDescriptor = typeDescriptor; } @@ -56,9 +99,15 @@ public abstract class AbstractDescriptorTest extends BaseUnitTestCase { protected abstract boolean shouldBeMutable(); + protected boolean isIdentityDifferentFromEquality() { + return true; + } + @Test public void testEquality() { - assertFalse( testData.originalValue == testData.copyOfOriginalValue ); + if ( isIdentityDifferentFromEquality() ) { + assertFalse( testData.originalValue == testData.copyOfOriginalValue ); + } assertTrue( typeDescriptor.areEqual( testData.originalValue, testData.originalValue ) ); assertTrue( typeDescriptor.areEqual( testData.originalValue, testData.copyOfOriginalValue ) ); assertFalse( typeDescriptor.areEqual( testData.originalValue, testData.differentValue ) ); @@ -72,6 +121,23 @@ public abstract class AbstractDescriptorTest extends BaseUnitTestCase { assertTrue( typeDescriptor.areEqual( testData.originalValue, consumed ) ); } + /** + * Check that wrapping/unwrapping a value that already has the expected type + * does not fail and returns a value that is considered equal. + */ + @Test + @JiraKey("HHH-17466") + public void testPassThrough() { + assertTrue( typeDescriptor.areEqual( + testData.originalValue, + typeDescriptor.wrap( testData.originalValue, wrapperOptions ) + ) ); + assertTrue( typeDescriptor.areEqual( + testData.originalValue, + typeDescriptor.unwrap( testData.originalValue, typeDescriptor.getJavaTypeClass(), wrapperOptions ) + ) ); + } + @Test public void testMutabilityPlan() { assertTrue( shouldBeMutable() == typeDescriptor.getMutabilityPlan().isMutable() ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/BlobDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/BlobDescriptorTest.java index 1e8a33c868..d3d081b9a4 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/BlobDescriptorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/BlobDescriptorTest.java @@ -6,6 +6,7 @@ */ package org.hibernate.orm.test.mapping.type.java; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -23,6 +24,8 @@ import org.hibernate.type.descriptor.java.DataHelper; import org.hibernate.type.descriptor.java.PrimitiveByteArrayJavaType; import org.junit.Test; +import org.assertj.core.api.Assertions; + /** * @author Steve Ebersole */ @@ -55,6 +58,16 @@ public class BlobDescriptorTest extends AbstractDescriptorTest { assertFalse( BlobJavaType.INSTANCE.areEqual( original, different ) ); } + @Override + public void testPassThrough() { + // blobs of the same internal value are not really comparable + // we'll just check that operations don't fail, not that the output is equal to the input + assertThatCode( () -> BlobJavaType.INSTANCE.wrap( original, wrapperOptions ) ) + .doesNotThrowAnyException(); + assertThatCode( () -> BlobJavaType.INSTANCE.unwrap( original, Blob.class, wrapperOptions ) ) + .doesNotThrowAnyException(); + } + @Test @Override public void testExternalization() { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/LocaleJavaTypeDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/LocaleJavaTypeDescriptorTest.java index 4d6bfc2124..04f2ffe770 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/LocaleJavaTypeDescriptorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/LocaleJavaTypeDescriptorTest.java @@ -11,8 +11,8 @@ import static org.junit.Assert.assertEquals; import java.util.Locale; import org.hibernate.internal.util.StringHelper; -import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.type.descriptor.java.LocaleJavaType; + import org.junit.Test; /** @@ -21,7 +21,29 @@ import org.junit.Test; * @author Christian Beikov * @author Steve Ebersole */ -public class LocaleJavaTypeDescriptorTest extends BaseUnitTestCase { +public class LocaleJavaTypeDescriptorTest extends AbstractDescriptorTest { + final Locale original = toLocale( "de", "DE", null ); + final Locale copy = toLocale( "de", "DE", null ); + final Locale different = toLocale( "de", null, null ); + + public LocaleJavaTypeDescriptorTest() { + super( LocaleJavaType.INSTANCE ); + } + + @Override + protected Data getTestData() { + return new Data<>( original, copy, different ); + } + + @Override + protected boolean shouldBeMutable() { + return false; + } + + @Override + protected boolean isIdentityDifferentFromEquality() { + return false; + } @Test public void testConversionFromString() { @@ -36,7 +58,7 @@ public class LocaleJavaTypeDescriptorTest extends BaseUnitTestCase { assertEquals( Locale.ROOT, LocaleJavaType.INSTANCE.fromString( "" ) ); } - public Locale toLocale(String lang, String region, String variant) { + private static Locale toLocale(String lang, String region, String variant) { final Locale.Builder builder = new Locale.Builder(); if ( StringHelper.isNotEmpty( lang ) ) { builder.setLanguage( lang );