zstan commented on code in PR #3558: URL: https://github.com/apache/ignite-3/pull/3558#discussion_r1557319992
########## modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcClientTimeZoneTest.java: ########## @@ -0,0 +1,226 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.jdbc; + +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.zone.ZoneRulesException; +import java.util.Objects; +import java.util.TimeZone; +import org.apache.ignite.internal.jdbc.ConnectionProperties; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Test checks the client time zone propagation from the jdbc client to the server node. + */ +@SuppressWarnings("CallToDriverManagerGetConnection") +public class ItJdbcClientTimeZoneTest extends AbstractJdbcSelfTest { + private static final String TIMESTAMP_STR = "1970-01-01 00:00:00"; + + private ZoneId origin; + + @BeforeAll + static void createTable() throws SQLException { + try (Statement stmt = conn.createStatement()) { + stmt.execute("CREATE TABLE test(id INT PRIMARY KEY, ts TIMESTAMP, ts_tz TIMESTAMP WITH LOCAL TIME ZONE)"); + } + } + + @BeforeEach + void saveTimeZoneAndClearTable() throws SQLException { + origin = TimeZone.getDefault().toZoneId(); + + stmt.execute("DELETE FROM test"); + } + + @AfterEach + void restoreTimezone() { + ZoneId current = TimeZone.getDefault().toZoneId(); + + if (!Objects.equals(origin, current)) { + TimeZone.setDefault(TimeZone.getTimeZone(origin)); + } + } + + /** Ensures that the default JVM time zone is passed to the server. */ + @Test + public void jvmTimeZonePassedToServer() throws SQLException { + ZoneId serverTimezone = TimeZone.getTimeZone("GMT+1").toZoneId(); + + // Client time zone. + TimeZone.setDefault(TimeZone.getTimeZone("GMT+02:00")); + + withNewConnection(URL, stmt -> { + // Set server timezone. + TimeZone.setDefault(TimeZone.getTimeZone(serverTimezone)); + + stmt.executeUpdate(format("INSERT INTO test VALUES(0, '{}', '{}')", TIMESTAMP_STR, TIMESTAMP_STR)); + + validateSingleRow("SELECT ts::VARCHAR, ts_tz::VARCHAR FROM test", stmt, + "1970-01-01 00:00:00", "1970-01-01 00:00:00 GMT+02:00"); + }); + + TimeZone.setDefault(TimeZone.getTimeZone("GMT+03:00")); + + withNewConnection(URL, stmt -> { + // Set server timezone. + TimeZone.setDefault(TimeZone.getTimeZone(serverTimezone)); + + validateSingleRow("SELECT ts::VARCHAR, ts_tz::VARCHAR FROM test", stmt, + "1970-01-01 00:00:00", "1970-01-01 01:00:00 GMT+03:00"); + }); + } + + /** + * Ensures that session time zone can be changed using + * connection property {@link ConnectionProperties#setConnectionTimeZone(ZoneId)}. + */ + @Test + public void timeZoneCanBeSetUsingProperty() throws SQLException { + String originTimeZone = TimeZone.getDefault().getID(); + + { + String timeZone = "GMT+02:00"; + + withNewConnection(URL + "?connectionTimeZone=" + timeZone, stmt -> { + stmt.executeUpdate(format("INSERT INTO test VALUES(0, '{}', '{}')", TIMESTAMP_STR, TIMESTAMP_STR)); + + validateSingleRow("SELECT ts::VARCHAR, ts_tz::VARCHAR FROM test", stmt, + "1970-01-01 00:00:00", "1970-01-01 00:00:00 " + timeZone); + }); + } + + { + String timeZone = "GMT+03:00"; + + withNewConnection(URL + "?connectionTimeZone=" + timeZone, stmt -> { + validateSingleRow("SELECT ts::VARCHAR, ts_tz::VARCHAR FROM test", stmt, + "1970-01-01 00:00:00", "1970-01-01 01:00:00 " + timeZone); + }); + } + + { + String timeZone = "invalid/timezone"; + + assertThrows( + ZoneRulesException.class, Review Comment: do we need to wrap such exception into appropriate sqlexception ? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
