View Javadoc
1   /*
2    *    Copyright 2009-2023 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    *       https://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",
52          new StaticSqlSource(configuration, "sql"), null);
53    }
54  
55    @AfterEach
56    void resetMocks() {
57      reset(configuration, statement);
58    }
59  
60    @Test
61    void notSpecifyTimeout() throws SQLException {
62      BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null,
63          null);
64      handler.setStatementTimeout(statement, null);
65  
66      verifyNoInteractions(statement); // not apply anything
67    }
68  
69    @Test
70    void specifyMappedStatementTimeoutOnly() throws SQLException {
71      mappedStatementBuilder.timeout(10);
72  
73      BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null,
74          null);
75      handler.setStatementTimeout(statement, null);
76  
77      verify(statement).setQueryTimeout(10); // apply a mapped statement timeout
78    }
79  
80    @Test
81    void specifyDefaultTimeoutOnly() throws SQLException {
82      doReturn(20).when(configuration).getDefaultStatementTimeout();
83  
84      BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null,
85          null);
86      handler.setStatementTimeout(statement, null);
87  
88      verify(statement).setQueryTimeout(20); // apply a default timeout
89    }
90  
91    @Test
92    void specifyTransactionTimeout() throws SQLException {
93      BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null,
94          null);
95      handler.setStatementTimeout(statement, 5);
96  
97      verify(statement).setQueryTimeout(5); // apply a transaction timeout
98    }
99  
100   @Test
101   void specifyQueryTimeoutZeroAndTransactionTimeout() throws SQLException {
102     doReturn(0).when(configuration).getDefaultStatementTimeout();
103 
104     BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null,
105         null);
106     handler.setStatementTimeout(statement, 5);
107 
108     verify(statement).setQueryTimeout(5); // apply a transaction timeout
109   }
110 
111   @Test
112   void specifyMappedStatementTimeoutAndDefaultTimeout() throws SQLException {
113     doReturn(20).when(configuration).getDefaultStatementTimeout();
114     mappedStatementBuilder.timeout(30);
115 
116     BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null,
117         null);
118     handler.setStatementTimeout(statement, null);
119 
120     verify(statement).setQueryTimeout(30); // apply a mapped statement timeout
121     verify(configuration, never()).getDefaultStatementTimeout();
122   }
123 
124   @Test
125   void specifyQueryTimeoutAndTransactionTimeoutMinIsQueryTimeout() throws SQLException {
126     doReturn(10).when(configuration).getDefaultStatementTimeout();
127 
128     BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null,
129         null);
130     handler.setStatementTimeout(statement, 20);
131 
132     verify(statement).setQueryTimeout(10); // apply a query timeout
133   }
134 
135   @Test
136   void specifyQueryTimeoutAndTransactionTimeoutMinIsTransactionTimeout() throws SQLException {
137     doReturn(10).when(configuration).getDefaultStatementTimeout();
138 
139     BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null,
140         null);
141     handler.setStatementTimeout(statement, 5);
142 
143     verify(statement).setQueryTimeout(10);
144     verify(statement).setQueryTimeout(5); // apply a transaction timeout
145   }
146 
147   @Test
148   void specifyQueryTimeoutAndTransactionTimeoutWithSameValue() throws SQLException {
149     doReturn(10).when(configuration).getDefaultStatementTimeout();
150 
151     BaseStatementHandler handler = new SimpleStatementHandler(null, mappedStatementBuilder.build(), null, null, null,
152         null);
153     handler.setStatementTimeout(statement, 10);
154 
155     verify(statement).setQueryTimeout(10);
156   }
157 
158 }