KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > freemarker > FreeMarkerConfigurerTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.view.freemarker;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import freemarker.template.Configuration;
26 import freemarker.template.Template;
27 import freemarker.template.TemplateException;
28 import junit.framework.TestCase;
29
30 import org.springframework.core.io.FileSystemResource;
31 import org.springframework.core.io.InputStreamResource;
32 import org.springframework.core.io.Resource;
33 import org.springframework.core.io.ResourceLoader;
34 import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
35 import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
36
37 /**
38  * @author Juergen Hoeller
39  * @since 14.03.2004
40  */

41 public class FreeMarkerConfigurerTests extends TestCase {
42
43     public void testFreemarkerConfigurationFactoryBeanWithConfigLocation() throws TemplateException {
44         FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
45         fcfb.setConfigLocation(new FileSystemResource("myprops.properties"));
46         Properties JavaDoc props = new Properties JavaDoc();
47         props.setProperty("myprop", "/mydir");
48         fcfb.setFreemarkerSettings(props);
49         try {
50             fcfb.afterPropertiesSet();
51             fail("Should have thrown IOException");
52         }
53         catch (IOException JavaDoc ex) {
54             // expected
55
}
56     }
57
58     public void testVelocityEngineFactoryBeanWithResourceLoaderPath() throws IOException JavaDoc, TemplateException {
59         final File JavaDoc[] files = new File JavaDoc[1];
60         FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean() {
61             protected Configuration newConfiguration() {
62                 return new Configuration() {
63                     public void setDirectoryForTemplateLoading(File JavaDoc file) throws IOException JavaDoc {
64                         files[0] = file;
65                     }
66                 };
67             }
68         };
69         fcfb.setTemplateLoaderPath("file:/mydir");
70         fcfb.afterPropertiesSet();
71         assertTrue(fcfb.getObject() instanceof Configuration);
72         assertEquals(new File JavaDoc("/mydir").getPath(), files[0].getPath());
73     }
74
75     public void testFreemarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath()
76             throws IOException JavaDoc, TemplateException {
77         FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
78         fcfb.setTemplateLoaderPath("file:/mydir");
79         Properties JavaDoc settings = new Properties JavaDoc();
80         settings.setProperty("localized_lookup", "false");
81         fcfb.setFreemarkerSettings(settings);
82         fcfb.setResourceLoader(new ResourceLoader() {
83             public Resource getResource(String JavaDoc location) {
84                 if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
85                     throw new IllegalArgumentException JavaDoc(location);
86                 }
87                 return new InputStreamResource(new ByteArrayInputStream JavaDoc("test".getBytes()), "test");
88             }
89         });
90         fcfb.afterPropertiesSet();
91         assertTrue(fcfb.getObject() instanceof Configuration);
92         Configuration fc = (Configuration) fcfb.getObject();
93         Template ft = fc.getTemplate("test");
94         assertEquals("test", FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap JavaDoc()));
95     }
96
97     public void testFreemarkerConfigurer() throws IOException JavaDoc, TemplateException {
98         final File JavaDoc[] files = new File JavaDoc[1];
99         FreeMarkerConfigurer fc = new FreeMarkerConfigurer() {
100             protected Configuration newConfiguration() {
101                 return new Configuration() {
102                     public void setDirectoryForTemplateLoading(File JavaDoc file) throws IOException JavaDoc {
103                         files[0] = file;
104                     }
105                 };
106             }
107         };
108         fc.setTemplateLoaderPath("file:/mydir");
109         fc.afterPropertiesSet();
110         assertTrue(fc.getConfiguration() instanceof Configuration);
111         assertEquals(new File JavaDoc("/mydir").getPath(), files[0].getPath());
112     }
113
114 }
115
Popular Tags