1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
71
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 }