View Javadoc
1   /*
2    *    Copyright 2009-2023 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    *       https://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.assertEquals;
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  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      // Newer hsqldb requires we use a bogus timezone as timezone now works
67      TimeZone.setDefault(TimeZone.getTimeZone("Bad/Zone"));
68      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
69        Mapper mapper = sqlSession.getMapper(Mapper.class);
70        Record record = mapper.selectById(1);
71        assertEquals(LocalDateTime.of(LocalDate.of(2019, 3, 10), LocalTime.of(2, 30)), record.getTs());
72      }
73    }
74  
75    @Test
76    void shouldInsertNonExistentLocalTimestampAsIs() throws Exception {
77      // Newer hsqldb requires we use a bogus timezone as timezone now works
78      TimeZone.setDefault(TimeZone.getTimeZone("Bad/Zone"));
79      LocalDateTime localDateTime = LocalDateTime.of(LocalDate.of(2019, 3, 10), LocalTime.of(2, 30));
80      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
81        Mapper mapper = sqlSession.getMapper(Mapper.class);
82        Record record = new Record();
83        record.setId(2);
84        record.setTs(localDateTime);
85        mapper.insert(record);
86        sqlSession.commit();
87      }
88      try (SqlSession sqlSession = sqlSessionFactory.openSession(); Connection con = sqlSession.getConnection();
89          Statement stmt = con.createStatement();
90          ResultSet rs = stmt.executeQuery("select count(*) from records where id = 2 and ts = '2019-03-10 02:30:00'")) {
91        rs.next();
92        assertEquals(1, rs.getInt(1));
93      }
94    }
95  
96    @Test
97    void shouldSelectNonExistentLocalDateAsIs() {
98      // Newer hsqldb requires we use a bogus timezone as timezone now works
99      TimeZone.setDefault(TimeZone.getTimeZone("Bad/Zone"));
100     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
101       Mapper mapper = sqlSession.getMapper(Mapper.class);
102       Record record = mapper.selectById(1);
103       assertEquals(LocalDate.of(2011, 12, 30), record.getD());
104     }
105   }
106 
107   @Test
108   void shouldInsertNonExistentLocalDateAsIs() throws Exception {
109     // Newer hsqldb requires we use a bogus timezone as timezone now works
110     TimeZone.setDefault(TimeZone.getTimeZone("Bad/Zone"));
111     LocalDate localDate = LocalDate.of(2011, 12, 30);
112     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
113       Mapper mapper = sqlSession.getMapper(Mapper.class);
114       Record record = new Record();
115       record.setId(3);
116       record.setD(localDate);
117       mapper.insert(record);
118       sqlSession.commit();
119     }
120     try (SqlSession sqlSession = sqlSessionFactory.openSession(); Connection con = sqlSession.getConnection();
121         Statement stmt = con.createStatement();
122         ResultSet rs = stmt.executeQuery("select count(*) from records where id = 3 and d = '2011-12-30'")) {
123       rs.next();
124       assertEquals(1, rs.getInt(1));
125     }
126   }
127 }