Skip to content

Commit 24ce380

Browse files
authored
Merge pull request #20 from microsphere-projects/dev
Dev
2 parents 19340d7 + 1b7fefe commit 24ce380

File tree

3 files changed

+258
-1
lines changed

3 files changed

+258
-1
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.microsphere.mybatis.executor;
18+
19+
import org.apache.ibatis.cache.CacheKey;
20+
import org.apache.ibatis.cursor.Cursor;
21+
import org.apache.ibatis.mapping.BoundSql;
22+
import org.apache.ibatis.mapping.MappedStatement;
23+
import org.apache.ibatis.reflection.MetaObject;
24+
import org.apache.ibatis.session.ResultHandler;
25+
import org.apache.ibatis.session.RowBounds;
26+
import org.apache.ibatis.transaction.Transaction;
27+
28+
import java.sql.SQLException;
29+
import java.util.List;
30+
31+
/**
32+
* {@link ExecutorFilter} class throws the errors.
33+
*
34+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/>
35+
* @see ExecutorFilter
36+
* @since 1.0.0
37+
*/
38+
public class ThrowingErrorExecutorFilter implements ExecutorFilter {
39+
40+
@Override
41+
public int update(MappedStatement ms, Object parameter, ExecutorFilterChain chain) throws SQLException {
42+
throwsError();
43+
return ExecutorFilter.super.update(ms, parameter, chain);
44+
}
45+
46+
@Override
47+
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler,
48+
CacheKey cacheKey, BoundSql boundSql, ExecutorFilterChain chain) throws SQLException {
49+
throwsError();
50+
return ExecutorFilter.super.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql, chain);
51+
}
52+
53+
@Override
54+
public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler,
55+
ExecutorFilterChain chain) throws SQLException {
56+
throwsError();
57+
return ExecutorFilter.super.query(ms, parameter, rowBounds, resultHandler, chain);
58+
}
59+
60+
@Override
61+
public <E> Cursor<E> queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, ExecutorFilterChain chain) throws SQLException {
62+
throwsError();
63+
return ExecutorFilter.super.queryCursor(ms, parameter, rowBounds, chain);
64+
}
65+
66+
@Override
67+
public void commit(boolean required, ExecutorFilterChain chain) throws SQLException {
68+
throwsError();
69+
ExecutorFilter.super.commit(required, chain);
70+
}
71+
72+
@Override
73+
public void rollback(boolean required, ExecutorFilterChain chain) throws SQLException {
74+
throwsError();
75+
ExecutorFilter.super.rollback(required, chain);
76+
}
77+
78+
@Override
79+
public CacheKey createCacheKey(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql, ExecutorFilterChain chain) {
80+
throwsError();
81+
return ExecutorFilter.super.createCacheKey(ms, parameter, rowBounds, boundSql, chain);
82+
}
83+
84+
@Override
85+
public void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType, ExecutorFilterChain chain) {
86+
throwsError();
87+
ExecutorFilter.super.deferLoad(ms, resultObject, property, key, targetType, chain);
88+
}
89+
90+
@Override
91+
public Transaction getTransaction(ExecutorFilterChain chain) {
92+
throwsError();
93+
return ExecutorFilter.super.getTransaction(chain);
94+
}
95+
96+
@Override
97+
public void close(boolean forceRollback, ExecutorFilterChain chain) {
98+
throwsError();
99+
ExecutorFilter.super.close(forceRollback, chain);
100+
}
101+
102+
static void throwsError() {
103+
throw new RuntimeException("For testing");
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package io.microsphere.mybatis.executor;
18+
19+
import io.microsphere.mybatis.plugin.InterceptorContext;
20+
import org.apache.ibatis.cache.CacheKey;
21+
import org.apache.ibatis.cursor.Cursor;
22+
import org.apache.ibatis.executor.Executor;
23+
import org.apache.ibatis.mapping.BoundSql;
24+
import org.apache.ibatis.mapping.MappedStatement;
25+
import org.apache.ibatis.reflection.MetaObject;
26+
import org.apache.ibatis.session.ResultHandler;
27+
import org.apache.ibatis.session.RowBounds;
28+
29+
import javax.annotation.Nullable;
30+
import java.sql.SQLException;
31+
import java.util.List;
32+
33+
import static io.microsphere.mybatis.executor.ThrowingErrorExecutorFilter.throwsError;
34+
35+
/**
36+
* {@link ExecutorInterceptor} class throws the errors.
37+
*
38+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/>
39+
* @see ExecutorInterceptor
40+
* @since 1.0.0
41+
*/
42+
public class ThrowingErrorExecutorInterceptor implements ExecutorInterceptor {
43+
44+
@Override
45+
public void beforeUpdate(InterceptorContext<Executor> context, MappedStatement ms, Object parameter) {
46+
throwsError();
47+
ExecutorInterceptor.super.beforeUpdate(context, ms, parameter);
48+
}
49+
50+
@Override
51+
public void afterUpdate(InterceptorContext<Executor> context, MappedStatement ms, Object parameter, @Nullable Integer result, @Nullable SQLException failure) {
52+
throwsError();
53+
ExecutorInterceptor.super.afterUpdate(context, ms, parameter, result, failure);
54+
}
55+
56+
@Override
57+
public void beforeQuery(InterceptorContext<Executor> context, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, @Nullable CacheKey cacheKey, @Nullable BoundSql boundSql) {
58+
throwsError();
59+
ExecutorInterceptor.super.beforeQuery(context, ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
60+
}
61+
62+
@Override
63+
public <E> void afterQuery(InterceptorContext<Executor> context, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, @Nullable CacheKey cacheKey, @Nullable BoundSql boundSql, @Nullable List<E> result, @Nullable SQLException failure) {
64+
throwsError();
65+
ExecutorInterceptor.super.afterQuery(context, ms, parameter, rowBounds, resultHandler, cacheKey, boundSql, result, failure);
66+
}
67+
68+
@Override
69+
public void beforeQueryCursor(InterceptorContext<Executor> context, MappedStatement ms, Object parameter, RowBounds rowBounds) {
70+
throwsError();
71+
ExecutorInterceptor.super.beforeQueryCursor(context, ms, parameter, rowBounds);
72+
}
73+
74+
@Override
75+
public <E> void afterQueryCursor(InterceptorContext<Executor> context, MappedStatement ms, Object parameter, RowBounds rowBounds, @Nullable Cursor<E> result, @Nullable SQLException failure) {
76+
throwsError();
77+
ExecutorInterceptor.super.afterQueryCursor(context, ms, parameter, rowBounds, result, failure);
78+
}
79+
80+
@Override
81+
public void beforeCommit(InterceptorContext<Executor> context, boolean required) {
82+
throwsError();
83+
ExecutorInterceptor.super.beforeCommit(context, required);
84+
}
85+
86+
@Override
87+
public void afterCommit(InterceptorContext<Executor> context, boolean required, @Nullable SQLException failure) {
88+
throwsError();
89+
ExecutorInterceptor.super.afterCommit(context, required, failure);
90+
}
91+
92+
@Override
93+
public void beforeRollback(InterceptorContext<Executor> context, boolean required) {
94+
throwsError();
95+
ExecutorInterceptor.super.beforeRollback(context, required);
96+
}
97+
98+
@Override
99+
public void afterRollback(InterceptorContext<Executor> context, boolean required, @Nullable SQLException failure) {
100+
throwsError();
101+
ExecutorInterceptor.super.afterRollback(context, required, failure);
102+
}
103+
104+
@Override
105+
public void beforeGetTransaction(InterceptorContext<Executor> context) {
106+
throwsError();
107+
ExecutorInterceptor.super.beforeGetTransaction(context);
108+
}
109+
110+
@Override
111+
public void afterGetTransaction(InterceptorContext<Executor> context, @Nullable Throwable failure) {
112+
throwsError();
113+
ExecutorInterceptor.super.afterGetTransaction(context, failure);
114+
}
115+
116+
@Override
117+
public void beforeCreateCacheKey(InterceptorContext<Executor> context, MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql) {
118+
throwsError();
119+
ExecutorInterceptor.super.beforeCreateCacheKey(context, ms, parameter, rowBounds, boundSql);
120+
}
121+
122+
@Override
123+
public void afterCreateCacheKey(InterceptorContext<Executor> context, MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql, @Nullable CacheKey key, @Nullable Throwable failure) {
124+
throwsError();
125+
ExecutorInterceptor.super.afterCreateCacheKey(context, ms, parameter, rowBounds, boundSql, key, failure);
126+
}
127+
128+
@Override
129+
public void beforeDeferLoad(InterceptorContext<Executor> context, MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType) {
130+
throwsError();
131+
ExecutorInterceptor.super.beforeDeferLoad(context, ms, resultObject, property, key, targetType);
132+
}
133+
134+
@Override
135+
public void afterDeferLoad(InterceptorContext<Executor> context, MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType, @Nullable Throwable failure) {
136+
throwsError();
137+
ExecutorInterceptor.super.afterDeferLoad(context, ms, resultObject, property, key, targetType, failure);
138+
}
139+
140+
@Override
141+
public void beforeClose(InterceptorContext<Executor> context, boolean forceRollback) {
142+
throwsError();
143+
ExecutorInterceptor.super.beforeClose(context, forceRollback);
144+
}
145+
146+
@Override
147+
public void afterClose(InterceptorContext<Executor> context, boolean forceRollback) {
148+
throwsError();
149+
ExecutorInterceptor.super.afterClose(context, forceRollback);
150+
}
151+
}

microsphere-mybatis-core/src/test/java/io/microsphere/mybatis/plugin/InterceptingExecutorInterceptorTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package io.microsphere.mybatis.plugin;
1818

19+
import io.microsphere.mybatis.executor.ThrowingErrorExecutorFilter;
1920
import io.microsphere.mybatis.executor.LogggingExecutorInterceptor;
2021
import io.microsphere.mybatis.executor.LoggingExecutorFilter;
2122
import io.microsphere.mybatis.executor.TestExecutorFilter;
@@ -44,7 +45,7 @@ protected void customize(Configuration configuration) {
4445

4546
private InterceptingExecutorInterceptor createInterceptingExecutorInterceptor() {
4647
InterceptingExecutorInterceptor interceptor = new InterceptingExecutorInterceptor(
47-
of(new LoggingExecutorFilter(), new TestExecutorFilter()),
48+
of(new LoggingExecutorFilter(), new TestExecutorFilter(), new ThrowingErrorExecutorFilter()),
4849
new LogggingExecutorInterceptor(), new TestInterceptorContextExecutorInterceptor());
4950
Properties properties = new Properties();
5051
properties.setProperty(TEST_PROPERTY_KEY, this.getClass().getName());

0 commit comments

Comments
 (0)