From 1f028095cf2fc99d02df8e400963bd02ba0852f1 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 22 Mar 2021 15:40:25 -0500 Subject: [PATCH] re-enable tests remove org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry re-organize some tests --- .../internal/AbstractConverterDescriptor.java | 19 +- .../ClassBasedConverterDescriptor.java | 6 +- .../InstanceBasedConverterDescriptor.java | 12 +- .../internal/NamedConverterResolution.java | 16 +- .../internal/JpaAttributeConverterImpl.java | 45 +++- .../AttributeConverterTypeAdapter.java | 6 +- .../java/JavaTypeDescriptorRegistry.java | 196 ---------------- .../java/SerializableTypeDescriptor.java | 6 +- .../spi/JavaTypeDescriptorBasicAdaptor.java | 5 + .../java/spi/JavaTypeDescriptorRegistry.java | 33 +-- .../descriptor/java/spi/RegistryHelper.java | 83 ++++--- .../ExplicitJavaTypeDescriptorTest.java | 220 +++++++++++------- .../java/BooleanTypeDescriptorTest.java | 10 +- .../java/JavaTypeDescriptorRegistryTest.java | 110 +++++++++ .../jdbc}/JdbcTypeJavaClassMappingsTest.java | 8 +- .../type/JavaTypeDescriptorRegistryTest.java | 116 --------- 16 files changed, 405 insertions(+), 486 deletions(-) delete mode 100644 hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JavaTypeDescriptorRegistry.java rename hibernate-core/src/test/java/org/hibernate/{test/converter => orm/test/mapping/converted}/ExplicitJavaTypeDescriptorTest.java (61%) rename hibernate-core/src/test/java/org/hibernate/{ => orm/test}/type/descriptor/java/BooleanTypeDescriptorTest.java (77%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/java/JavaTypeDescriptorRegistryTest.java rename hibernate-core/src/test/java/org/hibernate/{type/descriptor/sql => orm/test/type/descriptor/jdbc}/JdbcTypeJavaClassMappingsTest.java (80%) delete mode 100644 hibernate-core/src/test/java/org/hibernate/type/JavaTypeDescriptorRegistryTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/AbstractConverterDescriptor.java b/hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/AbstractConverterDescriptor.java index a3551982ee..24d6159298 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/AbstractConverterDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/AbstractConverterDescriptor.java @@ -18,6 +18,7 @@ import org.hibernate.boot.model.convert.spi.JpaAttributeConverterCreationContext import org.hibernate.metamodel.model.convert.internal.JpaAttributeConverterImpl; import org.hibernate.metamodel.model.convert.spi.JpaAttributeConverter; import org.hibernate.resource.beans.spi.ManagedBean; +import org.hibernate.type.descriptor.java.JavaTypeDescriptor; import com.fasterxml.classmate.ResolvedType; @@ -101,15 +102,23 @@ public abstract class AbstractConverterDescriptor implements ConverterDescriptor } @Override - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) public JpaAttributeConverter createJpaAttributeConverter(JpaAttributeConverterCreationContext context) { + final JavaTypeDescriptor converterJtd = context + .getJavaTypeDescriptorRegistry() + .getDescriptor( getAttributeConverterClass() ); + + final Class domainJavaType = getDomainValueResolvedType().getErasedType(); + final Class jdbcJavaType = getRelationalValueResolvedType().getErasedType(); + return new JpaAttributeConverterImpl( createManagedBean( context ), - context.getJavaTypeDescriptorRegistry().getDescriptor( getAttributeConverterClass() ), - context.getJavaTypeDescriptorRegistry().getDescriptor( getDomainValueResolvedType().getErasedType() ), - context.getJavaTypeDescriptorRegistry().getDescriptor( getRelationalValueResolvedType().getErasedType() ) + converterJtd, + domainJavaType, + jdbcJavaType, + context ); } - protected abstract ManagedBean createManagedBean(JpaAttributeConverterCreationContext context); + protected abstract ManagedBean> createManagedBean(JpaAttributeConverterCreationContext context); } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/ClassBasedConverterDescriptor.java b/hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/ClassBasedConverterDescriptor.java index d498085fa0..2df2223341 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/ClassBasedConverterDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/ClassBasedConverterDescriptor.java @@ -32,9 +32,9 @@ public class ClassBasedConverterDescriptor extends AbstractConverterDescriptor { super( converterClass, forceAutoApply, classmateContext ); } - @SuppressWarnings("unchecked") @Override - protected ManagedBean createManagedBean(JpaAttributeConverterCreationContext context) { - return context.getManagedBeanRegistry().getBean( getAttributeConverterClass() ); + protected ManagedBean> createManagedBean(JpaAttributeConverterCreationContext context) { + //noinspection unchecked + return (ManagedBean>) context.getManagedBeanRegistry().getBean( getAttributeConverterClass() ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/InstanceBasedConverterDescriptor.java b/hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/InstanceBasedConverterDescriptor.java index fc85ce8092..f391d6a078 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/InstanceBasedConverterDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/convert/internal/InstanceBasedConverterDescriptor.java @@ -20,16 +20,16 @@ import org.hibernate.resource.beans.spi.ProvidedInstanceManagedBeanImpl; * @author Steve Ebersole */ public class InstanceBasedConverterDescriptor extends AbstractConverterDescriptor { - private final AttributeConverter converterInstance; + private final AttributeConverter converterInstance; public InstanceBasedConverterDescriptor( - AttributeConverter converterInstance, + AttributeConverter converterInstance, ClassmateContext classmateContext) { this( converterInstance, null, classmateContext ); } public InstanceBasedConverterDescriptor( - AttributeConverter converterInstance, + AttributeConverter converterInstance, Boolean forceAutoApply, ClassmateContext classmateContext) { super( converterInstance.getClass(), forceAutoApply, classmateContext ); @@ -37,8 +37,8 @@ public class InstanceBasedConverterDescriptor extends AbstractConverterDescripto } @Override - @SuppressWarnings("unchecked") - protected ManagedBean createManagedBean(JpaAttributeConverterCreationContext context) { - return new ProvidedInstanceManagedBeanImpl( converterInstance ); + protected ManagedBean> createManagedBean(JpaAttributeConverterCreationContext context) { + return new ProvidedInstanceManagedBeanImpl<>( converterInstance ); } + } diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/process/internal/NamedConverterResolution.java b/hibernate-core/src/main/java/org/hibernate/boot/model/process/internal/NamedConverterResolution.java index 746371d67e..1f31cdb1ec 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/process/internal/NamedConverterResolution.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/process/internal/NamedConverterResolution.java @@ -24,6 +24,7 @@ import org.hibernate.type.descriptor.ValueExtractor; import org.hibernate.type.descriptor.converter.AttributeConverterMutabilityPlanImpl; import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; import org.hibernate.type.descriptor.java.BasicJavaDescriptor; +import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan; import org.hibernate.type.descriptor.java.JavaTypeDescriptor; import org.hibernate.type.descriptor.java.MutabilityPlan; import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; @@ -114,9 +115,18 @@ public class NamedConverterResolution implements BasicValue.Resolution { ? explicitMutabilityPlanAccess.apply( typeConfiguration ) : null; - final MutabilityPlan mutabilityPlan = explicitMutabilityPlan != null - ? explicitMutabilityPlan - : new AttributeConverterMutabilityPlanImpl( converter, true ); + + final MutabilityPlan mutabilityPlan; + if ( explicitMutabilityPlan != null ) { + mutabilityPlan = explicitMutabilityPlan; + } + else if ( domainJtd.getMutabilityPlan().isMutable() ) { + mutabilityPlan = new AttributeConverterMutabilityPlanImpl( converter, true ); + } + else { + mutabilityPlan = ImmutableMutabilityPlan.INSTANCE; + } + return new NamedConverterResolution( domainJtd, relationalJtd, diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/convert/internal/JpaAttributeConverterImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/convert/internal/JpaAttributeConverterImpl.java index 55d0e649e4..39fcc7da3c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/convert/internal/JpaAttributeConverterImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/convert/internal/JpaAttributeConverterImpl.java @@ -8,9 +8,12 @@ package org.hibernate.metamodel.model.convert.internal; import javax.persistence.AttributeConverter; +import org.hibernate.boot.model.convert.spi.JpaAttributeConverterCreationContext; import org.hibernate.metamodel.model.convert.spi.JpaAttributeConverter; import org.hibernate.resource.beans.spi.ManagedBean; import org.hibernate.type.descriptor.java.JavaTypeDescriptor; +import org.hibernate.type.descriptor.java.spi.JavaTypeDescriptorRegistry; +import org.hibernate.type.descriptor.java.spi.RegistryHelper; /** * Standard implementation of JpaAttributeConverter @@ -19,19 +22,37 @@ import org.hibernate.type.descriptor.java.JavaTypeDescriptor; */ public class JpaAttributeConverterImpl implements JpaAttributeConverter { private final ManagedBean> attributeConverterBean; - private final JavaTypeDescriptor> converterJavaTypeDescriptor; - private final JavaTypeDescriptor domainJavaTypeDescriptor; - private final JavaTypeDescriptor relationalJavaTypeDescriptor; + private final JavaTypeDescriptor> converterJtd; + private final JavaTypeDescriptor domainJtd; + private final JavaTypeDescriptor jdbcJtd; public JpaAttributeConverterImpl( ManagedBean> attributeConverterBean, - JavaTypeDescriptor> converterJavaTypeDescriptor, - JavaTypeDescriptor domainJavaTypeDescriptor, - JavaTypeDescriptor relationalJavaTypeDescriptor) { + JavaTypeDescriptor> converterJtd, + JavaTypeDescriptor domainJtd, + JavaTypeDescriptor jdbcJtd) { this.attributeConverterBean = attributeConverterBean; - this.converterJavaTypeDescriptor = converterJavaTypeDescriptor; - this.domainJavaTypeDescriptor = domainJavaTypeDescriptor; - this.relationalJavaTypeDescriptor = relationalJavaTypeDescriptor; + this.converterJtd = converterJtd; + this.domainJtd = domainJtd; + this.jdbcJtd = jdbcJtd; + } + + public JpaAttributeConverterImpl( + ManagedBean> attributeConverterBean, + JavaTypeDescriptor> converterJtd, + Class domainJavaType, + Class jdbcJavaType, + JpaAttributeConverterCreationContext context) { + this.attributeConverterBean = attributeConverterBean; + this.converterJtd = converterJtd; + + final JavaTypeDescriptorRegistry jtdRegistry = context.getJavaTypeDescriptorRegistry(); + + jdbcJtd = jtdRegistry.getDescriptor( jdbcJavaType ); + domainJtd = jtdRegistry.resolveDescriptor( + domainJavaType, + () -> RegistryHelper.INSTANCE.createTypeDescriptor( domainJavaType, context.getTypeConfiguration() ) + ); } @Override @@ -51,7 +72,7 @@ public class JpaAttributeConverterImpl implements JpaAttributeConverter> getConverterJavaTypeDescriptor() { - return converterJavaTypeDescriptor; + return converterJtd; } @Override @@ -66,11 +87,11 @@ public class JpaAttributeConverterImpl implements JpaAttributeConverter getDomainJavaTypeDescriptor() { - return domainJavaTypeDescriptor; + return domainJtd; } @Override public JavaTypeDescriptor getRelationalJavaTypeDescriptor() { - return relationalJavaTypeDescriptor; + return jdbcJtd; } } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/AttributeConverterTypeAdapter.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/AttributeConverterTypeAdapter.java index 58a76a6566..3ee98b37a4 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/AttributeConverterTypeAdapter.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/converter/AttributeConverterTypeAdapter.java @@ -61,10 +61,10 @@ public class AttributeConverterTypeAdapter extends AbstractSingleColumnStanda this.relationalJtd = relationalJtd; this.attributeConverter = attributeConverter; + // NOTE : the way that JpaAttributeConverter get built, their "domain JTD" already + // contains the proper MutabilityPlan based on whether the `@Immuatble` is present if ( mutabilityPlan == null ) { - this.mutabilityPlan = domainJtd.getMutabilityPlan().isMutable() - ? new AttributeConverterMutabilityPlanImpl<>( attributeConverter, true ) - : ImmutableMutabilityPlan.INSTANCE; + this.mutabilityPlan = (MutabilityPlan) attributeConverter.getDomainJavaTypeDescriptor().getMutabilityPlan(); } else { this.mutabilityPlan = mutabilityPlan; diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JavaTypeDescriptorRegistry.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JavaTypeDescriptorRegistry.java deleted file mode 100644 index 907ef319dc..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JavaTypeDescriptorRegistry.java +++ /dev/null @@ -1,196 +0,0 @@ -/* - * 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.type.descriptor.java; - -import java.io.Serializable; -import java.util.concurrent.ConcurrentHashMap; - -import javax.persistence.AttributeConverter; - -import org.hibernate.HibernateException; -import org.hibernate.annotations.Immutable; -import org.hibernate.annotations.Remove; -import org.hibernate.internal.CoreLogging; -import org.hibernate.internal.CoreMessageLogger; -import org.hibernate.internal.util.ReflectHelper; -import org.hibernate.type.descriptor.WrapperOptions; -import org.hibernate.type.descriptor.java.spi.RegistryHelper; -import org.hibernate.type.spi.TypeConfiguration; - -/** - * Basically a map from {@link Class} -> {@link JavaTypeDescriptor} - * - * @author Steve Ebersole - * - * @deprecated Use (5.3) Use {@link org.hibernate.type.descriptor.java.spi.JavaTypeDescriptorRegistry} instead - */ -@Deprecated -@Remove -public class JavaTypeDescriptorRegistry implements Serializable { - private static final CoreMessageLogger log = CoreLogging.messageLogger( JavaTypeDescriptorRegistry.class ); - - /** - * @deprecated (5.3) Use {@link TypeConfiguration#getJavaTypeDescriptorRegistry()} instead. - */ - @Deprecated - public static final JavaTypeDescriptorRegistry INSTANCE = new JavaTypeDescriptorRegistry(); - - private ConcurrentHashMap descriptorsByClass = new ConcurrentHashMap<>(); - - public JavaTypeDescriptorRegistry() { - addDescriptorInternal( ByteTypeDescriptor.INSTANCE ); - addDescriptorInternal( BooleanTypeDescriptor.INSTANCE ); - addDescriptorInternal( CharacterTypeDescriptor.INSTANCE ); - addDescriptorInternal( ShortTypeDescriptor.INSTANCE ); - addDescriptorInternal( IntegerTypeDescriptor.INSTANCE ); - addDescriptorInternal( LongTypeDescriptor.INSTANCE ); - addDescriptorInternal( FloatTypeDescriptor.INSTANCE ); - addDescriptorInternal( DoubleTypeDescriptor.INSTANCE ); - addDescriptorInternal( BigDecimalTypeDescriptor.INSTANCE ); - addDescriptorInternal( BigIntegerTypeDescriptor.INSTANCE ); - - addDescriptorInternal( StringTypeDescriptor.INSTANCE ); - - addDescriptorInternal( BlobTypeDescriptor.INSTANCE ); - addDescriptorInternal( ClobTypeDescriptor.INSTANCE ); - addDescriptorInternal( NClobTypeDescriptor.INSTANCE ); - - addDescriptorInternal( ByteArrayTypeDescriptor.INSTANCE ); - addDescriptorInternal( CharacterArrayTypeDescriptor.INSTANCE ); - addDescriptorInternal( PrimitiveByteArrayTypeDescriptor.INSTANCE ); - addDescriptorInternal( PrimitiveCharacterArrayTypeDescriptor.INSTANCE ); - - addDescriptorInternal( DurationJavaDescriptor.INSTANCE ); - addDescriptorInternal( InstantJavaDescriptor.INSTANCE ); - addDescriptorInternal( LocalDateJavaDescriptor.INSTANCE ); - addDescriptorInternal( LocalDateTimeJavaDescriptor.INSTANCE ); - addDescriptorInternal( OffsetDateTimeJavaDescriptor.INSTANCE ); - addDescriptorInternal( OffsetTimeJavaDescriptor.INSTANCE ); - addDescriptorInternal( ZonedDateTimeJavaDescriptor.INSTANCE ); - - addDescriptorInternal( CalendarTypeDescriptor.INSTANCE ); - descriptorsByClass.put( java.util.Date.class, JdbcTimestampTypeDescriptor.INSTANCE ); - descriptorsByClass.put( java.sql.Timestamp.class, JdbcTimestampTypeDescriptor.INSTANCE ); - descriptorsByClass.put( java.sql.Date.class, JdbcDateTypeDescriptor.INSTANCE ); - descriptorsByClass.put( java.sql.Time.class, JdbcTimeTypeDescriptor.INSTANCE ); - addDescriptorInternal( TimeZoneTypeDescriptor.INSTANCE ); - - addDescriptorInternal( ClassTypeDescriptor.INSTANCE ); - - addDescriptorInternal( CurrencyTypeDescriptor.INSTANCE ); - addDescriptorInternal( LocaleTypeDescriptor.INSTANCE ); - addDescriptorInternal( UrlTypeDescriptor.INSTANCE ); - addDescriptorInternal( UUIDTypeDescriptor.INSTANCE ); - } - - private JavaTypeDescriptor addDescriptorInternal(JavaTypeDescriptor descriptor) { - return descriptorsByClass.put( descriptor.getJavaTypeClass(), descriptor ); - } - - /** - * Adds the given descriptor to this registry - * - * @param descriptor The descriptor to add. - * - * @deprecated (5.3) Use {@link org.hibernate.type.descriptor.java.spi.JavaTypeDescriptorRegistry#addDescriptor(JavaTypeDescriptor)} instead. - */ - @Deprecated - public void addDescriptor(JavaTypeDescriptor descriptor) { - JavaTypeDescriptor old = addDescriptorInternal( descriptor ); - if ( old != null ) { - log.debugf( - "JavaTypeDescriptorRegistry entry replaced : %s -> %s (was %s)", - descriptor.getJavaType(), - descriptor, - old - ); - } - } - - /** - * @deprecated (5.3) Use {@link org.hibernate.type.descriptor.java.spi.JavaTypeDescriptorRegistry#getDescriptor(Class)} instead. - */ - @Deprecated - @SuppressWarnings("unchecked") - public JavaTypeDescriptor getDescriptor(Class cls) { - return RegistryHelper.INSTANCE.resolveDescriptor( - descriptorsByClass, - cls, - () -> { - if ( Serializable.class.isAssignableFrom( cls ) ) { - return new SerializableTypeDescriptor( cls ); - } - - if ( !AttributeConverter.class.isAssignableFrom( cls ) ) { - log.debugf( - "Could not find matching JavaTypeDescriptor for requested Java class [%s]; using fallback. " + - "This means Hibernate does not know how to perform certain basic operations in relation to this Java type.", - cls.getName() - ); - checkEqualsAndHashCode( cls ); - } - - return new FallbackJavaTypeDescriptor<>( cls ); - } - ); - } - - @SuppressWarnings("unchecked") - private void checkEqualsAndHashCode(Class javaType) { - if ( !ReflectHelper.overridesEquals( javaType ) || !ReflectHelper.overridesHashCode( javaType ) ) { - log.unknownJavaTypeNoEqualsHashCode( javaType ); - } - } - - - public static class FallbackJavaTypeDescriptor extends AbstractClassTypeDescriptor { - protected FallbackJavaTypeDescriptor(final Class type) { - super( type, createMutabilityPlan( type ) ); - } - - @SuppressWarnings("unchecked") - private static MutabilityPlan createMutabilityPlan(final Class type) { - if ( type.isAnnotationPresent( Immutable.class ) ) { - return ImmutableMutabilityPlan.INSTANCE; - } - // MutableMutabilityPlan is the "safest" option, but we do not necessarily know how to deepCopy etc... - return new MutableMutabilityPlan() { - @Override - protected T deepCopyNotNull(T value) { - throw new HibernateException( - "Not known how to deep copy value of type: [" + type - .getName() + "]" - ); - } - }; - } - - @Override - public String toString(T value) { - return value == null ? "" : value.toString(); - } - - @Override - public T fromString(String string) { - throw new HibernateException( - "Not known how to convert String to given type [" + getJavaType().getTypeName() + "]" - ); - } - - @Override - @SuppressWarnings("unchecked") - public X unwrap(T value, Class type, WrapperOptions options) { - return (X) value; - } - - @Override - @SuppressWarnings("unchecked") - public T wrap(X value, WrapperOptions options) { - return (T) value; - } - } -} diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/SerializableTypeDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/SerializableTypeDescriptor.java index 47cef95490..466b671169 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/SerializableTypeDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/SerializableTypeDescriptor.java @@ -44,7 +44,11 @@ public class SerializableTypeDescriptor extends Abstract } public SerializableTypeDescriptor(Class type) { - super( type, createMutabilityPlan( type ) ); + this( type, createMutabilityPlan( type ) ); + } + + public SerializableTypeDescriptor(Class type, MutabilityPlan mutabilityPlan) { + super( type, mutabilityPlan == null ? createMutabilityPlan( type ) : mutabilityPlan ); } @SuppressWarnings({ "unchecked" }) diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JavaTypeDescriptorBasicAdaptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JavaTypeDescriptorBasicAdaptor.java index 80df1500d4..89a44e0ff1 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JavaTypeDescriptorBasicAdaptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JavaTypeDescriptorBasicAdaptor.java @@ -8,6 +8,7 @@ package org.hibernate.type.descriptor.java.spi; import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.java.AbstractClassTypeDescriptor; +import org.hibernate.type.descriptor.java.MutabilityPlan; import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; import org.hibernate.type.descriptor.sql.SqlTypeDescriptorIndicators; @@ -22,6 +23,10 @@ public class JavaTypeDescriptorBasicAdaptor extends AbstractClassTypeDescript super( type ); } + public JavaTypeDescriptorBasicAdaptor(Class type, MutabilityPlan mutabilityPlan) { + super( type, mutabilityPlan ); + } + @Override public SqlTypeDescriptor getJdbcRecommendedSqlType(SqlTypeDescriptorIndicators context) { throw new UnsupportedOperationException( diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JavaTypeDescriptorRegistry.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JavaTypeDescriptorRegistry.java index 05de543b5a..12196dea23 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JavaTypeDescriptorRegistry.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/JavaTypeDescriptorRegistry.java @@ -12,9 +12,16 @@ import java.lang.reflect.Type; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Supplier; +import org.hibernate.annotations.Immutable; +import org.hibernate.annotations.Mutability; +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; +import org.hibernate.resource.beans.spi.ManagedBean; +import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.type.descriptor.java.AbstractClassTypeDescriptor; import org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor; +import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan; import org.hibernate.type.descriptor.java.JavaTypeDescriptor; +import org.hibernate.type.descriptor.java.MutabilityPlan; import org.hibernate.type.descriptor.java.SerializableTypeDescriptor; import org.hibernate.type.spi.TypeConfiguration; import org.hibernate.type.spi.TypeConfigurationAware; @@ -35,7 +42,6 @@ public class JavaTypeDescriptorRegistry implements JavaTypeDescriptorBaseline.Ba private final TypeConfiguration typeConfiguration; private final ConcurrentHashMap> descriptorsByType = new ConcurrentHashMap<>(); - @SuppressWarnings("unused") public JavaTypeDescriptorRegistry(TypeConfiguration typeConfiguration) { this.typeConfiguration = typeConfiguration; JavaTypeDescriptorBaseline.prime( this ); @@ -131,8 +137,6 @@ public class JavaTypeDescriptorRegistry implements JavaTypeDescriptorBaseline.Ba return resolveDescriptor( javaType, () -> { - // the fallback will always be a basic type - final AbstractClassTypeDescriptor fallbackDescriptor; final Class javaTypeClass; if ( javaType instanceof Class ) { javaTypeClass = (Class) javaType; @@ -142,25 +146,10 @@ public class JavaTypeDescriptorRegistry implements JavaTypeDescriptorBaseline.Ba javaTypeClass = (Class) parameterizedType.getRawType(); } - if ( javaTypeClass.isEnum() ) { - //noinspection rawtypes - fallbackDescriptor = new EnumJavaTypeDescriptor( javaTypeClass ); - } - else if ( Serializable.class.isAssignableFrom( javaTypeClass ) ) { - //noinspection rawtypes - fallbackDescriptor = new SerializableTypeDescriptor( javaTypeClass ); - } - else { - //noinspection rawtypes - fallbackDescriptor = new JavaTypeDescriptorBasicAdaptor( javaTypeClass ); - } - - // todo (6.0) : here we assume that all temporal type descriptors are registered - // ahead of time. Allow for on-the-fly temporal types? The 2 impediments for that are: - // 1) How can we recognize non-JDK date/time types? - // 2) What is the temporal precision for the types we have deemed temporal? - - return fallbackDescriptor; + return RegistryHelper.INSTANCE.createTypeDescriptor( + javaTypeClass, + typeConfiguration + ); } ); } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/RegistryHelper.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/RegistryHelper.java index d1e12f4d1c..5979072f6c 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/RegistryHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/spi/RegistryHelper.java @@ -6,19 +6,26 @@ */ package org.hibernate.type.descriptor.java.spi; -import java.util.Map; -import java.util.function.Supplier; +import java.io.Serializable; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.function.Function; +import org.hibernate.annotations.Immutable; +import org.hibernate.annotations.Mutability; +import org.hibernate.resource.beans.spi.ManagedBean; +import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.type.descriptor.java.EnumJavaTypeDescriptor; +import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan; import org.hibernate.type.descriptor.java.JavaTypeDescriptor; - -import org.jboss.logging.Logger; +import org.hibernate.type.descriptor.java.MutabilityPlan; +import org.hibernate.type.descriptor.java.SerializableTypeDescriptor; +import org.hibernate.type.spi.TypeConfiguration; /** * @author Steve Ebersole */ public class RegistryHelper { - private static final Logger log = Logger.getLogger( RegistryHelper.class ); /** * Singleton access @@ -29,33 +36,55 @@ public class RegistryHelper { } @SuppressWarnings("unchecked") - public JavaTypeDescriptor resolveDescriptor( - Map descriptorsByClass, - Class cls, - Supplier> defaultValueSupplier) { - if ( cls == null ) { - throw new IllegalArgumentException( "Class passed to locate JavaTypeDescriptor cannot be null" ); + public JavaTypeDescriptor createTypeDescriptor(Type javaType, TypeConfiguration typeConfiguration) { + return createTypeDescriptor( + javaType, + (javaTypeClass) -> { + if ( javaTypeClass.isAnnotationPresent( Immutable.class ) ) { + return ImmutableMutabilityPlan.INSTANCE; + } + + if ( javaTypeClass.isAnnotationPresent( Mutability.class ) ) { + final Mutability annotation = javaTypeClass.getAnnotation( Mutability.class ); + final Class> planClass = annotation.value(); + final ManagedBeanRegistry managedBeanRegistry = typeConfiguration + .getServiceRegistry() + .getService( ManagedBeanRegistry.class ); + final ManagedBean> planBean = managedBeanRegistry.getBean( planClass ); + return (MutabilityPlan) planBean.getBeanInstance(); + } + + return ImmutableMutabilityPlan.INSTANCE; + } + ); + } + + @SuppressWarnings("unchecked") + public JavaTypeDescriptor createTypeDescriptor( + Type javaType, + Function,MutabilityPlan> mutabilityPlanResolver) { + final Class javaTypeClass; + if ( javaType instanceof Class ) { + javaTypeClass = (Class) javaType; + } + else { + final ParameterizedType parameterizedType = (ParameterizedType) javaType; + javaTypeClass = (Class) parameterizedType.getRawType(); } - JavaTypeDescriptor descriptor = descriptorsByClass.get( cls ); - if ( descriptor != null ) { - return descriptor; + if ( javaTypeClass.isEnum() ) { + // enums are unequivocally immutable + //noinspection rawtypes + return new EnumJavaTypeDescriptor( javaTypeClass ); } - if ( cls.isEnum() ) { - descriptor = new EnumJavaTypeDescriptor( cls ); - descriptorsByClass.put( cls, descriptor ); - return descriptor; + final MutabilityPlan plan = mutabilityPlanResolver.apply( javaTypeClass ); + + if ( Serializable.class.isAssignableFrom( javaTypeClass ) ) { + //noinspection rawtypes + return new SerializableTypeDescriptor( javaTypeClass, plan ); } - // find the first "assignable" match - for ( Map.Entry entry : descriptorsByClass.entrySet() ) { - if ( entry.getKey().isAssignableFrom( cls ) ) { - log.debugf( "Using cached JavaTypeDescriptor instance for Java class [%s]", cls.getName() ); - return entry.getValue(); - } - } - - return defaultValueSupplier.get(); + return new JavaTypeDescriptorBasicAdaptor<>( javaTypeClass, plan ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/converter/ExplicitJavaTypeDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/ExplicitJavaTypeDescriptorTest.java similarity index 61% rename from hibernate-core/src/test/java/org/hibernate/test/converter/ExplicitJavaTypeDescriptorTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/ExplicitJavaTypeDescriptorTest.java index b35110f7be..8d695c23f6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/converter/ExplicitJavaTypeDescriptorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/ExplicitJavaTypeDescriptorTest.java @@ -4,8 +4,10 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html */ -package org.hibernate.test.converter; +package org.hibernate.orm.test.mapping.converted; +import java.sql.Types; +import java.util.Locale; import javax.persistence.AttributeConverter; import javax.persistence.Cacheable; import javax.persistence.Convert; @@ -18,18 +20,21 @@ import org.hibernate.Session; import org.hibernate.annotations.Immutable; import org.hibernate.boot.MetadataBuilder; import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.internal.MetadataBuilderImpl; +import org.hibernate.boot.model.TypeContributions; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.metamodel.spi.MetamodelImplementor; +import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan; -import org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry; +import org.hibernate.type.descriptor.java.JavaTypeDescriptor; import org.hibernate.type.descriptor.java.MutabilityPlan; +import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; +import org.hibernate.type.descriptor.sql.SqlTypeDescriptorIndicators; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -39,20 +44,66 @@ import static org.hamcrest.MatcherAssert.assertThat; */ public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalTestCase { + private static int mutableToDatabaseCallCount; + private static int mutableToDomainCallCount; + + private static int immutableToDatabaseCallCount; + private static int immutableToDomainCallCount; + + private static int pseudoMutableToDatabaseCallCount; + private static int pseudoMutableToDomainCallCount; + + @Test + @TestForIssue( jiraKey = "HHH-11098" ) + public void testIt() { + // create data and check assertions + inTransaction( + (session) -> session.persist( new TheEntity( 1 ) ) + ); + + // assertions based on the persist call + assertThat( mutableToDomainCallCount, is(1 ) ); // 1 instead of 0 because of the deep copy call + assertThat( mutableToDatabaseCallCount, is(2 ) ); // 2 instead of 1 because of the deep copy call + + assertThat( immutableToDomainCallCount, is(0 ) ); // logical + assertThat( immutableToDatabaseCallCount, is(1 ) ); // logical + + assertThat( pseudoMutableToDomainCallCount, is(0 ) ); // was 1 (like mutable) before the JavaTypeDescriptor registration + assertThat( pseudoMutableToDatabaseCallCount, is(1 ) ); // was 2 (like mutable) before the JavaTypeDescriptor registration + } + + @BeforeEach + public void clearCounts() { + // in case we add additional tests + sessionFactory().getStatistics().clear(); + + mutableToDatabaseCallCount = 0; + mutableToDomainCallCount = 0; + + immutableToDatabaseCallCount = 0; + immutableToDomainCallCount = 0; + + pseudoMutableToDatabaseCallCount = 0; + pseudoMutableToDomainCallCount = 0; + } + + @AfterEach + public void dropTestData() { + inTransaction( + (session) -> session.createQuery( "delete TheEntity" ).executeUpdate() + ); + } + @Override protected void configureMetadataBuilder(MetadataBuilder metadataBuilder) { - ((MetadataBuilderImpl)metadataBuilder).contributeJavaTypeDescriptor(new JavaTypeDescriptorRegistry.FallbackJavaTypeDescriptor( MutableState2.class ) { - @Override - public MutabilityPlan getMutabilityPlan() { - return ImmutableMutabilityPlan.INSTANCE; - } - }); + ( (TypeContributions) metadataBuilder ).contributeJavaTypeDescriptor( PseudoMutableStateJavaTypeDescriptor.INSTANCE ); } @Override protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { super.configureStandardServiceRegistryBuilder( ssrb ); + // to make sure we get the deepCopy calls ssrb.applySetting( AvailableSettings.USE_SECOND_LEVEL_CACHE, "true" ); ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); } @@ -60,48 +111,9 @@ public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalT @Override protected void applyMetadataSources(MetadataSources sources) { super.applyMetadataSources( sources ); - sources.addAnnotatedClass( TheEntity.class ); } - @Test - @TestForIssue( jiraKey = "HHH-11098" ) - public void testIt() { - // set up test data - Session session = openSession(); - ( (MetamodelImplementor) session.getMetamodel() ).getTypeConfiguration() - .getJavaTypeDescriptorRegistry() - .addDescriptor( - new JavaTypeDescriptorRegistry.FallbackJavaTypeDescriptor( MutableState2.class ) { - @Override - public MutabilityPlan getMutabilityPlan() { - return ImmutableMutabilityPlan.INSTANCE; - } - } - ); - session.beginTransaction(); - session.persist( new TheEntity(1) ); - session.getTransaction().commit(); - session.close(); - - // assertions based on the persist call - assertThat( mutableToDomainCallCount, is(1) ); // 1 instead of 0 because of the deep copy call - assertThat( mutableToDatabaseCallCount, is(2) ); // 2 instead of 1 because of the deep copy call - - assertThat( immutableToDomainCallCount, is(0) ); // logical - assertThat( immutableToDatabaseCallCount, is(1) ); // logical - - assertThat( immutableMutableToDomainCallCount, is(0) ); // was 1 (like mutable) before the JavaTypeDescriptor registration - assertThat( immutableMutableToDatabaseCallCount, is(1) ); // was 2 (like mutable) before the JavaTypeDescriptor registration - - // clean up test data - session = openSession(); - session.beginTransaction(); - session.delete( session.byId( TheEntity.class ).getReference( 1 ) ); - session.getTransaction().commit(); - session.close(); - } - @Entity( name = "TheEntity") @Table( name = "T_ENTITY" ) @@ -116,8 +128,8 @@ public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalT @Convert( converter = ImmutableConverterImpl.class ) private ImmutableState immutableState; - @Convert( converter = ImmutableMutable2ConverterImpl.class ) - private MutableState2 immutableMutableState; + @Convert( converter = PseudoMutableConverterImpl.class ) + private PseudoMutableState immutableMutableState; public TheEntity() { } @@ -127,34 +139,10 @@ public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalT this.mutableState = new MutableState( id.toString() ); this.immutableState = new ImmutableState( id.toString() ); - this.immutableMutableState = new MutableState2( id.toString() ); + this.immutableMutableState = new PseudoMutableState( id.toString() ); } } - @Before - public void clearCounts() { - // in case we add additional tests - sessionFactory().getStatistics().clear(); - - mutableToDatabaseCallCount = 0; - mutableToDomainCallCount = 0; - - immutableToDatabaseCallCount = 0; - immutableToDomainCallCount = 0; - - immutableMutableToDatabaseCallCount = 0; - immutableMutableToDomainCallCount = 0; - } - - private static int mutableToDatabaseCallCount; - private static int mutableToDomainCallCount; - - private static int immutableToDatabaseCallCount; - private static int immutableToDomainCallCount; - - private static int immutableMutableToDatabaseCallCount; - private static int immutableMutableToDomainCallCount; - // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Purely mutable state @@ -267,10 +255,10 @@ public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalT // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Mutable state we treat as immutable - public static class MutableState2 { + public static class PseudoMutableState { private String state; - public MutableState2(String state) { + public PseudoMutableState(String state) { this.state = state; } @@ -293,7 +281,7 @@ public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalT return false; } - MutableState2 that = (MutableState2) o; + PseudoMutableState that = (PseudoMutableState) o; return getState() != null ? getState().equals( that.getState() ) : that.getState() == null; @@ -306,20 +294,76 @@ public class ExplicitJavaTypeDescriptorTest extends BaseNonConfigCoreFunctionalT } @Converter - public static class ImmutableMutable2ConverterImpl implements AttributeConverter { + public static class PseudoMutableConverterImpl implements AttributeConverter { @Override - public String convertToDatabaseColumn(MutableState2 attribute) { - immutableMutableToDatabaseCallCount++; + public String convertToDatabaseColumn(PseudoMutableState attribute) { + pseudoMutableToDatabaseCallCount++; return attribute == null ? null : attribute.getState(); } @Override - public MutableState2 convertToEntityAttribute(String dbData) { - immutableMutableToDomainCallCount++; - return new MutableState2( dbData ); + public PseudoMutableState convertToEntityAttribute(String dbData) { + pseudoMutableToDomainCallCount++; + return new PseudoMutableState( dbData ); + } + } + + public static class PseudoMutableStateJavaTypeDescriptor implements JavaTypeDescriptor { + /** + * Singleton access + */ + public static final PseudoMutableStateJavaTypeDescriptor INSTANCE = new PseudoMutableStateJavaTypeDescriptor(); + + @Override + public Class getJavaTypeClass() { + return PseudoMutableState.class; + } + + @Override + public MutabilityPlan getMutabilityPlan() { + //noinspection unchecked + return ImmutableMutabilityPlan.INSTANCE; + } + + @Override + public SqlTypeDescriptor getJdbcRecommendedSqlType(SqlTypeDescriptorIndicators context) { + return context.getTypeConfiguration().getSqlTypeDescriptorRegistry().getDescriptor( Types.VARCHAR ); + } + + @Override + public PseudoMutableState fromString(String string) { + return string == null ? null : new PseudoMutableState( string ); + } + + @Override + public X unwrap(PseudoMutableState value, Class type, WrapperOptions options) { + if ( value == null ) { + return null; + } + + if ( PseudoMutableState.class.equals( type ) ) { + return (X) value; + } + + if ( String.class.equals( type ) ) { + return (X) value.state; + } + + throw new IllegalArgumentException( + String.format( + Locale.ROOT, + "Cannot convert value '%s' to type `%s`", + value.state, + type + ) + ); + } + + @Override + public PseudoMutableState wrap(X value, WrapperOptions options) { + return null; } } - } diff --git a/hibernate-core/src/test/java/org/hibernate/type/descriptor/java/BooleanTypeDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/java/BooleanTypeDescriptorTest.java similarity index 77% rename from hibernate-core/src/test/java/org/hibernate/type/descriptor/java/BooleanTypeDescriptorTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/java/BooleanTypeDescriptorTest.java index f752d935cf..9cb77fc0bb 100644 --- a/hibernate-core/src/test/java/org/hibernate/type/descriptor/java/BooleanTypeDescriptorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/java/BooleanTypeDescriptorTest.java @@ -1,4 +1,12 @@ -package org.hibernate.type.descriptor.java; +/* + * 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 http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.type.descriptor.java; + +import org.hibernate.type.descriptor.java.BooleanTypeDescriptor; import org.junit.Test; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/java/JavaTypeDescriptorRegistryTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/java/JavaTypeDescriptorRegistryTest.java new file mode 100644 index 0000000000..2520c4ab57 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/java/JavaTypeDescriptorRegistryTest.java @@ -0,0 +1,110 @@ +/* + * 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 http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.type.descriptor.java; + +import java.util.Comparator; + +import org.hibernate.type.descriptor.WrapperOptions; +import org.hibernate.type.descriptor.java.JavaTypeDescriptor; +import org.hibernate.type.descriptor.java.MutabilityPlan; +import org.hibernate.type.descriptor.java.StringTypeDescriptor; +import org.hibernate.type.descriptor.java.spi.JavaTypeDescriptorRegistry; +import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; +import org.hibernate.type.descriptor.sql.SqlTypeDescriptorIndicators; +import org.hibernate.type.spi.TypeConfiguration; + +import org.junit.Test; + +import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.junit.Assert.assertThat; + +/** + * @author Andrea Boriero + */ +public class JavaTypeDescriptorRegistryTest { + + @Test + public void testGetJavaTypeDescriptorRegistry() { + final TypeConfiguration typeConfiguration = new TypeConfiguration(); + final JavaTypeDescriptorRegistry registry = new JavaTypeDescriptorRegistry( typeConfiguration ); + + final JavaTypeDescriptor descriptor = registry.getDescriptor( String.class ); + + assertThat( descriptor, instanceOf( StringTypeDescriptor.class ) ); + } + + @Test + public void testRegisterJavaTypeDescriptorRegistry(){ + final TypeConfiguration typeConfiguration = new TypeConfiguration(); + final JavaTypeDescriptorRegistry registry = new JavaTypeDescriptorRegistry( typeConfiguration ); + + registry.addDescriptor( new CustomJavaTypeDescriptor() ); + + final JavaTypeDescriptor descriptor = registry.getDescriptor( CustomType.class ); + + assertThat( descriptor, instanceOf( CustomJavaTypeDescriptor.class ) ); + } + + public static class CustomType {} + + public static class CustomJavaTypeDescriptor implements JavaTypeDescriptor { + @Override + public Class getJavaTypeClass() { + return CustomType.class; + } + + @Override + public MutabilityPlan getMutabilityPlan() { + return null; + } + + @Override + public SqlTypeDescriptor getJdbcRecommendedSqlType(SqlTypeDescriptorIndicators context) { + return null; + } + + @Override + public Comparator getComparator() { + return null; + } + + @Override + public int extractHashCode(CustomType value) { + return 0; + } + + @Override + public boolean areEqual(CustomType one, CustomType another) { + return false; + } + + @Override + public String extractLoggableRepresentation(CustomType value) { + return null; + } + + @Override + public String toString(CustomType value) { + return null; + } + + @Override + public CustomType fromString(String string) { + return null; + } + + @Override + public CustomType wrap(Object value, WrapperOptions options) { + return null; + } + + @Override + public X unwrap(CustomType value, Class type, WrapperOptions options) { + return null; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/type/descriptor/sql/JdbcTypeJavaClassMappingsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/jdbc/JdbcTypeJavaClassMappingsTest.java similarity index 80% rename from hibernate-core/src/test/java/org/hibernate/type/descriptor/sql/JdbcTypeJavaClassMappingsTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/jdbc/JdbcTypeJavaClassMappingsTest.java index c1d091fcef..84be532f2b 100644 --- a/hibernate-core/src/test/java/org/hibernate/type/descriptor/sql/JdbcTypeJavaClassMappingsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/type/descriptor/jdbc/JdbcTypeJavaClassMappingsTest.java @@ -1,13 +1,15 @@ /* * 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 . + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html */ -package org.hibernate.type.descriptor.sql; +package org.hibernate.orm.test.type.descriptor.jdbc; import java.sql.Types; +import org.hibernate.type.descriptor.sql.JdbcTypeJavaClassMappings; + import org.junit.After; import org.junit.Before; import org.junit.Test; diff --git a/hibernate-core/src/test/java/org/hibernate/type/JavaTypeDescriptorRegistryTest.java b/hibernate-core/src/test/java/org/hibernate/type/JavaTypeDescriptorRegistryTest.java deleted file mode 100644 index b032512109..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/type/JavaTypeDescriptorRegistryTest.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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.type; - -import java.util.Comparator; - -import org.hibernate.type.descriptor.WrapperOptions; -import org.hibernate.type.descriptor.java.JavaTypeDescriptor; -import org.hibernate.type.descriptor.java.MutabilityPlan; -import org.hibernate.type.descriptor.java.StringTypeDescriptor; -import org.hibernate.type.descriptor.sql.SqlTypeDescriptor; -import org.hibernate.type.descriptor.sql.SqlTypeDescriptorIndicators; -import org.hibernate.type.spi.TypeConfiguration; - -import org.junit.Test; - -import static org.hamcrest.core.IsInstanceOf.instanceOf; -import static org.junit.Assert.assertThat; - -/** - * @author Andrea Boriero - */ -public class JavaTypeDescriptorRegistryTest { - - @Test - public void testGetJavaTypeDescriptorRegistry(){ - TypeConfiguration typeConfiguration = new TypeConfiguration(); - JavaTypeDescriptor descriptor = typeConfiguration.getJavaTypeDescriptorRegistry() - .getDescriptor( String.class ); - - assertThat(descriptor, instanceOf(StringTypeDescriptor.class)); - } - - @Test - public void testRegisterJavaTypeDescriptorRegistry(){ - TypeConfiguration typeConfiguration = new TypeConfiguration(); - typeConfiguration.getJavaTypeDescriptorRegistry().addDescriptor( new CustomJavaTypeDescriptor() ); - JavaTypeDescriptor descriptor = typeConfiguration.getJavaTypeDescriptorRegistry() - .getDescriptor( CustomType.class ); - - assertThat(descriptor, instanceOf(CustomJavaTypeDescriptor.class)); - } - - @Test - public void testAddDirectlyToJavaTypeDescriptorRegistry(){ - TypeConfiguration typeConfiguration = new TypeConfiguration(); - org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( new CustomJavaTypeDescriptor() ); - JavaTypeDescriptor descriptor = typeConfiguration.getJavaTypeDescriptorRegistry() - .getDescriptor( CustomType.class ); - - assertThat(descriptor, instanceOf(CustomJavaTypeDescriptor.class)); - } - - public class CustomType {} - - public class CustomJavaTypeDescriptor implements JavaTypeDescriptor{ - @Override - public Class getJavaTypeClass() { - return CustomType.class; - } - - @Override - public MutabilityPlan getMutabilityPlan() { - return null; - } - - @Override - public SqlTypeDescriptor getJdbcRecommendedSqlType(SqlTypeDescriptorIndicators context) { - return null; - } - - @Override - public Comparator getComparator() { - return null; - } - - @Override - public int extractHashCode(Object value) { - return 0; - } - - @Override - public boolean areEqual(Object one, Object another) { - return false; - } - - @Override - public String extractLoggableRepresentation(Object value) { - return null; - } - - @Override - public String toString(Object value) { - return null; - } - - @Override - public Object fromString(String string) { - return null; - } - - @Override - public Object wrap(Object value, WrapperOptions options) { - return null; - } - - @Override - public Object unwrap(Object value, Class type, WrapperOptions options) { - return null; - } - } -}