Skip to content

Commit a93e8e6

Browse files
cigalybeikov
authored andcommitted
HHH-18881 Modified test case from Jira issue
1 parent 26bbaac commit a93e8e6

File tree

1 file changed

+166
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)