View Javadoc
1   /*
2    *    Copyright 2009-2021 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.maptypehandler;
17  
18  import java.io.Reader;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.apache.ibatis.BaseDataTest;
23  import org.apache.ibatis.exceptions.PersistenceException;
24  import org.apache.ibatis.io.Resources;
25  import org.apache.ibatis.session.SqlSession;
26  import org.apache.ibatis.session.SqlSessionFactory;
27  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28  import org.junit.jupiter.api.Assertions;
29  import org.junit.jupiter.api.BeforeAll;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * See issue #135
34   *
35   */
36  class MapTypeHandlerTest {
37  
38    private static SqlSessionFactory sqlSessionFactory;
39  
40    @BeforeAll
41    static void setUp() throws Exception {
42      // create an SqlSessionFactory
43      try (Reader reader = Resources
44          .getResourceAsReader("org/apache/ibatis/submitted/maptypehandler/mybatis-config.xml")) {
45        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
46      }
47  
48      // populate in-memory database
49      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
50          "org/apache/ibatis/submitted/maptypehandler/CreateDB.sql");
51    }
52  
53    @Test
54    void shouldGetAUserFromAnnotation() {
55      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
56        Mapper mapper = sqlSession.getMapper(Mapper.class);
57        User user = mapper.getUser(1, "User1");
58        Assertions.assertEquals("User1", user.getName());
59      }
60    }
61  
62    @Test
63    void shouldGetAUserFromXML() {
64      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
65        Mapper mapper = sqlSession.getMapper(Mapper.class);
66        Map<String, Object> params = new HashMap<>();
67        params.put("id", 1);
68        params.put("name", "User1");
69        Assertions.assertThrows(PersistenceException.class, () -> mapper.getUserXML(params));
70      }
71    }
72  
73  }