View Javadoc
1   /**
2    *    Copyright 2009-2020 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    *       http://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.*;
19  import static org.assertj.core.api.BDDAssertions.then;
20  
21  import java.io.Reader;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.apache.ibatis.BaseDataTest;
28  import org.apache.ibatis.exceptions.PersistenceException;
29  import org.apache.ibatis.io.Resources;
30  import org.apache.ibatis.session.SqlSession;
31  import org.apache.ibatis.session.SqlSessionFactory;
32  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
33  import org.junit.jupiter.api.Assertions;
34  import org.junit.jupiter.api.BeforeAll;
35  import org.junit.jupiter.api.Test;
36  
37  class ForEachTest {
38  
39    private static SqlSessionFactory sqlSessionFactory;
40  
41    @BeforeAll
42    static void setUp() throws Exception {
43      // create a SqlSessionFactory
44      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/foreach/mybatis-config.xml")) {
45        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
46      }
47  
48      // populate in-memory database
49      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
50              "org/apache/ibatis/submitted/foreach/CreateDB.sql");
51    }
52  
53    @Test
54    void shouldGetAUser() {
55      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
56        Mapper mapper = sqlSession.getMapper(Mapper.class);
57        Userforeach/User.html#User">User testProfile = new User();
58        testProfile.setId(2);
59        Userreach/User.html#User">User friendProfile = new User();
60        friendProfile.setId(6);
61        List<User> friendList = new ArrayList<>();
62        friendList.add(friendProfile);
63        testProfile.setFriendList(friendList);
64        User user = mapper.getUser(testProfile);
65        Assertions.assertEquals("User6", user.getName());
66      }
67    }
68  
69    @Test
70    void shouldHandleComplexNullItem() {
71      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72        Mapper mapper = sqlSession.getMapper(Mapper.class);
73        Useritted/foreach/User.html#User">User user1 = new User();
74        user1.setId(2);
75        user1.setName("User2");
76        List<User> users = new ArrayList<>();
77        users.add(user1);
78        users.add(null);
79        int count = mapper.countByUserList(users);
80        Assertions.assertEquals(1, count);
81      }
82    }
83  
84    @Test
85    void shouldHandleMoreComplexNullItem() {
86      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
87        Mapper mapper = sqlSession.getMapper(Mapper.class);
88        Useritted/foreach/User.html#User">User user1 = new User();
89        User/foreach/User.html#User">User bestFriend = new User();
90        bestFriend.setId(5);
91        user1.setBestFriend(bestFriend);
92        List<User> users = new ArrayList<>();
93        users.add(user1);
94        users.add(null);
95        int count = mapper.countByBestFriend(users);
96        Assertions.assertEquals(1, count);
97      }
98    }
99  
100   @Test
101   void nullItemInContext() {
102     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
103       Mapper mapper = sqlSession.getMapper(Mapper.class);
104       Useritted/foreach/User.html#User">User user1 = new User();
105       user1.setId(3);
106       List<User> users = new ArrayList<>();
107       users.add(user1);
108       users.add(null);
109       String name = mapper.selectWithNullItemCheck(users);
110       Assertions.assertEquals("User3", name);
111     }
112   }
113 
114   @Test
115   void shouldReportMissingPropertyName() {
116     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
117       Mapper mapper = sqlSession.getMapper(Mapper.class);
118       when(() -> mapper.typoInItemProperty(Collections.singletonList(new User())));
119       then(caughtException()).isInstanceOf(PersistenceException.class)
120         .hasMessageContaining("There is no getter for property named 'idd' in 'class org.apache.ibatis.submitted.foreach.User'");
121     }
122   }
123 
124   @Test
125   void shouldRemoveItemVariableInTheContext() {
126     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
127       Mapper mapper = sqlSession.getMapper(Mapper.class);
128       int result = mapper.itemVariableConflict(5, Arrays.asList(1, 2), Arrays.asList(3, 4));
129       Assertions.assertEquals(5, result);
130     }
131   }
132 
133   @Test
134   void shouldRemoveIndexVariableInTheContext() {
135     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
136       Mapper mapper = sqlSession.getMapper(Mapper.class);
137       int result = mapper.indexVariableConflict(4, Arrays.asList(6, 7), Arrays.asList(8, 9));
138       Assertions.assertEquals(4, result);
139     }
140   }
141 
142 }