Skip to content

Commit c27e5ec

Browse files
dreab8DavideD
authored andcommitted
[#1724] Align timezones tests to changes applied to ORM corresponding tests
1 parent 9448e1f commit c27e5ec

File tree

5 files changed

+80
-41
lines changed

5 files changed

+80
-41
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/timezones/AutoZonedTest.java

+19-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.time.ZoneId;
2020
import java.time.ZoneOffset;
2121
import java.time.ZonedDateTime;
22+
import java.time.temporal.ChronoUnit;
2223
import java.util.Collection;
2324
import java.util.List;
2425

@@ -29,8 +30,11 @@
2930
import static org.hibernate.cfg.AvailableSettings.TIMEZONE_DEFAULT_STORAGE;
3031
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
3132
import static org.hibernate.reactive.testing.ReactiveAssertions.assertWithTruncationThat;
32-
import static org.hibernate.type.descriptor.DateTimeUtils.roundToDefaultPrecision;
33+
import static org.hibernate.type.descriptor.DateTimeUtils.adjustToDefaultPrecision;
3334

35+
/**
36+
* Test adapted from {@link org.hibernate.orm.test.timezones.AutoZonedTest}
37+
*/
3438
@Timeout(value = 10, timeUnit = MINUTES)
3539
@DisabledFor(value = DB2, reason = "Exception: IllegalStateException: Needed to have 6 in buffer but only had 0")
3640
public class AutoZonedTest extends BaseReactiveTest {
@@ -48,8 +52,16 @@ protected void setProperties(Configuration configuration) {
4852

4953
@Test
5054
public void test(VertxTestContext context) {
51-
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of( "CET" ) );
52-
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours( 3 ) );
55+
final ZonedDateTime nowZoned;
56+
final OffsetDateTime nowOffset;
57+
if ( getDialect().getDefaultTimestampPrecision() == 6 ) {
58+
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ).truncatedTo( ChronoUnit.MICROS );
59+
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ).truncatedTo( ChronoUnit.MICROS );
60+
}
61+
else {
62+
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") );
63+
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) );
64+
}
5365
test( context, getSessionFactory()
5466
.withTransaction( s -> {
5567
Zoned z = new Zoned();
@@ -60,10 +72,10 @@ public void test(VertxTestContext context) {
6072
.thenCompose( zid -> openSession()
6173
.thenCompose( s -> s.find( Zoned.class, zid )
6274
.thenAccept( z -> {
63-
assertWithTruncationThat( roundToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
64-
.isEqualTo( roundToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
65-
assertWithTruncationThat( roundToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
66-
.isEqualTo( roundToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
75+
assertWithTruncationThat( adjustToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
76+
.isEqualTo( adjustToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
77+
assertWithTruncationThat( adjustToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
78+
.isEqualTo( adjustToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
6779
assertThat( z.zonedDateTime.toOffsetDateTime().getOffset() )
6880
.isEqualTo( nowZoned.toOffsetDateTime().getOffset() );
6981
assertThat( z.offsetDateTime.getOffset() )

hibernate-reactive-core/src/test/java/org/hibernate/reactive/timezones/ColumnZonedTest.java

+19-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.time.ZoneId;
1010
import java.time.ZoneOffset;
1111
import java.time.ZonedDateTime;
12+
import java.time.temporal.ChronoUnit;
1213
import java.util.Collection;
1314
import java.util.List;
1415

@@ -29,8 +30,11 @@
2930
import static org.hibernate.cfg.AvailableSettings.TIMEZONE_DEFAULT_STORAGE;
3031
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
3132
import static org.hibernate.reactive.testing.ReactiveAssertions.assertWithTruncationThat;
32-
import static org.hibernate.type.descriptor.DateTimeUtils.roundToDefaultPrecision;
33+
import static org.hibernate.type.descriptor.DateTimeUtils.adjustToDefaultPrecision;
3334

35+
/**
36+
* Test adapted from {@link org.hibernate.orm.test.timezones.ColumnZonedTest}
37+
*/
3438
@Timeout(value = 10, timeUnit = MINUTES)
3539
@DisabledFor(value = DB2, reason = "java.sql.SQLException: An error occurred with a DB2 operation, SQLCODE=-180 SQLSTATE=22007")
3640
public class ColumnZonedTest extends BaseReactiveTest {
@@ -48,8 +52,16 @@ protected void setProperties(Configuration configuration) {
4852

4953
@Test
5054
public void test(VertxTestContext context) {
51-
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of( "CET" ) );
52-
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours( 3 ) );
55+
final ZonedDateTime nowZoned;
56+
final OffsetDateTime nowOffset;
57+
if ( getDialect().getDefaultTimestampPrecision() == 6 ) {
58+
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ).truncatedTo( ChronoUnit.MICROS );
59+
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ).truncatedTo( ChronoUnit.MICROS );
60+
}
61+
else {
62+
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") );
63+
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) );
64+
}
5365
test( context, getSessionFactory()
5466
.withTransaction( s -> {
5567
Zoned z = new Zoned();
@@ -60,10 +72,10 @@ public void test(VertxTestContext context) {
6072
.thenCompose( zid -> openSession()
6173
.thenCompose( s -> s.find( Zoned.class, zid )
6274
.thenAccept( z -> {
63-
assertWithTruncationThat( roundToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
64-
.isEqualTo( roundToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
65-
assertWithTruncationThat( roundToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
66-
.isEqualTo( roundToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
75+
assertWithTruncationThat( adjustToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
76+
.isEqualTo( adjustToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
77+
assertWithTruncationThat( adjustToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
78+
.isEqualTo( adjustToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
6779
assertThat( z.zonedDateTime.toOffsetDateTime().getOffset() )
6880
.isEqualTo( nowZoned.toOffsetDateTime().getOffset() );
6981
assertThat( z.offsetDateTime.getOffset() )

hibernate-reactive-core/src/test/java/org/hibernate/reactive/timezones/DefaultZonedTest.java

+19-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.time.ZoneId;
1010
import java.time.ZoneOffset;
1111
import java.time.ZonedDateTime;
12+
import java.time.temporal.ChronoUnit;
1213
import java.util.Collection;
1314
import java.util.List;
1415

@@ -28,8 +29,11 @@
2829
import static org.assertj.core.api.Assertions.assertThat;
2930
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
3031
import static org.hibernate.reactive.testing.ReactiveAssertions.assertWithTruncationThat;
31-
import static org.hibernate.type.descriptor.DateTimeUtils.roundToDefaultPrecision;
32+
import static org.hibernate.type.descriptor.DateTimeUtils.adjustToDefaultPrecision;
3233

34+
/**
35+
* Test adapted from {@link org.hibernate.orm.test.timezones.DefaultZonedTest}
36+
*/
3337
@Timeout(value = 10, timeUnit = MINUTES)
3438
@DisabledFor(value = DB2, reason = "Exception: IllegalStateException: Needed to have 6 in buffer but only had 0")
3539
public class DefaultZonedTest extends BaseReactiveTest {
@@ -41,8 +45,16 @@ protected Collection<Class<?>> annotatedEntities() {
4145

4246
@Test
4347
public void test(VertxTestContext context) {
44-
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of( "CET" ) );
45-
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours( 3 ) );
48+
final ZonedDateTime nowZoned;
49+
final OffsetDateTime nowOffset;
50+
if ( getDialect().getDefaultTimestampPrecision() == 6 ) {
51+
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ).truncatedTo( ChronoUnit.MICROS );
52+
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ).truncatedTo( ChronoUnit.MICROS );
53+
}
54+
else {
55+
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") );
56+
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) );
57+
}
4658
test( context, getSessionFactory()
4759
.withTransaction( s -> {
4860
Zoned z = new Zoned();
@@ -53,10 +65,10 @@ public void test(VertxTestContext context) {
5365
.thenCompose( zid -> openSession()
5466
.thenCompose( s -> s.find( Zoned.class, zid )
5567
.thenAccept( z -> {
56-
assertWithTruncationThat( roundToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
57-
.isEqualTo( roundToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
58-
assertWithTruncationThat( roundToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
59-
.isEqualTo( roundToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
68+
assertWithTruncationThat( adjustToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
69+
.isEqualTo( adjustToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
70+
assertWithTruncationThat( adjustToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
71+
.isEqualTo( adjustToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
6072

6173
if ( getDialect().getTimeZoneSupport() == TimeZoneSupport.NATIVE ) {
6274
assertThat( z.zonedDateTime.toOffsetDateTime().getOffset() )

hibernate-reactive-core/src/test/java/org/hibernate/reactive/timezones/JDBCTimeZoneZonedTest.java

+4-13
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@
1010
import java.time.ZoneId;
1111
import java.time.ZoneOffset;
1212
import java.time.ZonedDateTime;
13-
import java.time.temporal.ChronoField;
1413
import java.time.temporal.ChronoUnit;
1514
import java.util.Collection;
1615
import java.util.List;
1716

1817
import org.hibernate.cfg.Configuration;
19-
import org.hibernate.dialect.Dialect;
20-
import org.hibernate.dialect.MySQLDialect;
21-
import org.hibernate.dialect.SybaseDialect;
2218
import org.hibernate.reactive.BaseReactiveTest;
2319
import org.hibernate.reactive.annotations.DisabledFor;
2420

@@ -38,6 +34,9 @@
3834
import static org.hibernate.reactive.testing.ReactiveAssertions.assertWithTruncationThat;
3935
import static org.hibernate.type.descriptor.DateTimeUtils.adjustToDefaultPrecision;
4036

37+
/**
38+
* Test adapted from {@link org.hibernate.orm.test.timezones.JDBCTimeZoneZonedTest}
39+
*/
4140
@Timeout(value = 10, timeUnit = MINUTES)
4241
@DisabledFor(value = DB2, reason = "Exception: IllegalStateException: Needed to have 6 in buffer but only had 0")
4342
public class JDBCTimeZoneZonedTest extends BaseReactiveTest {
@@ -58,15 +57,7 @@ protected void setProperties(Configuration configuration) {
5857
public void test(VertxTestContext context) {
5958
final ZonedDateTime nowZoned;
6059
final OffsetDateTime nowOffset;
61-
final Dialect dialect = getDialect();
62-
if ( dialect instanceof SybaseDialect || dialect instanceof MySQLDialect ) {
63-
// Sybase has 1/300th sec precision
64-
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") )
65-
.with( ChronoField.NANO_OF_SECOND, 0L );
66-
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) )
67-
.with( ChronoField.NANO_OF_SECOND, 0L );
68-
}
69-
else if ( dialect.getDefaultTimestampPrecision() == 6 ) {
60+
if ( getDialect().getDefaultTimestampPrecision() == 6 ) {
7061
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ).truncatedTo( ChronoUnit.MICROS );
7162
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ).truncatedTo( ChronoUnit.MICROS );
7263
}

hibernate-reactive-core/src/test/java/org/hibernate/reactive/timezones/PassThruZonedTest.java

+19-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.time.ZoneId;
1111
import java.time.ZoneOffset;
1212
import java.time.ZonedDateTime;
13+
import java.time.temporal.ChronoUnit;
1314
import java.util.Collection;
1415
import java.util.List;
1516

@@ -30,8 +31,11 @@
3031
import static org.hibernate.cfg.AvailableSettings.TIMEZONE_DEFAULT_STORAGE;
3132
import static org.hibernate.reactive.testing.ReactiveAssertions.assertWithTruncationThat;
3233
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
33-
import static org.hibernate.type.descriptor.DateTimeUtils.roundToDefaultPrecision;
34+
import static org.hibernate.type.descriptor.DateTimeUtils.adjustToDefaultPrecision;
3435

36+
/**
37+
* Test adapted from {@link org.hibernate.orm.test.timezones.PassThruZonedTest}
38+
*/
3539
@Timeout(value = 10, timeUnit = MINUTES)
3640
@DisabledFor(value = DB2, reason = "Exception: SQLException: An error occurred with a DB2 operation, SQLCODE=-180 SQLSTATE=22007")
3741
public class PassThruZonedTest extends BaseReactiveTest {
@@ -49,8 +53,16 @@ protected void setProperties(Configuration configuration) {
4953

5054
@Test
5155
public void test(VertxTestContext context) {
52-
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of( "CET" ) );
53-
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours( 3 ) );
56+
final ZonedDateTime nowZoned;
57+
final OffsetDateTime nowOffset;
58+
if ( getDialect().getDefaultTimestampPrecision() == 6 ) {
59+
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ).truncatedTo( ChronoUnit.MICROS );
60+
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ).truncatedTo( ChronoUnit.MICROS );
61+
}
62+
else {
63+
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") );
64+
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) );
65+
}
5466
test( context, getSessionFactory()
5567
.withTransaction( s -> {
5668
Zoned z = new Zoned();
@@ -61,10 +73,10 @@ public void test(VertxTestContext context) {
6173
.thenCompose( zid -> openSession()
6274
.thenCompose( s -> s.find( Zoned.class, zid )
6375
.thenAccept( z -> {
64-
assertWithTruncationThat( roundToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
65-
.isEqualTo( roundToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
66-
assertWithTruncationThat( roundToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
67-
.isEqualTo( roundToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
76+
assertWithTruncationThat( adjustToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
77+
.isEqualTo( adjustToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
78+
assertWithTruncationThat( adjustToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
79+
.isEqualTo( adjustToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
6880

6981
ZoneId systemZone = ZoneId.systemDefault();
7082
ZoneOffset systemOffset = systemZone.getRules().getOffset( Instant.now() );

0 commit comments

Comments
 (0)