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.Connection;
25  import java.sql.PreparedStatement;
26  import java.sql.SQLException;
27  
28  import org.apache.ibatis.logging.Log;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  import org.junit.jupiter.api.extension.ExtendWith;
32  import org.mockito.Mock;
33  import org.mockito.junit.jupiter.MockitoExtension;
34  
35  @ExtendWith(MockitoExtension.class)
36  class ConnectionLoggerTest {
37  
38    @Mock
39    Connection connection;
40  
41    @Mock
42    PreparedStatement preparedStatement;
43  
44    @Mock
45    Log log;
46  
47    private Connection conn;
48  
49    @BeforeEach
50    void setUp() throws SQLException {
51      conn = ConnectionLogger.newInstance(connection, log, 1);
52    }
53  
54    @Test
55    void shouldPrintPrepareStatement() throws SQLException {
56      when(log.isDebugEnabled()).thenReturn(true);
57      conn.prepareStatement("select 1");
58      verify(log).debug(contains("Preparing: select 1"));
59    }
60  
61    @Test
62    void shouldPrintPrepareCall() throws SQLException {
63      when(log.isDebugEnabled()).thenReturn(true);
64      conn.prepareCall("{ call test() }");
65      verify(log).debug(contains("Preparing: { call test() }"));
66    }
67  
68    @Test
69    void shouldNotPrintCreateStatement() throws SQLException {
70      conn.createStatement();
71      conn.close();
72      verify(log, times(0)).debug(anyString());
73    }
74  }