View Javadoc
1   /*
2    *    Copyright 2009-2022 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.logging.jdbc;
17  
18  import static org.mockito.ArgumentMatchers.anyString;
19  import static org.mockito.ArgumentMatchers.contains;
20  import static org.mockito.Mockito.times;
21  import static org.mockito.Mockito.verify;
22  import static org.mockito.Mockito.when;
23  
24  import java.sql.PreparedStatement;
25  import java.sql.ResultSet;
26  import java.sql.SQLException;
27  
28  import org.apache.ibatis.logging.Log;
29  import org.apache.ibatis.type.JdbcType;
30  import org.junit.jupiter.api.Assertions;
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.junit.jupiter.MockitoExtension;
36  
37  @ExtendWith(MockitoExtension.class)
38  class PreparedStatementLoggerTest {
39  
40    @Mock
41    Log log;
42  
43    @Mock
44    PreparedStatement preparedStatement;
45  
46    @Mock
47    ResultSet resultSet;
48  
49    private PreparedStatement ps;
50  
51    @BeforeEach
52    void setUp() throws SQLException {
53      ps = PreparedStatementLogger.newInstance(this.preparedStatement, log, 1);
54    }
55  
56    @Test
57    void shouldPrintParameters() throws SQLException {
58      when(log.isDebugEnabled()).thenReturn(true);
59      when(preparedStatement.executeQuery(anyString())).thenReturn(resultSet);
60  
61      ps.setInt(1, 10);
62      ResultSet rs = ps.executeQuery("select 1 limit ?");
63  
64      verify(log).debug(contains("Parameters: 10(Integer)"));
65      Assertions.assertNotNull(rs);
66      Assertions.assertNotSame(resultSet, rs);
67    }
68  
69    @Test
70    void shouldPrintNullParameters() throws SQLException {
71      when(log.isDebugEnabled()).thenReturn(true);
72      when(preparedStatement.execute(anyString())).thenReturn(true);
73  
74      ps.setNull(1, JdbcType.VARCHAR.TYPE_CODE);
75      boolean result = ps.execute("update name = ? from test");
76  
77      verify(log).debug(contains("Parameters: null"));
78      Assertions.assertTrue(result);
79    }
80  
81    @Test
82    void shouldNotPrintLog() throws SQLException {
83      ps.getResultSet();
84      ps.getParameterMetaData();
85  
86      verify(log, times(0)).debug(anyString());
87    }
88  
89    @Test
90    void shouldPrintUpdateCount() throws SQLException {
91      when(log.isDebugEnabled()).thenReturn(true);
92      when(preparedStatement.getUpdateCount()).thenReturn(1);
93  
94      ps.getUpdateCount();
95  
96      verify(log).debug(contains("Updates: 1"));
97    }
98  }