Skip to content

Commit c4228b1

Browse files
committed
Updated tests that are related to #591
1 parent 3e82947 commit c4228b1

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

src/test/java/org/apache/ibatis/submitted/typehandler/Mapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.apache.ibatis.submitted.typehandler;
1717

18+
import java.util.Map;
19+
1820
import org.apache.ibatis.annotations.Arg;
1921
import org.apache.ibatis.annotations.ConstructorArgs;
2022
import org.apache.ibatis.annotations.Insert;
@@ -58,4 +60,7 @@ public interface Mapper {
5860

5961
@Select("select id from product where name = #{value}")
6062
ProductId getProductIdByName(String name);
63+
64+
@Select("select id, name, released_on from product where id = #{id}")
65+
Map<String, Object> getProductAsMap(Integer id);
6166
}

src/test/java/org/apache/ibatis/submitted/typehandler/TypeHandlerTest.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
import static org.junit.jupiter.api.Assertions.assertNotNull;
2020

2121
import java.io.Reader;
22+
import java.time.LocalDate;
23+
import java.util.Map;
2224

2325
import org.apache.ibatis.BaseDataTest;
24-
import org.apache.ibatis.builder.BuilderException;
2526
import org.apache.ibatis.io.Resources;
2627
import org.apache.ibatis.session.SqlSession;
2728
import org.apache.ibatis.session.SqlSessionFactory;
@@ -30,9 +31,8 @@
3031
import org.apache.ibatis.submitted.typehandler.Product.ProductId;
3132
import org.apache.ibatis.submitted.typehandler.Product.ProductIdTypeHandler;
3233
import org.apache.ibatis.type.JdbcType;
33-
import org.junit.jupiter.api.Assertions;
34+
import org.apache.ibatis.type.LocalDateTypeHandler;
3435
import org.junit.jupiter.api.BeforeEach;
35-
import org.junit.jupiter.api.Disabled;
3636
import org.junit.jupiter.api.Test;
3737

3838
class TypeHandlerTest {
@@ -135,29 +135,49 @@ void shouldPickSameTypeHandlerMappedToDifferentJdbcTypes() {
135135
}
136136
}
137137

138-
@Disabled("This does not fail anymore because jdbcType resolution is performed at exection time using metadata")
139138
@Test
140139
void shouldFailIfMultipleHandlerMappedToAType() {
141140
sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(ProductId.class, JdbcType.BIGINT,
142141
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+
}
146151
}
147152

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.")
150153
@Test
151-
void shouldPickHandlerForNull() {
154+
void shouldHandlerBePickedBasedOnRuntimeJdbcType() {
152155
sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(ProductId.class, null,
153156
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.
156160
addMapper();
157161
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
158162
Mapper mapper = sqlSession.getMapper(Mapper.class);
159163
Product product = mapper.getProductByNameXml("iPad");
160-
assertEquals(Integer.valueOf(999), product.getId().getValue());
164+
assertEquals(Integer.valueOf(2), product.getId().getValue());
161165
}
162166
}
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+
163183
}

src/test/resources/org/apache/ibatis/submitted/typehandler/CreateDB.sql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ create table users (
2626

2727
create table product (
2828
id int identity,
29-
name varchar(20)
29+
name varchar(20),
30+
released_on date
3031
);
3132

3233
insert into users (id, name, city, state) values(1, ' User1', ' Carmel ', ' IN ');
3334

34-
insert into product (id, name) values
35-
(1, 'iPod'),
36-
(2, 'iPad');
35+
insert into product (id, name, released_on) values
36+
(1, 'iPod', '2001-11-10'),
37+
(2, 'iPad', '2010-04-03');

0 commit comments

Comments
 (0)