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 ParameterMapReferenceTest {
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        ParameterMapReferencePersonMapper personMapper = sqlSession.getMapper(ParameterMapReferencePersonMapper.class);
50        Person parameter = new Person();
51        parameter.setId(1);
52        Person person = personMapper.select(parameter);
53        assertEquals((Integer) 1, person.getId());
54      }
55    }
56  
57    private SqlSessionFactory getSqlSessionFactoryXmlConfig() throws Exception {
58      try (Reader configReader = Resources
59          .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/ParameterMapReferenceMapperConfig.xml")) {
60        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
61  
62        initDb(sqlSessionFactory);
63  
64        return sqlSessionFactory;
65      }
66    }
67  
68    private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
69      Configuration configuration = new Configuration();
70      Environment environment = new Environment("development", new JdbcTransactionFactory(),
71          new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
72      configuration.setEnvironment(environment);
73      configuration.addMapper(ParameterMapReferencePersonMapper.class);
74      configuration.addMapper(ParameterMapReferencePetMapper.class);
75  
76      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
77  
78      initDb(sqlSessionFactory);
79  
80      return sqlSessionFactory;
81    }
82  
83    private static void initDb(SqlSessionFactory sqlSessionFactory) throws IOException, SQLException {
84      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
85          "org/apache/ibatis/submitted/xml_external_ref/CreateDB.sql");
86    }
87  
88  }