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.deferload_common_property;
17  
18  import static org.junit.jupiter.api.Assertions.assertNotNull;
19  
20  import java.io.Reader;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.ibatis.BaseDataTest;
25  import org.apache.ibatis.io.Resources;
26  import org.apache.ibatis.session.ResultContext;
27  import org.apache.ibatis.session.ResultHandler;
28  import org.apache.ibatis.session.SqlSession;
29  import org.apache.ibatis.session.SqlSessionFactory;
30  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31  import org.junit.jupiter.api.BeforeAll;
32  import org.junit.jupiter.api.Test;
33  
34  class CommonPropertyDeferLoadTest {
35  
36    private static SqlSessionFactory sqlSessionFactory;
37    private static SqlSessionFactory lazyLoadSqlSessionFactory;
38  
39    @BeforeAll
40    static void initDatabase() throws Exception {
41      try (Reader reader = Resources
42          .getResourceAsReader("org/apache/ibatis/submitted/deferload_common_property/ibatisConfig.xml")) {
43        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
44      }
45      try (Reader reader = Resources
46          .getResourceAsReader("org/apache/ibatis/submitted/deferload_common_property/lazyLoadIbatisConfig.xml")) {
47        lazyLoadSqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
48      }
49  
50      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
51          "org/apache/ibatis/submitted/deferload_common_property/CreateDB.sql");
52    }
53  
54    @Test
55    void testDeferLoadAfterResultHandler() {
56      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
57        class MyResultHandler implements ResultHandler {
58          private final List<Child> children = new ArrayList<>();
59  
60          @Override
61          public void handleResult(ResultContext context) {
62            Child child = (Child) context.getResultObject();
63            children.add(child);
64          }
65        }
66        MyResultHandler myResultHandler = new MyResultHandler();
67        sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", myResultHandler);
68        for (Child child : myResultHandler.children) {
69          assertNotNull(child.getFather());
70        }
71      }
72    }
73  
74    @Test
75    void testDeferLoadDuringResultHandler() {
76      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
77        class MyResultHandler implements ResultHandler {
78          @Override
79          public void handleResult(ResultContext context) {
80            Child child = (Child) context.getResultObject();
81            assertNotNull(child.getFather());
82          }
83        }
84        sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll",
85            new MyResultHandler());
86      }
87    }
88  
89    @Test
90    void testDeferLoadAfterResultHandlerWithLazyLoad() {
91      try (SqlSession sqlSession = lazyLoadSqlSessionFactory.openSession()) {
92        class MyResultHandler implements ResultHandler {
93          private final List<Child> children = new ArrayList<>();
94  
95          @Override
96          public void handleResult(ResultContext context) {
97            Child child = (Child) context.getResultObject();
98            children.add(child);
99          }
100       }
101       MyResultHandler myResultHandler = new MyResultHandler();
102       sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", myResultHandler);
103       for (Child child : myResultHandler.children) {
104         assertNotNull(child.getFather());
105       }
106     }
107   }
108 
109   @Test
110   void testDeferLoadDuringResultHandlerWithLazyLoad() {
111     try (SqlSession sqlSession = lazyLoadSqlSessionFactory.openSession()) {
112       class MyResultHandler implements ResultHandler {
113         @Override
114         public void handleResult(ResultContext context) {
115           Child child = (Child) context.getResultObject();
116           assertNotNull(child.getFather());
117         }
118       }
119       sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll",
120           new MyResultHandler());
121     }
122   }
123 }