2020import java .util .HashMap ;
2121import java .util .Properties ;
2222
23+ import freemarker .template .Configuration ;
24+ import freemarker .template .Template ;
2325import org .junit .Test ;
2426
27+ import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
28+ import org .springframework .beans .factory .support .RootBeanDefinition ;
2529import org .springframework .core .io .ByteArrayResource ;
30+ import org .springframework .core .io .DefaultResourceLoader ;
2631import org .springframework .core .io .FileSystemResource ;
2732import org .springframework .core .io .Resource ;
2833import org .springframework .core .io .ResourceLoader ;
2934import org .springframework .ui .freemarker .FreeMarkerConfigurationFactoryBean ;
3035import org .springframework .ui .freemarker .FreeMarkerTemplateUtils ;
3136import org .springframework .ui .freemarker .SpringTemplateLoader ;
3237
33- import freemarker .template .Configuration ;
34- import freemarker .template .Template ;
35-
3638import static org .hamcrest .Matchers .*;
3739import static org .junit .Assert .*;
3840
4345public 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