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.use_actual_param_name;
17  
18  import static org.junit.jupiter.api.Assertions.*;
19  
20  import java.io.Reader;
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.apache.ibatis.BaseDataTest;
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.BeforeAll;
30  import org.junit.jupiter.api.Test;
31  
32  class UseActualParamNameTest {
33  
34    private static SqlSessionFactory sqlSessionFactory;
35  
36    @BeforeAll
37    static void setUp() throws Exception {
38      // create an SqlSessionFactory
39      try (Reader reader = Resources
40          .getResourceAsReader("org/apache/ibatis/submitted/use_actual_param_name/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/use_actual_param_name/CreateDB.sql");
47    }
48  
49    @Test
50    void shouldSingleParamBeReferencedByAnyName() {
51      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
52        Mapper mapper = sqlSession.getMapper(Mapper.class);
53        User user = mapper.getUserById(1);
54        assertNotNull(user);
55      }
56    }
57  
58    @Test
59    void shouldMultipleParamsBeReferencedByActualNames() {
60      // This test requires -parameters compiler option
61      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
62        Mapper mapper = sqlSession.getMapper(Mapper.class);
63        User user = mapper.getUserByIdAndName(1, "User1");
64        assertNotNull(user);
65      }
66    }
67  
68    @Test
69    void shouldSoleListParamBeReferencedByImplicitName() {
70      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
71        Mapper mapper = sqlSession.getMapper(Mapper.class);
72        List<User> users = mapper.getUsersByIdList(Arrays.asList(1, 2));
73        assertEquals(2, users.size());
74      }
75    }
76  
77    @Test
78    void shouldListParamBeReferencedByActualNameIfAnotherParamExists() {
79      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
80        Mapper mapper = sqlSession.getMapper(Mapper.class);
81        List<User> users = mapper.getUsersByIdListAndName(Arrays.asList(1, 2), null);
82        assertEquals(2, users.size());
83      }
84    }
85  
86  }