View Javadoc
1   /*
2    *    Copyright 2009-2021 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.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      // create a SqlSessionFactory
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      // populate in-memory database
49      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
50          "org/apache/ibatis/submitted/typehandler/CreateDB.sql");
51    }
52  
53    // Some tests need to register additional type handler
54    // before adding mapper.
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     // multiple type handlers are mapped to ProductId and
141     // none of them are mapped to null jdbcType.
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     // multiple type handlers are mapped to ProductId and
150     // one of them are mapped to null jdbcType.
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 }