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.io.Resources;
26  import org.apache.ibatis.mapping.MappedStatement;
27  import org.apache.ibatis.session.Configuration;
28  import org.apache.ibatis.session.SqlSession;
29  import org.apache.ibatis.session.SqlSessionFactory;
30  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31  import org.junit.jupiter.api.Test;
32  
33  class NonFullyQualifiedNamespaceTest {
34    @Test
35    void testCrossReferenceXmlConfig() throws Exception {
36      try (Reader configReader = Resources
37          .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/NonFullyQualifiedNamespaceConfig.xml")) {
38        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
39  
40        Configuration configuration = sqlSessionFactory.getConfiguration();
41  
42        MappedStatement selectPerson = configuration.getMappedStatement("person namespace.select");
43        assertEquals("org/apache/ibatis/submitted/xml_external_ref/NonFullyQualifiedNamespacePersonMapper.xml",
44            selectPerson.getResource());
45  
46        initDb(sqlSessionFactory);
47  
48        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
49          Person person = sqlSession.selectOne("person namespace.select", 1);
50          assertEquals((Integer) 1, person.getId());
51          assertEquals(2, person.getPets().size());
52          assertEquals((Integer) 2, person.getPets().get(1).getId());
53  
54          Pet pet = sqlSession.selectOne("person namespace.selectPet", 1);
55          assertEquals(Integer.valueOf(1), pet.getId());
56  
57          Pet pet2 = sqlSession.selectOne("pet namespace.select", 3);
58          assertEquals((Integer) 3, pet2.getId());
59          assertEquals((Integer) 2, pet2.getOwner().getId());
60        }
61      }
62    }
63  
64    private static void initDb(SqlSessionFactory sqlSessionFactory) throws IOException, SQLException {
65      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
66          "org/apache/ibatis/submitted/xml_external_ref/CreateDB.sql");
67    }
68  
69  }