Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-11396 - Conversion of Date to LocalDate does not consider timezon…
…e difference - add test case to replicate the actual root cause behind this issue
- Loading branch information
1 parent
be74174
commit 3dda9d7
Showing
1 changed file
with
110 additions
and
0 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
...e/src/test/java/org/hibernate/test/timestamp/LocalDateCustomSessionLevelTimeZoneTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.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 ); | ||
} | ||
} | ||
|