View Javadoc
1   /*
2    *    Copyright 2009-2022 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.arg_name_baesd_constructor_automapping;
17  
18  import static org.junit.jupiter.api.Assertions.*;
19  
20  import java.io.Reader;
21  
22  import org.apache.ibatis.BaseDataTest;
23  import org.apache.ibatis.exceptions.PersistenceException;
24  import org.apache.ibatis.executor.ExecutorException;
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 ArgNameBasedConstructorAutoMappingTest {
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/arg_name_baesd_constructor_automapping/mybatis-config.xml")) {
41        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42      }
43      sqlSessionFactory.getConfiguration().setArgNameBasedConstructorAutoMapping(true);
44      // populate in-memory database
45      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
46          "org/apache/ibatis/submitted/arg_name_baesd_constructor_automapping/CreateDB.sql");
47    }
48  
49    @Test
50    void shouldFindResultsInDifferentOrder() {
51      // This test requires -parameters compiler option
52      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
53        Mapper mapper = sqlSession.getMapper(Mapper.class);
54        User user = mapper.selectNameAndId(1);
55        assertEquals(Integer.valueOf(1), user.getId());
56        assertEquals("User1!", user.getName());
57      }
58    }
59  
60    @Test
61    void shouldRespectUseColumnLabelSetting() {
62      // This test requires -parameters compiler option
63      sqlSessionFactory.getConfiguration().setUseColumnLabel(false);
64      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
65        Mapper mapper = sqlSession.getMapper(Mapper.class);
66        User user = mapper.selectNameAndIdWithBogusLabel(1);
67        assertEquals(Integer.valueOf(1), user.getId());
68        assertEquals("User1!", user.getName());
69      } finally {
70        sqlSessionFactory.getConfiguration().setUseColumnLabel(true);
71      }
72    }
73  
74    @Test
75    void shouldErrorMessageBeHelpful() {
76      // This test requires -parameters compiler option
77      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
78        Mapper mapper = sqlSession.getMapper(Mapper.class);
79        mapper.selectNameAndIdWithBogusLabel(1);
80        fail("Exception should be thrown");
81      } catch (PersistenceException e) {
82        ExecutorException ex = (ExecutorException) e.getCause();
83        assertEquals(
84            "Constructor auto-mapping of 'public org.apache.ibatis.submitted.arg_name_baesd_constructor_automapping."
85                + "User(java.lang.Integer,java.lang.String)' failed "
86                + "because '[id]' were not found in the result set; "
87                + "Available columns are '[NAME, BAR]' and mapUnderscoreToCamelCase is 'true'.",
88            ex.getMessage());
89      }
90    }
91  
92    @Test
93    void shouldWorkWithExtraColumns() {
94      // This test requires -parameters compiler option
95      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
96        Mapper mapper = sqlSession.getMapper(Mapper.class);
97        User user = mapper.selectNameTeamAndId(1);
98        assertEquals(Integer.valueOf(1), user.getId());
99        assertEquals("User1!", user.getName());
100       assertEquals(99, user.getTeam());
101     }
102   }
103 
104   @Test
105   void shouldRespectParamAnnotation() {
106     // This test requires -parameters compiler option
107     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
108       Mapper mapper = sqlSession.getMapper(Mapper.class);
109       User2 user = mapper.selectUserIdAndUserName(1);
110       assertEquals(Integer.valueOf(1), user.getUserId());
111       assertEquals("User1", user.getName());
112     }
113   }
114 
115   @Test
116   void shouldRespectMapUnderscoreToCamelCaseSetting() {
117     // This test requires -parameters compiler option
118     sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
119     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
120       Mapper mapper = sqlSession.getMapper(Mapper.class);
121       User2 user = mapper.selectUserIdAndUserNameUnderscore(1);
122       assertEquals(Integer.valueOf(1), user.getUserId());
123       assertEquals("User1", user.getName());
124     }
125   }
126 
127   @Test
128   void shouldApplyColumnPrefix() {
129     sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
130     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
131       Mapper mapper = sqlSession.getMapper(Mapper.class);
132       Task task = mapper.selectTask(11);
133       assertEquals(Integer.valueOf(1), task.getAssignee().getId());
134       assertEquals("User1!", task.getAssignee().getName());
135       assertEquals(99, task.getAssignee().getTeam());
136     }
137   }
138 }