1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }