|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.instant; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import org.hibernate.annotations.JdbcTypeCode; |
| 10 | +import org.hibernate.cfg.AvailableSettings; |
| 11 | +import org.hibernate.cfg.JdbcSettings; |
| 12 | +import org.hibernate.dialect.MySQLDialect; |
| 13 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 14 | +import org.hibernate.testing.orm.junit.Jpa; |
| 15 | +import org.hibernate.testing.orm.junit.Setting; |
| 16 | +import org.hibernate.testing.orm.junit.SkipForDialect; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import java.time.Instant; |
| 20 | +import java.time.LocalDateTime; |
| 21 | +import java.time.OffsetDateTime; |
| 22 | +import java.time.ZoneOffset; |
| 23 | +import java.time.temporal.ChronoUnit; |
| 24 | + |
| 25 | +import static org.hibernate.type.SqlTypes.TIMESTAMP; |
| 26 | +import static org.hibernate.type.SqlTypes.TIMESTAMP_WITH_TIMEZONE; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 28 | + |
| 29 | +@Jpa(annotatedClasses = InstantWithNormalizedTest.Instants.class, |
| 30 | + integrationSettings = |
| 31 | + {@Setting(name = AvailableSettings.TIMEZONE_DEFAULT_STORAGE, value = "NORMALIZE"), |
| 32 | + @Setting(name = JdbcSettings.JDBC_TIME_ZONE, value = "+01:00")}) |
| 33 | +@SkipForDialect(dialectClass = MySQLDialect.class, reason = "MySQL hangs dropping the table") |
| 34 | +public class InstantWithNormalizedTest { |
| 35 | + @Test |
| 36 | + public void test(EntityManagerFactoryScope scope) { |
| 37 | + Instant now = Instant.now(); |
| 38 | + ZoneOffset zone = ZoneOffset.of("+01:00");// ZoneOffset.ofHours(1); |
| 39 | + scope.getEntityManagerFactory() |
| 40 | + .runInTransaction( entityManager -> { |
| 41 | + Instants instants = new Instants(); |
| 42 | + instants.instantInUtc = now; |
| 43 | + instants.instantInLocalTimeZone = now; |
| 44 | + instants.instantWithTimeZone = now; |
| 45 | + instants.localDateTime = LocalDateTime.ofInstant( now, zone ); |
| 46 | + instants.offsetDateTime = OffsetDateTime.ofInstant( now, zone ); |
| 47 | + instants.localDateTimeUtc = LocalDateTime.ofInstant( now, ZoneOffset.UTC ); |
| 48 | + instants.offsetDateTimeUtc = OffsetDateTime.ofInstant( now, ZoneOffset.UTC ); |
| 49 | + entityManager.persist( instants ); |
| 50 | + } ); |
| 51 | + scope.getEntityManagerFactory() |
| 52 | + .runInTransaction( entityManager -> { |
| 53 | + Instants instants = entityManager.find( Instants.class, 0 ); |
| 54 | + assertEqualInstants( now, instants.instantInUtc ); |
| 55 | + assertEqualInstants( now, instants.instantInLocalTimeZone ); |
| 56 | + assertEqualInstants( now, instants.instantWithTimeZone ); |
| 57 | + assertEqualInstants( now, instants.offsetDateTime.toInstant() ); |
| 58 | + assertEqualInstants( now, instants.localDateTime.toInstant( zone ) ); |
| 59 | + assertEqualInstants( now, instants.offsetDateTimeUtc.toInstant() ); |
| 60 | + assertEqualInstants( now, instants.localDateTimeUtc.toInstant( ZoneOffset.UTC ) ); |
| 61 | + } ); |
| 62 | + } |
| 63 | + void assertEqualInstants(Instant x, Instant y) { |
| 64 | + assertEquals( x.truncatedTo( ChronoUnit.SECONDS ), y.truncatedTo( ChronoUnit.SECONDS ) ); |
| 65 | + } |
| 66 | + |
| 67 | + @Entity(name="Instants2") |
| 68 | + static class Instants { |
| 69 | + @Id long id; |
| 70 | + Instant instantInUtc; |
| 71 | + @JdbcTypeCode(TIMESTAMP) Instant instantInLocalTimeZone; |
| 72 | + @JdbcTypeCode(TIMESTAMP_WITH_TIMEZONE) Instant instantWithTimeZone; |
| 73 | + LocalDateTime localDateTime; |
| 74 | + OffsetDateTime offsetDateTime; |
| 75 | + LocalDateTime localDateTimeUtc; |
| 76 | + OffsetDateTime offsetDateTimeUtc; |
| 77 | + } |
| 78 | +} |
0 commit comments