Improve test reliability on Sybase

This commit is contained in:
Christian Beikov 2023-08-07 17:19:22 +02:00
parent 6c72ea5d84
commit cad7fb3d40
6 changed files with 181 additions and 152 deletions

View File

@ -1,8 +1,11 @@
package org.hibernate.orm.test.timezones; package org.hibernate.orm.test.timezones;
import jakarta.persistence.Entity; import java.time.OffsetDateTime;
import jakarta.persistence.GeneratedValue; import java.time.ZoneId;
import jakarta.persistence.Id; import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.SybaseDialect; 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.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.OffsetDateTime; import jakarta.persistence.Entity;
import java.time.ZoneId; import jakarta.persistence.GeneratedValue;
import java.time.ZoneOffset; import jakarta.persistence.Id;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -29,8 +30,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class AutoZonedTest { public class AutoZonedTest {
@Test void test(SessionFactoryScope scope) { @Test void test(SessionFactoryScope scope) {
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); final ZonedDateTime nowZoned;
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); 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-> { long id = scope.fromTransaction( s-> {
Zoned z = new Zoned(); Zoned z = new Zoned();
z.zonedDateTime = nowZoned; z.zonedDateTime = nowZoned;
@ -41,12 +53,6 @@ public class AutoZonedTest {
scope.inSession( s -> { scope.inSession( s -> {
Zoned z = s.find(Zoned.class, id); Zoned z = s.find(Zoned.class, id);
final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect(); 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( assertEquals(
DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ),
DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect )
@ -55,7 +61,6 @@ public class AutoZonedTest {
DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ),
DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect )
); );
}
assertEquals( nowZoned.toOffsetDateTime().getOffset(), z.zonedDateTime.toOffsetDateTime().getOffset() ); assertEquals( nowZoned.toOffsetDateTime().getOffset(), z.zonedDateTime.toOffsetDateTime().getOffset() );
assertEquals( nowOffset.getOffset(), z.offsetDateTime.getOffset() ); assertEquals( nowOffset.getOffset(), z.offsetDateTime.getOffset() );
}); });

View File

@ -1,8 +1,11 @@
package org.hibernate.orm.test.timezones; package org.hibernate.orm.test.timezones;
import jakarta.persistence.Entity; import java.time.OffsetDateTime;
import jakarta.persistence.GeneratedValue; import java.time.ZoneId;
import jakarta.persistence.Id; import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.SybaseDialect; 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.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.OffsetDateTime; import jakarta.persistence.Entity;
import java.time.ZoneId; import jakarta.persistence.GeneratedValue;
import java.time.ZoneOffset; import jakarta.persistence.Id;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -29,8 +30,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class ColumnZonedTest { public class ColumnZonedTest {
@Test void test(SessionFactoryScope scope) { @Test void test(SessionFactoryScope scope) {
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); final ZonedDateTime nowZoned;
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); 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-> { long id = scope.fromTransaction( s-> {
Zoned z = new Zoned(); Zoned z = new Zoned();
z.zonedDateTime = nowZoned; z.zonedDateTime = nowZoned;
@ -41,12 +53,6 @@ public class ColumnZonedTest {
scope.inSession( s -> { scope.inSession( s -> {
Zoned z = s.find(Zoned.class, id); Zoned z = s.find(Zoned.class, id);
final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect(); 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( assertEquals(
DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ),
DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect )
@ -55,7 +61,6 @@ public class ColumnZonedTest {
DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ),
DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect )
); );
}
assertEquals( nowZoned.toOffsetDateTime().getOffset(), z.zonedDateTime.toOffsetDateTime().getOffset() ); assertEquals( nowZoned.toOffsetDateTime().getOffset(), z.zonedDateTime.toOffsetDateTime().getOffset() );
assertEquals( nowOffset.getOffset(), z.offsetDateTime.getOffset() ); assertEquals( nowOffset.getOffset(), z.offsetDateTime.getOffset() );
}); });

View File

@ -1,8 +1,10 @@
package org.hibernate.orm.test.timezones; package org.hibernate.orm.test.timezones;
import jakarta.persistence.Entity; import java.time.OffsetDateTime;
import jakarta.persistence.GeneratedValue; import java.time.ZoneId;
import jakarta.persistence.Id; import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.SybaseDialect; 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.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.OffsetDateTime; import jakarta.persistence.Entity;
import java.time.ZoneId; import jakarta.persistence.GeneratedValue;
import java.time.ZoneOffset; import jakarta.persistence.Id;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -27,8 +27,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class DefaultZonedTest { public class DefaultZonedTest {
@Test void test(SessionFactoryScope scope) { @Test void test(SessionFactoryScope scope) {
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); final ZonedDateTime nowZoned;
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); 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-> { long id = scope.fromTransaction( s-> {
Zoned z = new Zoned(); Zoned z = new Zoned();
z.zonedDateTime = nowZoned; z.zonedDateTime = nowZoned;
@ -39,12 +50,6 @@ public class DefaultZonedTest {
scope.inSession( s -> { scope.inSession( s -> {
Zoned z = s.find(Zoned.class, id); Zoned z = s.find(Zoned.class, id);
final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect(); 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( assertEquals(
DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ),
DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect )
@ -53,7 +58,6 @@ public class DefaultZonedTest {
DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ),
DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect )
); );
}
if ( dialect.getTimeZoneSupport() == TimeZoneSupport.NATIVE ) { if ( dialect.getTimeZoneSupport() == TimeZoneSupport.NATIVE ) {
assertEquals( nowZoned.toOffsetDateTime().getOffset(), z.zonedDateTime.toOffsetDateTime().getOffset() ); assertEquals( nowZoned.toOffsetDateTime().getOffset(), z.zonedDateTime.toOffsetDateTime().getOffset() );
assertEquals( nowOffset.getOffset(), z.offsetDateTime.getOffset() ); assertEquals( nowOffset.getOffset(), z.offsetDateTime.getOffset() );

View File

@ -1,8 +1,12 @@
package org.hibernate.orm.test.timezones; package org.hibernate.orm.test.timezones;
import jakarta.persistence.Entity; import java.time.Instant;
import jakarta.persistence.GeneratedValue; import java.time.OffsetDateTime;
import jakarta.persistence.Id; 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.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.SybaseDialect; 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.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.Instant; import jakarta.persistence.Entity;
import java.time.OffsetDateTime; import jakarta.persistence.GeneratedValue;
import java.time.ZoneId; import jakarta.persistence.Id;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -31,8 +32,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class JDBCTimeZoneZonedTest { public class JDBCTimeZoneZonedTest {
@Test void test(SessionFactoryScope scope) { @Test void test(SessionFactoryScope scope) {
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); final ZonedDateTime nowZoned;
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours( 3) ); 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-> { long id = scope.fromTransaction( s-> {
Zoned z = new Zoned(); Zoned z = new Zoned();
z.zonedDateTime = nowZoned; z.zonedDateTime = nowZoned;
@ -45,12 +57,6 @@ public class JDBCTimeZoneZonedTest {
ZoneId systemZone = ZoneId.systemDefault(); ZoneId systemZone = ZoneId.systemDefault();
ZoneOffset systemOffset = systemZone.getRules().getOffset( Instant.now() ); ZoneOffset systemOffset = systemZone.getRules().getOffset( Instant.now() );
final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect(); 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( assertEquals(
DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ),
DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect )
@ -59,7 +65,6 @@ public class JDBCTimeZoneZonedTest {
DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ),
DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect )
); );
}
assertEquals( systemZone, z.zonedDateTime.getZone() ); assertEquals( systemZone, z.zonedDateTime.getZone() );
assertEquals( systemOffset, z.offsetDateTime.getOffset() ); assertEquals( systemOffset, z.offsetDateTime.getOffset() );
}); });

View File

@ -1,8 +1,12 @@
package org.hibernate.orm.test.timezones; package org.hibernate.orm.test.timezones;
import jakarta.persistence.Entity; import java.time.Instant;
import jakarta.persistence.GeneratedValue; import java.time.OffsetDateTime;
import jakarta.persistence.Id; 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.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.SybaseDialect; 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.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.Instant; import jakarta.persistence.Entity;
import java.time.OffsetDateTime; import jakarta.persistence.GeneratedValue;
import java.time.ZoneId; import jakarta.persistence.Id;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -30,8 +31,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class PassThruZonedTest { public class PassThruZonedTest {
@Test void test(SessionFactoryScope scope) { @Test void test(SessionFactoryScope scope) {
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); final ZonedDateTime nowZoned;
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours( 3) ); 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-> { long id = scope.fromTransaction( s-> {
Zoned z = new Zoned(); Zoned z = new Zoned();
z.zonedDateTime = nowZoned; z.zonedDateTime = nowZoned;
@ -44,12 +56,6 @@ public class PassThruZonedTest {
ZoneId systemZone = ZoneId.systemDefault(); ZoneId systemZone = ZoneId.systemDefault();
ZoneOffset systemOffset = systemZone.getRules().getOffset( Instant.now() ); ZoneOffset systemOffset = systemZone.getRules().getOffset( Instant.now() );
final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect(); 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( assertEquals(
DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ),
DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect )
@ -58,7 +64,6 @@ public class PassThruZonedTest {
DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ),
DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect )
); );
}
assertEquals( systemZone, z.zonedDateTime.getZone() ); assertEquals( systemZone, z.zonedDateTime.getZone() );
assertEquals( systemOffset, z.offsetDateTime.getOffset() ); assertEquals( systemOffset, z.offsetDateTime.getOffset() );
}); });

View File

@ -1,8 +1,11 @@
package org.hibernate.orm.test.timezones; package org.hibernate.orm.test.timezones;
import jakarta.persistence.Entity; import java.time.OffsetDateTime;
import jakarta.persistence.GeneratedValue; import java.time.ZoneId;
import jakarta.persistence.Id; import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.SybaseDialect; 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.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.OffsetDateTime; import jakarta.persistence.Entity;
import java.time.ZoneId; import jakarta.persistence.GeneratedValue;
import java.time.ZoneOffset; import jakarta.persistence.Id;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -29,8 +30,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class UTCNormalizedZonedTest { public class UTCNormalizedZonedTest {
@Test void test(SessionFactoryScope scope) { @Test void test(SessionFactoryScope scope) {
ZonedDateTime nowZoned = ZonedDateTime.now().withZoneSameInstant( ZoneId.of("CET") ); final ZonedDateTime nowZoned;
OffsetDateTime nowOffset = OffsetDateTime.now().withOffsetSameInstant( ZoneOffset.ofHours(3) ); 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-> { long id = scope.fromTransaction( s-> {
Zoned z = new Zoned(); Zoned z = new Zoned();
z.zonedDateTime = nowZoned; z.zonedDateTime = nowZoned;
@ -41,12 +53,6 @@ public class UTCNormalizedZonedTest {
scope.inSession( s-> { scope.inSession( s-> {
Zoned z = s.find(Zoned.class, id); Zoned z = s.find(Zoned.class, id);
final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect(); 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( assertEquals(
DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ), DateTimeUtils.roundToDefaultPrecision( nowZoned.toInstant(), dialect ),
DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect ) DateTimeUtils.roundToDefaultPrecision( z.zonedDateTime.toInstant(), dialect )
@ -55,7 +61,6 @@ public class UTCNormalizedZonedTest {
DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ), DateTimeUtils.roundToDefaultPrecision( nowOffset.toInstant(), dialect ),
DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect ) DateTimeUtils.roundToDefaultPrecision( z.offsetDateTime.toInstant(), dialect )
); );
}
assertEquals( ZoneId.of("Z"), z.zonedDateTime.getZone() ); assertEquals( ZoneId.of("Z"), z.zonedDateTime.getZone() );
assertEquals( ZoneOffset.ofHours(0), z.offsetDateTime.getOffset() ); assertEquals( ZoneOffset.ofHours(0), z.offsetDateTime.getOffset() );
}); });