View Javadoc
1   /*
2    *    Copyright 2009-2023 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.cursor.defaults;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  import static org.mockito.Mockito.doReturn;
22  import static org.mockito.Mockito.when;
23  
24  import java.sql.ResultSet;
25  import java.sql.ResultSetMetaData;
26  import java.sql.SQLException;
27  import java.sql.Types;
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.apache.ibatis.builder.StaticSqlSource;
35  import org.apache.ibatis.executor.Executor;
36  import org.apache.ibatis.executor.parameter.ParameterHandler;
37  import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;
38  import org.apache.ibatis.executor.resultset.ResultSetWrapper;
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.TypeHandlerRegistry;
48  import org.junit.jupiter.api.Test;
49  import org.junit.jupiter.api.extension.ExtendWith;
50  import org.mockito.Mock;
51  import org.mockito.Spy;
52  import org.mockito.junit.jupiter.MockitoExtension;
53  
54  @ExtendWith(MockitoExtension.class)
55  class DefaultCursorTest {
56    @Spy
57    private ImpatientResultSet rs;
58    @Mock
59    protected ResultSetMetaData rsmd;
60  
61    @SuppressWarnings("unchecked")
62    @Test
63    void shouldCloseImmediatelyIfResultSetIsClosed() throws Exception {
64      final MappedStatement ms = getNestedAndOrderedMappedStatement();
65      final ResultMap rm = ms.getResultMaps().get(0);
66  
67      final Executor executor = null;
68      final ParameterHandler parameterHandler = null;
69      final ResultHandler<?> resultHandler = null;
70      final BoundSql boundSql = null;
71      final RowBounds rowBounds = RowBounds.DEFAULT;
72  
73      final DefaultResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, ms, parameterHandler,
74          resultHandler, boundSql, rowBounds);
75  
76      when(rsmd.getColumnCount()).thenReturn(2);
77      doReturn("id").when(rsmd).getColumnLabel(1);
78      doReturn(Types.INTEGER).when(rsmd).getColumnType(1);
79      doReturn(Integer.class.getCanonicalName()).when(rsmd).getColumnClassName(1);
80      doReturn("role").when(rsmd).getColumnLabel(2);
81      doReturn(Types.VARCHAR).when(rsmd).getColumnType(2);
82      doReturn(String.class.getCanonicalName()).when(rsmd).getColumnClassName(2);
83  
84      final ResultSetWrapper rsw = new ResultSetWrapper(rs, ms.getConfiguration());
85  
86      try (DefaultCursor<?> cursor = new DefaultCursor<>(resultSetHandler, rm, rsw, RowBounds.DEFAULT)) {
87        Iterator<?> iter = cursor.iterator();
88        assertTrue(iter.hasNext());
89        Map<String, Object> map = (Map<String, Object>) iter.next();
90        assertEquals(1, map.get("id"));
91        assertEquals("CEO", ((Map<String, Object>) map.get("roles")).get("role"));
92  
93        assertFalse(cursor.isConsumed());
94        assertTrue(cursor.isOpen());
95  
96        assertFalse(iter.hasNext());
97        assertTrue(cursor.isConsumed());
98        assertFalse(cursor.isOpen());
99      }
100   }
101 
102   private MappedStatement getNestedAndOrderedMappedStatement() {
103     final Configuration config = new Configuration();
104     final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();
105 
106     ResultMap nestedResultMap = new ResultMap.Builder(config, "roleMap", HashMap.class, new ArrayList<ResultMapping>() {
107       private static final long serialVersionUID = 1L;
108       {
109         add(new ResultMapping.Builder(config, "role", "role", registry.getTypeHandler(String.class)).build());
110       }
111     }).build();
112     config.addResultMap(nestedResultMap);
113 
114     return new MappedStatement.Builder(config, "selectPerson", new StaticSqlSource(config, "select person..."),
115         SqlCommandType.SELECT).resultMaps(new ArrayList<ResultMap>() {
116           private static final long serialVersionUID = 1L;
117           {
118             add(new ResultMap.Builder(config, "personMap", HashMap.class, new ArrayList<ResultMapping>() {
119               private static final long serialVersionUID = 1L;
120               {
121                 add(new ResultMapping.Builder(config, "id", "id", registry.getTypeHandler(Integer.class)).build());
122                 add(new ResultMapping.Builder(config, "roles").nestedResultMapId("roleMap").build());
123               }
124             }).build());
125           }
126         }).resultOrdered(true).build();
127   }
128 
129   /*
130    * Simulate a driver that closes ResultSet automatically when next() returns false (e.g. DB2).
131    */
132   protected abstract class ImpatientResultSet implements ResultSet {
133     private int rowIndex = -1;
134     private List<Map<String, Object>> rows = new ArrayList<>();
135 
136     protected ImpatientResultSet() {
137       Map<String, Object> row = new HashMap<>();
138       row.put("id", 1);
139       row.put("role", "CEO");
140       rows.add(row);
141     }
142 
143     @Override
144     public boolean next() throws SQLException {
145       throwIfClosed();
146       return ++rowIndex < rows.size();
147     }
148 
149     @Override
150     public boolean isClosed() {
151       return rowIndex >= rows.size();
152     }
153 
154     @Override
155     public String getString(String columnLabel) throws SQLException {
156       throwIfClosed();
157       return (String) rows.get(rowIndex).get(columnLabel);
158     }
159 
160     @Override
161     public int getInt(String columnLabel) throws SQLException {
162       throwIfClosed();
163       return (Integer) rows.get(rowIndex).get(columnLabel);
164     }
165 
166     @Override
167     public boolean wasNull() throws SQLException {
168       throwIfClosed();
169       return false;
170     }
171 
172     @Override
173     public ResultSetMetaData getMetaData() {
174       return rsmd;
175     }
176 
177     @Override
178     public int getType() throws SQLException {
179       throwIfClosed();
180       return ResultSet.TYPE_FORWARD_ONLY;
181     }
182 
183     private void throwIfClosed() throws SQLException {
184       if (rowIndex >= rows.size()) {
185         throw new SQLException("Invalid operation: result set is closed.");
186       }
187     }
188   }
189 }