View Javadoc
1   /*
2    *    Copyright 2009-2022 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.valueinmap;
17  
18  import java.io.Reader;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.ibatis.BaseDataTest;
25  import org.apache.ibatis.exceptions.PersistenceException;
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.Assertions;
31  import org.junit.jupiter.api.BeforeAll;
32  import org.junit.jupiter.api.Test;
33  
34  class ValueInMapTest {
35  
36    private static SqlSessionFactory sqlSessionFactory;
37  
38    @BeforeAll
39    static void setUp() throws Exception {
40      // create a SqlSessionFactory
41      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/valueinmap/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/valueinmap/CreateDB.sql");
48    }
49  
50    @Test // issue #165
51    void shouldWorkWithAPropertyNamedValue() {
52      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
53        Map<String, String> map = new HashMap<>();
54        map.put("table", "users");
55        map.put("column", "name");
56        map.put("value", "User1");
57        Integer count = sqlSession.selectOne("count", map);
58        Assertions.assertEquals(Integer.valueOf(1), count);
59      }
60    }
61  
62    @Test
63    void shouldWorkWithAList() {
64      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
65        List<String> list = new ArrayList<>();
66        list.add("users");
67        Assertions.assertThrows(PersistenceException.class, () -> sqlSession.selectOne("count2", list));
68      }
69    }
70  
71  }