1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.executor.statement;
17
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.never;
20 import static org.mockito.Mockito.reset;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.verifyNoInteractions;
23
24 import java.sql.SQLException;
25 import java.sql.Statement;
26
27 import org.apache.ibatis.builder.StaticSqlSource;
28 import org.apache.ibatis.mapping.MappedStatement;
29 import org.apache.ibatis.session.Configuration;
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.Mock;
35 import org.mockito.Spy;
36 import org.mockito.junit.jupiter.MockitoExtension;
37
38 @ExtendWith(MockitoExtension.class)
39 class BaseStatementHandlerTest {
40
41 @Spy
42 Configuration configuration;
43
44 @Mock
45 Statement statement;
46
47 private MappedStatement.Builder mappedStatementBuilder;
48
49 @BeforeEach
50 void setupMappedStatement() {
51 this.mappedStatementBuilder = new MappedStatement.Builder(configuration, "id", new StaticSqlSource(configuration, "sql"), null);
52 }
53
54 @AfterEach
55 void resetMocks() {
56 reset(configuration, statement);
57 }
58
59 @Test
60 void notSpecifyTimeout() throws SQLException {
61 BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null, null);
62 handler.setStatementTimeout(statement, null);
63
64 verifyNoInteractions(statement);
65 }
66
67 @Test
68 void specifyMappedStatementTimeoutOnly() throws SQLException {
69 mappedStatementBuilder.timeout(10);
70
71 BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null, null);
72 handler.setStatementTimeout(statement, null);
73
74 verify(statement).setQueryTimeout(10);
75 }
76
77 @Test
78 void specifyDefaultTimeoutOnly() throws SQLException {
79 doReturn(20).when(configuration).getDefaultStatementTimeout();
80
81 BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null, null);
82 handler.setStatementTimeout(statement, null);
83
84 verify(statement).setQueryTimeout(20);
85 }
86
87 @Test
88 void specifyTransactionTimeout() throws SQLException {
89 BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null, null);
90 handler.setStatementTimeout(statement, 5);
91
92 verify(statement).setQueryTimeout(5);
93 }
94
95 @Test
96 void specifyQueryTimeoutZeroAndTransactionTimeout() throws SQLException {
97 doReturn(0).when(configuration).getDefaultStatementTimeout();
98
99 BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null, null);
100 handler.setStatementTimeout(statement, 5);
101
102 verify(statement).setQueryTimeout(5);
103 }
104
105 @Test
106 void specifyMappedStatementTimeoutAndDefaultTimeout() throws SQLException {
107 doReturn(20).when(configuration).getDefaultStatementTimeout();
108 mappedStatementBuilder.timeout(30);
109
110 BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null, null);
111 handler.setStatementTimeout(statement, null);
112
113 verify(statement).setQueryTimeout(30);
114 verify(configuration, never()).getDefaultStatementTimeout();
115 }
116
117 @Test
118 void specifyQueryTimeoutAndTransactionTimeoutMinIsQueryTimeout() throws SQLException {
119 doReturn(10).when(configuration).getDefaultStatementTimeout();
120
121 BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null, null);
122 handler.setStatementTimeout(statement, 20);
123
124 verify(statement).setQueryTimeout(10);
125 }
126
127 @Test
128 void specifyQueryTimeoutAndTransactionTimeoutMinIsTransactionTimeout() throws SQLException {
129 doReturn(10).when(configuration).getDefaultStatementTimeout();
130
131 BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null, null);
132 handler.setStatementTimeout(statement, 5);
133
134 verify(statement).setQueryTimeout(10);
135 verify(statement).setQueryTimeout(5);
136 }
137
138 @Test
139 void specifyQueryTimeoutAndTransactionTimeoutWithSameValue() throws SQLException {
140 doReturn(10).when(configuration).getDefaultStatementTimeout();
141
142 BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null, null);
143 handler.setStatementTimeout(statement, 10);
144
145 verify(statement).setQueryTimeout(10);
146 }
147
148 }