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.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
41          .getResourceAsReader("org/apache/ibatis/submitted/collectionparameters/mybatis-config.xml")) {
42        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
43      }
44  
45      // populate in-memory database
46      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
47          "org/apache/ibatis/submitted/collectionparameters/CreateDB.sql");
48    }
49  
50    @Test
51    void shouldGetTwoUsersPassingAList() {
52      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
53        Mapper mapper = sqlSession.getMapper(Mapper.class);
54        ArrayList<Integer> list = new ArrayList<>();
55        list.add(1);
56        list.add(2);
57        List<User> users = mapper.getUsersFromList(list);
58        Assertions.assertEquals(2, users.size());
59      }
60    }
61  
62    @Test
63    void shouldGetTwoUsersPassingAnArray() {
64      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
65        Mapper mapper = sqlSession.getMapper(Mapper.class);
66        Integer[] list = new Integer[2];
67        list[0] = 1;
68        list[1] = 2;
69        List<User> users = mapper.getUsersFromArray(list);
70        Assertions.assertEquals(2, users.size());
71      }
72    }
73  
74    @Test
75    void shouldGetTwoUsersPassingACollection() {
76      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
77        Mapper mapper = sqlSession.getMapper(Mapper.class);
78        Set<Integer> list = new HashSet<>();
79        list.add(1);
80        list.add(2);
81        List<User> users = mapper.getUsersFromCollection(list);
82        Assertions.assertEquals(2, users.size());
83      }
84    }
85  
86  }