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  
21  import org.apache.ibatis.io.Resources;
22  import org.apache.ibatis.jdbc.ScriptRunner;
23  import org.apache.ibatis.mapping.Environment;
24  import org.apache.ibatis.session.Configuration;
25  import org.apache.ibatis.session.SqlSession;
26  import org.apache.ibatis.session.SqlSessionFactory;
27  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28  import org.apache.ibatis.transaction.TransactionFactory;
29  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
30  import org.hsqldb.jdbc.JDBCDataSource;
31  import org.junit.jupiter.api.AfterAll;
32  import org.junit.jupiter.api.Assertions;
33  import org.junit.jupiter.api.BeforeAll;
34  import org.junit.jupiter.api.Test;
35  import org.mybatis.scripting.freemarker.support.TemplateFilePathProvider;
36  
37  class TemplateFilePathProviderMapperTest {
38    private static SqlSessionFactory sqlSessionFactory;
39    private static FreeMarkerLanguageDriverConfig driverConfig;
40  
41    @BeforeAll
42    @AfterAll
43    static void cleanup() {
44      TemplateFilePathProvider.clearCache();
45    }
46  
47    @BeforeAll
48    static void setUp() throws Exception {
49      Class.forName("org.hsqldb.jdbcDriver");
50      JDBCDataSource dataSource = new JDBCDataSource();
51      dataSource.setUrl("jdbc:hsqldb:mem:template-file-path-provider");
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      Configuration configuration = new Configuration(environment);
69      configuration.setMapUnderscoreToCamelCase(true);
70      driverConfig = FreeMarkerLanguageDriverConfig.newInstance(c -> {
71        c.getTemplateFile().getPathProvider().setIncludesPackagePath(false);
72        c.getTemplateFile().getPathProvider().setSeparateDirectoryPerMapper(false);
73        c.getFreemarkerSettings().put("interpolation_syntax", "dollar");
74      });
75      configuration.getLanguageRegistry().register(new FreeMarkerLanguageDriver(driverConfig));
76      configuration.setDefaultScriptingLanguage(FreeMarkerLanguageDriver.class);
77  
78      configuration.addMapper(TemplateFilePathProviderMapper.class);
79      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
80    }
81  
82    @Test
83    void testInsert() {
84      driverConfig.getTemplateFile().getPathProvider().setCacheEnabled(true);
85      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
86        TemplateFilePathProviderMapper mapper = sqlSession.getMapper(TemplateFilePathProviderMapper.class);
87        Name name = new Name();
88        name.setFirstName("Thymeleaf");
89        name.setLastName("MyBatis");
90        mapper.insert(name);
91  
92        Name loadedName = mapper.findById(name.getId());
93        Assertions.assertEquals(name.getFirstName(), loadedName.getFirstName());
94        Assertions.assertEquals(name.getLastName(), loadedName.getLastName());
95      }
96    }
97  
98    @Test
99    void testUpdate() {
100     driverConfig.getTemplateFile().getPathProvider().setCacheEnabled(false);
101     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
102       TemplateFilePathProviderMapper mapper = sqlSession.getMapper(TemplateFilePathProviderMapper.class);
103       Name name = new Name();
104       name.setFirstName("Thymeleaf");
105       name.setLastName("MyBatis");
106       mapper.insert(name);
107 
108       Name updatingName = new Name();
109       updatingName.setId(name.getId());
110       updatingName.setFirstName("Thymeleaf3");
111       mapper.update(updatingName);
112 
113       Name loadedName = mapper.findById(name.getId());
114       Assertions.assertEquals(updatingName.getFirstName(), loadedName.getFirstName());
115       Assertions.assertEquals(name.getLastName(), loadedName.getLastName());
116     }
117   }
118 
119   @Test
120   void testDelete() {
121     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
122       TemplateFilePathProviderMapper mapper = sqlSession.getMapper(TemplateFilePathProviderMapper.class);
123       Name name = new Name();
124       name.setFirstName("Thymeleaf");
125       name.setLastName("MyBatis");
126       mapper.insert(name);
127 
128       mapper.delete(name);
129 
130       Name loadedName = mapper.findById(name.getId());
131       Assertions.assertNull(loadedName);
132     }
133   }
134 
135 }