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.foreach_map;
17  
18  import java.io.Reader;
19  import java.util.List;
20  
21  import org.apache.ibatis.BaseDataTest;
22  import org.apache.ibatis.io.Resources;
23  import org.apache.ibatis.session.SqlSession;
24  import org.apache.ibatis.session.SqlSessionFactory;
25  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
26  import org.junit.jupiter.api.AfterEach;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.BeforeAll;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  class ForEachMapTest {
33  
34    private static SqlSessionFactory sqlSessionFactory;
35  
36    @BeforeAll
37    static void setUpClass() throws Exception {
38      // create a SqlSessionFactory
39      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/foreach_map/mybatis-config.xml")) {
40        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41      }
42  
43      // populate in-memory database
44      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
45          "org/apache/ibatis/submitted/foreach_map/CreateDB.sql");
46    }
47  
48    @BeforeEach
49    void setUp() {
50      sqlSession = sqlSessionFactory.openSession();
51    }
52  
53    @AfterEach
54    void tearDown() {
55      sqlSession.close();
56    }
57  
58    @Test
59    void shouldGetStringKeyStringValueEntries() {
60      MapParam mapParam = new MapParam();
61      mapParam.getMap().put("key 1", "value 1");
62      mapParam.getMap().put("key 2", "value 2");
63      sqlSession.insert("ins_string_string", mapParam);
64  
65      List<StringStringMapEntry> entries = sqlSession.selectList("sel_string_string", new MapParam());
66      Assertions.assertEquals(new StringStringMapEntry("key 1", "value 1"), entries.get(0));
67      Assertions.assertEquals(new StringStringMapEntry("key 2", "value 2"), entries.get(1));
68    }
69  
70    @Test
71    void shouldGetIntKeyBoolValueEntries() {
72      MapParam mapParam = new MapParam();
73      mapParam.getMap().put(12345, true);
74      mapParam.getMap().put(54321, false);
75      sqlSession.insert("ins_int_bool", mapParam);
76  
77      List<IntBoolMapEntry> entries = sqlSession.selectList("sel_int_bool");
78      Assertions.assertEquals(new IntBoolMapEntry(12345, true), entries.get(0));
79      Assertions.assertEquals(new IntBoolMapEntry(54321, false), entries.get(1));
80    }
81  
82    @Test
83    void shouldGetNestedBeanKeyValueEntries() {
84      MapParam mapParam = new MapParam();
85      mapParam.getMap().put(new NestedBean(12345, true), new NestedBean(54321, false));
86      mapParam.getMap().put(new NestedBean(67890, true), new NestedBean(9876, false));
87      sqlSession.insert("ins_nested_bean", mapParam);
88  
89      List<NestedBeanMapEntry> entries = sqlSession.selectList("sel_nested_bean");
90      Assertions.assertEquals(new NestedBeanMapEntry(12345, true, 54321, false), entries.get(0));
91      Assertions.assertEquals(new NestedBeanMapEntry(67890, true, 9876, false), entries.get(1));
92    }
93  
94    @Test
95    void shouldSubstituteIndexWithKey() {
96      MapParam mapParam = new MapParam();
97      mapParam.getMap().put("col_a", 22);
98      mapParam.getMap().put("col_b", 222);
99      Integer count = sqlSession.selectOne("sel_key_cols", mapParam);
100     Assertions.assertEquals(Integer.valueOf(1), count);
101   }
102 
103   private SqlSession sqlSession;
104 }