View Javadoc
1   /*
2    *    Copyright 2009-2022 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.submitted.multiple_resultsets;
17  
18  import java.io.IOException;
19  import java.util.List;
20  
21  import org.apache.ibatis.BaseDataTest;
22  import org.apache.ibatis.mapping.Environment;
23  import org.apache.ibatis.session.Configuration;
24  import org.apache.ibatis.session.SqlSession;
25  import org.apache.ibatis.session.SqlSessionFactory;
26  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27  import org.apache.ibatis.testcontainers.PgContainer;
28  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
29  import org.junit.jupiter.api.Assertions;
30  import org.junit.jupiter.api.BeforeAll;
31  import org.junit.jupiter.api.Tag;
32  import org.junit.jupiter.api.Test;
33  
34  /*
35   * This class contains tests for multiple results.
36   * It is based on Jeff's ref cursor tests.
37   */
38  @Tag("TestcontainersTests")
39  class MultipleResultTest {
40  
41    private static SqlSessionFactory sqlSessionFactory;
42  
43    @BeforeAll
44    static void setUp() throws Exception {
45      Configuration configuration = new Configuration();
46      Environment environment = new Environment("development", new JdbcTransactionFactory(),
47          PgContainer.getUnpooledDataSource());
48      configuration.setEnvironment(environment);
49      configuration.setMapUnderscoreToCamelCase(true);
50      configuration.addMapper(Mapper.class);
51      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
52  
53      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
54          "org/apache/ibatis/submitted/multiple_resultsets/CreateDB.sql");
55    }
56  
57    @Test
58    void shouldGetMultipleResultSetsWithOneStatement() throws IOException {
59      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
60        Mapper mapper = sqlSession.getMapper(Mapper.class);
61        List<List<?>> results = mapper.getUsersAndGroups();
62        Assertions.assertEquals(2, results.size());
63  
64        Assertions.assertEquals(6, results.get(0).size());
65        OrderDetail detail = (OrderDetail) results.get(0).get(0);
66        Assertions.assertEquals(1, detail.getOrderId());
67        Assertions.assertEquals(1, detail.getLineNumber());
68        Assertions.assertEquals(1, detail.getQuantity());
69        Assertions.assertEquals("Pen", detail.getItemDescription());
70  
71        Assertions.assertEquals(2, results.get(1).size());
72        OrderHeader header = (OrderHeader) results.get(1).get(0);
73        Assertions.assertEquals(1, header.getOrderId());
74        Assertions.assertEquals("Fred", header.getCustName());
75      }
76    }
77  
78    @Test
79    void shouldSkipNullResultSet() {
80      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
81        try {
82          Mapper mapper = sqlSession.getMapper(Mapper.class);
83          List<List<?>> results = mapper.multiResultsWithUpdate();
84          Assertions.assertEquals(2, results.size());
85  
86          Assertions.assertEquals(6, results.get(0).size());
87          OrderDetail detail = (OrderDetail) results.get(0).get(0);
88          Assertions.assertEquals(1, detail.getOrderId());
89          Assertions.assertEquals(1, detail.getLineNumber());
90          Assertions.assertEquals(1, detail.getQuantity());
91          Assertions.assertEquals("Pen", detail.getItemDescription());
92  
93          Assertions.assertEquals(2, results.get(1).size());
94          OrderHeader header = (OrderHeader) results.get(1).get(0);
95          Assertions.assertEquals(1, header.getOrderId());
96          Assertions.assertEquals("Fred", header.getCustName());
97  
98          results = mapper.getUsersAndGroups();
99          Assertions.assertEquals(7, results.get(0).size());
100         detail = (OrderDetail) results.get(0).get(6);
101         Assertions.assertEquals(2, detail.getOrderId());
102         Assertions.assertEquals(4, detail.getLineNumber());
103         Assertions.assertEquals(5, detail.getQuantity());
104         Assertions.assertEquals("Eraser", detail.getItemDescription());
105       } finally {
106         sqlSession.rollback(true);
107       }
108     }
109   }
110 }