Skip to content

Commit

Permalink
[#1724] Align timezones tests to changes applied to ORM corresponding…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
dreab8 committed Jan 21, 2025
1 parent d2fff38 commit 6c00893
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.List;

Expand All @@ -29,8 +30,11 @@
import static org.hibernate.cfg.AvailableSettings.TIMEZONE_DEFAULT_STORAGE;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
import static org.hibernate.reactive.testing.ReactiveAssertions.assertWithTruncationThat;
import static org.hibernate.type.descriptor.DateTimeUtils.roundToDefaultPrecision;
import static org.hibernate.type.descriptor.DateTimeUtils.adjustToDefaultPrecision;

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

@Test
public void test(VertxTestContext context) {
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of( "CET" ) );
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours( 3 ) );
final ZonedDateTime nowZoned;
final OffsetDateTime nowOffset;
if ( getDialect().getDefaultTimestampPrecision() == 6 ) {
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ).truncatedTo( ChronoUnit.MICROS );
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ).truncatedTo( ChronoUnit.MICROS );
}
else {
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") );
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) );
}
test( context, getSessionFactory()
.withTransaction( s -> {
Zoned z = new Zoned();
Expand All @@ -60,10 +72,10 @@ public void test(VertxTestContext context) {
.thenCompose( zid -> openSession()
.thenCompose( s -> s.find( Zoned.class, zid )
.thenAccept( z -> {
assertWithTruncationThat( roundToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
.isEqualTo( roundToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
assertWithTruncationThat( roundToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
.isEqualTo( roundToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
assertWithTruncationThat( adjustToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
.isEqualTo( adjustToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
assertWithTruncationThat( adjustToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
.isEqualTo( adjustToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
assertThat( z.zonedDateTime.toOffsetDateTime().getOffset() )
.isEqualTo( nowZoned.toOffsetDateTime().getOffset() );
assertThat( z.offsetDateTime.getOffset() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.List;

Expand All @@ -29,8 +30,11 @@
import static org.hibernate.cfg.AvailableSettings.TIMEZONE_DEFAULT_STORAGE;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
import static org.hibernate.reactive.testing.ReactiveAssertions.assertWithTruncationThat;
import static org.hibernate.type.descriptor.DateTimeUtils.roundToDefaultPrecision;
import static org.hibernate.type.descriptor.DateTimeUtils.adjustToDefaultPrecision;

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

@Test
public void test(VertxTestContext context) {
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of( "CET" ) );
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours( 3 ) );
final ZonedDateTime nowZoned;
final OffsetDateTime nowOffset;
if ( getDialect().getDefaultTimestampPrecision() == 6 ) {
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ).truncatedTo( ChronoUnit.MICROS );
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ).truncatedTo( ChronoUnit.MICROS );
}
else {
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") );
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) );
}
test( context, getSessionFactory()
.withTransaction( s -> {
Zoned z = new Zoned();
Expand All @@ -60,10 +72,10 @@ public void test(VertxTestContext context) {
.thenCompose( zid -> openSession()
.thenCompose( s -> s.find( Zoned.class, zid )
.thenAccept( z -> {
assertWithTruncationThat( roundToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
.isEqualTo( roundToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
assertWithTruncationThat( roundToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
.isEqualTo( roundToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
assertWithTruncationThat( adjustToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
.isEqualTo( adjustToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
assertWithTruncationThat( adjustToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
.isEqualTo( adjustToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
assertThat( z.zonedDateTime.toOffsetDateTime().getOffset() )
.isEqualTo( nowZoned.toOffsetDateTime().getOffset() );
assertThat( z.offsetDateTime.getOffset() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.List;

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

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

@Test
public void test(VertxTestContext context) {
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of( "CET" ) );
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours( 3 ) );
final ZonedDateTime nowZoned;
final OffsetDateTime nowOffset;
if ( getDialect().getDefaultTimestampPrecision() == 6 ) {
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ).truncatedTo( ChronoUnit.MICROS );
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ).truncatedTo( ChronoUnit.MICROS );
}
else {
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") );
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) );
}
test( context, getSessionFactory()
.withTransaction( s -> {
Zoned z = new Zoned();
Expand All @@ -53,10 +65,10 @@ public void test(VertxTestContext context) {
.thenCompose( zid -> openSession()
.thenCompose( s -> s.find( Zoned.class, zid )
.thenAccept( z -> {
assertWithTruncationThat( roundToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
.isEqualTo( roundToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
assertWithTruncationThat( roundToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
.isEqualTo( roundToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
assertWithTruncationThat( adjustToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
.isEqualTo( adjustToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
assertWithTruncationThat( adjustToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
.isEqualTo( adjustToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );

if ( getDialect().getTimeZoneSupport() == TimeZoneSupport.NATIVE ) {
assertThat( z.zonedDateTime.toOffsetDateTime().getOffset() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.List;

import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.SybaseDialect;
import org.hibernate.reactive.BaseReactiveTest;
import org.hibernate.reactive.annotations.DisabledFor;

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

/**
* Test adapted from {@link org.hibernate.orm.test.timezones.JDBCTimeZoneZonedTest}
*/
@Timeout(value = 10, timeUnit = MINUTES)
@DisabledFor(value = DB2, reason = "Exception: IllegalStateException: Needed to have 6 in buffer but only had 0")
public class JDBCTimeZoneZonedTest extends BaseReactiveTest {
Expand All @@ -58,15 +57,7 @@ protected void setProperties(Configuration configuration) {
public void test(VertxTestContext context) {
final ZonedDateTime nowZoned;
final OffsetDateTime nowOffset;
final Dialect dialect = getDialect();
if ( dialect instanceof SybaseDialect || dialect instanceof MySQLDialect ) {
// Sybase has 1/300th sec precision
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") )
.with( ChronoField.NANO_OF_SECOND, 0L );
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) )
.with( ChronoField.NANO_OF_SECOND, 0L );
}
else if ( dialect.getDefaultTimestampPrecision() == 6 ) {
if ( getDialect().getDefaultTimestampPrecision() == 6 ) {
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ).truncatedTo( ChronoUnit.MICROS );
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ).truncatedTo( ChronoUnit.MICROS );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.List;

Expand All @@ -30,8 +31,11 @@
import static org.hibernate.cfg.AvailableSettings.TIMEZONE_DEFAULT_STORAGE;
import static org.hibernate.reactive.testing.ReactiveAssertions.assertWithTruncationThat;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
import static org.hibernate.type.descriptor.DateTimeUtils.roundToDefaultPrecision;
import static org.hibernate.type.descriptor.DateTimeUtils.adjustToDefaultPrecision;

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

@Test
public void test(VertxTestContext context) {
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of( "CET" ) );
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours( 3 ) );
final ZonedDateTime nowZoned;
final OffsetDateTime nowOffset;
if ( getDialect().getDefaultTimestampPrecision() == 6 ) {
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ).truncatedTo( ChronoUnit.MICROS );
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ).truncatedTo( ChronoUnit.MICROS );
}
else {
nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") );
nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) );
}
test( context, getSessionFactory()
.withTransaction( s -> {
Zoned z = new Zoned();
Expand All @@ -61,10 +73,10 @@ public void test(VertxTestContext context) {
.thenCompose( zid -> openSession()
.thenCompose( s -> s.find( Zoned.class, zid )
.thenAccept( z -> {
assertWithTruncationThat( roundToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
.isEqualTo( roundToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
assertWithTruncationThat( roundToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
.isEqualTo( roundToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );
assertWithTruncationThat( adjustToDefaultPrecision( z.zonedDateTime.toInstant(), getDialect() ) )
.isEqualTo( adjustToDefaultPrecision( nowZoned.toInstant(), getDialect() ) );
assertWithTruncationThat( adjustToDefaultPrecision( z.offsetDateTime.toInstant(), getDialect() ) )
.isEqualTo( adjustToDefaultPrecision( nowOffset.toInstant(), getDialect() ) );

ZoneId systemZone = ZoneId.systemDefault();
ZoneOffset systemOffset = systemZone.getRules().getOffset( Instant.now() );
Expand Down

0 comments on commit 6c00893

Please sign in to comment.