View Javadoc
1   /*
2    *    Copyright 2009-2024 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  import static org.junit.jupiter.api.Assertions.assertSame;
20  
21  import java.io.IOException;
22  import java.io.Reader;
23  import java.sql.SQLException;
24  
25  import org.apache.ibatis.BaseDataTest;
26  import org.apache.ibatis.builder.BuilderException;
27  import org.apache.ibatis.cache.Cache;
28  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
29  import org.apache.ibatis.io.Resources;
30  import org.apache.ibatis.mapping.Environment;
31  import org.apache.ibatis.mapping.MappedStatement;
32  import org.apache.ibatis.session.Configuration;
33  import org.apache.ibatis.session.SqlSession;
34  import org.apache.ibatis.session.SqlSessionFactory;
35  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
36  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
37  import org.junit.jupiter.api.Assertions;
38  import org.junit.jupiter.api.Test;
39  
40  class XmlExternalRefTest {
41  
42    @Test
43    void crossReferenceXmlConfig() throws Exception {
44      testCrossReference(getSqlSessionFactoryXmlConfig());
45    }
46  
47    @Test
48    void crossReferenceJavaConfig() throws Exception {
49      testCrossReference(getSqlSessionFactoryJavaConfig());
50    }
51  
52    @Test
53    void failFastOnBuildAll() {
54      Configuration configuration = new Configuration();
55      Assertions.assertDoesNotThrow(() -> {
56        configuration.addMapper(InvalidMapper.class);
57      }, "No exception should be thrown before parsing statement nodes.");
58      Assertions.assertThrows(BuilderException.class, configuration::getMappedStatementNames);
59    }
60  
61    @Test
62    void failFastOnBuildAllWithInsert() {
63      Configuration configuration = new Configuration();
64      Assertions.assertDoesNotThrow(() -> {
65        configuration.addMapper(InvalidWithInsertMapper.class);
66        configuration.addMapper(InvalidMapper.class);
67      }, "No exception should be thrown before parsing statement nodes.");
68      Assertions.assertThrows(BuilderException.class, configuration::getMappedStatementNames);
69    }
70  
71    @Test
72    void mappedStatementCache() throws Exception {
73      try (Reader configReader = Resources
74          .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MapperConfig.xml")) {
75        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
76  
77        Configuration configuration = sqlSessionFactory.getConfiguration();
78        configuration.getMappedStatementNames();
79  
80        MappedStatement selectPetStatement = configuration
81            .getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.PetMapper.select");
82        MappedStatement selectPersonStatement = configuration
83            .getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.PersonMapper.select");
84        Cache cache = selectPetStatement.getCache();
85        assertEquals("org.apache.ibatis.submitted.xml_external_ref.PetMapper", cache.getId());
86        assertSame(cache, selectPersonStatement.getCache());
87      }
88    }
89  
90    private void testCrossReference(SqlSessionFactory sqlSessionFactory) {
91      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
92        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
93        Person person = personMapper.select(1);
94        assertEquals((Integer) 1, person.getId());
95        assertEquals(2, person.getPets().size());
96        assertEquals((Integer) 2, person.getPets().get(1).getId());
97  
98        Pet pet = personMapper.selectPet(1);
99        assertEquals(Integer.valueOf(1), pet.getId());
100 
101       PetMapper petMapper = sqlSession.getMapper(PetMapper.class);
102       Pet pet2 = petMapper.select(3);
103       assertEquals((Integer) 3, pet2.getId());
104       assertEquals((Integer) 2, pet2.getOwner().getId());
105     }
106   }
107 
108   private SqlSessionFactory getSqlSessionFactoryXmlConfig() throws Exception {
109     try (Reader configReader = Resources
110         .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MapperConfig.xml")) {
111       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
112 
113       initDb(sqlSessionFactory);
114 
115       return sqlSessionFactory;
116     }
117   }
118 
119   private SqlSessionFactory getSqlSessionFactoryJavaConfig() throws Exception {
120     Configuration configuration = new Configuration();
121     Environment environment = new Environment("development", new JdbcTransactionFactory(),
122         new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:xmlextref", null));
123     configuration.setEnvironment(environment);
124     configuration.addMapper(PersonMapper.class);
125     configuration.addMapper(PetMapper.class);
126 
127     SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
128 
129     initDb(sqlSessionFactory);
130 
131     return sqlSessionFactory;
132   }
133 
134   private static void initDb(SqlSessionFactory sqlSessionFactory) throws IOException, SQLException {
135     BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
136         "org/apache/ibatis/submitted/xml_external_ref/CreateDB.sql");
137   }
138 
139 }