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.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      // create an SqlSessionFactory
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      // populate in-memory database
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 }