|
| 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.JdbcSettings; |
| 11 | +import org.hibernate.cfg.MappingSettings; |
| 12 | +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
| 13 | +import org.hibernate.testing.orm.junit.Jpa; |
| 14 | +import org.hibernate.testing.orm.junit.Setting; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import java.time.Instant; |
| 18 | +import java.time.LocalDateTime; |
| 19 | +import java.time.OffsetDateTime; |
| 20 | +import java.time.ZoneOffset; |
| 21 | +import java.time.temporal.ChronoUnit; |
| 22 | + |
| 23 | +import static org.hibernate.type.SqlTypes.TIMESTAMP; |
| 24 | +import static org.hibernate.type.SqlTypes.TIMESTAMP_WITH_TIMEZONE; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 26 | + |
| 27 | +@Jpa(annotatedClasses = InstantTest2.Instants.class, |
| 28 | + integrationSettings = {@Setting(name= MappingSettings.TIMEZONE_DEFAULT_STORAGE, |
| 29 | + value = "NORMALIZE"), |
| 30 | + @Setting(name= JdbcSettings.JDBC_TIME_ZONE, value = "+01:00")}) |
| 31 | +public class InstantTest2 { |
| 32 | + @Test |
| 33 | + public void test(EntityManagerFactoryScope scope) { |
| 34 | + Instant now = Instant.now(); |
| 35 | + ZoneOffset zone = ZoneOffset.of("+01:00");// ZoneOffset.ofHours(1); |
| 36 | + scope.getEntityManagerFactory() |
| 37 | + .runInTransaction( entityManager -> { |
| 38 | + Instants instants = new Instants(); |
| 39 | + instants.instantInUtc = now; |
| 40 | + instants.instantInLocalTimeZone = now; |
| 41 | + instants.instantWithTimeZone = now; |
| 42 | + instants.localDateTime = LocalDateTime.ofInstant( now, zone ); |
| 43 | + instants.offsetDateTime = OffsetDateTime.ofInstant( now, zone ); |
| 44 | + instants.localDateTimeUtc = LocalDateTime.ofInstant( now, ZoneOffset.UTC ); |
| 45 | + instants.offsetDateTimeUtc = OffsetDateTime.ofInstant( now, ZoneOffset.UTC ); |
| 46 | + entityManager.persist( instants ); |
| 47 | + } ); |
| 48 | + scope.getEntityManagerFactory() |
| 49 | + .runInTransaction( entityManager -> { |
| 50 | + Instants instants = entityManager.find( Instants.class, 0 ); |
| 51 | + assertEqualInstants( now, instants.instantInUtc ); |
| 52 | + assertEqualInstants( now, instants.instantInLocalTimeZone ); |
| 53 | + assertEqualInstants( now, instants.instantWithTimeZone ); |
| 54 | + assertEqualInstants( now, instants.offsetDateTime.toInstant() ); |
| 55 | + assertEqualInstants( now, instants.localDateTime.toInstant( zone ) ); |
| 56 | + assertEqualInstants( now, instants.offsetDateTimeUtc.toInstant() ); |
| 57 | + assertEqualInstants( now, instants.localDateTimeUtc.toInstant( ZoneOffset.UTC ) ); |
| 58 | + } ); |
| 59 | + } |
| 60 | + void assertEqualInstants(Instant x, Instant y) { |
| 61 | + assertEquals( x.truncatedTo( ChronoUnit.SECONDS ), y.truncatedTo( ChronoUnit.SECONDS ) ); |
| 62 | + } |
| 63 | + |
| 64 | + @Entity(name="Instants") |
| 65 | + static class Instants { |
| 66 | + @Id long id; |
| 67 | + Instant instantInUtc; |
| 68 | + @JdbcTypeCode(TIMESTAMP) Instant instantInLocalTimeZone; |
| 69 | + @JdbcTypeCode(TIMESTAMP_WITH_TIMEZONE) Instant instantWithTimeZone; |
| 70 | + LocalDateTime localDateTime; |
| 71 | + OffsetDateTime offsetDateTime; |
| 72 | + LocalDateTime localDateTimeUtc; |
| 73 | + OffsetDateTime offsetDateTimeUtc; |
| 74 | + } |
| 75 | +} |
0 commit comments