Skip to content

Commit 1f45dd2

Browse files
committed
Unit test for JavaBeans introspection against FreeMarker Configuration class
Issue: SPR-12448 (cherry picked from commit 282aded)
1 parent 21b8ce5 commit 1f45dd2

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@
2020
import java.util.HashMap;
2121
import java.util.Properties;
2222

23+
import freemarker.template.Configuration;
24+
import freemarker.template.Template;
2325
import org.junit.Test;
2426

27+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
28+
import org.springframework.beans.factory.support.RootBeanDefinition;
2529
import org.springframework.core.io.ByteArrayResource;
30+
import org.springframework.core.io.DefaultResourceLoader;
2631
import org.springframework.core.io.FileSystemResource;
2732
import org.springframework.core.io.Resource;
2833
import org.springframework.core.io.ResourceLoader;
2934
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
3035
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
3136
import org.springframework.ui.freemarker.SpringTemplateLoader;
3237

33-
import freemarker.template.Configuration;
34-
import freemarker.template.Template;
35-
3638
import static org.hamcrest.Matchers.*;
3739
import static org.junit.Assert.*;
3840

@@ -43,7 +45,7 @@
4345
public class FreeMarkerConfigurerTests {
4446

4547
@Test(expected = IOException.class)
46-
public void freemarkerConfigurationFactoryBeanWithConfigLocation() throws Exception {
48+
public void freeMarkerConfigurationFactoryBeanWithConfigLocation() throws Exception {
4749
FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
4850
fcfb.setConfigLocation(new FileSystemResource("myprops.properties"));
4951
Properties props = new Properties();
@@ -63,22 +65,20 @@ public void freeMarkerConfigurationFactoryBeanWithResourceLoaderPath() throws Ex
6365

6466
@Test
6567
@SuppressWarnings("rawtypes")
66-
public void freemarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
68+
public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
6769
FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
6870
fcfb.setTemplateLoaderPath("file:/mydir");
6971
Properties settings = new Properties();
7072
settings.setProperty("localized_lookup", "false");
7173
fcfb.setFreemarkerSettings(settings);
7274
fcfb.setResourceLoader(new ResourceLoader() {
73-
7475
@Override
7576
public Resource getResource(String location) {
7677
if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
7778
throw new IllegalArgumentException(location);
7879
}
7980
return new ByteArrayResource("test".getBytes(), "test");
8081
}
81-
8282
@Override
8383
public ClassLoader getClassLoader() {
8484
return getClass().getClassLoader();
@@ -91,4 +91,16 @@ public ClassLoader getClassLoader() {
9191
assertEquals("test", FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap()));
9292
}
9393

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+
94106
}

0 commit comments

Comments
 (0)