<commit_before>/**
 *    Copyright 2009-2018 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.apache.ibatis.cursor.defaults;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;
import org.apache.ibatis.executor.resultset.ResultSetWrapper;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class DefaultCursorTest {
  @Spy
  private ImpatientResultSet rs;
  @Mock
  protected ResultSetMetaData rsmd;
  @Mock
  private Connection conn;
  @Mock
  private DatabaseMetaData dbmd;
  @Mock
  private Statement stmt;

  @SuppressWarnings("unchecked")
  @Test
  public void shouldCloseImmediatelyIfResultSetIsClosed() throws Exception {
    final MappedStatement ms = getNestedAndOrderedMappedStatement();
    final ResultMap rm = ms.getResultMaps().get(0);

    final Executor executor = null;
    final ParameterHandler parameterHandler = null;
    final ResultHandler<?> resultHandler = null;
    final BoundSql boundSql = null;
    final RowBounds rowBounds = RowBounds.DEFAULT;

    final DefaultResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, ms, parameterHandler,
      resultHandler, boundSql, rowBounds);

    when(stmt.getResultSet()).thenReturn(rs);
    when(rsmd.getColumnCount()).thenReturn(2);
    when(rsmd.getColumnLabel(1)).thenReturn("id");
    when(rsmd.getColumnType(1)).thenReturn(Types.INTEGER);
    when(rsmd.getColumnClassName(1)).thenReturn(Integer.class.getCanonicalName());
    when(rsmd.getColumnLabel(2)).thenReturn("role");
    when(rsmd.getColumnType(2)).thenReturn(Types.VARCHAR);
    when(rsmd.getColumnClassName(2)).thenReturn(String.class.getCanonicalName());
    when(stmt.getConnection()).thenReturn(conn);
    when(conn.getMetaData()).thenReturn(dbmd);
    when(dbmd.supportsMultipleResultSets()).thenReturn(false);

    final ResultSetWrapper rsw = new ResultSetWrapper(rs, ms.getConfiguration());

    try (DefaultCursor<?> cursor = new DefaultCursor<>(resultSetHandler, rm, rsw, RowBounds.DEFAULT)) {
      Iterator<?> iter = cursor.iterator();
      assertTrue(iter.hasNext());
      Map<String, Object> map = (Map<String, Object>) iter.next();
      assertEquals(Integer.valueOf(1), map.get("id"));
      assertEquals("CEO", ((Map<String, Object>) map.get("roles")).get("role"));

      assertFalse(cursor.isConsumed());
      assertTrue(cursor.isOpen());

      assertFalse(iter.hasNext());
      assertTrue(cursor.isConsumed());
      assertFalse(cursor.isOpen());
    }
  }

  @SuppressWarnings("serial")
  private MappedStatement getNestedAndOrderedMappedStatement() {
    final Configuration config = new Configuration();
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();

    ResultMap nestedResultMap = new ResultMap.Builder(config, "roleMap", HashMap.class,
      new ArrayList<ResultMapping>() {
        {
          add(new ResultMapping.Builder(config, "role", "role", registry.getTypeHandler(String.class))
            .build());
        }
      }).build();<commit_msg>Polishing test<commit_after>/**
 *    Copyright 2009-2018 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.apache.ibatis.cursor.defaults;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;
import org.apache.ibatis.executor.resultset.ResultSetWrapper;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class DefaultCursorTest {
  @Spy
  private ImpatientResultSet rs;
  @Mock
  protected ResultSetMetaData rsmd;

  @SuppressWarnings("unchecked")
  @Test
  public void shouldCloseImmediatelyIfResultSetIsClosed() throws Exception {
    final MappedStatement ms = getNestedAndOrderedMappedStatement();
    final ResultMap rm = ms.getResultMaps().get(0);

    final Executor executor = null;
    final ParameterHandler parameterHandler = null;
    final ResultHandler<?> resultHandler = null;
    final BoundSql boundSql = null;
    final RowBounds rowBounds = RowBounds.DEFAULT;

    final DefaultResultSetHandler resultSetHandler = new DefaultResultSetHandler(executor, ms, parameterHandler,
      resultHandler, boundSql, rowBounds);

    when(rsmd.getColumnCount()).thenReturn(2);
    when(rsmd.getColumnLabel(1)).thenReturn("id");
    when(rsmd.getColumnType(1)).thenReturn(Types.INTEGER);
    when(rsmd.getColumnClassName(1)).thenReturn(Integer.class.getCanonicalName());
    when(rsmd.getColumnLabel(2)).thenReturn("role");
    when(rsmd.getColumnType(2)).thenReturn(Types.VARCHAR);
    when(rsmd.getColumnClassName(2)).thenReturn(String.class.getCanonicalName());

    final ResultSetWrapper rsw = new ResultSetWrapper(rs, ms.getConfiguration());

    try (DefaultCursor<?> cursor = new DefaultCursor<>(resultSetHandler, rm, rsw, RowBounds.DEFAULT)) {
      Iterator<?> iter = cursor.iterator();
      assertTrue(iter.hasNext());
      Map<String, Object> map = (Map<String, Object>) iter.next();
      assertEquals(Integer.valueOf(1), map.get("id"));
      assertEquals("CEO", ((Map<String, Object>) map.get("roles")).get("role"));

      assertFalse(cursor.isConsumed());
      assertTrue(cursor.isOpen());

      assertFalse(iter.hasNext());
      assertTrue(cursor.isConsumed());
      assertFalse(cursor.isOpen());
    }
  }

  @SuppressWarnings("serial")
  private MappedStatement getNestedAndOrderedMappedStatement() {
    final Configuration config = new Configuration();
    final TypeHandlerRegistry registry = config.getTypeHandlerRegistry();

    ResultMap nestedResultMap = new ResultMap.Builder(config, "roleMap", HashMap.class,
      new ArrayList<ResultMapping>() {
        {
          add(new ResultMapping.Builder(config, "role", "role", registry.getTypeHandler(String.class))
            .build());
        }
      }).build();
{'fraction_non_alphanumeric': 0.11365913391962595, 'fraction_numerical': 0.00467549816319715, 'mean_word_length': 4.686075949367089, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4265417', 'n_tokens_mistral': 2571, 'n_tokens_neox': 2600, 'n_words': 576}