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