|
| 1 | +/** |
| 2 | + * Copyright 2009-2019 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 | + * http://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.submitted.cursor_simple; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Iterator; |
| 22 | + |
| 23 | +import org.apache.ibatis.BaseDataTest; |
| 24 | +import org.apache.ibatis.cursor.Cursor; |
| 25 | +import org.apache.ibatis.mapping.Environment; |
| 26 | +import org.apache.ibatis.session.Configuration; |
| 27 | +import org.apache.ibatis.session.ExecutorType; |
| 28 | +import org.apache.ibatis.session.SqlSession; |
| 29 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 30 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 31 | +import org.apache.ibatis.testcontainers.MysqlContainer; |
| 32 | +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; |
| 33 | +import org.junit.jupiter.api.BeforeAll; |
| 34 | +import org.junit.jupiter.api.Tag; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +@Tag("TestcontainersTests") |
| 38 | +class MysqlCursorTest { |
| 39 | + |
| 40 | + private static SqlSessionFactory sqlSessionFactory; |
| 41 | + |
| 42 | + @BeforeAll |
| 43 | + static void setUp() throws Exception { |
| 44 | + Configuration configuration = new Configuration(); |
| 45 | + Environment environment = new Environment("development", new JdbcTransactionFactory(), |
| 46 | + MysqlContainer.getUnpooledDataSource()); |
| 47 | + configuration.setEnvironment(environment); |
| 48 | + configuration.addMapper(Mapper.class); |
| 49 | + sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); |
| 50 | + |
| 51 | + BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), |
| 52 | + "org/apache/ibatis/submitted/cursor_simple/CreateDB.sql"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testMySqlStreamResultSet() throws IOException { |
| 57 | + // #1654 and https://bugs.mysql.com/bug.php?id=96786 |
| 58 | + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { |
| 59 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 60 | + { |
| 61 | + Cursor<User> cursor = mapper.getUsersMysqlStream(); |
| 62 | + Iterator<User> iterator = cursor.iterator(); |
| 63 | + User user = iterator.next(); |
| 64 | + assertEquals("User1", user.getName()); |
| 65 | + cursor.close(); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testMySqlStreamResultSetBatch() throws IOException { |
| 72 | + // #1654 and https://bugs.mysql.com/bug.php?id=96786 |
| 73 | + try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) { |
| 74 | + Mapper mapper = sqlSession.getMapper(Mapper.class); |
| 75 | + { |
| 76 | + Cursor<User> cursor = mapper.getUsersMysqlStream(); |
| 77 | + Iterator<User> iterator = cursor.iterator(); |
| 78 | + User user = iterator.next(); |
| 79 | + assertEquals("User1", user.getName()); |
| 80 | + cursor.close(); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments