/**
 * Copyright 2009-2019 the original author or authors.
 *
 * Licensed 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 net.javacrumbs.shedlock.provider.jdbctemplate;

import net.javacrumbs.shedlock.core.LockConfiguration;
import net.javacrumbs.shedlock.support.StorageBasedLockProvider;
import net.javacrumbs.shedlock.test.support.jdbc.AbstractPostgresJdbcLockProviderIntegrationTest;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.TimeZone;

import static net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider.Configuration.builder;
import static org.assertj.core.api.Assertions.assertThat;

public class PostgresJdbcTemplateLockProviderIntegrationTest extends AbstractPostgresJdbcLockProviderIntegrationTest {
    @Override
    protected StorageBasedLockProvider getLockProvider() {
        return new JdbcTemplateLockProvider(getDatasource());
    }

    @Test
    void shouldHonorTimezone() throws SQLException {
        TimeZone timezone = TimeZone.getTimeZone("America/Los_Angeles");


        DataSource datasource = getDatasource();

        TimeZone.setDefault(timezone);

        try (
            Connection conn = datasource.getConnection();
            Statement statement = conn.createStatement()
        ) {
            statement.execute("SET TIMEZONE='UTC';");
            statement.execute("CREATE TABLE shedlock_tz(name VARCHAR(64), lock_until TIMESTAMP WITH TIME ZONE, locked_at TIMESTAMP WITH TIME ZONE, locked_by  VARCHAR(255), PRIMARY KEY (name))");
        }

        JdbcTemplateLockProvider provider = new JdbcTemplateLockProvider(builder()
            .withJdbcTemplate(new JdbcTemplate(datasource))
            .withTableName("shedlock_tz")
            .withTimeZone(timezone)
            .build());


        Instant lockUntil = Instant.parse("2030-04-10T17:30:00Z");
        provider.lock(new LockConfiguration("timezone_test", lockUntil));
        new JdbcTemplate(datasource).query("SELECT * FROM shedlock_tz where name='timezone_test'", rs -> {
            Timestamp timestamp = rs.getTimestamp("lock_until");
            assertThat(timestamp.getTimezoneOffset()).isEqualTo(7 * 60);
            assertThat(timestamp.toInstant()).isEqualTo(lockUntil);
        });
    }
}

@@ -1009,93 +1009,8 @@
 ce;%0A
-import java.sql.Connection;%0Aimport java.sql.SQLException;%0Aimport java.sql.Statement;%0A
 impo
@@ -1559,29 +1559,69 @@
 e() 
-throws SQLException %7B
+%7B%0A        TimeZone originalTimezone = TimeZone.getDefault();%0A
 %0A   
@@ -1687,25 +1687,24 @@
 Angeles%22);%0A%0A
-%0A
         Data
@@ -1793,17 +1793,17 @@
     try 
-(
+%7B
 %0A       
@@ -1811,174 +1811,63 @@
     
-Connection conn = datasource.getConnection();%0A            Statement statement = conn.createStatement()%0A        ) %7B%0A            statement.execute(%22SET TIMEZONE='UTC';%22
+JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource
 );%0A 
@@ -1881,17 +1881,20 @@
     
-statement
+jdbcTemplate
 .exe
@@ -2067,19 +2067,13 @@
 %22);%0A
-    
+%0A
     
-%7D%0A%0A
     
@@ -2159,24 +2159,28 @@
             
+   
+ 
 .withJdbcTem
@@ -2223,24 +2223,28 @@
             
+    
 .withTableNa
@@ -2269,24 +2269,28 @@
             
+ 
+   
 .withTimeZon
@@ -2313,16 +2313,20 @@
         
+   
+ 
 .build()
@@ -2330,16 +2330,20 @@
 d());%0A%0A%0A
+    
         
@@ -2409,16 +2409,20 @@
         
+ 
+   
 provider
@@ -2487,16 +2487,20 @@
         
+   
+ 
 new Jdbc
@@ -2598,24 +2598,28 @@
             
+    
 Timestamp ti
@@ -2663,32 +2663,36 @@
 %22);%0A            
+  
+  
 assertThat(times
@@ -2736,16 +2736,20 @@
  * 60);%0A
+    
         
@@ -2820,11 +2820,96 @@
     
-%7D);
+    %7D);%0A        %7D finally %7B%0A            TimeZone.setDefault(originalTimezone);%0A        %7D
 %0A   

Enhance timezone test