Skip to content

Commit 4a8d726

Browse files
committed
yet another time zone test
1 parent 779d9c9 commit 4a8d726

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.testing.orm.junit.EntityManagerFactoryScope;
11+
import org.hibernate.testing.orm.junit.Jpa;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.time.Instant;
15+
import java.time.LocalDateTime;
16+
import java.time.OffsetDateTime;
17+
import java.time.ZoneOffset;
18+
import java.time.temporal.ChronoUnit;
19+
20+
import static org.hibernate.type.SqlTypes.TIMESTAMP;
21+
import static org.hibernate.type.SqlTypes.TIMESTAMP_WITH_TIMEZONE;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
24+
@Jpa(annotatedClasses = InstantTest.Instants.class)
25+
public class InstantTest {
26+
@Test
27+
public void test(EntityManagerFactoryScope scope) {
28+
Instant now = Instant.now();
29+
ZoneOffset zone = ZoneOffset.ofHours(1);
30+
scope.getEntityManagerFactory()
31+
.runInTransaction( entityManager -> {
32+
Instants instants = new Instants();
33+
instants.instantInUtc = now;
34+
instants.instantInLocalTimeZone = now;
35+
instants.instantWithTimeZone = now;
36+
instants.localDateTime = LocalDateTime.ofInstant( now, zone );
37+
instants.offsetDateTime = OffsetDateTime.ofInstant( now, zone );
38+
instants.localDateTimeUtc = LocalDateTime.ofInstant( now, ZoneOffset.UTC );
39+
instants.offsetDateTimeUtc = OffsetDateTime.ofInstant( now, ZoneOffset.UTC );
40+
entityManager.persist( instants );
41+
} );
42+
scope.getEntityManagerFactory()
43+
.runInTransaction( entityManager -> {
44+
Instants instants = entityManager.find( Instants.class, 0 );
45+
assertEqualInstants( now, instants.instantInUtc );
46+
assertEqualInstants( now, instants.instantInLocalTimeZone );
47+
assertEqualInstants( now, instants.instantWithTimeZone );
48+
assertEqualInstants( now, instants.offsetDateTime.toInstant() );
49+
assertEqualInstants( now, instants.localDateTime.toInstant( zone ) );
50+
assertEqualInstants( now, instants.offsetDateTimeUtc.toInstant() );
51+
assertEqualInstants( now, instants.localDateTimeUtc.toInstant( ZoneOffset.UTC ) );
52+
} );
53+
}
54+
void assertEqualInstants(Instant x, Instant y) {
55+
assertEquals( x.truncatedTo( ChronoUnit.SECONDS ), y.truncatedTo( ChronoUnit.SECONDS ) );
56+
}
57+
@Entity(name="Instants")
58+
static class Instants {
59+
@Id long id;
60+
Instant instantInUtc;
61+
@JdbcTypeCode(TIMESTAMP) Instant instantInLocalTimeZone;
62+
@JdbcTypeCode(TIMESTAMP_WITH_TIMEZONE) Instant instantWithTimeZone;
63+
LocalDateTime localDateTime;
64+
OffsetDateTime offsetDateTime;
65+
LocalDateTime localDateTimeUtc;
66+
OffsetDateTime offsetDateTimeUtc;
67+
}
68+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)