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