View Javadoc
1   /*
2    *    Copyright 2015-2024 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.Optional;
21  
22  import org.apache.ibatis.io.Resources;
23  import org.apache.ibatis.jdbc.ScriptRunner;
24  import org.apache.ibatis.mapping.Environment;
25  import org.apache.ibatis.session.Configuration;
26  import org.apache.ibatis.session.SqlSession;
27  import org.apache.ibatis.session.SqlSessionFactory;
28  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29  import org.apache.ibatis.transaction.TransactionFactory;
30  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
31  import org.hsqldb.jdbc.JDBCDataSource;
32  import org.junit.jupiter.api.Assertions;
33  import org.junit.jupiter.api.BeforeAll;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Test of using FreeMarker to generate prepared statements parameters.
38   *
39   * @author s-nakao
40   */
41  class PreparedDatabaseIdParamsTest {
42    private static SqlSessionFactory sqlSessionFactory;
43  
44    @BeforeAll
45    static void setUp() throws Exception {
46      Class.forName("org.hsqldb.jdbcDriver");
47  
48      JDBCDataSource dataSource = new JDBCDataSource();
49      dataSource.setUrl("jdbc:hsqldb:mem:db5");
50      dataSource.setUser("sa");
51      dataSource.setPassword("");
52  
53      try (Connection conn = dataSource.getConnection()) {
54        try (Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql")) {
55          ScriptRunner runner = new ScriptRunner(conn);
56          runner.setLogWriter(null);
57          runner.setErrorLogWriter(null);
58          runner.runScript(reader);
59          conn.commit();
60        }
61      }
62  
63      TransactionFactory transactionFactory = new JdbcTransactionFactory();
64      Environment environment = new Environment("development", transactionFactory, dataSource);
65  
66      // You can call configuration.setDefaultScriptingLanguage(FreeMarkerLanguageDriver.class)
67      // after this to use FreeMarker driver by default.
68      Configuration configuration = new Configuration(environment);
69  
70      // set databaseId. default null
71      // If it is a property, please refer to the following
72      // https://mybatis.org/mybatis-3/ja/configuration.html#databaseIdProvider.
73      configuration.setDatabaseId("hsqldb");
74  
75      configuration.addMapper(PreparedDatabaseIdParamsMapper.class);
76      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
77    }
78  
79    @Test
80    void testReferDatabaseIdInTemplate() throws Exception {
81      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
82        PreparedDatabaseIdParamsMapper mapper = sqlSession.getMapper(PreparedDatabaseIdParamsMapper.class);
83        Optional<Name> nameList = mapper.getDatabaseIdTest();
84        Assertions.assertTrue(nameList.isPresent());
85        Assertions.assertEquals("Fred", nameList.orElseThrow(() -> new Exception()).getFirstName());
86        Assertions.assertEquals("Flintstone", nameList.orElseThrow(() -> new Exception()).getLastName());
87      }
88    }
89  
90    @Test
91    void testReferDatabaseIdInTemplateWithParam() throws Exception {
92      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
93        PreparedDatabaseIdParamsMapper mapper = sqlSession.getMapper(PreparedDatabaseIdParamsMapper.class);
94        Optional<Name> nameList = mapper.getDatabaseIdTestWithParam(new PreparedParam());
95        Assertions.assertTrue(nameList.isPresent());
96        Assertions.assertEquals("Fred", nameList.orElseThrow(() -> new Exception()).getFirstName());
97        Assertions.assertEquals("Flintstone", nameList.orElseThrow(() -> new Exception()).getLastName());
98      }
99    }
100 
101 }