1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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
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
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
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
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
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 }