1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.mapper_extend;
17
18 import static com.googlecode.catchexception.apis.BDDCatchException.caughtException;
19 import static com.googlecode.catchexception.apis.BDDCatchException.when;
20 import static org.assertj.core.api.BDDAssertions.then;
21
22 import java.io.Reader;
23
24 import org.apache.ibatis.BaseDataTest;
25 import org.apache.ibatis.binding.BindingException;
26 import org.apache.ibatis.io.Resources;
27 import org.apache.ibatis.session.SqlSession;
28 import org.apache.ibatis.session.SqlSessionFactory;
29 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30 import org.junit.jupiter.api.Assertions;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33
34 class MapperExtendTest {
35
36 private static SqlSessionFactory sqlSessionFactory;
37
38 @BeforeAll
39 static void setUp() throws Exception {
40
41 try (
42 Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/mapper_extend/mybatis-config.xml")) {
43 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
44 }
45
46
47 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
48 "org/apache/ibatis/submitted/mapper_extend/CreateDB.sql");
49 }
50
51 @Test
52 void shouldGetAUserWithAnExtendedXMLMethod() {
53 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
54 ParentMapper mapper = sqlSession.getMapper(Mapper.class);
55 User user = mapper.getUserXML();
56 Assertions.assertEquals("User1", user.getName());
57 }
58 }
59
60 @Test
61 void shouldGetAUserWithAnExtendedAnnotatedMethod() {
62 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
63 ParentMapper mapper = sqlSession.getMapper(Mapper.class);
64 User user = mapper.getUserAnnotated();
65 Assertions.assertEquals("User1", user.getName());
66 }
67 }
68
69 @Test
70 void shouldGetAUserWithAnOverloadedXMLMethod() {
71 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72 ParentMapper mapper = sqlSession.getMapper(MapperOverload.class);
73 User user = mapper.getUserXML();
74 Assertions.assertEquals("User2", user.getName());
75 }
76 }
77
78 @Test
79 void shouldGetAUserWithAnOverloadedAnnotatedMethod() {
80 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
81 ParentMapper mapper = sqlSession.getMapper(MapperOverload.class);
82 User user = mapper.getUserAnnotated();
83 Assertions.assertEquals("User2", user.getName());
84 }
85 }
86
87 @Test
88 void shouldFindStatementInSubInterfaceOfDeclaringClass() {
89 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
90 ChildMapper mapper = sqlSession.getMapper(ChildMapper.class);
91 User user = mapper.getUserByName("User1");
92 Assertions.assertNotNull(user);
93 }
94 }
95
96 @Test
97 void shouldThrowExceptionIfNoMatchingStatementFound() {
98 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
99 Mapper mapper = sqlSession.getMapper(Mapper.class);
100 when(mapper::noMappedStatement);
101 then(caughtException()).isInstanceOf(BindingException.class)
102 .hasMessage("Invalid bound statement (not found): " + Mapper.class.getName() + ".noMappedStatement");
103 }
104 }
105 }