|
| 1 | +/** |
| 2 | + * Copyright 2009-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.apache.ibatis.submitted.timezone_edge_case; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.*; |
| 19 | + |
| 20 | +import java.io.Reader; |
| 21 | +import java.sql.Connection; |
| 22 | +import java.sql.ResultSet; |
| 23 | +import java.sql.Statement; |
| 24 | +import java.time.LocalDate; |
| 25 | +import java.time.LocalDateTime; |
| 26 | +import java.time.LocalTime; |
| 27 | +import java.util.TimeZone; |
| 28 | + |
| 29 | +import org.apache.ibatis.BaseDataTest; |
| 30 | +import org.apache.ibatis.io.Resources; |
| 31 | +import org.apache.ibatis.session.SqlSession; |
| 32 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 33 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 34 | +import org.junit.jupiter.api.AfterEach; |
| 35 | +import org.junit.jupiter.api.BeforeAll; |
| 36 | +import org.junit.jupiter.api.BeforeEach; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | + |
| 39 | +public class TimezoneEdgeCaseTest { |
| 40 | + |
| 41 | + private static SqlSessionFactory sqlSessionFactory; |
| 42 | + private TimeZone timeZone; |
| 43 | + |
| 44 | + @BeforeAll |
| 45 | + static void setUp() throws Exception { |
| 46 | + try (Reader reader = Resources |
| 47 | + .getResourceAsReader("org/apache/ibatis/submitted/timezone_edge_case/mybatis-config.xml")) { |
| 48 | + sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); |
| 49 | + } |
| 50 | + BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), |
| 51 | + "org/apache/ibatis/submitted/timezone_edge_case/CreateDB.sql"); |
| 52 | + } |
| 53 | + |
| 54 | + @BeforeEach |
| 55 | + void saveTimeZone() { |
| 56 | + timeZone = TimeZone.getDefault(); |
| 57 | + } |
| 58 | + |
| 59 | + @AfterEach |
| 60 | + void restoreTimeZone() { |
| 61 | + TimeZone.setDefault(timeZone); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void shouldSelectNonExistentLocalTimestampAsIs() { |
| 66 | + TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles")); |
| 67 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 68 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 69 | + Record record = mapper.selectById(1); |
| 70 | + assertEquals(LocalDateTime.of(LocalDate.of(2019, 3, 10), LocalTime.of(2, 30)), record.getTs()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void shouldInsertNonExistentLocalTimestampAsIs() throws Exception { |
| 76 | + TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles")); |
| 77 | + LocalDateTime localDateTime = LocalDateTime.of(LocalDate.of(2019, 3, 10), LocalTime.of(2, 30)); |
| 78 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 79 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 80 | + Record record = new Record(); |
| 81 | + record.setId(2); |
| 82 | + record.setTs(localDateTime); |
| 83 | + mapper.insert(record); |
| 84 | + sqlSession.commit(); |
| 85 | + } |
| 86 | + try (SqlSession sqlSession = sqlSessionFactory.openSession(); |
| 87 | + Connection con = sqlSession.getConnection(); |
| 88 | + Statement stmt = con.createStatement(); |
| 89 | + ResultSet rs = stmt.executeQuery("select count(*) from records where id = 2 and ts = '2019-03-10 02:30:00'")) { |
| 90 | + rs.next(); |
| 91 | + assertEquals(1, rs.getInt(1)); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void shouldSelectNonExistentLocalDateAsIs() { |
| 97 | + TimeZone.setDefault(TimeZone.getTimeZone("Pacific/Apia")); |
| 98 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 99 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 100 | + Record record = mapper.selectById(1); |
| 101 | + assertEquals(LocalDate.of(2011, 12, 30), record.getD()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void shouldInsertNonExistentLocalDateAsIs() throws Exception { |
| 107 | + TimeZone.setDefault(TimeZone.getTimeZone("Pacific/Apia")); |
| 108 | + LocalDate localDate = LocalDate.of(2011, 12, 30); |
| 109 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 110 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 111 | + Record record = new Record(); |
| 112 | + record.setId(3); |
| 113 | + record.setD(localDate); |
| 114 | + mapper.insert(record); |
| 115 | + sqlSession.commit(); |
| 116 | + } |
| 117 | + try (SqlSession sqlSession = sqlSessionFactory.openSession(); |
| 118 | + Connection con = sqlSession.getConnection(); |
| 119 | + Statement stmt = con.createStatement(); |
| 120 | + ResultSet rs = stmt.executeQuery("select count(*) from records where id = 3 and d = '2011-12-30'")) { |
| 121 | + rs.next(); |
| 122 | + assertEquals(1, rs.getInt(1)); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments