/**
 * 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.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() {
        TimeZone originalTimezone = TimeZone.getDefault();

        TimeZone timezone = TimeZone.getTimeZone("America/Los_Angeles");

        DataSource datasource = getDatasource();

        TimeZone.setDefault(timezone);

        try {
            JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
            jdbcTemplate.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);
            });
        } finally {
            TimeZone.setDefault(originalTimezone);
        }
    }
}

@@ -667,24 +667,75 @@
 ctemplate;%0A%0A
+import net.javacrumbs.shedlock.core.ClockProvider;%0A
 import net.j
@@ -936,24 +936,64 @@
 rationTest;%0A
+import org.junit.jupiter.api.AfterEach;%0A
 import org.j
@@ -1123,16 +1123,40 @@
 estamp;%0A
+import java.time.Clock;%0A
 import j
@@ -1629,16 +1629,121 @@
 %0A    %7D%0A%0A
+    @AfterEach%0A    void resetClock() %7B%0A        ClockProvider.setClock(Clock.systemDefaultZone());%0A    %7D%0A%0A
     @Tes
@@ -1794,25 +1794,17 @@
 imeZone 
-originalT
+t
 imezone 
@@ -1821,94 +1821,265 @@
 .get
-Default();%0A%0A        TimeZone timezone = TimeZone.get
+TimeZone(%22America/Los_Angeles%22);%0A%0A        Instant lockUntil = Instant.parse(%222020-04-10T17:30:00Z%22);%0A        ClockProvider.setClock(Clock.fixed(lockUntil.minusSeconds(10), timezone.toZoneId()));%0A%0A        TimeZone original
 Time
-Zone(%22America/Los_Angeles%22
+zone = TimeZone.getDefault(
 );%0A%0A
+%0A
     
@@ -2717,79 +2717,8 @@
 ;%0A%0A%0A
-            Instant lockUntil = Instant.parse(%222030-04-10T17:30:00Z%22);%0A
     

Use ClockProvider in timezone test