|
| 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.orm.test.mapping.array; |
| 8 | + |
| 9 | +import jakarta.persistence.Entity; |
| 10 | +import jakarta.persistence.GeneratedValue; |
| 11 | +import jakarta.persistence.Id; |
| 12 | + |
| 13 | +import org.hibernate.dialect.MariaDBDialect; |
| 14 | +import org.hibernate.dialect.MySQLDialect; |
| 15 | +import org.hibernate.sql.ast.spi.SqlAppender; |
| 16 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 17 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 18 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 19 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 20 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 21 | +import org.hibernate.type.descriptor.java.JdbcTimestampJavaType; |
| 22 | +import org.junit.jupiter.api.AfterAll; |
| 23 | +import org.junit.jupiter.api.Assertions; |
| 24 | +import org.junit.jupiter.api.BeforeAll; |
| 25 | +import org.junit.jupiter.api.Order; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import java.sql.Timestamp; |
| 29 | +import java.time.LocalDate; |
| 30 | +import java.time.LocalDateTime; |
| 31 | +import java.time.Month; |
| 32 | +import java.util.Calendar; |
| 33 | +import java.util.Date; |
| 34 | +import java.util.TimeZone; |
| 35 | +import java.util.stream.Stream; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | + |
| 39 | +@DomainModel(annotatedClasses = MySqlArrayOfTimestampsTest.Foo.class) |
| 40 | +@SessionFactory |
| 41 | +@JiraKey("HHH-18881") |
| 42 | +class MySqlArrayOfTimestampsTest { |
| 43 | + |
| 44 | + private static final LocalDateTime[] dataArray = { |
| 45 | + // Unix epoch start if you're in the UK |
| 46 | + LocalDateTime.of( 1970, Month.JANUARY, 1, 0, 0, 0, 0 ), |
| 47 | + // pre-Y2K |
| 48 | + LocalDateTime.of( 1999, Month.DECEMBER, 31, 23, 59, 59, 0 ), |
| 49 | + // We survived! Why was anyone worried? |
| 50 | + LocalDateTime.of( 2000, Month.JANUARY, 1, 0, 0, 0, 0 ), |
| 51 | + // Silence will fall! |
| 52 | + LocalDateTime.of( 2010, Month.JUNE, 26, 20, 4, 0, 0 ), |
| 53 | + // 2024 summer time |
| 54 | + LocalDateTime.of( 2024, 6, 20, 0, 0, 0 ), |
| 55 | + // 2023 winer time |
| 56 | + LocalDateTime.of( 2023, 12, 22, 0, 0, 0 ) |
| 57 | + }; |
| 58 | + |
| 59 | + private TimeZone currentDefault; |
| 60 | + |
| 61 | + @BeforeAll |
| 62 | + void setTimeZone() { |
| 63 | + currentDefault = TimeZone.getDefault(); |
| 64 | + TimeZone.setDefault( TimeZone.getTimeZone( "Europe/Zagreb" ) ); |
| 65 | + } |
| 66 | + |
| 67 | + @AfterAll |
| 68 | + void restoreTimeZone() { |
| 69 | + TimeZone.setDefault( currentDefault ); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + @Order(1) |
| 74 | + @RequiresDialect(MySQLDialect.class) |
| 75 | + @RequiresDialect(MariaDBDialect.class) |
| 76 | + public void testLocalDateTime(SessionFactoryScope scope) { |
| 77 | + |
| 78 | + final Integer basicId = scope.fromTransaction( session -> { |
| 79 | + Foo basic = new Foo(); |
| 80 | + basic.localDateTimeArray = dataArray; |
| 81 | + basic.localDateTimeField = dataArray[0]; |
| 82 | + session.persist( basic ); |
| 83 | + return basic.id; |
| 84 | + } ); |
| 85 | + |
| 86 | + scope.inTransaction( session -> { |
| 87 | + Foo found = session.find( Foo.class, basicId ); |
| 88 | + assertThat( found.localDateTimeField ).isEqualTo( dataArray[0] ); |
| 89 | + assertThat( found.localDateTimeArray ).isEqualTo( dataArray ); |
| 90 | + } ); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + @Test |
| 95 | + @Order(2) |
| 96 | + @RequiresDialect(MySQLDialect.class) |
| 97 | + @RequiresDialect(MariaDBDialect.class) |
| 98 | + public void testDate(SessionFactoryScope scope) { |
| 99 | + Date[] dataArray = {Calendar.getInstance().getTime(), Calendar.getInstance().getTime()}; |
| 100 | + |
| 101 | + final Integer basicId = scope.fromTransaction( session -> { |
| 102 | + Foo basic = new Foo(); |
| 103 | + basic.dateArray = dataArray; |
| 104 | + basic.dateField = dataArray[0]; |
| 105 | + session.persist( basic ); |
| 106 | + return basic.id; |
| 107 | + } ); |
| 108 | + |
| 109 | + scope.inTransaction( session -> { |
| 110 | + Foo found = session.find( Foo.class, basicId ); |
| 111 | + assertThat( found.dateField.getTime() ).isEqualTo( dataArray[0].getTime() ); |
| 112 | + for ( int i = 0; i < dataArray.length; i++ ) { |
| 113 | + assertThat( found.dateArray[i].getTime() ).isEqualTo( dataArray[i].getTime() ); |
| 114 | + } |
| 115 | + } ); |
| 116 | + } |
| 117 | + |
| 118 | + private static final LocalDateTime SUMMER = LocalDate.of( 2024, 6, 20 ).atStartOfDay(); |
| 119 | + private static final LocalDateTime WINTER = LocalDate.of( 2023, 12, 22 ).atStartOfDay(); |
| 120 | + private static final LocalDate EPOCH = LocalDate.of( 1970, Month.JANUARY, 1 ); |
| 121 | + |
| 122 | + private static final TimeZone[] TEST_TIME_ZONES = Stream.of( |
| 123 | + "Africa/Monrovia", |
| 124 | + "Europe/Zagreb", |
| 125 | + "Asia/Singapore", |
| 126 | + "Europe/Tallinn", |
| 127 | + "Europe/Minsk", |
| 128 | + "America/Anchorage" |
| 129 | + ).map( TimeZone::getTimeZone ).toArray( TimeZone[]::new ); |
| 130 | + |
| 131 | + @Test |
| 132 | + void encodeThenDecodeLocalDateTime() { |
| 133 | + for ( final TimeZone zone : TEST_TIME_ZONES ) { |
| 134 | + final TimeZone currentTimeZone = TimeZone.getDefault(); |
| 135 | + TimeZone.setDefault( zone ); |
| 136 | + try { |
| 137 | + for ( LocalDateTime dateTime : dataArray ) { |
| 138 | + final MySqlAppender appender = new MySqlAppender(); |
| 139 | + final Timestamp expected = Timestamp.valueOf( dateTime ); |
| 140 | + JdbcTimestampJavaType.INSTANCE.appendEncodedString( appender, expected ); |
| 141 | + final Date actual = JdbcTimestampJavaType.INSTANCE.fromEncodedString( appender.stringBuilder, 0, |
| 142 | + appender.stringBuilder.length() ); |
| 143 | + Assertions.assertEquals( expected, actual ); |
| 144 | + } |
| 145 | + } |
| 146 | + finally { |
| 147 | + TimeZone.setDefault( currentTimeZone ); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Entity(name = "Foo") |
| 153 | + public static class Foo { |
| 154 | + @Id |
| 155 | + @GeneratedValue |
| 156 | + public Integer id; |
| 157 | + public Date[] dateArray; |
| 158 | + public LocalDateTime[] localDateTimeArray; |
| 159 | + public Date dateField; |
| 160 | + public LocalDateTime localDateTimeField; |
| 161 | + } |
| 162 | + |
| 163 | + private static class MySqlAppender implements SqlAppender { |
| 164 | + |
| 165 | + private final StringBuilder stringBuilder = new StringBuilder(); |
| 166 | + |
| 167 | + @Override |
| 168 | + public void appendSql(String fragment) { |
| 169 | + stringBuilder.append( fragment ); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments