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