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