|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.test.timestamp; |
| 8 | + |
| 9 | +import java.time.LocalDate; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.TimeZone; |
| 12 | +import javax.persistence.Entity; |
| 13 | +import javax.persistence.Id; |
| 14 | + |
| 15 | +import org.hibernate.cfg.AvailableSettings; |
| 16 | +import org.hibernate.dialect.MySQLDialect; |
| 17 | + |
| 18 | +import org.hibernate.testing.RequiresDialect; |
| 19 | +import org.hibernate.testing.TestForIssue; |
| 20 | +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; |
| 21 | +import org.hibernate.test.util.jdbc.ConnectionProviderDelegate; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernateSessionBuilder; |
| 25 | +import static org.junit.Assert.assertEquals; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Vlad Mihalcea |
| 29 | + */ |
| 30 | +@RequiresDialect(MySQLDialect.class) |
| 31 | +public class LocalDateCustomSessionLevelTimeZoneTest |
| 32 | + extends BaseNonConfigCoreFunctionalTestCase { |
| 33 | + |
| 34 | + private static final TimeZone TIME_ZONE = TimeZone.getTimeZone( |
| 35 | + "Europe/Berlin" ); |
| 36 | + |
| 37 | + private ConnectionProviderDelegate connectionProvider = new ConnectionProviderDelegate() { |
| 38 | + @Override |
| 39 | + public void configure(Map configurationValues) { |
| 40 | + String url = (String) configurationValues.get( AvailableSettings.URL ); |
| 41 | + if(!url.contains( "?" )) { |
| 42 | + url += "?"; |
| 43 | + } |
| 44 | + else if(!url.endsWith( "&" )) { |
| 45 | + url += "&"; |
| 46 | + } |
| 47 | + url += "zeroDateTimeBehavior=convertToNull&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Berlin"; |
| 48 | + |
| 49 | + configurationValues.put( AvailableSettings.URL, url); |
| 50 | + super.configure( configurationValues ); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + @Override |
| 55 | + protected Class<?>[] getAnnotatedClasses() { |
| 56 | + return new Class<?>[] { |
| 57 | + Person.class |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected void addSettings(Map settings) { |
| 63 | + settings.put( |
| 64 | + AvailableSettings.CONNECTION_PROVIDER, |
| 65 | + connectionProvider |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void releaseResources() { |
| 71 | + super.releaseResources(); |
| 72 | + connectionProvider.stop(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @TestForIssue( jiraKey = "HHH-11396" ) |
| 77 | + public void testTimeZone() { |
| 78 | + TimeZone old = TimeZone.getDefault(); |
| 79 | + try { |
| 80 | + // The producer (MySQL) Berlin and returns 1980-01-01 |
| 81 | + TimeZone jdbcTimeZone = TimeZone.getTimeZone( "Europe/Berlin" ); |
| 82 | + TimeZone.setDefault( jdbcTimeZone ); |
| 83 | + |
| 84 | + //hibernate.connection.url jdbc:mysql://localhost/hibernate_orm_test |
| 85 | + doInHibernateSessionBuilder( () -> sessionFactory().withOptions().jdbcTimeZone( TIME_ZONE ), s -> { |
| 86 | + Person person = new Person(); |
| 87 | + person.id = 1L; |
| 88 | + s.persist( person ); |
| 89 | + } ); |
| 90 | + |
| 91 | + doInHibernateSessionBuilder( () -> sessionFactory().withOptions().jdbcTimeZone( TIME_ZONE ), s -> { |
| 92 | + Person person = s.find( Person.class, 1L ); |
| 93 | + assertEquals( LocalDate.of( 2017, 3, 7 ), person.createdOn ); |
| 94 | + } ); |
| 95 | + } |
| 96 | + finally { |
| 97 | + TimeZone.setDefault( old ); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Entity(name = "Person") |
| 102 | + public static class Person { |
| 103 | + |
| 104 | + @Id |
| 105 | + private Long id; |
| 106 | + |
| 107 | + private LocalDate createdOn = LocalDate.of( 2017, 3, 7 ); |
| 108 | + } |
| 109 | +} |
| 110 | + |
0 commit comments