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