1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.typehandler;
17
18 import static org.junit.jupiter.api.Assertions.*;
19
20 import java.io.Reader;
21
22 import org.apache.ibatis.BaseDataTest;
23 import org.apache.ibatis.builder.BuilderException;
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.apache.ibatis.submitted.typehandler.Product.ConstantProductIdTypeHandler;
29 import org.apache.ibatis.submitted.typehandler.Product.ProductId;
30 import org.apache.ibatis.submitted.typehandler.Product.ProductIdTypeHandler;
31 import org.apache.ibatis.type.JdbcType;
32 import org.junit.jupiter.api.Assertions;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35
36 class TypeHandlerTest {
37
38 private SqlSessionFactory sqlSessionFactory;
39
40 @BeforeEach
41 void setUp() throws Exception {
42
43 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/typehandler/mybatis-config.xml")) {
44 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
45 sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(StringTrimmingTypeHandler.class);
46 }
47
48
49 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
50 "org/apache/ibatis/submitted/typehandler/CreateDB.sql");
51 }
52
53
54
55 private void addMapper() {
56 sqlSessionFactory.getConfiguration().addMapper(Mapper.class);
57 }
58
59 @Test
60 void shouldGetAUser() {
61 addMapper();
62 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
63 Mapper mapper = sqlSession.getMapper(Mapper.class);
64 User user = mapper.getUser(1);
65 assertEquals("User1", user.getName());
66 assertEquals("Carmel", user.getCity());
67 assertEquals("IN", user.getState());
68 }
69 }
70
71 @Test
72 void shouldApplyTypeHandlerOnGeneratedKey() {
73 addMapper();
74 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
75 Mapper mapper = sqlSession.getMapper(Mapper.class);
76 Product product = new Product();
77 product.setName("new product");
78 mapper.insertProduct(product);
79 assertNotNull(product.getId());
80 assertNotNull(product.getId().getValue());
81 }
82 }
83
84 @Test
85 void shouldApplyTypeHandlerWithJdbcTypeSpecified() {
86 addMapper();
87 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
88 Mapper mapper = sqlSession.getMapper(Mapper.class);
89 Product product = mapper.getProductByName("iPad");
90 assertEquals(Integer.valueOf(2), product.getId().getValue());
91 }
92 }
93
94 @Test
95 void shouldApplyTypeHandlerUsingConstructor() {
96 addMapper();
97 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
98 Mapper mapper = sqlSession.getMapper(Mapper.class);
99 Product product = mapper.getProductByName("iPad");
100 assertEquals(Integer.valueOf(2), product.getId().getValue());
101 }
102 }
103
104 @Test
105 void shouldApplyTypeHandlerOnReturnTypeWithJdbcTypeSpecified() {
106 addMapper();
107 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
108 Mapper mapper = sqlSession.getMapper(Mapper.class);
109 ProductId productId = mapper.getProductIdByName("iPad");
110 assertEquals(Integer.valueOf(2), productId.getValue());
111 }
112 }
113
114 @Test
115 void shouldPickSoleTypeHandlerOnXmlResultMap() {
116 addMapper();
117 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
118 Mapper mapper = sqlSession.getMapper(Mapper.class);
119 Product product = mapper.getProductByNameXml("iPad");
120 assertEquals(Integer.valueOf(2), product.getId().getValue());
121 }
122 }
123
124 @Test
125 void shouldPickSameTypeHandlerMappedToDifferentJdbcTypes() {
126 sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(ProductId.class, JdbcType.BIGINT,
127 ProductIdTypeHandler.class);
128 addMapper();
129 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
130 Mapper mapper = sqlSession.getMapper(Mapper.class);
131 Product product = mapper.getProductByNameXml("iPad");
132 assertEquals(Integer.valueOf(2), product.getId().getValue());
133 }
134 }
135
136 @Test
137 void shouldFailIfMultipleHandlerMappedToAType() {
138 sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(ProductId.class, JdbcType.BIGINT,
139 ConstantProductIdTypeHandler.class);
140
141
142 Assertions.assertThrows(BuilderException.class, this::addMapper);
143 }
144
145 @Test
146 void shouldPickHandlerForNull() {
147 sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(ProductId.class, null,
148 ConstantProductIdTypeHandler.class);
149
150
151 addMapper();
152 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
153 Mapper mapper = sqlSession.getMapper(Mapper.class);
154 Product product = mapper.getProductByNameXml("iPad");
155 assertEquals(Integer.valueOf(999), product.getId().getValue());
156 }
157 }
158 }