View Javadoc
1   /*
2    *    Copyright 2009-2024 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.executor.resultset;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.mockito.ArgumentMatchers.any;
20  import static org.mockito.Mockito.mock;
21  import static org.mockito.Mockito.when;
22  
23  import java.sql.Connection;
24  import java.sql.DatabaseMetaData;
25  import java.sql.ResultSet;
26  import java.sql.ResultSetMetaData;
27  import java.sql.SQLException;
28  import java.sql.Statement;
29  import java.sql.Types;
30  import java.util.ArrayList;
31  import java.util.HashMap;
32  import java.util.List;
33  
34  import org.apache.ibatis.builder.StaticSqlSource;
35  import org.apache.ibatis.executor.Executor;
36  import org.apache.ibatis.executor.ExecutorException;
37  import org.apache.ibatis.executor.parameter.ParameterHandler;
38  import org.apache.ibatis.mapping.BoundSql;
39  import org.apache.ibatis.mapping.MappedStatement;
40  import org.apache.ibatis.mapping.ResultMap;
41  import org.apache.ibatis.mapping.ResultMapping;
42  import org.apache.ibatis.mapping.SqlCommandType;
43  import org.apache.ibatis.session.Configuration;
44  import org.apache.ibatis.session.ResultHandler;
45  import org.apache.ibatis.session.RowBounds;
46  import org.apache.ibatis.type.TypeHandler;
47  import org.apache.ibatis.type.TypeHandlerRegistry;
48  import org.junit.jupiter.api.Assertions;
49  import org.junit.jupiter.api.Test;
50  import org.junit.jupiter.api.extension.ExtendWith;
51  import org.mockito.Mock;
52  import org.mockito.junit.jupiter.MockitoExtension;
53  
54  @ExtendWith(MockitoExtension.class)
55  class DefaultResultSetHandlerTest {
56  
57    @Mock
58    private Statement stmt;
59    @Mock
60    private ResultSet rs;
61    @Mock
62    private ResultSetMetaData rsmd;
63    @Mock
64    private Connection conn;
65    @Mock
66    private DatabaseMetaData dbmd;
67  
68    /**
69     * Contrary to the spec, some drivers require case-sensitive column names when getting result.
70     *
71     * @see <a href="https://github.com/mybatis/old-google-code-issues/issues/557">Issue 557</a>
72     */
73    @Test
74    void shouldRetainColumnNameCase() throws Exception {
75  
76      final MappedStatement ms = getMappedStatement();
77  
78      final Executor executor = null;
79      final ParameterHandler parameterHandler = null;
80      final ResultHandler resultHandler = null;
81      final BoundSql boundSql = null;
82      final RowBounds rowBounds = new RowBounds(0, 100);
83      final DefaultResultSetHandler fastResultSetHandler = new DefaultResultSetHandler(executor, ms, parameterHandler,
84          resultHandler, boundSql, rowBounds);
85  
86      when(stmt.getResultSet()).thenReturn(rs);
87      when(rs.getMetaData()).thenReturn(rsmd);
88      when(rs.getType()).thenReturn(ResultSet.TYPE_FORWARD_ONLY);
89      when(rs.next()).thenReturn(true).thenReturn(false);
90      when(rs.getInt("CoLuMn1")).thenReturn(100);
91      when(rsmd.getColumnCount()).thenReturn(1);
92      when(rsmd.getColumnLabel(1)).thenReturn("CoLuMn1");
93      when(rsmd.getColumnType(1)).thenReturn(Types.INTEGER);
94      when(rsmd.getColumnClassName(1)).thenReturn(Integer.class.getCanonicalName());
95      when(stmt.getConnection()).thenReturn(conn);
96      when(conn.getMetaData()).thenReturn(dbmd);
97      when(dbmd.supportsMultipleResultSets()).thenReturn(false); // for simplicity.
98  
99      final List<Object> results = fastResultSetHandler.handleResultSets(stmt);
100     assertEquals(1, results.size());
101     assertEquals(100, ((HashMap) results.get(0)).get("cOlUmN1"));
102   }
103 
104   @Test
105   void shouldThrowExceptionWithColumnName() throws Exception {
106     final MappedStatement ms = getMappedStatement();
107     final RowBounds rowBounds = new RowBounds(0, 100);
108 
109     final DefaultResultSetHandler defaultResultSetHandler = new DefaultResultSetHandler(null/* executor */, ms,
110         null/* parameterHandler */, null/* resultHandler */, null/* boundSql */, rowBounds);
111 
112     final ResultSetWrapper rsw = mock(ResultSetWrapper.class);
113     when(rsw.getResultSet()).thenReturn(mock(ResultSet.class));
114 
115     final ResultMapping resultMapping = mock(ResultMapping.class);
116     final TypeHandler typeHandler = mock(TypeHandler.class);
117     when(resultMapping.getColumn()).thenReturn("column");
118     when(resultMapping.getTypeHandler()).thenReturn(typeHandler);
119     when(typeHandler.getResult(any(ResultSet.class), any(String.class))).thenThrow(new SQLException("exception"));
120     List<ResultMapping> constructorMappings = List.of(resultMapping);
121 
122     try {
123       defaultResultSetHandler.createParameterizedResultObject(rsw, null/* resultType */, constructorMappings,
124           null/* constructorArgTypes */, null/* constructorArgs */, null/* columnPrefix */);
125       Assertions.fail("Should have thrown ExecutorException");
126     } catch (Exception e) {
127       Assertions.assertTrue(e instanceof ExecutorException, "Expected ExecutorException");
128       Assertions.assertTrue(e.getMessage().contains("mapping: " + resultMapping.toString()));
129     }
130   }
131 
132   MappedStatement getMappedStatement() {
133     final Configuration config = new Configuration();
134     final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
135     return new MappedStatement.Builder(config, "testSelect", new StaticSqlSource(config, "some select statement"),
136         SqlCommandType.SELECT).resultMaps(new ArrayList<ResultMap>() {
137           private static final long serialVersionUID = 1L;
138           {
139             add(new ResultMap.Builder(config, "testMap", HashMap.class, new ArrayList<ResultMapping>() {
140               private static final long serialVersionUID = 1L;
141               {
142                 add(new ResultMapping.Builder(config, "cOlUmN1", "CoLuMn1", registry.getTypeHandler(Integer.class))
143                     .build());
144               }
145             }).build());
146           }
147         }).build();
148   }
149 
150 }