Skip to content

Commit 95ac857

Browse files
committed
增加针对 BoundSqlInterceptor 的单元测试
1 parent e4d060c commit 95ac857

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.github.pagehelper.test.basic.provider;
2+
3+
import com.github.pagehelper.BoundSqlInterceptor;
4+
import com.github.pagehelper.util.MetaObjectUtil;
5+
import org.apache.ibatis.cache.CacheKey;
6+
import org.apache.ibatis.mapping.BoundSql;
7+
import org.apache.ibatis.reflection.MetaObject;
8+
9+
public class TestBoundSqlInterceptor implements BoundSqlInterceptor {
10+
public static final String COMMENT = "\n /* TestBoundSqlInterceptor */\n";
11+
12+
@Override
13+
public BoundSql boundSql(Type type, BoundSql boundSql, CacheKey cacheKey, Chain chain) {
14+
if (type == Type.ORIGINAL) {
15+
String sql = boundSql.getSql();
16+
MetaObject metaObject = MetaObjectUtil.forObject(boundSql);
17+
metaObject.setValue("sql", sql + COMMENT);
18+
}
19+
return chain.doBoundSql(type, boundSql, cacheKey);
20+
}
21+
22+
}

src/test/java/com/github/pagehelper/test/basic/provider/TestProviderInteceptor.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424

2525
package com.github.pagehelper.test.basic.provider;
2626

27+
import com.github.pagehelper.BoundSqlInterceptor;
2728
import com.github.pagehelper.PageHelper;
2829
import com.github.pagehelper.mapper.ProviderMethod;
2930
import com.github.pagehelper.mapper.UserMapper;
3031
import com.github.pagehelper.util.MybatisInterceptorHelper;
32+
import org.apache.ibatis.cache.CacheKey;
33+
import org.apache.ibatis.mapping.BoundSql;
3134
import org.apache.ibatis.session.SqlSession;
35+
import org.junit.Assert;
3236
import org.junit.Test;
3337

3438
import static org.junit.Assert.assertEquals;
@@ -40,7 +44,18 @@ public void testInterceptor() {
4044
SqlSession sqlSession = MybatisInterceptorHelper.getSqlSession();
4145
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
4246
try {
43-
PageHelper.startPage(1, 10);
47+
PageHelper.startPage(1, 10).boundSqlInterceptor(new BoundSqlInterceptor() {
48+
@Override
49+
public BoundSql boundSql(Type type, BoundSql boundSql, CacheKey cacheKey, Chain chain) {
50+
System.out.println("before: " + boundSql.getSql());
51+
BoundSql doBoundSql = chain.doBoundSql(type, boundSql, cacheKey);
52+
System.out.println("after: " + doBoundSql.getSql());
53+
if (type == Type.ORIGINAL) {
54+
Assert.assertTrue(doBoundSql.getSql().contains(TestBoundSqlInterceptor.COMMENT));
55+
}
56+
return doBoundSql;
57+
}
58+
});
4459
String str = "飞";
4560
userMapper.selectSimple(str);
4661
assertEquals(new ProviderMethod().selectSimple(str), SqlCache.get());

src/test/resources/hsqldb/mybatis-config-interceptor.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
<property name="helperDialect" value="mysql"/>
4848
<property name="supportMethodsArguments" value="true"/>
4949
<property name="rowBoundsWithCount" value="true"/>
50+
<property name="boundSqlInterceptors"
51+
value="com.github.pagehelper.test.basic.provider.TestBoundSqlInterceptor,com.github.pagehelper.test.basic.provider.TestBoundSqlInterceptor"/>
5052
</plugin>
5153
</plugins>
5254

src/test/resources/mysql/mybatis-config-interceptor.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
<property name="helperDialect" value="mysql"/>
4848
<property name="supportMethodsArguments" value="true"/>
4949
<property name="rowBoundsWithCount" value="true"/>
50+
<property name="boundSqlInterceptors"
51+
value="com.github.pagehelper.test.basic.provider.TestBoundSqlInterceptor,com.github.pagehelper.test.basic.provider.TestBoundSqlInterceptor"/>
5052
</plugin>
5153
</plugins>
5254

0 commit comments

Comments
 (0)