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.foreach;
17  
18  import static com.googlecode.catchexception.apis.BDDCatchException.caughtException;
19  import static com.googlecode.catchexception.apis.BDDCatchException.when;
20  import static org.assertj.core.api.BDDAssertions.then;
21  
22  import java.io.IOException;
23  import java.io.Reader;
24  import java.sql.SQLException;
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.Collections;
28  import java.util.List;
29  
30  import org.apache.ibatis.BaseDataTest;
31  import org.apache.ibatis.exceptions.PersistenceException;
32  import org.apache.ibatis.io.Resources;
33  import org.apache.ibatis.session.SqlSession;
34  import org.apache.ibatis.session.SqlSessionFactory;
35  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
36  import org.junit.jupiter.api.Assertions;
37  import org.junit.jupiter.api.BeforeAll;
38  import org.junit.jupiter.api.Test;
39  
40  class ForEachTest {
41  
42    private static SqlSessionFactory sqlSessionFactory;
43  
44    @BeforeAll
45    static void setUp() throws Exception {
46      // create a SqlSessionFactory
47      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/foreach/mybatis-config.xml")) {
48        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
49      }
50  
51      // populate in-memory database
52      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
53          "org/apache/ibatis/submitted/foreach/CreateDB.sql");
54    }
55  
56    @Test
57    void shouldGetAUser() {
58      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
59        Mapper mapper = sqlSession.getMapper(Mapper.class);
60        User testProfile = new User();
61        testProfile.setId(2);
62        User friendProfile = new User();
63        friendProfile.setId(6);
64        List<User> friendList = new ArrayList<>();
65        friendList.add(friendProfile);
66        testProfile.setFriendList(friendList);
67        User user = mapper.getUser(testProfile);
68        Assertions.assertEquals("User6", user.getName());
69      }
70    }
71  
72    @Test
73    void shouldHandleComplexNullItem() {
74      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
75        Mapper mapper = sqlSession.getMapper(Mapper.class);
76        User user1 = new User();
77        user1.setId(2);
78        user1.setName("User2");
79        List<User> users = new ArrayList<>();
80        users.add(user1);
81        users.add(null);
82        int count = mapper.countByUserList(users);
83        Assertions.assertEquals(1, count);
84      }
85    }
86  
87    @Test
88    void shouldHandleMoreComplexNullItem() {
89      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
90        Mapper mapper = sqlSession.getMapper(Mapper.class);
91        User user1 = new User();
92        User bestFriend = new User();
93        bestFriend.setId(5);
94        user1.setBestFriend(bestFriend);
95        List<User> users = new ArrayList<>();
96        users.add(user1);
97        users.add(null);
98        int count = mapper.countByBestFriend(users);
99        Assertions.assertEquals(1, count);
100     }
101   }
102 
103   @Test
104   void nullItemInContext() {
105     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
106       Mapper mapper = sqlSession.getMapper(Mapper.class);
107       User user1 = new User();
108       user1.setId(3);
109       List<User> users = new ArrayList<>();
110       users.add(user1);
111       users.add(null);
112       String name = mapper.selectWithNullItemCheck(users);
113       Assertions.assertEquals("User3", name);
114     }
115   }
116 
117   @Test
118   void shouldReportMissingPropertyName() {
119     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
120       Mapper mapper = sqlSession.getMapper(Mapper.class);
121       when(() -> mapper.typoInItemProperty(Collections.singletonList(new User())));
122       then(caughtException()).isInstanceOf(PersistenceException.class).hasMessageContaining(
123           "There is no getter for property named 'idd' in 'class org.apache.ibatis.submitted.foreach.User'");
124     }
125   }
126 
127   @Test
128   void shouldRemoveItemVariableInTheContext() {
129     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
130       Mapper mapper = sqlSession.getMapper(Mapper.class);
131       int result = mapper.itemVariableConflict(5, Arrays.asList(1, 2), Arrays.asList(3, 4));
132       Assertions.assertEquals(5, result);
133     }
134   }
135 
136   @Test
137   void shouldRemoveIndexVariableInTheContext() {
138     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
139       Mapper mapper = sqlSession.getMapper(Mapper.class);
140       int result = mapper.indexVariableConflict(4, Arrays.asList(6, 7), Arrays.asList(8, 9));
141       Assertions.assertEquals(4, result);
142     }
143   }
144 
145   @Test
146   void shouldAllowNullWhenAttributeIsOmitAndConfigurationIsDefault() throws IOException, SQLException {
147     SqlSessionFactory sqlSessionFactory;
148     try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/foreach/mybatis-config.xml")) {
149       sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
150     }
151     BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
152         "org/apache/ibatis/submitted/foreach/CreateDB.sql");
153 
154     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
155       Mapper mapper = sqlSession.getMapper(Mapper.class);
156       User user = new User();
157       user.setFriendList(null);
158       mapper.countUserWithNullableIsOmit(user);
159       Assertions.fail();
160     } catch (PersistenceException e) {
161       Assertions.assertEquals("The expression 'friendList' evaluated to a null value.", e.getCause().getMessage());
162     }
163   }
164 
165   @Test
166   void shouldAllowNullWhenAttributeIsOmitAndConfigurationIsTrue() {
167     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
168       sqlSessionFactory.getConfiguration().setNullableOnForEach(true);
169       Mapper mapper = sqlSession.getMapper(Mapper.class);
170       User user = new User();
171       user.setFriendList(null);
172       int result = mapper.countUserWithNullableIsOmit(user);
173       Assertions.assertEquals(6, result);
174     }
175   }
176 
177   @Test
178   void shouldNotAllowNullWhenAttributeIsOmitAndConfigurationIsFalse() {
179     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
180       sqlSessionFactory.getConfiguration().setNullableOnForEach(false);
181       Mapper mapper = sqlSession.getMapper(Mapper.class);
182       User user = new User();
183       user.setFriendList(null);
184       mapper.countUserWithNullableIsOmit(user);
185       Assertions.fail();
186     } catch (PersistenceException e) {
187       Assertions.assertEquals("The expression 'friendList' evaluated to a null value.", e.getCause().getMessage());
188     }
189   }
190 
191   @Test
192   void shouldAllowNullWhenAttributeIsTrueAndConfigurationIsFalse() {
193     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
194       sqlSessionFactory.getConfiguration().setNullableOnForEach(false);
195       Mapper mapper = sqlSession.getMapper(Mapper.class);
196       User user = new User();
197       user.setFriendList(null);
198       int result = mapper.countUserWithNullableIsTrue(user);
199       Assertions.assertEquals(6, result);
200     }
201   }
202 
203   @Test
204   void shouldNotAllowNullWhenAttributeIsFalseAndConfigurationIsTrue() {
205     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
206       sqlSessionFactory.getConfiguration().setNullableOnForEach(true);
207       Mapper mapper = sqlSession.getMapper(Mapper.class);
208       User user = new User();
209       user.setFriendList(null);
210       mapper.countUserWithNullableIsFalse(user);
211       Assertions.fail();
212     } catch (PersistenceException e) {
213       Assertions.assertEquals("The expression 'friendList' evaluated to a null value.", e.getCause().getMessage());
214     }
215   }
216 
217 }