View Javadoc
1   /*
2    *    Copyright 2009-2021 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.deferload_common_property;
17  
18  import static org.junit.jupiter.api.Assertions.*;
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.getResourceAsReader("org/apache/ibatis/submitted/deferload_common_property/ibatisConfig.xml")) {
42              sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
43          }
44          try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/deferload_common_property/lazyLoadIbatisConfig.xml")) {
45              lazyLoadSqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
46          }
47  
48          BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
49                  "org/apache/ibatis/submitted/deferload_common_property/CreateDB.sql");
50      }
51  
52      @Test
53      void testDeferLoadAfterResultHandler() {
54          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
55              class MyResultHandler implements ResultHandler {
56                  private List<Child> children = new ArrayList<>();
57                  @Override
58                  public void handleResult(ResultContext context) {
59                      Child child = (Child)context.getResultObject();
60                      children.add(child);
61                  }
62              }
63              MyResultHandler myResultHandler = new MyResultHandler();
64              sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", myResultHandler);
65              for (Child child: myResultHandler.children) {
66                  assertNotNull(child.getFather());
67              }
68          }
69      }
70  
71      @Test
72      void testDeferLoadDuringResultHandler() {
73          try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
74              class MyResultHandler implements ResultHandler {
75                  @Override
76                  public void handleResult(ResultContext context) {
77                      Child child = (Child)context.getResultObject();
78                      assertNotNull(child.getFather());
79                  }
80              }
81              sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", new MyResultHandler());
82          }
83      }
84  
85      @Test
86      void testDeferLoadAfterResultHandlerWithLazyLoad() {
87          try (SqlSession sqlSession = lazyLoadSqlSessionFactory.openSession()) {
88              class MyResultHandler implements ResultHandler {
89                  private List<Child> children = new ArrayList<>();
90                  @Override
91                  public void handleResult(ResultContext context) {
92                      Child child = (Child)context.getResultObject();
93                      children.add(child);
94                  }
95              }
96              MyResultHandler myResultHandler = new MyResultHandler();
97              sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", myResultHandler);
98              for (Child child: myResultHandler.children) {
99                  assertNotNull(child.getFather());
100             }
101         }
102     }
103 
104     @Test
105     void testDeferLoadDuringResultHandlerWithLazyLoad() {
106         try (SqlSession sqlSession = lazyLoadSqlSessionFactory.openSession()) {
107             class MyResultHandler implements ResultHandler {
108                 @Override
109                 public void handleResult(ResultContext context) {
110                     Child child = (Child)context.getResultObject();
111                     assertNotNull(child.getFather());
112                 }
113             }
114             sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", new MyResultHandler());
115         }
116     }
117 }