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.List;
21  import java.util.Map;
22  
23  import org.apache.ibatis.io.Resources;
24  import org.apache.ibatis.jdbc.ScriptRunner;
25  import org.apache.ibatis.mapping.Environment;
26  import org.apache.ibatis.mapping.SqlSource;
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  import freemarker.template.SimpleScalar;
39  import freemarker.template.Template;
40  import freemarker.template.Version;
41  
42  /**
43   * @author elwood
44   */
45  class CustomizedDataContextTest {
46    private static SqlSessionFactory sqlSessionFactory;
47  
48    @BeforeAll
49    static void setUp() throws Exception {
50      Class.forName("org.hsqldb.jdbcDriver");
51  
52      JDBCDataSource dataSource = new JDBCDataSource();
53      dataSource.setUrl("jdbc:hsqldb:mem:db4");
54      dataSource.setUser("sa");
55      dataSource.setPassword("");
56  
57      try (Connection conn = dataSource.getConnection()) {
58        try (Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql")) {
59          ScriptRunner runner = new ScriptRunner(conn);
60          runner.setLogWriter(null);
61          runner.setErrorLogWriter(null);
62          runner.runScript(reader);
63          conn.commit();
64        }
65      }
66  
67      TransactionFactory transactionFactory = new JdbcTransactionFactory();
68      Environment environment = new Environment("development", transactionFactory, dataSource);
69  
70      // You can call configuration.setDefaultScriptingLanguage(FreeMarkerLanguageDriver.class)
71      // after this to use FreeMarker driver by default.
72      Configuration configuration = new Configuration(environment);
73  
74      configuration.addMapper(CustomizedDataContextMapper.class);
75      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
76    }
77  
78    public static class CustomSqlSource extends FreeMarkerSqlSource {
79      CustomSqlSource(Template template, Configuration configuration, Version version) {
80        super(template, configuration, version);
81      }
82  
83      @Override
84      @SuppressWarnings("unchecked")
85      protected Object preProcessDataContext(Object dataContext, boolean isMap) {
86        dataContext = super.preProcessDataContext(dataContext, isMap);
87        if (isMap) {
88          ((Map<String, Object>) dataContext).put("MY_NAME", new SimpleScalar("Barney"));
89        } else {
90          ((ParamObjectAdapter) dataContext).putAdditionalParam("MY_NAME", new SimpleScalar("Barney"));
91        }
92        return dataContext;
93      }
94    }
95  
96    public static class CustomFreeMarkerLanguageDriver extends FreeMarkerLanguageDriver {
97      @Override
98      protected SqlSource createSqlSource(Template template, Configuration configuration) {
99        return new CustomSqlSource(template, configuration, freemarkerCfg.getIncompatibleImprovements());
100     }
101   }
102 
103   @Test
104   void test() {
105     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
106       CustomizedDataContextMapper mapper = sqlSession.getMapper(CustomizedDataContextMapper.class);
107       List<Name> names = mapper.find();
108       Assertions.assertEquals(1, names.size());
109     }
110   }
111 }