24
24
import freemarker .template .Template ;
25
25
import org .junit .Test ;
26
26
27
+ import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
28
+ import org .springframework .beans .factory .support .RootBeanDefinition ;
29
+ import org .springframework .beans .factory .xml .XmlBeanDefinitionReader ;
27
30
import org .springframework .core .io .ByteArrayResource ;
31
+ import org .springframework .core .io .ClassPathResource ;
32
+ import org .springframework .core .io .DefaultResourceLoader ;
28
33
import org .springframework .core .io .FileSystemResource ;
29
34
import org .springframework .core .io .Resource ;
30
35
import org .springframework .core .io .ResourceLoader ;
42
47
public class FreeMarkerConfigurerTests {
43
48
44
49
@ Test (expected = IOException .class )
45
- public void freemarkerConfigurationFactoryBeanWithConfigLocation () throws Exception {
50
+ public void freeMarkerConfigurationFactoryBeanWithConfigLocation () throws Exception {
46
51
FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean ();
47
52
fcfb .setConfigLocation (new FileSystemResource ("myprops.properties" ));
48
53
Properties props = new Properties ();
@@ -62,22 +67,20 @@ public void freeMarkerConfigurationFactoryBeanWithResourceLoaderPath() throws Ex
62
67
63
68
@ Test
64
69
@ SuppressWarnings ("rawtypes" )
65
- public void freemarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath () throws Exception {
70
+ public void freeMarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath () throws Exception {
66
71
FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean ();
67
72
fcfb .setTemplateLoaderPath ("file:/mydir" );
68
73
Properties settings = new Properties ();
69
74
settings .setProperty ("localized_lookup" , "false" );
70
75
fcfb .setFreemarkerSettings (settings );
71
76
fcfb .setResourceLoader (new ResourceLoader () {
72
-
73
77
@ Override
74
78
public Resource getResource (String location ) {
75
79
if (!("file:/mydir" .equals (location ) || "file:/mydir/test" .equals (location ))) {
76
80
throw new IllegalArgumentException (location );
77
81
}
78
82
return new ByteArrayResource ("test" .getBytes (), "test" );
79
83
}
80
-
81
84
@ Override
82
85
public ClassLoader getClassLoader () {
83
86
return getClass ().getClassLoader ();
@@ -90,4 +93,16 @@ public ClassLoader getClassLoader() {
90
93
assertEquals ("test" , FreeMarkerTemplateUtils .processTemplateIntoString (ft , new HashMap ()));
91
94
}
92
95
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
+
93
108
}
0 commit comments