|
7 | 7 | import jakarta.persistence.Entity; |
8 | 8 | import jakarta.persistence.Id; |
9 | 9 | import org.hibernate.annotations.JdbcTypeCode; |
10 | | -import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; |
11 | | -import org.hibernate.testing.orm.junit.Jpa; |
| 10 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 13 | +import org.junit.jupiter.api.AfterEach; |
12 | 14 | import org.junit.jupiter.api.Test; |
13 | 15 |
|
14 | 16 | import java.time.Instant; |
15 | 17 | import java.time.LocalDateTime; |
16 | 18 | import java.time.OffsetDateTime; |
17 | 19 | import java.time.ZoneOffset; |
| 20 | +import java.time.format.DateTimeFormatter; |
18 | 21 | import java.time.temporal.ChronoUnit; |
| 22 | +import java.util.List; |
19 | 23 |
|
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
20 | 25 | import static org.hibernate.type.SqlTypes.TIMESTAMP; |
21 | 26 | import static org.hibernate.type.SqlTypes.TIMESTAMP_WITH_TIMEZONE; |
22 | 27 | import static org.junit.jupiter.api.Assertions.assertEquals; |
23 | 28 |
|
24 | | -@Jpa(annotatedClasses = InstantTest.Instants.class) |
| 29 | +@SuppressWarnings("JUnitMalformedDeclaration") |
| 30 | +@DomainModel(annotatedClasses = InstantTest.Instants.class) |
| 31 | +@SessionFactory |
25 | 32 | public class InstantTest { |
26 | 33 | @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 | | - } ); |
| 34 | + public void testStorage(SessionFactoryScope scope) { |
| 35 | + final Instant now = Instant.now(); |
| 36 | + final ZoneOffset zone = ZoneOffset.ofHours(1); |
| 37 | + |
| 38 | + scope.inTransaction( (session) -> { |
| 39 | + final Instants instants = new Instants(); |
| 40 | + instants.instantInUtc = now; |
| 41 | + instants.instantInLocalTimeZone = now; |
| 42 | + instants.instantWithTimeZone = now; |
| 43 | + instants.localDateTime = LocalDateTime.ofInstant( now, zone ); |
| 44 | + instants.offsetDateTime = OffsetDateTime.ofInstant( now, zone ); |
| 45 | + instants.localDateTimeUtc = LocalDateTime.ofInstant( now, ZoneOffset.UTC ); |
| 46 | + instants.offsetDateTimeUtc = OffsetDateTime.ofInstant( now, ZoneOffset.UTC ); |
| 47 | + session.persist( instants ); |
| 48 | + } ); |
| 49 | + |
| 50 | + scope.inTransaction( (session) -> { |
| 51 | + final Instants instants = session.find( Instants.class, 0 ); |
| 52 | + assertEqualInstants( now, instants.instantInUtc ); |
| 53 | + assertEqualInstants( now, instants.instantInLocalTimeZone ); |
| 54 | + assertEqualInstants( now, instants.instantWithTimeZone ); |
| 55 | + assertEqualInstants( now, instants.offsetDateTime.toInstant() ); |
| 56 | + assertEqualInstants( now, instants.localDateTime.toInstant( zone ) ); |
| 57 | + assertEqualInstants( now, instants.offsetDateTimeUtc.toInstant() ); |
| 58 | + assertEqualInstants( now, instants.localDateTimeUtc.toInstant( ZoneOffset.UTC ) ); |
| 59 | + } ); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testQueryRestriction(SessionFactoryScope scope) { |
| 64 | + final Instant instant = Instant.from( DateTimeFormatter.ISO_INSTANT.parse( "2025-02-27T01:01:01.123Z" ) ); |
| 65 | + |
| 66 | + scope.inTransaction( (session) -> { |
| 67 | + session.persist( new Instants( 1, instant ) ); |
| 68 | + } ); |
| 69 | + |
| 70 | + // parameter |
| 71 | + scope.inTransaction( (session) -> { |
| 72 | + final String queryText = "select id from Instants where instantInUtc = :p"; |
| 73 | + final List<Long> matches = session.createSelectionQuery( queryText, Long.class ) |
| 74 | + .setParameter( "p", instant ) |
| 75 | + .list(); |
| 76 | + assertThat( matches ).hasSize( 1 ); |
| 77 | + } ); |
| 78 | + |
| 79 | + // literal |
| 80 | + scope.inTransaction( (session) -> { |
| 81 | + final String queryText = "select id from Instants where instantInUtc = zoned datetime 2025-02-27 01:01:01.123Z"; |
| 82 | + final List<Long> matches = session.createSelectionQuery( queryText, Long.class ) |
| 83 | + .list(); |
| 84 | + assertThat( matches ).hasSize( 1 ); |
| 85 | + } ); |
| 86 | + } |
| 87 | + |
| 88 | + @AfterEach |
| 89 | + void dropTestData(SessionFactoryScope scope) { |
| 90 | + scope.dropData(); |
53 | 91 | } |
| 92 | + |
54 | 93 | void assertEqualInstants(Instant x, Instant y) { |
55 | 94 | assertEquals( x.truncatedTo( ChronoUnit.SECONDS ), y.truncatedTo( ChronoUnit.SECONDS ) ); |
56 | 95 | } |
| 96 | + |
57 | 97 | @Entity(name="Instants") |
58 | 98 | static class Instants { |
59 | | - @Id long id; |
| 99 | + @Id |
| 100 | + long id; |
60 | 101 | Instant instantInUtc; |
61 | 102 | @JdbcTypeCode(TIMESTAMP) Instant instantInLocalTimeZone; |
62 | 103 | @JdbcTypeCode(TIMESTAMP_WITH_TIMEZONE) Instant instantWithTimeZone; |
63 | 104 | LocalDateTime localDateTime; |
64 | 105 | OffsetDateTime offsetDateTime; |
65 | 106 | LocalDateTime localDateTimeUtc; |
66 | 107 | OffsetDateTime offsetDateTimeUtc; |
| 108 | + |
| 109 | + public Instants() { |
| 110 | + } |
| 111 | + |
| 112 | + public Instants(long id, Instant instantInUtc) { |
| 113 | + this.id = id; |
| 114 | + this.instantInUtc = instantInUtc; |
| 115 | + } |
67 | 116 | } |
68 | 117 | } |
0 commit comments