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