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.SQLException;
25 import java.sql.Statement;
26
27 import org.apache.ibatis.logging.Log;
28 import org.junit.jupiter.api.Assertions;
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 StatementLoggerTest {
37
38 @Mock
39 Statement statement;
40
41 @Mock
42 Log log;
43
44 private Statement st;
45
46 @BeforeEach
47 void setUp() throws SQLException {
48 st = StatementLogger.newInstance(statement, log, 1);
49 }
50
51 @Test
52 void shouldPrintLog() throws SQLException {
53 when(log.isDebugEnabled()).thenReturn(true);
54 st.executeQuery("select 1");
55
56 verify(log).debug(contains("Executing: select 1"));
57 }
58
59 @Test
60 void shouldPrintLogForUpdate() throws SQLException {
61 when(log.isDebugEnabled()).thenReturn(true);
62 when(statement.execute(anyString())).thenReturn(true);
63 String sql = "update name = '' from test";
64 boolean execute = st.execute(sql);
65
66 verify(log).debug(contains(sql));
67 Assertions.assertTrue(execute);
68 }
69
70 @Test
71 void shouldNotPrintLog() throws SQLException {
72 st.getResultSet();
73 st.close();
74 verify(log, times(0)).debug(anyString());
75 }
76 }