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.timestamp_with_timezone;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import java.io.Reader;
21  import java.time.OffsetDateTime;
22  import java.time.OffsetTime;
23  import java.time.ZoneOffset;
24  
25  import org.apache.ibatis.BaseDataTest;
26  import org.apache.ibatis.io.Resources;
27  import org.apache.ibatis.session.SqlSession;
28  import org.apache.ibatis.session.SqlSessionFactory;
29  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30  import org.junit.jupiter.api.BeforeAll;
31  import org.junit.jupiter.api.Disabled;
32  import org.junit.jupiter.api.Test;
33  
34  class TimestampWithTimezoneTypeHandlerTest {
35  
36    private static SqlSessionFactory sqlSessionFactory;
37  
38    @BeforeAll
39    static void setUp() throws Exception {
40      try (Reader reader = Resources
41          .getResourceAsReader("org/apache/ibatis/submitted/timestamp_with_timezone/mybatis-config.xml")) {
42        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
43      }
44      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
45          "org/apache/ibatis/submitted/timestamp_with_timezone/CreateDB.sql");
46    }
47  
48    @Test
49    void shouldSelectOffsetDateTime() {
50      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
51        Mapper mapper = sqlSession.getMapper(Mapper.class);
52        Record record = mapper.selectById(1);
53        assertEquals(OffsetDateTime.of(2018, 1, 2, 11, 22, 33, 123456000, ZoneOffset.ofHoursMinutes(1, 23)),
54            record.getOdt());
55        assertEquals(OffsetTime.of(11, 22, 33, 123456000, ZoneOffset.ofHoursMinutes(1, 23)), record.getOt());
56      }
57    }
58  
59    @Test
60    void shouldInsertOffsetDateTime() {
61      OffsetDateTime odt = OffsetDateTime.of(2018, 1, 2, 11, 22, 33, 123456000, ZoneOffset.ofHoursMinutes(1, 23));
62      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
63        Mapper mapper = sqlSession.getMapper(Mapper.class);
64        Record record = new Record();
65        record.setId(2);
66        record.setOdt(odt);
67        int result = mapper.insertOffsetDateTime(record);
68        assertEquals(1, result);
69        sqlSession.commit();
70      }
71      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72        Mapper mapper = sqlSession.getMapper(Mapper.class);
73        Record record = mapper.selectById(2);
74        assertEquals(odt, record.getOdt());
75      }
76    }
77  
78    @Disabled("HSQLDB does not support this.")
79    @Test
80    void shouldInsertOffsetTime() {
81      OffsetTime ot = OffsetTime.of(11, 22, 33, 123456000, ZoneOffset.ofHoursMinutes(1, 23));
82      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
83        Mapper mapper = sqlSession.getMapper(Mapper.class);
84        Record record = new Record();
85        record.setId(3);
86        record.setOt(ot);
87        int result = mapper.insertOffsetTime(record);
88        assertEquals(1, result);
89        sqlSession.commit();
90      }
91      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
92        Mapper mapper = sqlSession.getMapper(Mapper.class);
93        Record record = mapper.selectById(3);
94        assertEquals(ot, record.getOt());
95      }
96    }
97  
98  }