View Javadoc
1   /*
2    *    Copyright 2009-2021 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.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); // not apply anything
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); // apply a mapped statement timeout
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); // apply a default timeout
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); // apply a transaction timeout
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); // apply a transaction timeout
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); // apply a mapped statement timeout
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); // apply a query timeout
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); // apply a transaction timeout
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 }