From 3dda9d7a54d70e3fde58643f4f0994939bc2f5e2 Mon Sep 17 00:00:00 2001 From: Vlad Mihalcea Date: Tue, 7 Mar 2017 16:25:10 +0200 Subject: [PATCH] HHH-11396 - Conversion of Date to LocalDate does not consider timezone difference - add test case to replicate the actual root cause behind this issue --- ...calDateCustomSessionLevelTimeZoneTest.java | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/timestamp/LocalDateCustomSessionLevelTimeZoneTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/timestamp/LocalDateCustomSessionLevelTimeZoneTest.java b/hibernate-core/src/test/java/org/hibernate/test/timestamp/LocalDateCustomSessionLevelTimeZoneTest.java new file mode 100644 index 0000000000..8d62b44362 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/timestamp/LocalDateCustomSessionLevelTimeZoneTest.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 . + */ +package org.hibernate.test.timestamp; + +import java.time.LocalDate; +import java.util.Map; +import java.util.TimeZone; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.dialect.MySQLDialect; + +import org.hibernate.testing.RequiresDialect; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.hibernate.test.util.jdbc.ConnectionProviderDelegate; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernateSessionBuilder; +import static org.junit.Assert.assertEquals; + +/** + * @author Vlad Mihalcea + */ +@RequiresDialect(MySQLDialect.class) +public class LocalDateCustomSessionLevelTimeZoneTest + extends BaseNonConfigCoreFunctionalTestCase { + + private static final TimeZone TIME_ZONE = TimeZone.getTimeZone( + "Europe/Berlin" ); + + private ConnectionProviderDelegate connectionProvider = new ConnectionProviderDelegate() { + @Override + public void configure(Map configurationValues) { + String url = (String) configurationValues.get( AvailableSettings.URL ); + if(!url.contains( "?" )) { + url += "?"; + } + else if(!url.endsWith( "&" )) { + url += "&"; + } + url += "zeroDateTimeBehavior=convertToNull&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Berlin"; + + configurationValues.put( AvailableSettings.URL, url); + super.configure( configurationValues ); + } + }; + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + Person.class + }; + } + + @Override + protected void addSettings(Map settings) { + settings.put( + AvailableSettings.CONNECTION_PROVIDER, + connectionProvider + ); + } + + @Override + protected void releaseResources() { + super.releaseResources(); + connectionProvider.stop(); + } + + @Test + @TestForIssue( jiraKey = "HHH-11396" ) + public void testTimeZone() { + TimeZone old = TimeZone.getDefault(); + try { + // The producer (MySQL) Berlin and returns 1980-01-01 + TimeZone jdbcTimeZone = TimeZone.getTimeZone( "Europe/Berlin" ); + TimeZone.setDefault( jdbcTimeZone ); + + //hibernate.connection.url jdbc:mysql://localhost/hibernate_orm_test + doInHibernateSessionBuilder( () -> sessionFactory().withOptions().jdbcTimeZone( TIME_ZONE ), s -> { + Person person = new Person(); + person.id = 1L; + s.persist( person ); + } ); + + doInHibernateSessionBuilder( () -> sessionFactory().withOptions().jdbcTimeZone( TIME_ZONE ), s -> { + Person person = s.find( Person.class, 1L ); + assertEquals( LocalDate.of( 2017, 3, 7 ), person.createdOn ); + } ); + } + finally { + TimeZone.setDefault( old ); + } + } + + @Entity(name = "Person") + public static class Person { + + @Id + private Long id; + + private LocalDate createdOn = LocalDate.of( 2017, 3, 7 ); + } +} +