From 8fce51c765311302305e52669ca778272e650144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Mon, 26 Aug 2019 11:44:53 +0200 Subject: [PATCH] HHH-13580 Use safer ZoneId => TimeZone conversion in AbstractJavaTimeTypeTest We were testing GMT instead of UTC-8 without even knowing it... --- .../test/type/AbstractJavaTimeTypeTest.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/AbstractJavaTimeTypeTest.java b/hibernate-core/src/test/java/org/hibernate/test/type/AbstractJavaTimeTypeTest.java index 69384e1675..8baef27657 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/AbstractJavaTimeTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/type/AbstractJavaTimeTypeTest.java @@ -191,7 +191,7 @@ abstract class AbstractJavaTimeTypeTest extends BaseCoreFunctionalTestCase protected final void withDefaultTimeZone(Runnable runnable) { TimeZone timeZoneBefore = TimeZone.getDefault(); - TimeZone.setDefault( TimeZone.getTimeZone( env.defaultJvmTimeZone ) ); + TimeZone.setDefault( toTimeZone( env.defaultJvmTimeZone ) ); /* * Run the code in a new thread, because some libraries (looking at you, h2 JDBC driver) * cache data dependent on the default timezone in thread local variables, @@ -223,6 +223,23 @@ abstract class AbstractJavaTimeTypeTest extends BaseCoreFunctionalTestCase } } + private static TimeZone toTimeZone(ZoneId zoneId) { + String idString = zoneId.getId(); + if ( idString.startsWith( "UTC+" ) || idString.startsWith( "UTC-" ) ) { + // Apparently TimeZone doesn't understand UTC+XXX nor UTC-XXX + // Using GMT+XXX or GMT-XXX as a fallback + idString = "GMT" + idString.substring( "UTC".length() ); + } + + TimeZone result = TimeZone.getTimeZone( idString ); + if ( !idString.equals( result.getID() ) ) { + // If the timezone is not understood, getTimeZone returns GMT and the condition above is true + throw new IllegalStateException( "Attempting to test an unsupported timezone: " + zoneId ); + } + + return result; + } + protected final Class getRemappingDialectClass() { return env.remappingDialectClass; }