KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > velocity > VelocityConfigurerTests


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.velocity;
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.Map JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import junit.framework.TestCase;
27 import org.apache.velocity.app.VelocityEngine;
28 import org.apache.velocity.exception.VelocityException;
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.velocity.VelocityEngineFactoryBean;
35 import org.springframework.ui.velocity.VelocityEngineUtils;
36
37 /**
38  * @author Rod Johnson
39  * @author Juergen Hoeller
40  */

41 public class VelocityConfigurerTests extends TestCase {
42
43     public void testVelocityEngineFactoryBeanWithConfigLocation() throws VelocityException {
44         VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
45         vefb.setConfigLocation(new FileSystemResource("myprops.properties"));
46         Properties JavaDoc props = new Properties JavaDoc();
47         props.setProperty("myprop", "/mydir");
48         vefb.setVelocityProperties(props);
49         try {
50             vefb.afterPropertiesSet();
51             fail("Should have thrown IOException");
52         }
53         catch (IOException JavaDoc ex) {
54             // expected
55
}
56     }
57
58     public void testVelocityEngineFactoryBeanWithVelocityProperties() throws VelocityException, IOException JavaDoc {
59         VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
60         Properties JavaDoc props = new Properties JavaDoc();
61         props.setProperty("myprop", "/mydir");
62         vefb.setVelocityProperties(props);
63         Object JavaDoc value = new Object JavaDoc();
64         Map JavaDoc map = new HashMap JavaDoc();
65         map.put("myentry", value);
66         vefb.setVelocityPropertiesMap(map);
67         vefb.afterPropertiesSet();
68         assertTrue(vefb.getObject() instanceof VelocityEngine);
69         VelocityEngine ve = (VelocityEngine) vefb.getObject();
70         assertEquals("/mydir", ve.getProperty("myprop"));
71         assertEquals(value, ve.getProperty("myentry"));
72     }
73
74     public void testVelocityEngineFactoryBeanWithResourceLoaderPath() throws IOException JavaDoc, VelocityException {
75         VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
76         vefb.setResourceLoaderPath("file:/mydir");
77         vefb.afterPropertiesSet();
78         assertTrue(vefb.getObject() instanceof VelocityEngine);
79         VelocityEngine ve = (VelocityEngine) vefb.getObject();
80         assertEquals(new File JavaDoc("/mydir").getAbsolutePath(), ve.getProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH));
81     }
82
83     public void testVelocityEngineFactoryBeanWithNonFileResourceLoaderPath() throws Exception JavaDoc {
84         VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
85         vefb.setResourceLoaderPath("file:/mydir");
86         vefb.setResourceLoader(new ResourceLoader() {
87             public Resource getResource(String JavaDoc location) {
88                 if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
89                     throw new IllegalArgumentException JavaDoc(location);
90                 }
91                 return new InputStreamResource(new ByteArrayInputStream JavaDoc("test".getBytes()), "test");
92             }
93         });
94         vefb.afterPropertiesSet();
95         assertTrue(vefb.getObject() instanceof VelocityEngine);
96         VelocityEngine ve = (VelocityEngine) vefb.getObject();
97         assertEquals("test", VelocityEngineUtils.mergeTemplateIntoString(ve, "test", new HashMap JavaDoc()));
98     }
99
100     public void testVelocityConfigurer() throws IOException JavaDoc, VelocityException {
101         VelocityConfigurer vc = new VelocityConfigurer();
102         vc.setResourceLoaderPath("file:/mydir");
103         vc.afterPropertiesSet();
104         assertTrue(vc.createVelocityEngine() instanceof VelocityEngine);
105         VelocityEngine ve = vc.createVelocityEngine();
106         assertEquals(new File JavaDoc("/mydir").getAbsolutePath(), ve.getProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH));
107     }
108
109 }
110
Popular Tags