Skip to content

Commit e27fb3f

Browse files
committed
HHH-10129 - Evaluate AttributeConverter tests in hibernate-entitymanager for move to hibernate-core;
HHH-10154 - Change built-in JavaTypeDescriptor impls to not auto-register themselves with JavaTypeDescriptorRegistry (cherry picked from commit 7af7eba)
1 parent d04d86c commit e27fb3f

File tree

41 files changed

+892
-1075
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+892
-1075
lines changed

hibernate-core/hibernate-core.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ dependencies {
3636
testCompile( libraries.jandex )
3737
testCompile( libraries.classmate )
3838
testCompile( libraries.mockito )
39+
testCompile( 'joda-time:joda-time:2.3' )
3940

4041
testCompile( libraries.validator ) {
4142
// for test runtime

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/AbstractTypeDescriptor.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ protected AbstractTypeDescriptor(Class<T> type, MutabilityPlan<T> mutabilityPlan
4848
this.comparator = Comparable.class.isAssignableFrom( type )
4949
? (Comparator<T>) ComparableComparator.INSTANCE
5050
: null;
51-
52-
JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( this );
5351
}
5452

5553
@Override

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/EnumJavaTypeDescriptor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class EnumJavaTypeDescriptor<T extends Enum> extends AbstractTypeDescript
1717
@SuppressWarnings("unchecked")
1818
protected EnumJavaTypeDescriptor(Class<T> type) {
1919
super( type, ImmutableMutabilityPlan.INSTANCE );
20+
21+
JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( this );
2022
}
2123

2224
@Override

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JavaTypeDescriptorRegistry.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,63 @@ public class JavaTypeDescriptorRegistry {
2727

2828
private ConcurrentHashMap<Class,JavaTypeDescriptor> descriptorsByClass = new ConcurrentHashMap<Class, JavaTypeDescriptor>();
2929

30+
public JavaTypeDescriptorRegistry() {
31+
addDescriptorInternal( ByteTypeDescriptor.INSTANCE );
32+
addDescriptorInternal( BooleanTypeDescriptor.INSTANCE );
33+
addDescriptorInternal( CharacterTypeDescriptor.INSTANCE );
34+
addDescriptorInternal( ShortTypeDescriptor.INSTANCE );
35+
addDescriptorInternal( IntegerTypeDescriptor.INSTANCE );
36+
addDescriptorInternal( LongTypeDescriptor.INSTANCE );
37+
addDescriptorInternal( FloatTypeDescriptor.INSTANCE );
38+
addDescriptorInternal( DoubleTypeDescriptor.INSTANCE );
39+
addDescriptorInternal( BigDecimalTypeDescriptor.INSTANCE );
40+
addDescriptorInternal( BigIntegerTypeDescriptor.INSTANCE );
41+
42+
addDescriptorInternal( StringTypeDescriptor.INSTANCE );
43+
44+
addDescriptorInternal( BlobTypeDescriptor.INSTANCE );
45+
addDescriptorInternal( ClobTypeDescriptor.INSTANCE );
46+
addDescriptorInternal( NClobTypeDescriptor.INSTANCE );
47+
48+
addDescriptorInternal( ByteArrayTypeDescriptor.INSTANCE );
49+
addDescriptorInternal( CharacterArrayTypeDescriptor.INSTANCE );
50+
addDescriptorInternal( PrimitiveByteArrayTypeDescriptor.INSTANCE );
51+
addDescriptorInternal( PrimitiveCharacterArrayTypeDescriptor.INSTANCE );
52+
53+
addDescriptorInternal( CalendarTypeDescriptor.INSTANCE );
54+
addDescriptorInternal( DateTypeDescriptor.INSTANCE );
55+
descriptorsByClass.put( java.sql.Date.class, JdbcDateTypeDescriptor.INSTANCE );
56+
descriptorsByClass.put( java.sql.Time.class, JdbcTimeTypeDescriptor.INSTANCE );
57+
descriptorsByClass.put( java.sql.Timestamp.class, JdbcTimestampTypeDescriptor.INSTANCE );
58+
addDescriptorInternal( TimeZoneTypeDescriptor.INSTANCE );
59+
60+
addDescriptorInternal( ClassTypeDescriptor.INSTANCE );
61+
62+
addDescriptorInternal( CurrencyTypeDescriptor.INSTANCE );
63+
addDescriptorInternal( LocaleTypeDescriptor.INSTANCE );
64+
addDescriptorInternal( UrlTypeDescriptor.INSTANCE );
65+
addDescriptorInternal( UUIDTypeDescriptor.INSTANCE );
66+
}
67+
68+
private JavaTypeDescriptor addDescriptorInternal(JavaTypeDescriptor descriptor) {
69+
return descriptorsByClass.put( descriptor.getJavaTypeClass(), descriptor );
70+
}
71+
3072
/**
3173
* Adds the given descriptor to this registry
3274
*
3375
* @param descriptor The descriptor to add.
3476
*/
3577
public void addDescriptor(JavaTypeDescriptor descriptor) {
36-
descriptorsByClass.put( descriptor.getJavaTypeClass(), descriptor );
78+
JavaTypeDescriptor old = addDescriptorInternal( descriptor );
79+
if ( old != null ) {
80+
log.debugf(
81+
"JavaTypeDescriptorRegistry entry replaced : %s -> %s (was %s)",
82+
descriptor.getJavaTypeClass(),
83+
descriptor,
84+
old
85+
);
86+
}
3787
}
3888

3989
@SuppressWarnings("unchecked")

hibernate-core/src/test/java/org/hibernate/test/type/converter/AndNationalizedTests.java renamed to hibernate-core/src/test/java/org/hibernate/test/converter/AndNationalizedTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
55
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
66
*/
7-
package org.hibernate.test.type.converter;
7+
package org.hibernate.test.converter;
88

99
import java.sql.Types;
1010
import javax.persistence.AttributeConverter;

hibernate-core/src/test/java/org/hibernate/test/type/AttributeConverterTest.java renamed to hibernate-core/src/test/java/org/hibernate/test/converter/AttributeConverterTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
55
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
66
*/
7-
package org.hibernate.test.type;
7+
package org.hibernate.test.converter;
88

99
import java.io.Serializable;
1010
import java.sql.Timestamp;
@@ -178,7 +178,7 @@ public void testBasicOrmXmlConverterApplication() {
178178
try {
179179
MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr )
180180
.addAnnotatedClass( Tester.class )
181-
.addURL( ConfigHelper.findAsResource( "org/hibernate/test/type/orm.xml" ) )
181+
.addURL( ConfigHelper.findAsResource( "org/hibernate/test/converter/orm.xml" ) )
182182
.getMetadataBuilder()
183183
.build();
184184

@@ -359,13 +359,13 @@ public void testEnumConverter() {
359359
JavaConstantNode javaConstantNode = new JavaConstantNode();
360360
javaConstantNode.setExpectedType( type );
361361
javaConstantNode.setSessionFactory( (SessionFactoryImplementor) sf );
362-
javaConstantNode.setText( "org.hibernate.test.type.AttributeConverterTest$ConvertibleEnum.VALUE" );
362+
javaConstantNode.setText( "org.hibernate.test.converter.AttributeConverterTest$ConvertibleEnum.VALUE" );
363363
final String outcome = javaConstantNode.getRenderText( (SessionFactoryImplementor) sf );
364364
assertEquals( "'VALUE'", outcome );
365365

366366
s = sf.openSession();
367367
s.beginTransaction();
368-
s.createQuery( "FROM EntityWithConvertibleField e where e.convertibleEnum = org.hibernate.test.type.AttributeConverterTest$ConvertibleEnum.VALUE" )
368+
s.createQuery( "FROM EntityWithConvertibleField e where e.convertibleEnum = org.hibernate.test.converter.AttributeConverterTest$ConvertibleEnum.VALUE" )
369369
.list();
370370
s.getTransaction().commit();
371371
s.close();
Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,22 @@
44
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
55
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
66
*/
7-
package org.hibernate.jpa.test.convert;
7+
package org.hibernate.test.converter;
88

99
import java.net.MalformedURLException;
10-
import java.util.Arrays;
1110
import java.util.Date;
12-
import java.util.HashMap;
13-
import java.util.List;
14-
import java.util.Map;
1511
import javax.persistence.AttributeConverter;
1612
import javax.persistence.Convert;
1713
import javax.persistence.Entity;
18-
import javax.persistence.EntityManager;
19-
import javax.persistence.EntityManagerFactory;
2014
import javax.persistence.Id;
2115

22-
import org.hibernate.cfg.AvailableSettings;
23-
import org.hibernate.engine.spi.SessionFactoryImplementor;
24-
import org.hibernate.jpa.boot.spi.Bootstrap;
25-
import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter;
16+
import org.hibernate.Session;
2617
import org.hibernate.persister.entity.EntityPersister;
2718
import org.hibernate.type.Type;
2819
import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter;
2920

3021
import org.hibernate.testing.TestForIssue;
22+
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
3123
import org.junit.Test;
3224

3325
import org.joda.time.LocalDate;
@@ -39,17 +31,23 @@
3931
* @author Steve Ebersole
4032
*/
4133
@TestForIssue( jiraKey = "HHH-8842" )
42-
public class BasicJodaTimeConversionTest {
43-
static int callsToConverter = 0;
34+
public class BasicJodaTimeConversionTest extends BaseNonConfigCoreFunctionalTestCase {
35+
static boolean convertToDatabaseColumnCalled = false;
36+
static boolean convertToEntityAttributeCalled = false;
37+
38+
private void resetFlags() {
39+
convertToDatabaseColumnCalled = false;
40+
convertToEntityAttributeCalled = false;
41+
}
4442

4543
public static class JodaLocalDateConverter implements AttributeConverter<LocalDate, Date> {
4644
public Date convertToDatabaseColumn(LocalDate localDate) {
47-
callsToConverter++;
45+
convertToDatabaseColumnCalled = true;
4846
return localDate.toDate();
4947
}
5048

5149
public LocalDate convertToEntityAttribute(Date date) {
52-
callsToConverter++;
50+
convertToEntityAttributeCalled = true;
5351
return LocalDate.fromDateFields( date );
5452
}
5553
}
@@ -70,52 +68,42 @@ public TheEntity(Integer id, LocalDate theDate) {
7068
}
7169
}
7270

71+
@Override
72+
protected Class[] getAnnotatedClasses() {
73+
return new Class[] { TheEntity.class };
74+
}
75+
7376
@Test
7477
public void testSimpleConvertUsage() throws MalformedURLException {
75-
final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() {
76-
@Override
77-
public List<String> getManagedClassNames() {
78-
return Arrays.asList( TheEntity.class.getName() );
79-
}
80-
};
81-
82-
final Map settings = new HashMap();
83-
settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
84-
85-
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build();
86-
final EntityPersister ep = emf.unwrap( SessionFactoryImplementor.class ).getEntityPersister( TheEntity.class.getName() );
78+
final EntityPersister ep = sessionFactory().getEntityPersister( TheEntity.class.getName() );
8779
final Type theDatePropertyType = ep.getPropertyType( "theDate" );
8880
final AttributeConverterTypeAdapter type = assertTyping( AttributeConverterTypeAdapter.class, theDatePropertyType );
8981
assertTyping( JodaLocalDateConverter.class, type.getAttributeConverter() );
9082

91-
int previousCallCount = 0;
83+
resetFlags();
9284

93-
try {
94-
EntityManager em = emf.createEntityManager();
95-
em.getTransaction().begin();
96-
em.persist( new TheEntity( 1, new LocalDate() ) );
97-
em.getTransaction().commit();
98-
em.close();
85+
Session session = openSession();
86+
session.getTransaction().begin();
87+
session.persist( new TheEntity( 1, new LocalDate() ) );
88+
session.getTransaction().commit();
89+
session.close();
9990

100-
assertTrue( previousCallCount < callsToConverter );
101-
previousCallCount = callsToConverter;
91+
assertTrue( convertToDatabaseColumnCalled );
92+
resetFlags();
10293

103-
em = emf.createEntityManager();
104-
em.getTransaction().begin();
105-
em.find( TheEntity.class, 1 );
106-
em.getTransaction().commit();
107-
em.close();
94+
session = openSession();
95+
session.getTransaction().begin();
96+
session.get( TheEntity.class, 1 );
97+
session.getTransaction().commit();
98+
session.close();
10899

109-
assertTrue( previousCallCount < callsToConverter );
100+
assertTrue( convertToEntityAttributeCalled );
101+
resetFlags();
110102

111-
em = emf.createEntityManager();
112-
em.getTransaction().begin();
113-
em.createQuery( "delete TheEntity" ).executeUpdate();
114-
em.getTransaction().commit();
115-
em.close();
116-
}
117-
finally {
118-
emf.close();
119-
}
103+
session = openSession();
104+
session.getTransaction().begin();
105+
session.createQuery( "delete TheEntity" ).executeUpdate();
106+
session.getTransaction().commit();
107+
session.close();
120108
}
121109
}

hibernate-core/src/test/java/org/hibernate/test/type/converter/DirtyCheckingTest.java renamed to hibernate-core/src/test/java/org/hibernate/test/converter/DirtyCheckingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
55
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
66
*/
7-
package org.hibernate.test.type.converter;
7+
package org.hibernate.test.converter;
88

99
import javax.persistence.AttributeConverter;
1010
import javax.persistence.Column;

0 commit comments

Comments
 (0)