Skip to content

Commit fe9dfbc

Browse files
committed
Enhance assertion for typehandler package scaning
Fixes gh-370
1 parent 00ed3d0 commit fe9dfbc

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

mybatis-spring-boot-autoconfigure/src/test/java/org/mybatis/spring/boot/autoconfigure/MybatisAutoConfigurationTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Map;
2323
import java.util.Properties;
2424
import java.util.UUID;
25+
import java.util.concurrent.atomic.AtomicInteger;
26+
import java.util.concurrent.atomic.AtomicLong;
2527

2628
import javax.sql.DataSource;
2729

@@ -54,6 +56,7 @@
5456
import org.mybatis.spring.SqlSessionFactoryBean;
5557
import org.mybatis.spring.SqlSessionTemplate;
5658
import org.mybatis.spring.annotation.MapperScan;
59+
import org.mybatis.spring.boot.autoconfigure.handler.AtomicNumberTypeHandler;
5760
import org.mybatis.spring.boot.autoconfigure.handler.DummyTypeHandler;
5861
import org.mybatis.spring.boot.autoconfigure.mapper.CityMapper;
5962
import org.mybatis.spring.boot.autoconfigure.repository.CityMapperImpl;
@@ -246,6 +249,8 @@ void testWithTypeHandlersPackage() {
246249
TypeHandlerRegistry typeHandlerRegistry = this.context.getBean(SqlSessionFactory.class).getConfiguration()
247250
.getTypeHandlerRegistry();
248251
assertThat(typeHandlerRegistry.hasTypeHandler(BigInteger.class)).isTrue();
252+
assertThat(typeHandlerRegistry.hasTypeHandler(AtomicInteger.class)).isTrue();
253+
assertThat(typeHandlerRegistry.hasTypeHandler(AtomicLong.class)).isTrue();
249254
}
250255

251256
@Test
@@ -360,6 +365,12 @@ void testMixedWithConfigurationFileAndTypeHandlersPackage() {
360365
assertThat(configuration.getDefaultFetchSize()).isEqualTo(1000);
361366
assertThat(configuration.getTypeHandlerRegistry().getTypeHandler(BigInteger.class))
362367
.isInstanceOf(DummyTypeHandler.class);
368+
assertThat(configuration.getTypeHandlerRegistry().getTypeHandler(AtomicInteger.class))
369+
.isInstanceOf(AtomicNumberTypeHandler.class);
370+
assertThat(configuration.getTypeHandlerRegistry().getTypeHandler(AtomicLong.class))
371+
.isInstanceOf(AtomicNumberTypeHandler.class);
372+
assertThat(configuration.getTypeHandlerRegistry().getTypeHandler(AtomicInteger.class).toString())
373+
.isEqualTo("type=" + AtomicInteger.class);
363374
}
364375

365376
@Test
@@ -388,7 +399,7 @@ void testMixedWithConfigurationFileAndTypeAliasesPackageAndMapperLocations() {
388399
@Test
389400
void testMixedWithFullConfigurations() {
390401
TestPropertyValues.of("mybatis.config-location:mybatis-config-settings-only.xml",
391-
"mybatis.type-handlers-package:org.mybatis.spring.boot.autoconfigure.handler",
402+
"mybatis.type-handlers-package:org.mybatis.spring.**.handler",
392403
"mybatis.type-aliases-package:org.mybatis.spring.boot.autoconfigure.domain",
393404
"mybatis.mapper-locations:classpath:org/mybatis/spring/boot/autoconfigure/repository/CityMapper.xml",
394405
"mybatis.executor-type=REUSE").applyTo(this.context);
@@ -404,6 +415,10 @@ void testMixedWithFullConfigurations() {
404415
assertThat(configuration.getDefaultFetchSize()).isEqualTo(1000);
405416
assertThat(configuration.getTypeHandlerRegistry().getTypeHandler(BigInteger.class))
406417
.isInstanceOf(DummyTypeHandler.class);
418+
assertThat(configuration.getTypeHandlerRegistry().getTypeHandler(AtomicInteger.class))
419+
.isInstanceOf(AtomicNumberTypeHandler.class);
420+
assertThat(configuration.getTypeHandlerRegistry().getTypeHandler(AtomicLong.class))
421+
.isInstanceOf(AtomicNumberTypeHandler.class);
407422
assertThat(configuration.getMappedStatementNames()).hasSize(4);
408423
assertThat(configuration.getMappedStatementNames()).contains("selectCityById");
409424
assertThat(configuration.getMappedStatementNames())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright 2015-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.mybatis.spring.boot.autoconfigure.handler;
17+
18+
import java.sql.CallableStatement;
19+
import java.sql.PreparedStatement;
20+
import java.sql.ResultSet;
21+
import java.sql.SQLException;
22+
import java.util.concurrent.atomic.AtomicInteger;
23+
import java.util.concurrent.atomic.AtomicLong;
24+
25+
import org.apache.ibatis.type.JdbcType;
26+
import org.apache.ibatis.type.MappedTypes;
27+
import org.apache.ibatis.type.TypeHandler;
28+
29+
@MappedTypes({ AtomicInteger.class, AtomicLong.class })
30+
public class AtomicNumberTypeHandler implements TypeHandler<Number> {
31+
32+
private final Class<? extends Number> type;
33+
34+
public AtomicNumberTypeHandler(Class<? extends Number> type) {
35+
this.type = type;
36+
}
37+
38+
public void setParameter(PreparedStatement ps, int i, Number parameter, JdbcType jdbcType) throws SQLException {
39+
}
40+
41+
public Number getResult(ResultSet rs, String columnName) throws SQLException {
42+
return null;
43+
}
44+
45+
public Number getResult(CallableStatement cs, int columnIndex) throws SQLException {
46+
return null;
47+
}
48+
49+
public Number getResult(ResultSet rs, int columnIndex) throws SQLException {
50+
return null;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return "type=" + type;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)