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.valueinmap;
17  
18  import java.io.Reader;
19  import java.sql.Connection;
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.ibatis.exceptions.PersistenceException;
26  import org.apache.ibatis.io.Resources;
27  import org.apache.ibatis.jdbc.ScriptRunner;
28  import org.apache.ibatis.session.SqlSession;
29  import org.apache.ibatis.session.SqlSessionFactory;
30  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31  import org.junit.Assert;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  
35  public class ValueInMapTest {
36  
37    private static SqlSessionFactory sqlSessionFactory;
38  
39    @BeforeClass
40    public static void setUp() throws Exception {
41      // create a SqlSessionFactory
42      Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/valueinmap/mybatis-config.xml");
43      sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
44      reader.close();
45  
46      // populate in-memory database
47      SqlSession session = sqlSessionFactory.openSession();
48      Connection conn = session.getConnection();
49      reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/valueinmap/CreateDB.sql");
50      ScriptRunner runner = new ScriptRunner(conn);
51      runner.setLogWriter(null);
52      runner.runScript(reader);
53      reader.close();
54      session.close();
55    }
56  
57    @Test // issue #165
58    public void shouldWorkWithAPropertyNamedValue() {
59      SqlSession sqlSession = sqlSessionFactory.openSession();
60      try {
61        Map<String, String> map = new HashMap<String, String>();
62        map.put("table", "users");
63        map.put("column", "name");
64        map.put("value", "User1");
65        Integer count = sqlSession.selectOne("count", map);
66        Assert.assertEquals(new Integer(1), count);
67      } finally {
68        sqlSession.close();
69      }
70    }
71  
72    @Test(expected=PersistenceException.class)
73    public void shouldWorkWithAList() {
74      SqlSession sqlSession = sqlSessionFactory.openSession();
75      try {
76        List<String> list = new ArrayList<String>();
77        list.add("users");
78        Integer count = sqlSession.selectOne("count2",list);
79        Assert.assertEquals(new Integer(1), count);
80      } finally {
81        sqlSession.close();
82      }
83    }
84  
85  }