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.dynsql2;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import java.io.Reader;
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.ibatis.BaseDataTest;
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.BeforeAll;
31  import org.junit.jupiter.api.Test;
32  
33  class DynSqlTest {
34  
35    protected static SqlSessionFactory sqlSessionFactory;
36  
37    @BeforeAll
38    static void setUp() throws Exception {
39      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/dynsql2/MapperConfig.xml")) {
40        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41      }
42  
43      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
44          "org/apache/ibatis/submitted/dynsql2/CreateDB.sql");
45    }
46  
47    @Test
48    void testDynamicSelectWithTypeHandler() {
49      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
50        List<Name> names = new ArrayList<>();
51  
52        Name name = new Name();
53        name.setFirstName("Fred");
54        name.setLastName("Flintstone");
55        names.add(name);
56  
57        name = new Name();
58        name.setFirstName("Barney");
59        name.setLastName("Rubble");
60        names.add(name);
61  
62        Parameter parameter = new Parameter();
63        parameter.setNames(names);
64  
65        List<Map<String, Object>> answer = sqlSession
66            .selectList("org.apache.ibatis.submitted.dynsql2.dynamicSelectWithTypeHandler", parameter);
67  
68        assertEquals(2, answer.size());
69      }
70    }
71  
72    @Test
73    void testSimpleSelect() {
74      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
75        Map<String, Object> answer = sqlSession.selectOne("org.apache.ibatis.submitted.dynsql2.simpleSelect", 1);
76  
77        assertEquals(answer.get("ID"), 1);
78        assertEquals(answer.get("FIRSTNAME"), "Fred");
79        assertEquals(answer.get("LASTNAME"), "Flintstone");
80      }
81    }
82  }