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.xml_external_ref;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import java.io.IOException;
21  import java.io.Reader;
22  import java.sql.SQLException;
23  
24  import org.apache.ibatis.BaseDataTest;
25  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
26  import org.apache.ibatis.io.Resources;
27  import org.apache.ibatis.mapping.Environment;
28  import org.apache.ibatis.session.Configuration;
29  import org.apache.ibatis.session.SqlSession;
30  import org.apache.ibatis.session.SqlSessionFactory;
31  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
32  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
33  import org.junit.jupiter.api.Test;
34  
35  class ResultMapReferenceTest {
36  
37    @Test
38    void testCrossReferenceXmlConfig() throws Exception {
39      testCrossReference(getSqlSessionFactoryXmlConfig());
40    }
41  
42    @Test
43    void testCrossReferenceJavaConfig() throws Exception {
44      testCrossReference(getSqlSessionFactoryJavaConfig());
45    }
46  
47    private void testCrossReference(SqlSessionFactory sqlSessionFactory) {
48      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
49        ResultMapReferencePersonMapper personMapper = sqlSession.getMapper(ResultMapReferencePersonMapper.class);
50  
51        Pet pet = personMapper.selectPet(1);
52        assertEquals(Integer.valueOf(1), pet.getId());
53      }
54    }
55  
56    private SqlSessionFactory getSqlSessionFactoryXmlConfig() throws Exception {
57      try (Reader configReader = Resources
58          .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/ResultMapReferenceMapperConfig.xml")) {
59        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
60  
61        initDb(sqlSessionFactory);
62  
63        return sqlSessionFactory;
64      }
65    }
66  
67    private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
68      Configuration configuration = new Configuration();
69      Environment environment = new Environment("development", new JdbcTransactionFactory(),
70          new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
71      configuration.setEnvironment(environment);
72      configuration.addMapper(ResultMapReferencePersonMapper.class);
73      configuration.addMapper(ResultMapReferencePetMapper.class);
74  
75      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
76  
77      initDb(sqlSessionFactory);
78  
79      return sqlSessionFactory;
80    }
81  
82    private static void initDb(SqlSessionFactory sqlSessionFactory) throws IOException, SQLException {
83      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
84          "org/apache/ibatis/submitted/xml_external_ref/CreateDB.sql");
85    }
86  
87  }