|
19 | 19 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
20 | 20 |
|
21 | 21 | import java.io.Reader;
|
| 22 | +import java.time.LocalDate; |
| 23 | +import java.util.Map; |
22 | 24 |
|
23 | 25 | import org.apache.ibatis.BaseDataTest;
|
24 |
| -import org.apache.ibatis.builder.BuilderException; |
25 | 26 | import org.apache.ibatis.io.Resources;
|
26 | 27 | import org.apache.ibatis.session.SqlSession;
|
27 | 28 | import org.apache.ibatis.session.SqlSessionFactory;
|
|
30 | 31 | import org.apache.ibatis.submitted.typehandler.Product.ProductId;
|
31 | 32 | import org.apache.ibatis.submitted.typehandler.Product.ProductIdTypeHandler;
|
32 | 33 | import org.apache.ibatis.type.JdbcType;
|
33 |
| -import org.junit.jupiter.api.Assertions; |
| 34 | +import org.apache.ibatis.type.LocalDateTypeHandler; |
34 | 35 | import org.junit.jupiter.api.BeforeEach;
|
35 |
| -import org.junit.jupiter.api.Disabled; |
36 | 36 | import org.junit.jupiter.api.Test;
|
37 | 37 |
|
38 | 38 | class TypeHandlerTest {
|
@@ -135,29 +135,49 @@ void shouldPickSameTypeHandlerMappedToDifferentJdbcTypes() {
|
135 | 135 | }
|
136 | 136 | }
|
137 | 137 |
|
138 |
| - @Disabled("This does not fail anymore because jdbcType resolution is performed at exection time using metadata") |
139 | 138 | @Test
|
140 | 139 | void shouldFailIfMultipleHandlerMappedToAType() {
|
141 | 140 | sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(ProductId.class, JdbcType.BIGINT,
|
142 | 141 | ConstantProductIdTypeHandler.class);
|
143 |
| - // multiple type handlers are mapped to ProductId and |
144 |
| - // none of them are mapped to null jdbcType. |
145 |
| - Assertions.assertThrows(BuilderException.class, this::addMapper); |
| 142 | + // Two type handlers are mapped to ProductId. |
| 143 | + // One for JdbcType=BIGINT and the other for JdbcType=INTEGER |
| 144 | + // The runtime JdbcType is INTEGER, so the second one should be used. |
| 145 | + addMapper(); |
| 146 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 147 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 148 | + Product product = mapper.getProductByNameXml("iPad"); |
| 149 | + assertEquals(Integer.valueOf(2), product.getId().getValue()); |
| 150 | + } |
146 | 151 | }
|
147 | 152 |
|
148 |
| - @Disabled("This is no longer the expected behavior. " |
149 |
| - + "As there is a type handler registered for ProductId:INTEGER combination, it will be used.") |
150 | 153 | @Test
|
151 |
| - void shouldPickHandlerForNull() { |
| 154 | + void shouldHandlerBePickedBasedOnRuntimeJdbcType() { |
152 | 155 | sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(ProductId.class, null,
|
153 | 156 | ConstantProductIdTypeHandler.class);
|
154 |
| - // multiple type handlers are mapped to ProductId and |
155 |
| - // one of them are mapped to null jdbcType. |
| 157 | + // Two type handlers are mapped to ProductId. |
| 158 | + // One for JdbcType=NULL and the other for JdbcType=INTEGER |
| 159 | + // The runtime JdbcType is INTEGER, so the second one should be used. |
156 | 160 | addMapper();
|
157 | 161 | try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
|
158 | 162 | Mapper mapper = sqlSession.getMapper(Mapper.class);
|
159 | 163 | Product product = mapper.getProductByNameXml("iPad");
|
160 |
| - assertEquals(Integer.valueOf(999), product.getId().getValue()); |
| 164 | + assertEquals(Integer.valueOf(2), product.getId().getValue()); |
161 | 165 | }
|
162 | 166 | }
|
| 167 | + |
| 168 | + @Test |
| 169 | + void shouldHandlerBePickedBasedOnRuntimeJdbcType_Map() { |
| 170 | + // gh-591 |
| 171 | + // If a handler is registered against Object and JdbcType, |
| 172 | + // it will be used when result type is Map |
| 173 | + sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(Object.class, JdbcType.DATE, |
| 174 | + LocalDateTypeHandler.class); |
| 175 | + addMapper(); |
| 176 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 177 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 178 | + Map<String, Object> map = mapper.getProductAsMap(1); |
| 179 | + assertEquals(LocalDate.of(2001, 11, 10), map.get("RELEASED_ON")); |
| 180 | + } |
| 181 | + } |
| 182 | + |
163 | 183 | }
|
0 commit comments