View Javadoc
1   /*
2    *    Copyright 2015-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.mybatis.scripting.freemarker;
17  
18  import java.io.Reader;
19  import java.sql.Connection;
20  import java.sql.DriverManager;
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  
25  import org.apache.ibatis.io.Resources;
26  import org.apache.ibatis.jdbc.ScriptRunner;
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  /**
35   * Test of using FreeMarker inside XML mapper config.
36   *
37   * @author elwood
38   */
39  class FreeMarkerInXmlTest {
40    private static SqlSessionFactory sqlSessionFactory;
41  
42    @BeforeAll
43    static void setUp() throws Exception {
44      Connection conn = null;
45  
46      try {
47        Class.forName("org.hsqldb.jdbcDriver");
48        conn = DriverManager.getConnection("jdbc:hsqldb:mem:db2", "sa", "");
49  
50        Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql");
51  
52        ScriptRunner runner = new ScriptRunner(conn);
53        runner.setLogWriter(null);
54        runner.setErrorLogWriter(null);
55        runner.runScript(reader);
56        conn.commit();
57        reader.close();
58  
59        reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/mapper-config.xml");
60        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
61        reader.close();
62      } finally {
63        if (conn != null) {
64          conn.close();
65        }
66      }
67    }
68  
69    @Test
70    void testNoParamsCall() {
71      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72        List<Name> allNames = sqlSession.selectList("getAllNames");
73        Assertions.assertEquals(5, allNames.size());
74      }
75    }
76  
77    @Test
78    void testMyBatisParamCall() {
79      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
80        HashMap<String, Object> paramsMap = new HashMap<>();
81        paramsMap.put("name", "Pebbles");
82        Name pebble = sqlSession.selectOne("findName", paramsMap);
83        Assertions.assertTrue(pebble != null && pebble.getFirstName().equals("Pebbles"));
84      }
85    }
86  
87    @Test
88    void testInQuery() {
89      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
90        HashMap<String, Object> paramsMap = new HashMap<>();
91        paramsMap.put("ids", new ArrayList<Integer>() {
92          private static final long serialVersionUID = 1L;
93          {
94            add(1);
95            add(3);
96            add(4);
97          }
98        });
99        List<Name> namesByIds = sqlSession.selectList("findNamesByIds", paramsMap);
100       Assertions.assertEquals(3, namesByIds.size());
101     }
102   }
103 
104   @Test
105   void testParamObject() {
106     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
107       Name name = sqlSession.selectOne("find", new NameParam(4));
108       Assertions.assertTrue(name != null && name.getId() == 4);
109     }
110   }
111 
112   @Test
113   void testStringParam() {
114     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
115       List<Name> stdLangResult = sqlSession.selectList("getNamesOddBehaviourStdLang", "Pebbles");
116       List<Name> freeMarkerLangResult = sqlSession.selectList("getNamesOddBehaviourFreeMarkerLang", "Pebbles");
117       Assertions.assertEquals(1, stdLangResult.size());
118       Assertions.assertEquals("Pebbles", stdLangResult.get(0).getFirstName());
119       Assertions.assertEquals(1, freeMarkerLangResult.size());
120       Assertions.assertEquals("Pebbles", freeMarkerLangResult.get(0).getFirstName());
121     }
122   }
123 }