From 2c8d6d719b7b4433ab3b8f9f4564c48821f88db1 Mon Sep 17 00:00:00 2001 From: Gavin Date: Thu, 8 Dec 2022 00:28:28 +0100 Subject: [PATCH] improve some assertions in time zone tests + rename --- ...tZonedTest.java => JDBCTimeZoneZonedTest.java} | 15 +++++++++------ .../orm/test/timezones/PassThruZonedTest.java | 11 +++++++---- ...ZonedTest.java => UTCNormalizedZonedTest.java} | 4 ++-- 3 files changed, 18 insertions(+), 12 deletions(-) rename hibernate-core/src/test/java/org/hibernate/orm/test/timezones/{ExplicitZonedTest.java => JDBCTimeZoneZonedTest.java} (78%) rename hibernate-core/src/test/java/org/hibernate/orm/test/timezones/{NormalizedZonedTest.java => UTCNormalizedZonedTest.java} (95%) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/ExplicitZonedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/JDBCTimeZoneZonedTest.java similarity index 78% rename from hibernate-core/src/test/java/org/hibernate/orm/test/timezones/ExplicitZonedTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/timezones/JDBCTimeZoneZonedTest.java index 3aca80d58d..d81f250fa5 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/ExplicitZonedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/JDBCTimeZoneZonedTest.java @@ -12,6 +12,7 @@ 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; @@ -20,11 +21,11 @@ import java.time.temporal.ChronoUnit; import static org.junit.jupiter.api.Assertions.assertEquals; -@DomainModel(annotatedClasses = ExplicitZonedTest.Zoned.class) +@DomainModel(annotatedClasses = JDBCTimeZoneZonedTest.Zoned.class) @SessionFactory @ServiceRegistry(settings = {@Setting(name = AvailableSettings.TIMEZONE_DEFAULT_STORAGE, value = "NORMALIZE"), @Setting(name = AvailableSettings.JDBC_TIME_ZONE, value = "GMT+5")}) -public class ExplicitZonedTest { +public class JDBCTimeZoneZonedTest { @Test void test(SessionFactoryScope scope) { ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); @@ -38,17 +39,19 @@ public class ExplicitZonedTest { }); scope.inSession( s-> { Zoned z = s.find(Zoned.class, id); + ZoneId systemZone = ZoneId.systemDefault(); + ZoneOffset systemOffset = systemZone.getRules().getOffset( Instant.now() ); if ( scope.getSessionFactory().getJdbcServices().getDialect() 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( nowZoned.toInstant(), z.zonedDateTime.toInstant() ); - assertEquals( nowOffset.toInstant(), z.offsetDateTime.toInstant() ); + assertEquals( nowZoned.withZoneSameInstant(systemZone), z.zonedDateTime ); + assertEquals( nowOffset.withOffsetSameInstant(systemOffset), z.offsetDateTime ); } - assertEquals( ZoneId.systemDefault(), z.zonedDateTime.getZone() ); - assertEquals( ZoneOffset.systemDefault().normalized(), z.offsetDateTime.getOffset().normalized() ); + 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 88dac300d7..6eea8baa92 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 @@ -12,6 +12,7 @@ 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; @@ -37,17 +38,19 @@ public class PassThruZonedTest { }); scope.inSession( s-> { Zoned z = s.find(Zoned.class, id); + ZoneId systemZone = ZoneId.systemDefault(); + ZoneOffset systemOffset = systemZone.getRules().getOffset( Instant.now() ); if ( scope.getSessionFactory().getJdbcServices().getDialect() 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( nowZoned.toInstant(), z.zonedDateTime.toInstant() ); - assertEquals( nowOffset.toInstant(), z.offsetDateTime.toInstant() ); + assertEquals( nowZoned.withZoneSameInstant(systemZone), z.zonedDateTime ); + assertEquals( nowOffset.withOffsetSameInstant(systemOffset), z.offsetDateTime ); } - assertEquals( ZoneId.systemDefault(), z.zonedDateTime.getZone() ); - assertEquals( ZoneOffset.systemDefault().normalized(), z.offsetDateTime.getOffset().normalized() ); + assertEquals( systemZone, z.zonedDateTime.getZone() ); + assertEquals( systemOffset, z.offsetDateTime.getOffset() ); }); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/NormalizedZonedTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/UTCNormalizedZonedTest.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/orm/test/timezones/NormalizedZonedTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/timezones/UTCNormalizedZonedTest.java index 7edbecec0e..3e0194f4d1 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/NormalizedZonedTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/timezones/UTCNormalizedZonedTest.java @@ -20,10 +20,10 @@ import java.time.temporal.ChronoUnit; import static org.junit.jupiter.api.Assertions.assertEquals; -@DomainModel(annotatedClasses = NormalizedZonedTest.Zoned.class) +@DomainModel(annotatedClasses = UTCNormalizedZonedTest.Zoned.class) @SessionFactory @ServiceRegistry(settings = @Setting(name = AvailableSettings.TIMEZONE_DEFAULT_STORAGE, value = "NORMALIZE_UTC")) -public class NormalizedZonedTest { +public class UTCNormalizedZonedTest { @Test void test(SessionFactoryScope scope) { ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") );