Skip to content

Commit 282aded

Browse files
committed
Unit test for JavaBeans introspection against FreeMarker Configuration class
Issue: SPR-12448
1 parent 7fcadaa commit 282aded

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerConfigurerTests.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
import freemarker.template.Template;
2525
import org.junit.Test;
2626

27+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
28+
import org.springframework.beans.factory.support.RootBeanDefinition;
29+
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
2730
import org.springframework.core.io.ByteArrayResource;
31+
import org.springframework.core.io.ClassPathResource;
32+
import org.springframework.core.io.DefaultResourceLoader;
2833
import org.springframework.core.io.FileSystemResource;
2934
import org.springframework.core.io.Resource;
3035
import org.springframework.core.io.ResourceLoader;
@@ -42,7 +47,7 @@
4247
public class FreeMarkerConfigurerTests {
4348

4449
@Test(expected = IOException.class)
45-
public void freemarkerConfigurationFactoryBeanWithConfigLocation() throws Exception {
50+
public void freeMarkerConfigurationFactoryBeanWithConfigLocation() throws Exception {
4651
FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
4752
fcfb.setConfigLocation(new FileSystemResource("myprops.properties"));
4853
Properties props = new Properties();
@@ -62,22 +67,20 @@ public void freeMarkerConfigurationFactoryBeanWithResourceLoaderPath() throws Ex
6267

6368
@Test
6469
@SuppressWarnings("rawtypes")
65-
public void freemarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
70+
public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
6671
FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
6772
fcfb.setTemplateLoaderPath("file:/mydir");
6873
Properties settings = new Properties();
6974
settings.setProperty("localized_lookup", "false");
7075
fcfb.setFreemarkerSettings(settings);
7176
fcfb.setResourceLoader(new ResourceLoader() {
72-
7377
@Override
7478
public Resource getResource(String location) {
7579
if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
7680
throw new IllegalArgumentException(location);
7781
}
7882
return new ByteArrayResource("test".getBytes(), "test");
7983
}
80-
8184
@Override
8285
public ClassLoader getClassLoader() {
8386
return getClass().getClassLoader();
@@ -90,4 +93,16 @@ public ClassLoader getClassLoader() {
9093
assertEquals("test", FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap()));
9194
}
9295

96+
@Test // SPR-12448
97+
public void freeMarkerConfigurationAsBean() {
98+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
99+
RootBeanDefinition loaderDef = new RootBeanDefinition(SpringTemplateLoader.class);
100+
loaderDef.getConstructorArgumentValues().addGenericArgumentValue(new DefaultResourceLoader());
101+
loaderDef.getConstructorArgumentValues().addGenericArgumentValue("/freemarker");
102+
RootBeanDefinition configDef = new RootBeanDefinition(Configuration.class);
103+
configDef.getPropertyValues().add("templateLoader", loaderDef);
104+
beanFactory.registerBeanDefinition("freeMarkerConfig", configDef);
105+
beanFactory.getBean(Configuration.class);
106+
}
107+
93108
}

0 commit comments

Comments
 (0)