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.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
40          .getResourceAsReader("org/apache/ibatis/submitted/duplicate_statements/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/duplicate_statements/CreateDB.sql");
47    }
48  
49    @Test
50    void shouldGetAllUsers() {
51      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
52        Mapper mapper = sqlSession.getMapper(Mapper.class);
53        List<User> users = mapper.getAllUsers();
54        Assertions.assertEquals(10, users.size());
55      }
56    }
57  
58    @Test
59    void shouldGetFirstFourUsers() {
60      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
61        Mapper mapper = sqlSession.getMapper(Mapper.class);
62        List<User> users = mapper.getAllUsers(new RowBounds(0, 4));
63        Assertions.assertEquals(4, users.size());
64      }
65    }
66  
67    @Test
68    @Disabled("fails currently - issue 507")
69    void shouldGetAllUsers_Annotated() {
70      sqlSessionFactory.getConfiguration().addMapper(AnnotatedMapper.class);
71      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72        AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
73        List<User> users = mapper.getAllUsers();
74        Assertions.assertEquals(10, users.size());
75      }
76    }
77  
78    @Test
79    @Disabled("fails currently - issue 507")
80    void shouldGetFirstFourUsers_Annotated() {
81      sqlSessionFactory.getConfiguration().addMapper(AnnotatedMapper.class);
82      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
83        AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
84        List<User> users = mapper.getAllUsers(new RowBounds(0, 4));
85        Assertions.assertEquals(4, users.size());
86      }
87    }
88  
89    @Test
90    void shouldFailForDuplicateMethod() {
91      Assertions.assertThrows(IllegalArgumentException.class,
92          () -> sqlSessionFactory.getConfiguration().addMapper(AnnotatedMapperExtended.class));
93    }
94  }