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.Mockito.verify;
19  import static org.mockito.Mockito.when;
20  
21  import java.sql.ResultSet;
22  import java.sql.ResultSetMetaData;
23  import java.sql.SQLException;
24  import java.sql.Types;
25  
26  import org.apache.ibatis.logging.Log;
27  import org.junit.jupiter.api.Test;
28  import org.junit.jupiter.api.extension.ExtendWith;
29  import org.mockito.Mock;
30  import org.mockito.junit.jupiter.MockitoExtension;
31  
32  @ExtendWith(MockitoExtension.class)
33  class ResultSetLoggerTest {
34  
35    @Mock
36    private ResultSet rs;
37  
38    @Mock
39    private Log log;
40  
41    @Mock
42    private ResultSetMetaData metaData;
43  
44    private void setup(int type) throws SQLException {
45      when(rs.next()).thenReturn(true);
46      when(rs.getMetaData()).thenReturn(metaData);
47      when(metaData.getColumnCount()).thenReturn(1);
48      when(metaData.getColumnType(1)).thenReturn(type);
49      when(metaData.getColumnLabel(1)).thenReturn("ColumnName");
50      when(log.isTraceEnabled()).thenReturn(true);
51      ResultSet resultSet = ResultSetLogger.newInstance(rs, log, 1);
52      resultSet.next();
53    }
54  
55    @Test
56    void shouldNotPrintBlobs() throws SQLException {
57      setup(Types.LONGNVARCHAR);
58      verify(log).trace("<==    Columns: ColumnName");
59      verify(log).trace("<==        Row: <<BLOB>>");
60    }
61  
62    @Test
63    void shouldPrintVarchars() throws SQLException {
64      when(rs.getString(1)).thenReturn("value");
65      setup(Types.VARCHAR);
66      verify(log).trace("<==    Columns: ColumnName");
67      verify(log).trace("<==        Row: value");
68    }
69  
70  }