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