From cad7fb3d4003de6f5d18908ecd6c58e2313ddb41 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Mon, 7 Aug 2023 17:19:22 +0200 Subject: [PATCH] Improve test reliability on Sybase --- .../orm/test/timezones/AutoZonedTest.java | 55 ++++++++++-------- .../orm/test/timezones/ColumnZonedTest.java | 55 ++++++++++-------- .../orm/test/timezones/DefaultZonedTest.java | 54 ++++++++++-------- .../test/timezones/JDBCTimeZoneZonedTest.java | 57 ++++++++++--------- .../orm/test/timezones/PassThruZonedTest.java | 57 ++++++++++--------- .../timezones/UTCNormalizedZonedTest.java | 55 ++++++++++-------- 6 files changed, 181 insertions(+), 152 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/AutoZonedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/AutoZonedTest.java index fab1d95e95..421fd7deb3 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/AutoZonedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/AutoZonedTest.java @@ -1,8 +1,11 @@ package org.hibernate.orm.test.timezones; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoField; + import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.SybaseDialect; @@ -15,11 +18,9 @@ import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.Setting; import org.junit.jupiter.api.Test; -import java.time.OffsetDateTime; -import java.time.ZoneId; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.time.temporal.ChronoUnit; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -29,8 +30,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class AutoZonedTest { @Test void test(SessionFactoryScope scope) { - ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); - OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); + final ZonedDateTime nowZoned; + final OffsetDateTime nowOffset; + if ( scope.getSessionFactory().getJdbcServices().getDialect() instanceof SybaseDialect ) { + // 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 { + nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); + nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); + } long id = scope.fromTransaction( s-> { Zoned z = new Zoned(); z.zonedDateTime = nowZoned; @@ -41,21 +53,14 @@ public class AutoZonedTest { scope.inSession( s -> { Zoned z = s.find(Zoned.class, id); final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect(); - if ( dialect instanceof SybaseDialect ) { - // Sybase with jTDS driver has 1/300th sec precision - assertEquals( nowZoned.toInstant().truncatedTo(ChronoUnit.SECONDS), z.zonedDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS) ); - assertEquals( nowOffset.toInstant().truncatedTo(ChronoUnit.SECONDS), z.offsetDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS) ); - } - else { - assertEquals( - DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), - DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) - ); - assertEquals( - DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), - DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) - ); - } + assertEquals( + DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), + DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) + ); + assertEquals( + DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), + DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) + ); assertEquals( nowZoned.toOffsetDateTime().getOffset(), z.zonedDateTime.toOffsetDateTime().getOffset() ); assertEquals( nowOffset.getOffset(), z.offsetDateTime.getOffset() ); }); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/ColumnZonedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/ColumnZonedTest.java index de44584745..dbbcb3a62e 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/ColumnZonedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/ColumnZonedTest.java @@ -1,8 +1,11 @@ package org.hibernate.orm.test.timezones; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoField; + import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.SybaseDialect; @@ -15,11 +18,9 @@ import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.Setting; import org.junit.jupiter.api.Test; -import java.time.OffsetDateTime; -import java.time.ZoneId; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.time.temporal.ChronoUnit; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -29,8 +30,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class ColumnZonedTest { @Test void test(SessionFactoryScope scope) { - ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); - OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); + final ZonedDateTime nowZoned; + final OffsetDateTime nowOffset; + if ( scope.getSessionFactory().getJdbcServices().getDialect() instanceof SybaseDialect ) { + // 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 { + nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); + nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); + } long id = scope.fromTransaction( s-> { Zoned z = new Zoned(); z.zonedDateTime = nowZoned; @@ -41,21 +53,14 @@ public class ColumnZonedTest { scope.inSession( s -> { Zoned z = s.find(Zoned.class, id); final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect(); - if ( dialect instanceof SybaseDialect) { - // Sybase with jTDS driver has 1/300th sec precision - assertEquals( nowZoned.toInstant().truncatedTo(ChronoUnit.SECONDS), z.zonedDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS) ); - assertEquals( nowOffset.toInstant().truncatedTo(ChronoUnit.SECONDS), z.offsetDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS) ); - } - else { - assertEquals( - DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), - DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) - ); - assertEquals( - DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), - DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) - ); - } + assertEquals( + DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), + DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) + ); + assertEquals( + DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), + DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) + ); assertEquals( nowZoned.toOffsetDateTime().getOffset(), z.zonedDateTime.toOffsetDateTime().getOffset() ); assertEquals( nowOffset.getOffset(), z.offsetDateTime.getOffset() ); }); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/DefaultZonedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/DefaultZonedTest.java index 6b8fa956d5..0bb8a1f3b2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/DefaultZonedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/DefaultZonedTest.java @@ -1,8 +1,10 @@ package org.hibernate.orm.test.timezones; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoField; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.SybaseDialect; @@ -14,11 +16,9 @@ import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.junit.jupiter.api.Test; -import java.time.OffsetDateTime; -import java.time.ZoneId; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.time.temporal.ChronoUnit; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -27,8 +27,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class DefaultZonedTest { @Test void test(SessionFactoryScope scope) { - ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); - OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); + final ZonedDateTime nowZoned; + final OffsetDateTime nowOffset; + if ( scope.getSessionFactory().getJdbcServices().getDialect() instanceof SybaseDialect ) { + // 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 { + nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); + nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); + } long id = scope.fromTransaction( s-> { Zoned z = new Zoned(); z.zonedDateTime = nowZoned; @@ -39,21 +50,14 @@ public class DefaultZonedTest { scope.inSession( s -> { Zoned z = s.find(Zoned.class, id); final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect(); - if ( dialect instanceof SybaseDialect) { - // Sybase with jTDS driver has 1/300th sec precision - assertEquals( nowZoned.toInstant().truncatedTo(ChronoUnit.SECONDS), z.zonedDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS) ); - assertEquals( nowOffset.toInstant().truncatedTo(ChronoUnit.SECONDS), z.offsetDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS) ); - } - else { - assertEquals( - DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), - DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) - ); - assertEquals( - DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), - DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) - ); - } + assertEquals( + DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), + DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) + ); + assertEquals( + DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), + DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) + ); if ( dialect.getTimeZoneSupport() == TimeZoneSupport.NATIVE ) { assertEquals( nowZoned.toOffsetDateTime().getOffset(), z.zonedDateTime.toOffsetDateTime().getOffset() ); assertEquals( nowOffset.getOffset(), z.offsetDateTime.getOffset() ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/JDBCTimeZoneZonedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/JDBCTimeZoneZonedTest.java index ed2bae14da..f77bc9ced6 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/JDBCTimeZoneZonedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/JDBCTimeZoneZonedTest.java @@ -1,8 +1,12 @@ package org.hibernate.orm.test.timezones; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; +import java.time.Instant; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoField; + import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.SybaseDialect; @@ -15,12 +19,9 @@ import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.Setting; import org.junit.jupiter.api.Test; -import java.time.Instant; -import java.time.OffsetDateTime; -import java.time.ZoneId; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.time.temporal.ChronoUnit; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -31,8 +32,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class JDBCTimeZoneZonedTest { @Test void test(SessionFactoryScope scope) { - ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); - OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours( 3) ); + final ZonedDateTime nowZoned; + final OffsetDateTime nowOffset; + if ( scope.getSessionFactory().getJdbcServices().getDialect() instanceof SybaseDialect ) { + // 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 { + nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); + nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); + } long id = scope.fromTransaction( s-> { Zoned z = new Zoned(); z.zonedDateTime = nowZoned; @@ -45,21 +57,14 @@ public class JDBCTimeZoneZonedTest { ZoneId systemZone = ZoneId.systemDefault(); ZoneOffset systemOffset = systemZone.getRules().getOffset( Instant.now() ); final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect(); - if ( dialect instanceof SybaseDialect) { - // Sybase with jTDS driver has 1/300th sec precision - assertEquals( nowZoned.toInstant().truncatedTo(ChronoUnit.SECONDS), z.zonedDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS)); - assertEquals( nowOffset.toInstant().truncatedTo(ChronoUnit.SECONDS), z.offsetDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS)); - } - else { - assertEquals( - DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), - DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) - ); - assertEquals( - DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), - DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) - ); - } + assertEquals( + DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), + DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) + ); + assertEquals( + DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), + DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) + ); assertEquals( systemZone, z.zonedDateTime.getZone() ); assertEquals( systemOffset, z.offsetDateTime.getOffset() ); }); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/PassThruZonedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/PassThruZonedTest.java index 85f897adad..8404eb25e4 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/PassThruZonedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/PassThruZonedTest.java @@ -1,8 +1,12 @@ package org.hibernate.orm.test.timezones; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; +import java.time.Instant; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoField; + import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.SybaseDialect; @@ -15,12 +19,9 @@ import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.Setting; import org.junit.jupiter.api.Test; -import java.time.Instant; -import java.time.OffsetDateTime; -import java.time.ZoneId; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.time.temporal.ChronoUnit; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -30,8 +31,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class PassThruZonedTest { @Test void test(SessionFactoryScope scope) { - ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); - OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours( 3) ); + final ZonedDateTime nowZoned; + final OffsetDateTime nowOffset; + if ( scope.getSessionFactory().getJdbcServices().getDialect() instanceof SybaseDialect ) { + // 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 { + nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); + nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); + } long id = scope.fromTransaction( s-> { Zoned z = new Zoned(); z.zonedDateTime = nowZoned; @@ -44,21 +56,14 @@ public class PassThruZonedTest { ZoneId systemZone = ZoneId.systemDefault(); ZoneOffset systemOffset = systemZone.getRules().getOffset( Instant.now() ); final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect(); - if ( dialect instanceof SybaseDialect) { - // Sybase with jTDS driver has 1/300th sec precision - assertEquals( nowZoned.toInstant().truncatedTo(ChronoUnit.SECONDS), z.zonedDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS)); - assertEquals( nowOffset.toInstant().truncatedTo(ChronoUnit.SECONDS), z.offsetDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS)); - } - else { - assertEquals( - DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), - DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) - ); - assertEquals( - DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), - DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) - ); - } + assertEquals( + DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), + DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) + ); + assertEquals( + DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), + DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) + ); assertEquals( systemZone, z.zonedDateTime.getZone() ); assertEquals( systemOffset, z.offsetDateTime.getOffset() ); }); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/UTCNormalizedZonedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/UTCNormalizedZonedTest.java index 00306c9622..cbab481e0f 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/UTCNormalizedZonedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/UTCNormalizedZonedTest.java @@ -1,8 +1,11 @@ package org.hibernate.orm.test.timezones; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.Id; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoField; + import org.hibernate.cfg.AvailableSettings; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.SybaseDialect; @@ -15,11 +18,9 @@ import org.hibernate.testing.orm.junit.SessionFactoryScope; import org.hibernate.testing.orm.junit.Setting; import org.junit.jupiter.api.Test; -import java.time.OffsetDateTime; -import java.time.ZoneId; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.time.temporal.ChronoUnit; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -29,8 +30,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class UTCNormalizedZonedTest { @Test void test(SessionFactoryScope scope) { - ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); - OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); + final ZonedDateTime nowZoned; + final OffsetDateTime nowOffset; + if ( scope.getSessionFactory().getJdbcServices().getDialect() instanceof SybaseDialect ) { + // 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 { + nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); + nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); + } long id = scope.fromTransaction( s-> { Zoned z = new Zoned(); z.zonedDateTime = nowZoned; @@ -41,21 +53,14 @@ public class UTCNormalizedZonedTest { scope.inSession( s-> { Zoned z = s.find(Zoned.class, id); final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect(); - if ( dialect instanceof SybaseDialect) { - // Sybase with jTDS driver has 1/300th sec precision - assertEquals( nowZoned.toInstant().truncatedTo(ChronoUnit.SECONDS), z.zonedDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS) ); - assertEquals( nowOffset.toInstant().truncatedTo(ChronoUnit.SECONDS), z.offsetDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS) ); - } - else { - assertEquals( - DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), - DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) - ); - assertEquals( - DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), - DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) - ); - } + assertEquals( + DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), + DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) + ); + assertEquals( + DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), + DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) + ); assertEquals( ZoneId.of("Z"), z.zonedDateTime.getZone() ); assertEquals( ZoneOffset.ofHours(0), z.offsetDateTime.getOffset() ); });