|
7 | 7 |
|
8 | 8 | import static java.util.Collections.singletonList; |
9 | 9 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 12 | +import static org.mockito.ArgumentMatchers.any; |
| 13 | +import static org.mockito.ArgumentMatchers.anyString; |
| 14 | +import static org.mockito.ArgumentMatchers.eq; |
| 15 | +import static org.mockito.Mockito.mock; |
| 16 | +import static org.mockito.Mockito.never; |
| 17 | +import static org.mockito.Mockito.verify; |
| 18 | +import static org.mockito.Mockito.when; |
10 | 19 |
|
| 20 | +import java.io.IOException; |
11 | 21 | import java.nio.charset.StandardCharsets; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
12 | 24 | import java.time.DayOfWeek; |
13 | 25 | import java.time.LocalDate; |
14 | 26 | import java.time.ZoneId; |
15 | 27 | import java.time.temporal.TemporalAdjusters; |
16 | | -import java.util.Arrays; |
17 | | -import java.util.Date; |
18 | | -import java.util.List; |
19 | | -import java.util.Locale; |
| 28 | +import java.util.*; |
20 | 29 |
|
| 30 | +import org.junit.jupiter.api.BeforeEach; |
21 | 31 | import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.io.TempDir; |
22 | 33 |
|
| 34 | +import com.typesafe.config.Config; |
23 | 35 | import com.typesafe.config.ConfigFactory; |
| 36 | +import com.typesafe.config.ConfigObject; |
24 | 37 | import com.typesafe.config.ConfigValueFactory; |
| 38 | +import freemarker.cache.ClassTemplateLoader; |
| 39 | +import freemarker.cache.FileTemplateLoader; |
| 40 | +import freemarker.cache.StringTemplateLoader; |
| 41 | +import freemarker.core.XMLOutputFormat; |
25 | 42 | import freemarker.template.Configuration; |
26 | | -import io.jooby.Environment; |
27 | | -import io.jooby.Jooby; |
28 | | -import io.jooby.ModelAndView; |
| 43 | +import io.jooby.*; |
29 | 44 | import io.jooby.test.MockContext; |
30 | 45 |
|
31 | 46 | public class FreemarkerModuleTest { |
@@ -60,6 +75,183 @@ public String getLastname() { |
60 | 75 | } |
61 | 76 | } |
62 | 77 |
|
| 78 | + private Jooby app; |
| 79 | + private Environment env; |
| 80 | + private ServiceRegistry registry; |
| 81 | + private Config config; |
| 82 | + |
| 83 | + @BeforeEach |
| 84 | + void setUp() { |
| 85 | + app = mock(Jooby.class); |
| 86 | + env = mock(Environment.class); |
| 87 | + registry = mock(ServiceRegistry.class); |
| 88 | + config = mock(Config.class); |
| 89 | + |
| 90 | + when(app.getEnvironment()).thenReturn(env); |
| 91 | + when(app.getServices()).thenReturn(registry); |
| 92 | + when(env.getConfig()).thenReturn(config); |
| 93 | + when(config.hasPath("freemarker")).thenReturn(false); |
| 94 | + when(env.isActive("dev", "test")).thenReturn(false); |
| 95 | + when(env.getProperty(eq(TemplateEngine.TEMPLATE_PATH), anyString())) |
| 96 | + .thenAnswer(inv -> inv.getArgument(1)); // Return default path |
| 97 | + when(env.getClassLoader()).thenReturn(getClass().getClassLoader()); |
| 98 | + } |
| 99 | + |
| 100 | + // --- CONSTRUCTOR & INSTALLATION TESTS --- |
| 101 | + |
| 102 | + @Test |
| 103 | + void testInstallWithExistingConfiguration() { |
| 104 | + Configuration customConfig = new Configuration(Configuration.VERSION_2_3_32); |
| 105 | + FreemarkerModule module = new FreemarkerModule(customConfig); |
| 106 | + |
| 107 | + module.install(app); |
| 108 | + |
| 109 | + verify(app).encoder(any()); |
| 110 | + verify(registry).put(Configuration.class, customConfig); |
| 111 | + // Since configuration was explicitly provided, it shouldn't query the environment for a new one |
| 112 | + verify(env, never()).getConfig(); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void testInstallWithDefaultPath() { |
| 117 | + FreemarkerModule module = new FreemarkerModule(); |
| 118 | + |
| 119 | + module.install(app); |
| 120 | + |
| 121 | + verify(app).encoder(any()); |
| 122 | + verify(registry).put(eq(Configuration.class), any(Configuration.class)); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void testInstallWithStringPath() { |
| 127 | + FreemarkerModule module = new FreemarkerModule("custom_views"); |
| 128 | + |
| 129 | + module.install(app); |
| 130 | + |
| 131 | + verify(app).encoder(any()); |
| 132 | + verify(registry).put(eq(Configuration.class), any(Configuration.class)); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void testInstallWithNioPath(@TempDir Path tempDir) { |
| 137 | + FreemarkerModule module = new FreemarkerModule(tempDir); |
| 138 | + |
| 139 | + module.install(app); |
| 140 | + |
| 141 | + verify(app).encoder(any()); |
| 142 | + verify(registry).put(eq(Configuration.class), any(Configuration.class)); |
| 143 | + } |
| 144 | + |
| 145 | + // --- BUILDER TESTS --- |
| 146 | + |
| 147 | + @Test |
| 148 | + void testBuilderWithCustomTemplateLoader() { |
| 149 | + StringTemplateLoader stringLoader = new StringTemplateLoader(); |
| 150 | + Configuration conf = FreemarkerModule.create().setTemplateLoader(stringLoader).build(env); |
| 151 | + |
| 152 | + assertEquals(stringLoader, conf.getTemplateLoader()); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + void testBuilderWithSettings() { |
| 157 | + Configuration conf = |
| 158 | + FreemarkerModule.create().setSetting("tag_syntax", "square_bracket").build(env); |
| 159 | + |
| 160 | + assertEquals(Configuration.SQUARE_BRACKET_TAG_SYNTAX, conf.getTagSyntax()); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + void testBuilderWithOutputFormat() { |
| 165 | + Configuration conf = |
| 166 | + FreemarkerModule.create().setOutputFormat(XMLOutputFormat.INSTANCE).build(env); |
| 167 | + |
| 168 | + assertEquals(XMLOutputFormat.INSTANCE, conf.getOutputFormat()); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + void testBuilderWithConfigMap() { |
| 173 | + when(config.hasPath("freemarker")).thenReturn(true); |
| 174 | + |
| 175 | + Config freemarkerConfig = mock(Config.class); |
| 176 | + ConfigObject root = mock(ConfigObject.class); |
| 177 | + |
| 178 | + when(config.getConfig("freemarker")).thenReturn(freemarkerConfig); |
| 179 | + when(freemarkerConfig.root()).thenReturn(root); |
| 180 | + |
| 181 | + Map<String, Object> settingsMap = new HashMap<>(); |
| 182 | + settingsMap.put("locale", "en_US"); |
| 183 | + settingsMap.put("number_format", "0.00"); |
| 184 | + when(root.unwrapped()).thenReturn(settingsMap); |
| 185 | + |
| 186 | + Configuration conf = FreemarkerModule.create().build(env); |
| 187 | + |
| 188 | + assertEquals(java.util.Locale.US, conf.getLocale()); |
| 189 | + assertEquals("0.00", conf.getNumberFormat()); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + void testBuilderCacheStorageInDevMode() { |
| 194 | + when(env.isActive("dev", "test")).thenReturn(true); |
| 195 | + |
| 196 | + Configuration conf = FreemarkerModule.create().build(env); |
| 197 | + |
| 198 | + assertEquals("freemarker.cache.NullCacheStorage", conf.getCacheStorage().getClass().getName()); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + void testBuilderCacheStorageInProdMode() { |
| 203 | + when(env.isActive("dev", "test")).thenReturn(false); |
| 204 | + |
| 205 | + Configuration conf = FreemarkerModule.create().build(env); |
| 206 | + |
| 207 | + // prod mode defaults to soft cache |
| 208 | + assertEquals("freemarker.cache.MruCacheStorage", conf.getCacheStorage().getClass().getName()); |
| 209 | + } |
| 210 | + |
| 211 | + // --- DEFAULT TEMPLATE LOADER RESOLUTION TESTS --- |
| 212 | + |
| 213 | + @Test |
| 214 | + void testDefaultTemplateLoaderFileSystemFallback(@TempDir Path tempDir) { |
| 215 | + Configuration conf = FreemarkerModule.create().setTemplatesPath(tempDir).build(env); |
| 216 | + |
| 217 | + // Because the temp directory exists on the file system, it should map to FileTemplateLoader |
| 218 | + assertTrue(conf.getTemplateLoader() instanceof FileTemplateLoader); |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + void testDefaultTemplateLoaderClasspathFallback() { |
| 223 | + Configuration conf = |
| 224 | + FreemarkerModule.create() |
| 225 | + .setTemplatesPath("this_path_does_not_exist_on_file_system") |
| 226 | + .build(env); |
| 227 | + |
| 228 | + // Because the path doesn't exist on the file system, it should fallback to ClassTemplateLoader |
| 229 | + assertTrue(conf.getTemplateLoader() instanceof ClassTemplateLoader); |
| 230 | + } |
| 231 | + |
| 232 | + // --- EXCEPTION HANDLING TESTS --- |
| 233 | + |
| 234 | + @Test |
| 235 | + void testBuilderThrowsTemplateExceptionViaSneakyThrows() { |
| 236 | + FreemarkerModule.Builder builder = |
| 237 | + FreemarkerModule.create().setSetting("this_is_an_invalid_freemarker_setting_key", "value"); |
| 238 | + |
| 239 | + // Setting an invalid freemarker config key causes setSettings to throw TemplateException |
| 240 | + Exception ex = assertThrows(Exception.class, () -> builder.build(env)); |
| 241 | + assertTrue(ex instanceof freemarker.core.Configurable.UnknownSettingException); |
| 242 | + } |
| 243 | + |
| 244 | + @Test |
| 245 | + void testDefaultTemplateLoaderThrowsExceptionViaSneakyThrows(@TempDir Path tempDir) |
| 246 | + throws IOException { |
| 247 | + // Create a FILE, not a directory |
| 248 | + Path tempFile = Files.createTempFile(tempDir, "dummy", ".ftl"); |
| 249 | + |
| 250 | + FreemarkerModule.Builder builder = FreemarkerModule.create().setTemplatesPath(tempFile); |
| 251 | + |
| 252 | + assertThrows(IOException.class, () -> builder.build(env)); |
| 253 | + } |
| 254 | + |
63 | 255 | @Test |
64 | 256 | public void render() throws Exception { |
65 | 257 | Configuration freemarker = |
|
0 commit comments