From 1f45dd297d320c1ca5d1a2a215c487d9576d7845 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 22 Nov 2014 16:47:19 +0100 Subject: [PATCH] Unit test for JavaBeans introspection against FreeMarker Configuration class Issue: SPR-12448 (cherry picked from commit 282aded) --- .../freemarker/FreeMarkerConfigurerTests.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerConfigurerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerConfigurerTests.java index f926aac046..08176e70f1 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerConfigurerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerConfigurerTests.java @@ -20,9 +20,14 @@ import java.io.IOException; import java.util.HashMap; import java.util.Properties; +import freemarker.template.Configuration; +import freemarker.template.Template; import org.junit.Test; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; @@ -30,9 +35,6 @@ import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean; import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import org.springframework.ui.freemarker.SpringTemplateLoader; -import freemarker.template.Configuration; -import freemarker.template.Template; - import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; @@ -43,7 +45,7 @@ import static org.junit.Assert.*; public class FreeMarkerConfigurerTests { @Test(expected = IOException.class) - public void freemarkerConfigurationFactoryBeanWithConfigLocation() throws Exception { + public void freeMarkerConfigurationFactoryBeanWithConfigLocation() throws Exception { FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean(); fcfb.setConfigLocation(new FileSystemResource("myprops.properties")); Properties props = new Properties(); @@ -63,14 +65,13 @@ public class FreeMarkerConfigurerTests { @Test @SuppressWarnings("rawtypes") - public void freemarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception { + public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception { FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean(); fcfb.setTemplateLoaderPath("file:/mydir"); Properties settings = new Properties(); settings.setProperty("localized_lookup", "false"); fcfb.setFreemarkerSettings(settings); fcfb.setResourceLoader(new ResourceLoader() { - @Override public Resource getResource(String location) { if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) { @@ -78,7 +79,6 @@ public class FreeMarkerConfigurerTests { } return new ByteArrayResource("test".getBytes(), "test"); } - @Override public ClassLoader getClassLoader() { return getClass().getClassLoader(); @@ -91,4 +91,16 @@ public class FreeMarkerConfigurerTests { assertEquals("test", FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap())); } + @Test // SPR-12448 + public void freeMarkerConfigurationAsBean() { + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); + RootBeanDefinition loaderDef = new RootBeanDefinition(SpringTemplateLoader.class); + loaderDef.getConstructorArgumentValues().addGenericArgumentValue(new DefaultResourceLoader()); + loaderDef.getConstructorArgumentValues().addGenericArgumentValue("/freemarker"); + RootBeanDefinition configDef = new RootBeanDefinition(Configuration.class); + configDef.getPropertyValues().add("templateLoader", loaderDef); + beanFactory.registerBeanDefinition("freeMarkerConfig", configDef); + beanFactory.getBean(Configuration.class); + } + }