View Javadoc
1   /**
2    *    Copyright 2009-2015 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 java.io.Reader;
19  import java.sql.Connection;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.ibatis.io.Resources;
24  import org.apache.ibatis.jdbc.ScriptRunner;
25  import org.apache.ibatis.session.SqlSession;
26  import org.apache.ibatis.session.SqlSessionFactory;
27  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28  import org.junit.Assert;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  
32  public class ForEachTest {
33  
34    private static SqlSessionFactory sqlSessionFactory;
35  
36    @BeforeClass
37    public static void setUp() throws Exception {
38      // create a SqlSessionFactory
39      Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/foreach/mybatis-config.xml");
40      sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41      reader.close();
42  
43      // populate in-memory database
44      SqlSession session = sqlSessionFactory.openSession();
45      Connection conn = session.getConnection();
46      reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/foreach/CreateDB.sql");
47      ScriptRunner runner = new ScriptRunner(conn);
48      runner.setLogWriter(null);
49      runner.runScript(reader);
50      reader.close();
51      session.close();
52    }
53  
54    @Test
55    public void shouldGetAUser() {
56      SqlSession sqlSession = sqlSessionFactory.openSession();
57      try {
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<User>();
64        friendList.add(friendProfile);
65        testProfile.setFriendList(friendList);
66        User user = mapper.getUser(testProfile);
67        Assert.assertEquals("User6", user.getName());
68      } finally {
69        sqlSession.close();
70      }
71    }
72  
73    @Test
74    public void shouldHandleComplexNullItem() {
75      SqlSession sqlSession = sqlSessionFactory.openSession();
76      try {
77        Mapper mapper = sqlSession.getMapper(Mapper.class);
78        User user1 = new User();
79        user1.setId(2);
80        user1.setName("User2");
81        List<User> users = new ArrayList<User>();
82        users.add(user1);
83        users.add(null);
84        int count = mapper.countByUserList(users);
85        Assert.assertEquals(1, count);
86      } finally {
87        sqlSession.close();
88      }
89    }
90  
91    @Test
92    public void shouldHandleMoreComplexNullItem() {
93      SqlSession sqlSession = sqlSessionFactory.openSession();
94      try {
95        Mapper mapper = sqlSession.getMapper(Mapper.class);
96        User user1 = new User();
97        User bestFriend = new User();
98        bestFriend.setId(5);
99        user1.setBestFriend(bestFriend);
100       List<User> users = new ArrayList<User>();
101       users.add(user1);
102       users.add(null);
103       int count = mapper.countByBestFriend(users);
104       Assert.assertEquals(1, count);
105     } finally {
106       sqlSession.close();
107     }
108   }
109 
110   @Test
111   public void nullItemInContext() {
112     SqlSession sqlSession = sqlSessionFactory.openSession();
113     try {
114       Mapper mapper = sqlSession.getMapper(Mapper.class);
115       User user1 = new User();
116       user1.setId(3);
117       List<User> users = new ArrayList<User>();
118       users.add(user1);
119       users.add(null);
120       String name = mapper.selectWithNullItemCheck(users);
121       Assert.assertEquals("User3", name);
122     } finally {
123       sqlSession.close();
124     }
125   }
126 
127 }