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.automapping;
17  
18  import java.io.Reader;
19  import java.util.List;
20  
21  import org.apache.ibatis.BaseDataTest;
22  import org.apache.ibatis.io.Resources;
23  import org.apache.ibatis.session.AutoMappingBehavior;
24  import org.apache.ibatis.session.SqlSession;
25  import org.apache.ibatis.session.SqlSessionFactory;
26  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.BeforeAll;
29  import org.junit.jupiter.api.Test;
30  
31  class AutomappingTest {
32  
33    private static SqlSessionFactory sqlSessionFactory;
34  
35    @BeforeAll
36    static void setUp() throws Exception {
37      // create a SqlSessionFactory
38      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/automapping/mybatis-config.xml")) {
39        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40      }
41  
42      // populate in-memory database
43      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
44          "org/apache/ibatis/submitted/automapping/CreateDB.sql");
45    }
46  
47    @Test
48    void shouldGetAUser() {
49      sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
50      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
51        Mapper mapper = sqlSession.getMapper(Mapper.class);
52        User user = mapper.getUser(1);
53        Assertions.assertEquals("User1", user.getName());
54      }
55    }
56  
57    @Test
58    void shouldGetAUserWhithPhoneNumber() {
59      sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
60      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
61        Mapper mapper = sqlSession.getMapper(Mapper.class);
62        User user = mapper.getUserWithPhoneNumber(1);
63        Assertions.assertEquals("User1", user.getName());
64        Assertions.assertEquals(Long.valueOf(12345678901L), user.getPhone());
65      }
66    }
67  
68    @Test
69    void shouldNotInheritAutoMappingInherited_InlineNestedResultMap() {
70      sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
71      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72        Mapper mapper = sqlSession.getMapper(Mapper.class);
73        User user = mapper.getUserWithPets_Inline(2);
74        Assertions.assertEquals(Integer.valueOf(2), user.getId());
75        Assertions.assertEquals("User2", user.getName());
76        Assertions.assertNull(user.getPets().get(0).getPetName(), "should not inherit auto-mapping");
77        Assertions.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
78      }
79    }
80  
81    @Test
82    void shouldNotInheritAutoMappingInherited_ExternalNestedResultMap() {
83      sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.NONE);
84      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
85        Mapper mapper = sqlSession.getMapper(Mapper.class);
86        User user = mapper.getUserWithPets_External(2);
87        Assertions.assertEquals(Integer.valueOf(2), user.getId());
88        Assertions.assertEquals("User2", user.getName());
89        Assertions.assertNull(user.getPets().get(0).getPetName(), "should not inherit auto-mapping");
90        Assertions.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
91      }
92    }
93  
94    @Test
95    void shouldIgnorePartialAutoMappingBehavior_InlineNestedResultMap() {
96      // For nested resultMaps, PARTIAL works the same as NONE
97      sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
98      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
99        Mapper mapper = sqlSession.getMapper(Mapper.class);
100       User user = mapper.getUserWithPets_Inline(2);
101       Assertions.assertEquals(Integer.valueOf(2), user.getId());
102       Assertions.assertEquals("User2", user.getName());
103       Assertions.assertNull(user.getPets().get(0).getPetName(), "should not inherit auto-mapping");
104       Assertions.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
105     }
106   }
107 
108   @Test
109   void shouldRespectFullAutoMappingBehavior_InlineNestedResultMap() {
110     sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.FULL);
111     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
112       Mapper mapper = sqlSession.getMapper(Mapper.class);
113       User user = mapper.getUserWithPets_Inline(2);
114       Assertions.assertEquals(Integer.valueOf(2), user.getId());
115       Assertions.assertEquals("User2", user.getName());
116       Assertions.assertEquals("Chien", user.getPets().get(0).getPetName());
117       Assertions.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
118     }
119   }
120 
121   @Test
122   void shouldIgnorePartialAutoMappingBehavior_ExternalNestedResultMap() {
123     // For nested resultMaps, PARTIAL works the same as NONE
124     sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
125     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
126       Mapper mapper = sqlSession.getMapper(Mapper.class);
127       User user = mapper.getUserWithPets_External(2);
128       Assertions.assertEquals(Integer.valueOf(2), user.getId());
129       Assertions.assertEquals("User2", user.getName());
130       Assertions.assertNull(user.getPets().get(0).getPetName(), "should not inherit auto-mapping");
131       Assertions.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
132     }
133   }
134 
135   @Test
136   void shouldRespectFullAutoMappingBehavior_ExternalNestedResultMap() {
137     sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.FULL);
138     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
139       Mapper mapper = sqlSession.getMapper(Mapper.class);
140       User user = mapper.getUserWithPets_External(2);
141       Assertions.assertEquals(Integer.valueOf(2), user.getId());
142       Assertions.assertEquals("User2", user.getName());
143       Assertions.assertEquals("Chien", user.getPets().get(0).getPetName());
144       Assertions.assertEquals("John", user.getPets().get(0).getBreeder().getBreederName());
145     }
146   }
147 
148   @Test
149   void shouldGetBooks() {
150     // set automapping to default partial
151     sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
152     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
153       Mapper mapper = sqlSession.getMapper(Mapper.class);
154       // no errors throw
155       List<Book> books = mapper.getBooks();
156       Assertions.assertTrue(!books.isEmpty(), "should return results,no errors throw");
157     }
158   }
159 
160   @Test
161   void shouldUpdateFinalField() {
162     // set automapping to default partial
163     sqlSessionFactory.getConfiguration().setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
164     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
165       Mapper mapper = sqlSession.getMapper(Mapper.class);
166       Article article = mapper.getArticle();
167       // Java Language Specification 17.5.3 Subsequent Modification of Final Fields
168       // http://docs.oracle.com/javase/specs/jls/se5.0/html/memory.html#17.5.3
169       // The final field should be updated in mapping
170       Assertions.assertTrue(article.version > 0, "should update version in mapping");
171     }
172   }
173 }