improve some assertions in time zone tests + rename

This commit is contained in:
Gavin 2022-12-08 00:28:28 +01:00 committed by Gavin King
parent 3196f2584b
commit 2c8d6d719b
3 changed files with 18 additions and 12 deletions

View File

@ -12,6 +12,7 @@ import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting; import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZoneOffset; import java.time.ZoneOffset;
@ -20,11 +21,11 @@ import java.time.temporal.ChronoUnit;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@DomainModel(annotatedClasses = ExplicitZonedTest.Zoned.class) @DomainModel(annotatedClasses = JDBCTimeZoneZonedTest.Zoned.class)
@SessionFactory @SessionFactory
@ServiceRegistry(settings = {@Setting(name = AvailableSettings.TIMEZONE_DEFAULT_STORAGE, value = "NORMALIZE"), @ServiceRegistry(settings = {@Setting(name = AvailableSettings.TIMEZONE_DEFAULT_STORAGE, value = "NORMALIZE"),
@Setting(name = AvailableSettings.JDBC_TIME_ZONE, value = "GMT+5")}) @Setting(name = AvailableSettings.JDBC_TIME_ZONE, value = "GMT+5")})
public class ExplicitZonedTest { public class JDBCTimeZoneZonedTest {
@Test void test(SessionFactoryScope scope) { @Test void test(SessionFactoryScope scope) {
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") );
@ -38,17 +39,19 @@ public class ExplicitZonedTest {
}); });
scope.inSession( s-> { scope.inSession( s-> {
Zoned z = s.find(Zoned.class, id); 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) { if ( scope.getSessionFactory().getJdbcServices().getDialect() instanceof SybaseDialect) {
// Sybase with jTDS driver has 1/300th sec precision // Sybase with jTDS driver has 1/300th sec precision
assertEquals( nowZoned.toInstant().truncatedTo(ChronoUnit.SECONDS), z.zonedDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS)); assertEquals( nowZoned.toInstant().truncatedTo(ChronoUnit.SECONDS), z.zonedDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS));
assertEquals( nowOffset.toInstant().truncatedTo(ChronoUnit.SECONDS), z.offsetDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS)); assertEquals( nowOffset.toInstant().truncatedTo(ChronoUnit.SECONDS), z.offsetDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS));
} }
else { else {
assertEquals( nowZoned.toInstant(), z.zonedDateTime.toInstant() ); assertEquals( nowZoned.withZoneSameInstant(systemZone), z.zonedDateTime );
assertEquals( nowOffset.toInstant(), z.offsetDateTime.toInstant() ); assertEquals( nowOffset.withOffsetSameInstant(systemOffset), z.offsetDateTime );
} }
assertEquals( ZoneId.systemDefault(), z.zonedDateTime.getZone() ); assertEquals( systemZone, z.zonedDateTime.getZone() );
assertEquals( ZoneOffset.systemDefault().normalized(), z.offsetDateTime.getOffset().normalized() ); assertEquals( systemOffset, z.offsetDateTime.getOffset() );
}); });
} }

View File

@ -12,6 +12,7 @@ import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.Setting; import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZoneOffset; import java.time.ZoneOffset;
@ -37,17 +38,19 @@ public class PassThruZonedTest {
}); });
scope.inSession( s-> { scope.inSession( s-> {
Zoned z = s.find(Zoned.class, id); 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) { if ( scope.getSessionFactory().getJdbcServices().getDialect() instanceof SybaseDialect) {
// Sybase with jTDS driver has 1/300th sec precision // Sybase with jTDS driver has 1/300th sec precision
assertEquals( nowZoned.toInstant().truncatedTo(ChronoUnit.SECONDS), z.zonedDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS)); assertEquals( nowZoned.toInstant().truncatedTo(ChronoUnit.SECONDS), z.zonedDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS));
assertEquals( nowOffset.toInstant().truncatedTo(ChronoUnit.SECONDS), z.offsetDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS)); assertEquals( nowOffset.toInstant().truncatedTo(ChronoUnit.SECONDS), z.offsetDateTime.toInstant().truncatedTo(ChronoUnit.SECONDS));
} }
else { else {
assertEquals( nowZoned.toInstant(), z.zonedDateTime.toInstant() ); assertEquals( nowZoned.withZoneSameInstant(systemZone), z.zonedDateTime );
assertEquals( nowOffset.toInstant(), z.offsetDateTime.toInstant() ); assertEquals( nowOffset.withOffsetSameInstant(systemOffset), z.offsetDateTime );
} }
assertEquals( ZoneId.systemDefault(), z.zonedDateTime.getZone() ); assertEquals( systemZone, z.zonedDateTime.getZone() );
assertEquals( ZoneOffset.systemDefault().normalized(), z.offsetDateTime.getOffset().normalized() ); assertEquals( systemOffset, z.offsetDateTime.getOffset() );
}); });
} }

View File

@ -20,10 +20,10 @@ import java.time.temporal.ChronoUnit;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@DomainModel(annotatedClasses = NormalizedZonedTest.Zoned.class) @DomainModel(annotatedClasses = UTCNormalizedZonedTest.Zoned.class)
@SessionFactory @SessionFactory
@ServiceRegistry(settings = @Setting(name = AvailableSettings.TIMEZONE_DEFAULT_STORAGE, value = "NORMALIZE_UTC")) @ServiceRegistry(settings = @Setting(name = AvailableSettings.TIMEZONE_DEFAULT_STORAGE, value = "NORMALIZE_UTC"))
public class NormalizedZonedTest { public class UTCNormalizedZonedTest {
@Test void test(SessionFactoryScope scope) { @Test void test(SessionFactoryScope scope) {
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") );