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.lazyload_proxyfactory_comparison;
17  
18  import java.io.Reader;
19  
20  import org.apache.ibatis.BaseDataTest;
21  import org.apache.ibatis.io.Resources;
22  import org.apache.ibatis.session.SqlSession;
23  import org.apache.ibatis.session.SqlSessionFactory;
24  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
25  import org.junit.jupiter.api.AfterEach;
26  import org.junit.jupiter.api.Assertions;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  abstract class AbstractLazyTest {
31  
32    private SqlSession sqlSession;
33    private Mapper mapper;
34  
35    protected abstract String getConfiguration();
36  
37    @BeforeEach
38    void before() throws Exception {
39      // create a SqlSessionFactory
40      SqlSessionFactory sqlSessionFactory;
41      try (Reader reader = Resources.getResourceAsReader(
42          "org/apache/ibatis/submitted/lazyload_proxyfactory_comparison/mybatis-config-" + getConfiguration() + ".xml")) {
43        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
44      }
45  
46      // populate in-memory database
47      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
48          "org/apache/ibatis/submitted/lazyload_proxyfactory_comparison/CreateDB.sql");
49  
50      sqlSession = sqlSessionFactory.openSession();
51      mapper = sqlSession.getMapper(Mapper.class);
52    }
53  
54    @AfterEach
55    void after() {
56      if (sqlSession != null) {
57        sqlSession.close();
58      }
59    }
60  
61    @Test
62    void lazyLoadUserWithGetObjectWithInterface() {
63      Assertions.assertNotNull(mapper.getUserWithGetObjectWithInterface(1).getOwner());
64    }
65  
66    @Test
67    void lazyLoadUserWithGetObjectWithoutInterface() {
68      Assertions.assertNotNull(mapper.getUserWithGetObjectWithoutInterface(1).getOwner());
69    }
70  
71    @Test
72    void lazyLoadUserWithGetXxxWithInterface() {
73      Assertions.assertNotNull(mapper.getUserWithGetXxxWithInterface(1).getOwner());
74    }
75  
76    @Test
77    void lazyLoadUserWithGetXxxWithoutInterface() {
78      Assertions.assertNotNull(mapper.getUserWithGetXxxWithoutInterface(1).getOwner());
79    }
80  
81    @Test
82    void lazyLoadUserWithNothingWithInterface() {
83      Assertions.assertNotNull(mapper.getUserWithNothingWithInterface(1).getOwner());
84    }
85  
86    @Test
87    void lazyLoadUserWithNothingWithoutInterface() {
88      Assertions.assertNotNull(mapper.getUserWithNothingWithoutInterface(1).getOwner());
89    }
90  }