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