1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.spring;
17
18 import static org.assertj.core.api.Assertions.assertThat;
19 import static org.junit.jupiter.api.Assertions.fail;
20
21 import com.mockrunner.mock.jdbc.MockConnection;
22 import com.mockrunner.mock.jdbc.MockResultSet;
23
24 import java.sql.SQLException;
25
26 import org.apache.ibatis.session.SqlSessionFactory;
27 import org.junit.jupiter.api.AfterEach;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.springframework.core.io.ClassPathResource;
31 import org.springframework.dao.support.PersistenceExceptionTranslator;
32 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
33
34 public abstract class AbstractMyBatisSpringTest {
35
36 protected static PooledMockDataSource dataSource = new PooledMockDataSource();
37
38 protected static SqlSessionFactory sqlSessionFactory;
39
40 protected static ExecutorInterceptor executorInterceptor = new ExecutorInterceptor();
41
42 protected static DataSourceTransactionManager txManager;
43
44 protected static PersistenceExceptionTranslator exceptionTranslator;
45
46 protected MockConnection connection;
47
48 protected MockConnection connectionTwo;
49
50 @BeforeAll
51 public static void setupBase() throws Exception {
52
53 SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
54 factoryBean.setMapperLocations(new ClassPathResource("org/mybatis/spring/TestMapper.xml"));
55
56 factoryBean.setDataSource(dataSource);
57 factoryBean.setPlugins(executorInterceptor);
58
59 exceptionTranslator = new MyBatisExceptionTranslator(dataSource, true);
60
61 sqlSessionFactory = factoryBean.getObject();
62
63 txManager = new DataSourceTransactionManager(dataSource);
64 }
65
66 protected void assertNoCommit() {
67 assertNoCommitJdbc();
68 assertNoCommitSession();
69 }
70
71 protected void assertNoCommitJdbc() {
72 assertThat(connection.getNumberCommits()).as("should not call commit on Connection").isEqualTo(0);
73 assertThat(connection.getNumberRollbacks()).as("should not call rollback on Connection").isEqualTo(0);
74 }
75
76 protected void assertNoCommitSession() {
77 assertThat(executorInterceptor.getCommitCount()).as("should not call commit on SqlSession").isEqualTo(0);
78 assertThat(executorInterceptor.getRollbackCount()).as("should not call rollback on SqlSession").isEqualTo(0);
79 }
80
81 protected void assertCommit() {
82 assertCommitJdbc();
83 assertCommitSession();
84 }
85
86 protected void assertCommitJdbc() {
87 assertThat(connection.getNumberCommits()).as("should call commit on Connection").isEqualTo(1);
88 assertThat(connection.getNumberRollbacks()).as("should not call rollback on Connection").isEqualTo(0);
89 }
90
91 protected void assertCommitSession() {
92 assertThat(executorInterceptor.getCommitCount()).as("should call commit on SqlSession").isEqualTo(1);
93 assertThat(executorInterceptor.getRollbackCount()).as("should not call rollback on SqlSession").isEqualTo(0);
94 }
95
96 protected void assertRollback() {
97 assertThat(connection.getNumberCommits()).as("should not call commit on Connection").isEqualTo(0);
98 assertThat(connection.getNumberRollbacks()).as("should call rollback on Connection").isEqualTo(1);
99 assertThat(executorInterceptor.getCommitCount()).as("should not call commit on SqlSession").isEqualTo(0);
100 assertThat(executorInterceptor.getRollbackCount()).as("should call rollback on SqlSession").isEqualTo(1);
101 }
102
103 protected void assertSingleConnection() {
104 assertThat(dataSource.getConnectionCount()).as("should only call DataSource.getConnection() once").isEqualTo(1);
105 }
106
107 protected void assertExecuteCount(int count) {
108 assertThat(connection.getPreparedStatementResultSetHandler().getExecutedStatements().size())
109 .as("should have executed %d SQL statements", count).isEqualTo(count);
110 }
111
112 protected void assertConnectionClosed(MockConnection connection) {
113 try {
114 if ((connection != null) && !connection.isClosed()) {
115 fail("Connection is not closed");
116 }
117 } catch (SQLException sqle) {
118 fail("cannot call Connection.isClosed() " + sqle.getMessage());
119 }
120 }
121
122 protected MockConnection createMockConnection() {
123
124 MockResultSet rs = new MockResultSet("SELECT 1");
125 rs.addRow(new Object[] { 1 });
126
127 MockConnection con = new MockConnection();
128 con.getPreparedStatementResultSetHandler().prepareResultSet("SELECT 1", rs);
129
130 return con;
131 }
132
133
134
135
136
137 @BeforeEach
138 public void setupConnection() throws SQLException {
139 dataSource.reset();
140 connection = createMockConnection();
141 connectionTwo = createMockConnection();
142 dataSource.addConnection(connectionTwo);
143 dataSource.addConnection(connection);
144 }
145
146 @BeforeEach
147 public void resetExecutorInterceptor() {
148 executorInterceptor.reset();
149 }
150
151 @AfterEach
152 public void validateConnectionClosed() {
153 assertConnectionClosed(connection);
154
155 connection = null;
156 }
157
158 }