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.util.ArrayList;
21  import java.util.List;
22  
23  import org.apache.ibatis.exceptions.PersistenceException;
24  import org.apache.ibatis.io.Resources;
25  import org.apache.ibatis.jdbc.ScriptRunner;
26  import org.apache.ibatis.mapping.Environment;
27  import org.apache.ibatis.session.Configuration;
28  import org.apache.ibatis.session.SqlSession;
29  import org.apache.ibatis.session.SqlSessionFactory;
30  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31  import org.apache.ibatis.transaction.TransactionFactory;
32  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
33  import org.hsqldb.jdbc.JDBCDataSource;
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.BeforeAll;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Test of using FreeMarker to generate prepared statements parameters.
40   *
41   * @author elwood
42   */
43  class PreparedParamsTest {
44    private static SqlSessionFactory sqlSessionFactory;
45  
46    @BeforeAll
47    static void setUp() throws Exception {
48      Class.forName("org.hsqldb.jdbcDriver");
49  
50      JDBCDataSource dataSource = new JDBCDataSource();
51      dataSource.setUrl("jdbc:hsqldb:mem:db3");
52      dataSource.setUser("sa");
53      dataSource.setPassword("");
54  
55      try (Connection conn = dataSource.getConnection()) {
56        try (Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql")) {
57          ScriptRunner runner = new ScriptRunner(conn);
58          runner.setLogWriter(null);
59          runner.setErrorLogWriter(null);
60          runner.runScript(reader);
61          conn.commit();
62        }
63      }
64  
65      TransactionFactory transactionFactory = new JdbcTransactionFactory();
66      Environment environment = new Environment("development", transactionFactory, dataSource);
67  
68      // You can call configuration.setDefaultScriptingLanguage(FreeMarkerLanguageDriver.class)
69      // after this to use FreeMarker driver by default.
70      Configuration configuration = new Configuration(environment);
71  
72      configuration.addMapper(PreparedParamsMapper.class);
73      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
74    }
75  
76    @Test
77    void testInCall() {
78      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
79        PreparedParamsMapper mapper = sqlSession.getMapper(PreparedParamsMapper.class);
80        List<Name> names = mapper.findByNames(new ArrayList<String>() {
81          private static final long serialVersionUID = 1L;
82          {
83            add("Pebbles");
84            add("Barney");
85            add("Betty");
86          }
87        });
88        Assertions.assertEquals(3, names.size());
89      }
90    }
91  
92    /**
93     * PersistenceException will be thrown with cause of UnsupportedOperationException
94     */
95    @Test
96    void testParamsObjectCall() {
97      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
98        final PreparedParamsMapper mapper = sqlSession.getMapper(PreparedParamsMapper.class);
99        Assertions.assertThrows(PersistenceException.class, () -> mapper.findUsingParamsObject(new PreparedParam()));
100     }
101   }
102 
103   @Test
104   void testNoParamsCall() {
105     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
106       PreparedParamsMapper mapper = sqlSession.getMapper(PreparedParamsMapper.class);
107       Name name = mapper.findUsingParams(new PreparedParam.InnerClass());
108       Assertions.assertTrue(name != null && name.getFirstName().equals("Wilma"));
109     }
110   }
111 }