1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.web.servlet.view.freemarker;
18
19 import java.io.IOException;
20 import java.util.HashMap;
21 import java.util.Properties;
22
23 import freemarker.template.Configuration;
24 import freemarker.template.Template;
25 import org.junit.Test;
26
27 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
28 import org.springframework.beans.factory.support.RootBeanDefinition;
29 import org.springframework.core.io.ByteArrayResource;
30 import org.springframework.core.io.DefaultResourceLoader;
31 import org.springframework.core.io.FileSystemResource;
32 import org.springframework.core.io.Resource;
33 import org.springframework.core.io.ResourceLoader;
34 import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
35 import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
36 import org.springframework.ui.freemarker.SpringTemplateLoader;
37
38 import static org.hamcrest.Matchers.*;
39 import static org.junit.Assert.*;
40
41
42
43
44
45 public class FreeMarkerConfigurerTests {
46
47 @Test(expected = IOException.class)
48 public void freeMarkerConfigurationFactoryBeanWithConfigLocation() throws Exception {
49 FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
50 fcfb.setConfigLocation(new FileSystemResource("myprops.properties"));
51 Properties props = new Properties();
52 props.setProperty("myprop", "/mydir");
53 fcfb.setFreemarkerSettings(props);
54 fcfb.afterPropertiesSet();
55 }
56
57 @Test
58 public void freeMarkerConfigurationFactoryBeanWithResourceLoaderPath() throws Exception {
59 FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
60 fcfb.setTemplateLoaderPath("file:/mydir");
61 fcfb.afterPropertiesSet();
62 Configuration cfg = fcfb.getObject();
63 assertTrue(cfg.getTemplateLoader() instanceof SpringTemplateLoader);
64 }
65
66 @Test
67 @SuppressWarnings("rawtypes")
68 public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
69 FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
70 fcfb.setTemplateLoaderPath("file:/mydir");
71 Properties settings = new Properties();
72 settings.setProperty("localized_lookup", "false");
73 fcfb.setFreemarkerSettings(settings);
74 fcfb.setResourceLoader(new ResourceLoader() {
75 @Override
76 public Resource getResource(String location) {
77 if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
78 throw new IllegalArgumentException(location);
79 }
80 return new ByteArrayResource("test".getBytes(), "test");
81 }
82 @Override
83 public ClassLoader getClassLoader() {
84 return getClass().getClassLoader();
85 }
86 });
87 fcfb.afterPropertiesSet();
88 assertThat(fcfb.getObject(), instanceOf(Configuration.class));
89 Configuration fc = fcfb.getObject();
90 Template ft = fc.getTemplate("test");
91 assertEquals("test", FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap()));
92 }
93
94 @Test
95 public void freeMarkerConfigurationAsBean() {
96 DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
97 RootBeanDefinition loaderDef = new RootBeanDefinition(SpringTemplateLoader.class);
98 loaderDef.getConstructorArgumentValues().addGenericArgumentValue(new DefaultResourceLoader());
99 loaderDef.getConstructorArgumentValues().addGenericArgumentValue("/freemarker");
100 RootBeanDefinition configDef = new RootBeanDefinition(Configuration.class);
101 configDef.getPropertyValues().add("templateLoader", loaderDef);
102 beanFactory.registerBeanDefinition("freeMarkerConfig", configDef);
103 beanFactory.getBean(Configuration.class);
104 }
105
106 }