View Javadoc
1   /*
2    * Copyright 2002-2014 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    *      http://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  
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   * @author Juergen Hoeller
43   * @since 14.03.2004
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  // SPR-12448
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 }