View Javadoc
1   /*
2    *    Copyright 2009-2021 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.collectionparameters;
17  
18  import java.io.Reader;
19  import java.util.ArrayList;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.apache.ibatis.BaseDataTest;
25  import org.apache.ibatis.io.Resources;
26  import org.apache.ibatis.session.SqlSession;
27  import org.apache.ibatis.session.SqlSessionFactory;
28  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29  import org.junit.jupiter.api.Assertions;
30  import org.junit.jupiter.api.BeforeAll;
31  import org.junit.jupiter.api.Test;
32  
33  class CollectionParametersTest {
34  
35    private static SqlSessionFactory sqlSessionFactory;
36  
37    @BeforeAll
38    static void setUp() throws Exception {
39      // create an SqlSessionFactory
40      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/collectionparameters/mybatis-config.xml")) {
41        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42      }
43  
44      // populate in-memory database
45      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
46              "org/apache/ibatis/submitted/collectionparameters/CreateDB.sql");
47    }
48  
49    @Test
50    void shouldGetTwoUsersPassingAList() {
51      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
52        Mapper mapper = sqlSession.getMapper(Mapper.class);
53        ArrayList<Integer> list = new ArrayList<>();
54        list.add(1);
55        list.add(2);
56        List<User> users = mapper.getUsersFromList(list);
57        Assertions.assertEquals(2, users.size());
58      }
59    }
60  
61    @Test
62    void shouldGetTwoUsersPassingAnArray() {
63      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
64        Mapper mapper = sqlSession.getMapper(Mapper.class);
65        Integer[] list = new Integer[2];
66        list[0] = 1;
67        list[1] = 2;
68        List<User> users = mapper.getUsersFromArray(list);
69        Assertions.assertEquals(2, users.size());
70      }
71    }
72  
73    @Test
74    void shouldGetTwoUsersPassingACollection() {
75      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
76        Mapper mapper = sqlSession.getMapper(Mapper.class);
77        Set<Integer> list = new HashSet<>();
78        list.add(1);
79        list.add(2);
80        List<User> users = mapper.getUsersFromCollection(list);
81        Assertions.assertEquals(2, users.size());
82      }
83    }
84  
85  }