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.nested;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import java.io.Reader;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.ibatis.BaseDataTest;
26  import org.apache.ibatis.io.Resources;
27  import org.apache.ibatis.session.SqlSession;
28  import org.apache.ibatis.session.SqlSessionFactory;
29  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30  import org.junit.jupiter.api.BeforeAll;
31  import org.junit.jupiter.api.Test;
32  
33  class NestedForEachTest {
34  
35    protected static SqlSessionFactory sqlSessionFactory;
36  
37    @BeforeAll
38    static void setUp() throws Exception {
39      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/nested/MapperConfig.xml")) {
40        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41      }
42  
43      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
44          "org/apache/ibatis/submitted/nested/CreateDB.sql");
45    }
46  
47    @Test
48    void testSimpleSelect() {
49      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
50        Name name = new Name();
51        name.setLastName("Flintstone");
52        Parameter parameter = new Parameter();
53        parameter.addName(name);
54  
55        List<Map<String, Object>> answer = sqlSession.selectList("org.apache.ibatis.submitted.nested.Mapper.simpleSelect",
56            parameter);
57  
58        assertEquals(3, answer.size());
59      }
60    }
61  
62    @Test
63    void testSimpleSelectWithPrimitives() {
64      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
65        Map<String, Object> parameter = new HashMap<>();
66        int[] array = { 1, 3, 5 };
67        parameter.put("ids", array);
68  
69        List<Map<String, Object>> answer = sqlSession
70            .selectList("org.apache.ibatis.submitted.nested.Mapper.simpleSelectWithPrimitives", parameter);
71  
72        assertEquals(3, answer.size());
73      }
74    }
75  
76    @Test
77    void testSimpleSelectWithMapperAndPrimitives() {
78      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
79        Mapper mapper = sqlSession.getMapper(Mapper.class);
80        List<Map<String, Object>> answer = mapper.simpleSelectWithMapperAndPrimitives(1, 3, 5);
81        assertEquals(3, answer.size());
82      }
83    }
84  
85    @Test
86    void testNestedSelect() {
87      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
88        Name name = new Name();
89        name.setLastName("Flintstone");
90        name.addFirstName("Fred");
91        name.addFirstName("Wilma");
92  
93        Parameter parameter = new Parameter();
94        parameter.addName(name);
95  
96        List<Map<String, Object>> answer = sqlSession.selectList("org.apache.ibatis.submitted.nested.Mapper.nestedSelect",
97            parameter);
98  
99        assertEquals(2, answer.size());
100     }
101   }
102 
103   @Test
104   void testNestedSelect2() {
105     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
106       Name name = new Name();
107       name.setLastName("Flintstone");
108       name.addFirstName("Fred");
109       name.addFirstName("Wilma");
110 
111       Parameter parameter = new Parameter();
112       parameter.addName(name);
113 
114       name = new Name();
115       name.setLastName("Rubble");
116       name.addFirstName("Betty");
117       parameter.addName(name);
118 
119       List<Map<String, Object>> answer = sqlSession.selectList("org.apache.ibatis.submitted.nested.Mapper.nestedSelect",
120           parameter);
121 
122       assertEquals(3, answer.size());
123     }
124   }
125 }