Skip to content

Commit 87b8ebe

Browse files
committed
Added tests. LocalTimeTypeHandler should not lose fractional seconds.
1 parent 963a8a5 commit 87b8ebe

File tree

5 files changed

+209
-0
lines changed

5 files changed

+209
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
17+
drop table records if exists;
18+
19+
create table records (
20+
id int,
21+
t time(9)
22+
);
23+
24+
insert into records (id, t) values
25+
(1, '11:22:33.123456789');
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.localtime;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
import java.io.Reader;
21+
import java.time.LocalTime;
22+
23+
import org.apache.ibatis.BaseDataTest;
24+
import org.apache.ibatis.io.Resources;
25+
import org.apache.ibatis.session.SqlSession;
26+
import org.apache.ibatis.session.SqlSessionFactory;
27+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28+
import org.junit.jupiter.api.BeforeAll;
29+
import org.junit.jupiter.api.Test;
30+
31+
public class LocalTimeTest {
32+
33+
private static SqlSessionFactory sqlSessionFactory;
34+
35+
@BeforeAll
36+
static void setUp() throws Exception {
37+
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/localtime/mybatis-config.xml")) {
38+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39+
}
40+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
41+
"org/apache/ibatis/submitted/localtime/CreateDB.sql");
42+
}
43+
44+
@Test
45+
void shouldSelectLocalTimeWithNanoseconds() {
46+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
47+
Mapper mapper = sqlSession.getMapper(Mapper.class);
48+
Record record = mapper.selectById(1);
49+
assertEquals(LocalTime.of(11, 22, 33, 123456789), record.getT());
50+
}
51+
}
52+
53+
@Test
54+
void shouldInsertLocalTimeWithNanoseconds() {
55+
LocalTime t = LocalTime.of(11, 22, 33, 123456789);
56+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
57+
Mapper mapper = sqlSession.getMapper(Mapper.class);
58+
Record record = new Record();
59+
record.setId(2);
60+
record.setT(t);
61+
int result = mapper.insertLocalTime(record);
62+
assertEquals(1, result);
63+
sqlSession.commit();
64+
}
65+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
66+
Mapper mapper = sqlSession.getMapper(Mapper.class);
67+
Record record = mapper.selectById(2);
68+
assertEquals(t, record.getT());
69+
}
70+
}
71+
72+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.localtime;
17+
18+
import org.apache.ibatis.annotations.Insert;
19+
import org.apache.ibatis.annotations.Select;
20+
21+
public interface Mapper {
22+
23+
@Select("select id, t from records where id = #{id}")
24+
Record selectById(Integer id);
25+
26+
@Insert("insert into records (id, t) values (#{id}, #{t})")
27+
int insertLocalTime(Record record);
28+
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.localtime;
17+
18+
import java.time.LocalTime;
19+
20+
public class Record {
21+
22+
private Integer id;
23+
24+
private LocalTime t;
25+
26+
public Integer getId() {
27+
return id;
28+
}
29+
30+
public void setId(Integer id) {
31+
this.id = id;
32+
}
33+
34+
public LocalTime getT() {
35+
return t;
36+
}
37+
38+
public void setT(LocalTime t) {
39+
this.t = t;
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2019 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE configuration
20+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
22+
23+
<configuration>
24+
25+
<environments default="development">
26+
<environment id="development">
27+
<transactionManager type="JDBC">
28+
<property name="" value="" />
29+
</transactionManager>
30+
<dataSource type="UNPOOLED">
31+
<property name="driver" value="org.hsqldb.jdbcDriver" />
32+
<property name="url" value="jdbc:hsqldb:mem:localtime" />
33+
<property name="username" value="sa" />
34+
</dataSource>
35+
</environment>
36+
</environments>
37+
38+
<mappers>
39+
<mapper class="org.apache.ibatis.submitted.localtime.Mapper" />
40+
</mappers>
41+
42+
</configuration>

0 commit comments

Comments
 (0)