View Javadoc
1   /**
2    *    Copyright 2009-2019 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    *       http://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.*;
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.builder.BuilderException;
26  import org.apache.ibatis.cache.Cache;
27  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
28  import org.apache.ibatis.io.Resources;
29  import org.apache.ibatis.mapping.Environment;
30  import org.apache.ibatis.mapping.MappedStatement;
31  import org.apache.ibatis.session.Configuration;
32  import org.apache.ibatis.session.SqlSession;
33  import org.apache.ibatis.session.SqlSessionFactory;
34  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
35  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
36  import org.junit.jupiter.api.Assertions;
37  import org.junit.jupiter.api.Test;
38  
39  class XmlExternalRefTest {
40  
41    @Test
42    void testCrossReferenceXmlConfig() throws Exception {
43      testCrossReference(getSqlSessionFactoryXmlConfig());
44    }
45  
46    @Test
47    void testCrossReferenceJavaConfig() throws Exception {
48      testCrossReference(getSqlSessionFactoryJavaConfig());
49    }
50  
51    @Test
52    void testFailFastOnBuildAll() {
53      Configuration configuration = new Configuration();
54      try {
55        configuration.addMapper(InvalidMapper.class);
56      } catch (Exception e) {
57        fail("No exception should be thrown before parsing statement nodes.");
58      }
59      Assertions.assertThrows(BuilderException.class, configuration::getMappedStatementNames);
60    }
61  
62    @Test
63    void testFailFastOnBuildAllWithInsert() {
64      Configuration configuration = new Configuration();
65      try {
66        configuration.addMapper(InvalidWithInsertMapper.class);
67        configuration.addMapper(InvalidMapper.class);
68      } catch (Exception e) {
69        fail("No exception should be thrown before parsing statement nodes.");
70      }
71      Assertions.assertThrows(BuilderException.class, configuration::getMappedStatementNames);
72    }
73  
74    @Test
75    void testMappedStatementCache() throws Exception {
76      try (Reader configReader = Resources.getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MapperConfig.xml")) {
77        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
78  
79        Configuration configuration = sqlSessionFactory.getConfiguration();
80        configuration.getMappedStatementNames();
81  
82        MappedStatement selectPetStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.PetMapper.select");
83        MappedStatement selectPersonStatement = configuration.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(), new UnpooledDataSource(
122         "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 }