1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }