1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.scripting.freemarker.support;
17
18 import java.lang.reflect.Method;
19 import java.util.Arrays;
20
21 import org.junit.jupiter.api.AfterAll;
22 import org.junit.jupiter.api.AfterEach;
23 import org.junit.jupiter.api.Assertions;
24 import org.junit.jupiter.api.BeforeAll;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.mybatis.scripting.freemarker.FreeMarkerLanguageDriverConfig;
28
29 class TemplateFilePathProviderTest {
30
31 @BeforeAll
32 static void setup() {
33 System.setProperty("mybatis-freemarker.config.file", "mybatis-freemarker-empty.properties");
34 }
35
36 @AfterAll
37 static void restore() {
38 System.clearProperty("mybatis-freemarker.config.file");
39 }
40
41 @BeforeEach
42 @AfterEach
43 void clean() {
44 TemplateFilePathProvider.setCustomTemplateFilePathGenerator(null);
45 TemplateFilePathProvider.setLanguageDriverConfig(FreeMarkerLanguageDriverConfig.newInstance());
46 }
47
48 @Test
49 void withoutDatabaseId() {
50 String path = TemplateFilePathProvider.providePath(TestMapper.class, extractMethod(TestMapper.class, "update"),
51 null);
52 Assertions.assertEquals("org/mybatis/scripting/freemarker/support/TestMapper/TestMapper-update.ftl", path);
53 }
54
55 @Test
56 void withDatabaseId() {
57 String path = TemplateFilePathProvider.providePath(TestMapper.class, extractMethod(TestMapper.class, "update"),
58 "h2");
59 Assertions.assertEquals("org/mybatis/scripting/freemarker/support/TestMapper/TestMapper-update-h2.ftl", path);
60 }
61
62 @Test
63 void fallbackWithDefaultDatabase() {
64 String path = TemplateFilePathProvider.providePath(TestMapper.class, extractMethod(TestMapper.class, "delete"),
65 "h2");
66 Assertions.assertEquals("org/mybatis/scripting/freemarker/support/TestMapper/TestMapper-delete.ftl", path);
67 }
68
69 @Test
70 void fallbackDeclaringClassWithoutDatabaseId() {
71 String path = TemplateFilePathProvider.providePath(TestMapper.class, extractMethod(TestMapper.class, "insert"),
72 null);
73 Assertions.assertEquals("org/mybatis/scripting/freemarker/support/BaseMapper/BaseMapper-insert.ftl", path);
74 }
75
76 @Test
77 void fallbackDeclaringClassWithDatabaseId() {
78 String path = TemplateFilePathProvider.providePath(TestMapper.class, extractMethod(TestMapper.class, "insert"),
79 "h2");
80 Assertions.assertEquals("org/mybatis/scripting/freemarker/support/BaseMapper/BaseMapper-insert-h2.ftl", path);
81 }
82
83 @Test
84 void fallbackDeclaringClassAndDefaultDatabase() {
85 String path = TemplateFilePathProvider.providePath(TestMapper.class, extractMethod(TestMapper.class, "count"),
86 "h2");
87 Assertions.assertEquals("org/mybatis/scripting/freemarker/support/BaseMapper/BaseMapper-count.ftl", path);
88 }
89
90 @Test
91 void notFoundSqlFile() {
92 IllegalStateException e = Assertions.assertThrows(IllegalStateException.class, () -> TemplateFilePathProvider
93 .providePath(TestMapper.class, extractMethod(TestMapper.class, "selectOne"), "h2"));
94 Assertions.assertEquals(
95 "The SQL template file not found. mapperType:[interface org.mybatis.scripting.freemarker.support.TestMapper] mapperMethod:[public abstract java.lang.Object org.mybatis.scripting.freemarker.support.BaseMapper.selectOne(int)] databaseId:[h2]",
96 e.getMessage());
97 }
98
99 @Test
100 void notFoundSqlFileWithoutDatabaseId() {
101 IllegalStateException e = Assertions.assertThrows(IllegalStateException.class, () -> TemplateFilePathProvider
102 .providePath(TestMapper.class, extractMethod(TestMapper.class, "selectOne"), null));
103 Assertions.assertEquals(
104 "The SQL template file not found. mapperType:[interface org.mybatis.scripting.freemarker.support.TestMapper] mapperMethod:[public abstract java.lang.Object org.mybatis.scripting.freemarker.support.BaseMapper.selectOne(int)] databaseId:[null]",
105 e.getMessage());
106 }
107
108 @Test
109 void notFoundSqlFileWithoutFallbackDeclaringClass() {
110 IllegalStateException e = Assertions.assertThrows(IllegalStateException.class, () -> TemplateFilePathProvider
111 .providePath(TestMapper.class, extractMethod(TestMapper.class, "selectAllByFirstName"), null));
112 Assertions.assertEquals(
113 "The SQL template file not found. mapperType:[interface org.mybatis.scripting.freemarker.support.TestMapper] mapperMethod:[public abstract java.util.List org.mybatis.scripting.freemarker.support.TestMapper.selectAllByFirstName(java.lang.String)] databaseId:[null]",
114 e.getMessage());
115 }
116
117 @Test
118 void includesPackagePathAndSeparatesDirectoryPerMapperIsFalse() {
119 TemplateFilePathProvider.setLanguageDriverConfig(FreeMarkerLanguageDriverConfig.newInstance(c -> {
120 c.getTemplateFile().setBaseDir("org/mybatis/scripting/freemarker/support/sql");
121 c.getTemplateFile().getPathProvider().setIncludesPackagePath(false);
122 c.getTemplateFile().getPathProvider().setSeparateDirectoryPerMapper(false);
123 }));
124 String path = TemplateFilePathProvider.providePath(TestMapper.class,
125 extractMethod(TestMapper.class, "selectAllDesc"), null);
126 Assertions.assertEquals("TestMapper-selectAllDesc.ftl", path);
127 }
128
129 @Test
130 void baseDirEndWithSlash() {
131 TemplateFilePathProvider.setLanguageDriverConfig(FreeMarkerLanguageDriverConfig.newInstance(c -> {
132 c.getTemplateFile().setBaseDir("org/mybatis/scripting/freemarker/support/sql/");
133 c.getTemplateFile().getPathProvider().setIncludesPackagePath(false);
134 c.getTemplateFile().getPathProvider().setSeparateDirectoryPerMapper(false);
135 }));
136 String path = TemplateFilePathProvider.providePath(TestMapper.class,
137 extractMethod(TestMapper.class, "selectAllDesc"), null);
138 Assertions.assertEquals("TestMapper-selectAllDesc.ftl", path);
139 }
140
141 @Test
142 void includesMapperNameWhenSeparateDirectoryIsFalse() {
143 TemplateFilePathProvider.setLanguageDriverConfig(FreeMarkerLanguageDriverConfig
144 .newInstance(c -> c.getTemplateFile().getPathProvider().setIncludesMapperNameWhenSeparateDirectory(false)));
145 String path = TemplateFilePathProvider.providePath(TestMapper.class,
146 extractMethod(TestMapper.class, "selectAllAsc"), null);
147 Assertions.assertEquals("org/mybatis/scripting/freemarker/support/TestMapper/selectAllAsc.ftl", path);
148 }
149
150 @Test
151 void prefix() {
152 TemplateFilePathProvider.setLanguageDriverConfig(FreeMarkerLanguageDriverConfig.newInstance(c -> {
153 c.getTemplateFile().getPathProvider().setPrefix("org/mybatis/scripting/freemarker/support/sql/");
154 c.getTemplateFile().getPathProvider().setIncludesPackagePath(false);
155 c.getTemplateFile().getPathProvider().setSeparateDirectoryPerMapper(false);
156 }));
157 String path = TemplateFilePathProvider.providePath(TestMapper.class,
158 extractMethod(TestMapper.class, "selectAllDesc"), null);
159 Assertions.assertEquals("org/mybatis/scripting/freemarker/support/sql/TestMapper-selectAllDesc.ftl", path);
160 }
161
162 @Test
163 void defaultPackageMapper() throws ClassNotFoundException {
164 TemplateFilePathProvider.setLanguageDriverConfig(FreeMarkerLanguageDriverConfig
165 .newInstance(c -> c.getTemplateFile().setBaseDir("org/mybatis/scripting/freemarker/support/")));
166 Class<?> mapperType = Class.forName("DefaultPackageNameMapper");
167 String path = TemplateFilePathProvider.providePath(mapperType, extractMethod(mapperType, "selectAllDesc"), null);
168 Assertions.assertEquals("DefaultPackageNameMapper/DefaultPackageNameMapper-selectAllDesc.ftl", path);
169 }
170
171 @Test
172 void defaultPackageMapperWithIncludesPackagePathIsFalse() throws ClassNotFoundException {
173 TemplateFilePathProvider.setLanguageDriverConfig(FreeMarkerLanguageDriverConfig.newInstance(c -> {
174 c.getTemplateFile().setBaseDir("org/mybatis/scripting/freemarker/support/");
175 c.getTemplateFile().getPathProvider().setIncludesPackagePath(false);
176 }));
177 Class<?> mapperType = Class.forName("DefaultPackageNameMapper");
178 String path = TemplateFilePathProvider.providePath(mapperType, extractMethod(mapperType, "selectAllDesc"), null);
179 Assertions.assertEquals("DefaultPackageNameMapper/DefaultPackageNameMapper-selectAllDesc.ftl", path);
180 }
181
182 @Test
183 void customTemplateFileGenerator() {
184 TemplateFilePathProvider.setCustomTemplateFilePathGenerator(
185 (type, method, databaseId) -> type.getName().replace('.', '/') + "_" + method.getName() + ".ftl");
186 String path = TemplateFilePathProvider.providePath(TestMapper.class, extractMethod(TestMapper.class, "selectOne"),
187 null);
188 Assertions.assertEquals("org/mybatis/scripting/freemarker/support/BaseMapper_selectOne.ftl", path);
189
190 }
191
192 private Method extractMethod(Class<?> type, String methodName) {
193 return Arrays.stream(type.getMethods()).filter(m -> m.getName().equals(methodName)).findFirst().orElseThrow(
194 () -> new IllegalArgumentException("The method not found. type:" + type + " methodName:" + methodName));
195 }
196
197 }