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.util.Properties;
19  
20  import org.junit.jupiter.api.AfterEach;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  
25  class FreeMarkerLanguageDriverConfigTest {
26  
27    private String currentConfigFile;
28    private String currentConfigEncoding;
29  
30    @BeforeEach
31    void saveCurrentConfig() {
32      currentConfigFile = System.getProperty("mybatis-freemarker.config");
33      currentConfigEncoding = System.getProperty("mybatis-freemarker.config.encoding");
34    }
35  
36    @AfterEach
37    void restoreConfig() {
38      if (currentConfigFile == null) {
39        System.clearProperty("mybatis-freemarker.config.file");
40      } else {
41        System.setProperty("mybatis-freemarker.config.file", currentConfigFile);
42      }
43      if (currentConfigEncoding == null) {
44        System.clearProperty("mybatis-freemarker.config.encoding");
45      } else {
46        System.setProperty("mybatis-freemarker.config.encoding", currentConfigEncoding);
47      }
48    }
49  
50    @Test
51    void newInstanceWithEmptyPropertiesFile() {
52      System.setProperty("mybatis-freemarker.config.file", "mybatis-freemarker-empty.properties");
53      FreeMarkerLanguageDriverConfig config = FreeMarkerLanguageDriverConfig.newInstance();
54      @SuppressWarnings("deprecation")
55      String basePackage = config.getBasePackage();
56      Assertions.assertEquals("", basePackage);
57      Assertions.assertEquals("", config.getTemplateFile().getBaseDir());
58      Assertions.assertEquals("", config.getTemplateFile().getPathProvider().getPrefix());
59      Assertions.assertTrue(config.getTemplateFile().getPathProvider().isIncludesPackagePath());
60      Assertions.assertTrue(config.getTemplateFile().getPathProvider().isSeparateDirectoryPerMapper());
61      Assertions.assertTrue(config.getTemplateFile().getPathProvider().isIncludesMapperNameWhenSeparateDirectory());
62      Assertions.assertTrue(config.getTemplateFile().getPathProvider().isCacheEnabled());
63    }
64  
65    @Test
66    void newInstanceWithPropertiesFileNotFound() {
67      System.setProperty("mybatis-freemarker.config.file", "mybatis-freemarker-notfound.properties");
68      FreeMarkerLanguageDriverConfig config = FreeMarkerLanguageDriverConfig.newInstance();
69      @SuppressWarnings("deprecation")
70      String basePackage = config.getBasePackage();
71      Assertions.assertEquals("", basePackage);
72      Assertions.assertEquals("", config.getTemplateFile().getBaseDir());
73      Assertions.assertEquals("", config.getTemplateFile().getPathProvider().getPrefix());
74      Assertions.assertTrue(config.getTemplateFile().getPathProvider().isIncludesPackagePath());
75      Assertions.assertTrue(config.getTemplateFile().getPathProvider().isSeparateDirectoryPerMapper());
76      Assertions.assertTrue(config.getTemplateFile().getPathProvider().isIncludesMapperNameWhenSeparateDirectory());
77      Assertions.assertTrue(config.getTemplateFile().getPathProvider().isCacheEnabled());
78    }
79  
80    @Test
81    void newInstanceWithCustomPropertiesFile() {
82      System.setProperty("mybatis-freemarker.config.file", "mybatis-freemarker-custom.properties");
83      FreeMarkerLanguageDriverConfig config = FreeMarkerLanguageDriverConfig.newInstance();
84      @SuppressWarnings("deprecation")
85      String basePackage = config.getBasePackage();
86      Assertions.assertEquals("sqls", basePackage);
87      Assertions.assertEquals("sqls", config.getTemplateFile().getBaseDir());
88      Assertions.assertEquals("mappers", config.getTemplateFile().getPathProvider().getPrefix());
89      Assertions.assertFalse(config.getTemplateFile().getPathProvider().isIncludesPackagePath());
90      Assertions.assertFalse(config.getTemplateFile().getPathProvider().isSeparateDirectoryPerMapper());
91      Assertions.assertFalse(config.getTemplateFile().getPathProvider().isIncludesMapperNameWhenSeparateDirectory());
92      Assertions.assertFalse(config.getTemplateFile().getPathProvider().isCacheEnabled());
93      Assertions.assertEquals(2, config.getFreemarkerSettings().size());
94      Assertions.assertEquals("dollar", config.getFreemarkerSettings().get("interpolation_syntax"));
95      Assertions.assertEquals("yes", config.getFreemarkerSettings().get("whitespace_stripping"));
96    }
97  
98    @Test
99    void newInstanceWithCustomProperties() {
100     Properties properties = new Properties();
101     properties.setProperty("templateFile.pathProvider.prefix", "mapper");
102     properties.setProperty("templateFile.pathProvider.includesPackagePath", "false");
103     properties.setProperty("templateFile.pathProvider.separateDirectoryPerMapper", "false");
104     properties.setProperty("templateFile.pathProvider.includesMapperNameWhenSeparateDirectory", "false");
105     properties.setProperty("templateFile.pathProvider.cacheEnabled", "false");
106     properties.setProperty("freemarkerSettings.interpolation_syntax", "dollar");
107     properties.setProperty("freemarkerSettings.whitespace_stripping", "yes");
108 
109     FreeMarkerLanguageDriverConfig config = FreeMarkerLanguageDriverConfig.newInstance(properties);
110     @SuppressWarnings("deprecation")
111     String basePackage = config.getBasePackage();
112     Assertions.assertEquals("sql", basePackage);
113     Assertions.assertEquals("sql", config.getTemplateFile().getBaseDir());
114     Assertions.assertEquals("mapper", config.getTemplateFile().getPathProvider().getPrefix());
115     Assertions.assertFalse(config.getTemplateFile().getPathProvider().isIncludesPackagePath());
116     Assertions.assertFalse(config.getTemplateFile().getPathProvider().isSeparateDirectoryPerMapper());
117     Assertions.assertFalse(config.getTemplateFile().getPathProvider().isIncludesMapperNameWhenSeparateDirectory());
118     Assertions.assertFalse(config.getTemplateFile().getPathProvider().isCacheEnabled());
119     Assertions.assertEquals("dollar", config.getFreemarkerSettings().get("interpolation_syntax"));
120     Assertions.assertEquals("yes", config.getFreemarkerSettings().get("whitespace_stripping"));
121   }
122 
123   @Test
124   @SuppressWarnings("deprecation")
125   void newInstanceWithConsumer() {
126     FreeMarkerLanguageDriverConfig config = FreeMarkerLanguageDriverConfig.newInstance(c -> {
127       c.setBasePackage("sqls");
128     });
129     Assertions.assertEquals("sql", config.getBasePackage());
130     Assertions.assertEquals("sql", config.getTemplateFile().getBaseDir());
131   }
132 
133 }