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.submitted.multipleresultsetswithassociation;
17  
18  import java.io.Reader;
19  import java.sql.Connection;
20  import java.util.List;
21  
22  import org.apache.ibatis.io.Resources;
23  import org.apache.ibatis.jdbc.ScriptRunner;
24  import org.apache.ibatis.session.SqlSession;
25  import org.apache.ibatis.session.SqlSessionFactory;
26  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.BeforeAll;
29  import org.junit.jupiter.api.Test;
30  
31  /*
32   * This class contains tests for multiple result sets with an association mapping.
33   * This test is based on the org.apache.ibatis.submitted.multiple_resultsets test.
34   *
35   */
36  class MultipleResultSetTest {
37  
38    private static SqlSessionFactory sqlSessionFactory;
39  
40    @BeforeAll
41    static void setUp() throws Exception {
42      try (Reader reader = Resources
43          .getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/mybatis-config.xml")) {
44        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
45      }
46  
47      // populate in-memory database
48      // Could not get the table creation, procedure creation, and data population to work from the same script.
49      // Once it was in three scripts, all seemed well.
50      try (SqlSession session = sqlSessionFactory.openSession(); Connection conn = session.getConnection()) {
51        try (Reader reader = Resources
52            .getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/CreateDB1.sql")) {
53          runReaderScript(conn, reader);
54        }
55        try (Reader reader = Resources
56            .getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/CreateDB2.sql")) {
57          runReaderScript(conn, reader);
58        }
59        try (Reader reader = Resources
60            .getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/CreateDB3.sql")) {
61          runReaderScript(conn, reader);
62        }
63      }
64    }
65  
66    private static void runReaderScript(Connection conn, Reader reader) {
67      ScriptRunner runner = new ScriptRunner(conn);
68      runner.setLogWriter(null);
69      runner.setSendFullScript(true);
70      runner.setAutoCommit(true);
71      runner.setStopOnError(false);
72      runner.runScript(reader);
73    }
74  
75    @Test
76    void shouldGetOrderDetailsEachHavingAnOrderHeader() {
77      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
78        Mapper mapper = sqlSession.getMapper(Mapper.class);
79        List<OrderDetail> orderDetails = mapper.getOrderDetailsWithHeaders();
80  
81        // There are six order detail records in the database
82        // As long as the data does not change this should be successful
83        Assertions.assertEquals(6, orderDetails.size());
84  
85        // Each order detail should have a corresponding OrderHeader
86        // Only 2 of 6 orderDetails have orderHeaders
87        for (OrderDetail orderDetail : orderDetails) {
88          Assertions.assertNotNull(orderDetail.getOrderHeader());
89        }
90      }
91    }
92  
93    @Test
94    void shouldGetOrderDetailsEachHavingAnOrderHeaderAnnotationBased() {
95      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
96        Mapper mapper = sqlSession.getMapper(Mapper.class);
97        List<OrderDetail> orderDetails = mapper.getOrderDetailsWithHeadersAnnotationBased();
98  
99        // There are six order detail records in the database
100       // As long as the data does not change this should be successful
101       Assertions.assertEquals(6, orderDetails.size());
102 
103       // Each order detail should have a corresponding OrderHeader
104       // Only 2 of 6 orderDetails have orderHeaders
105       for (OrderDetail orderDetail : orderDetails) {
106         Assertions.assertNotNull(orderDetail.getOrderHeader());
107       }
108     }
109   }
110 
111 }