View Javadoc
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.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.getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/mybatis-config.xml")) {
43        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
44      }
45  
46      // populate in-memory database
47      // Could not get the table creation, procedure creation, and data population to work from the same script.
48      // Once it was in three scripts, all seemed well.
49      try (SqlSession session = sqlSessionFactory.openSession();
50           Connection conn = session.getConnection()) {
51        try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/CreateDB1.sql")) {
52          runReaderScript(conn, reader);
53        }
54        try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/CreateDB2.sql")) {
55          runReaderScript(conn, reader);
56        }
57        try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multipleresultsetswithassociation/CreateDB3.sql")) {
58          runReaderScript(conn, reader);
59        }
60      }
61    }
62  
63    private static void runReaderScript(Connection conn, Reader reader) {
64      ScriptRunner runner = new ScriptRunner(conn);
65      runner.setLogWriter(null);
66      runner.setSendFullScript(true);
67      runner.setAutoCommit(true);
68      runner.setStopOnError(false);
69      runner.runScript(reader);
70    }
71  
72    @Test
73    void shouldGetOrderDetailsEachHavingAnOrderHeader() {
74      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
75        Mapper mapper = sqlSession.getMapper(Mapper.class);
76        List<OrderDetail> orderDetails = mapper.getOrderDetailsWithHeaders();
77  
78        // There are six order detail records in the database
79        // As long as the data does not change this should be successful
80        Assertions.assertEquals(6, orderDetails.size());
81  
82        // Each order detail should have a corresponding OrderHeader
83        // Only 2 of 6 orderDetails have orderHeaders
84        for(OrderDetail orderDetail : orderDetails){
85            Assertions.assertNotNull(orderDetail.getOrderHeader());
86        }
87      }
88    }
89  
90    @Test
91    void shouldGetOrderDetailsEachHavingAnOrderHeaderAnnotationBased() {
92      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
93        Mapper mapper = sqlSession.getMapper(Mapper.class);
94        List<OrderDetail> orderDetails = mapper.getOrderDetailsWithHeadersAnnotationBased();
95  
96        // There are six order detail records in the database
97        // As long as the data does not change this should be successful
98        Assertions.assertEquals(6, orderDetails.size());
99  
100       // Each order detail should have a corresponding OrderHeader
101       // Only 2 of 6 orderDetails have orderHeaders
102       for(OrderDetail orderDetail : orderDetails){
103           Assertions.assertNotNull(orderDetail.getOrderHeader());
104       }
105     }
106   }
107 
108 }