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.duplicate_statements;
17  
18  import java.io.Reader;
19  import java.util.List;
20  
21  import org.apache.ibatis.BaseDataTest;
22  import org.apache.ibatis.io.Resources;
23  import org.apache.ibatis.session.RowBounds;
24  import org.apache.ibatis.session.SqlSession;
25  import org.apache.ibatis.session.SqlSessionFactory;
26  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Disabled;
30  import org.junit.jupiter.api.Test;
31  
32  class DuplicateStatementsTest {
33  
34    private SqlSessionFactory sqlSessionFactory;
35  
36    @BeforeEach
37    void setupDb() throws Exception {
38        // create a SqlSessionFactory
39        try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/duplicate_statements/mybatis-config.xml")) {
40          sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41        }
42  
43        // populate in-memory database
44        BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
45                "org/apache/ibatis/submitted/duplicate_statements/CreateDB.sql");
46    }
47  
48    @Test
49    void shouldGetAllUsers() {
50      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
51        Mapper mapper = sqlSession.getMapper(Mapper.class);
52        List<User> users = mapper.getAllUsers();
53        Assertions.assertEquals(10, users.size());
54      }
55    }
56  
57    @Test
58    void shouldGetFirstFourUsers() {
59      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
60        Mapper mapper = sqlSession.getMapper(Mapper.class);
61        List<User> users = mapper.getAllUsers(new RowBounds(0, 4));
62        Assertions.assertEquals(4, users.size());
63      }
64    }
65  
66    @Test
67    @Disabled("fails currently - issue 507")
68    void shouldGetAllUsers_Annotated() {
69      sqlSessionFactory.getConfiguration().addMapper(AnnotatedMapper.class);
70      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
71        AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
72        List<User> users = mapper.getAllUsers();
73        Assertions.assertEquals(10, users.size());
74      }
75    }
76  
77    @Test
78    @Disabled("fails currently - issue 507")
79    void shouldGetFirstFourUsers_Annotated() {
80      sqlSessionFactory.getConfiguration().addMapper(AnnotatedMapper.class);
81      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
82        AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
83        List<User> users = mapper.getAllUsers(new RowBounds(0, 4));
84        Assertions.assertEquals(4, users.size());
85      }
86    }
87  
88    @Test
89    void shouldFailForDuplicateMethod() {
90      Assertions.assertThrows(IllegalArgumentException.class,
91          () -> sqlSessionFactory.getConfiguration().addMapper(AnnotatedMapperExtended.class));
92    }
93  }