e.g. Calendar Search Help
You must enter a value before pressing Search
spring

Class: org.springframework.web.servlet.view.velocity.VelocityConfigurerTests   ©

 OK to copy?
001 /*
002  * Copyright 2002-2004 the original author or authors.
003  * 
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  * 
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  * 
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */ 
016 
017 package org.springframework.web.servlet.view.velocity;
018 
019 import java.io.ByteArrayInputStream;
020 import java.io.File;
021 import java.io.IOException;
022 import java.util.HashMap;
023 import java.util.Map;
024 import java.util.Properties;
025 
026 import junit.framework.TestCase;
027 import org.apache.velocity.app.VelocityEngine;
028 import org.apache.velocity.exception.VelocityException;
029 
030 import org.springframework.core.io.FileSystemResource;
031 import org.springframework.core.io.InputStreamResource;
032 import org.springframework.core.io.Resource;
033 import org.springframework.core.io.ResourceLoader;
034 import org.springframework.ui.velocity.VelocityEngineFactoryBean;
035 import org.springframework.ui.velocity.VelocityEngineUtils;
036 
037 /**
038  * @author Rod Johnson
039  * @author Juergen Hoeller
040  */
041 public class VelocityConfigurerTests extends TestCase {
042 
043     public void testVelocityEngineFactoryBeanWithConfigLocation() throws VelocityException {
044         VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
045         vefb.setConfigLocation(new FileSystemResource("myprops.properties"));
046         Properties props = new Properties();
047         props.setProperty("myprop", "/mydir");
048         vefb.setVelocityProperties(props);
049         try {
050             vefb.afterPropertiesSet();
051             fail("Should have thrown IOException");
052         }
053         catch (IOException ex) {
054             // expected
055         }
056     }
057 
058     public void testVelocityEngineFactoryBeanWithVelocityProperties() throws VelocityException, IOException {
059         VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
060         Properties props = new Properties();
061         props.setProperty("myprop", "/mydir");
062         vefb.setVelocityProperties(props);
063         Object value = new Object();
064         Map map = new HashMap();
065         map.put("myentry", value);
066         vefb.setVelocityPropertiesMap(map);
067         vefb.afterPropertiesSet();
068         assertTrue(vefb.getObject() instanceof VelocityEngine);
069         VelocityEngine ve = (VelocityEngine) vefb.getObject();
070         assertEquals("/mydir", ve.getProperty("myprop"));
071         assertEquals(value, ve.getProperty("myentry"));
072     }
073 
074     public void testVelocityEngineFactoryBeanWithResourceLoaderPath() throws IOException, VelocityException {
075         VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
076         vefb.setResourceLoaderPath("file:/mydir");
077         vefb.afterPropertiesSet();
078         assertTrue(vefb.getObject() instanceof VelocityEngine);
079         VelocityEngine ve = (VelocityEngine) vefb.getObject();
Rate080         assertEquals(new File("/mydir").getAbsolutePath(), ve.getProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH));
081     }
082 
083     public void testVelocityEngineFactoryBeanWithNonFileResourceLoaderPath() throws Exception {
084         VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean();
085         vefb.setResourceLoaderPath("file:/mydir");
086         vefb.setResourceLoader(new ResourceLoader() {
087             public Resource getResource(String location) {
088                 if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
089                     throw new IllegalArgumentException(location);
090                 }
091                 return new InputStreamResource(new ByteArrayInputStream("test".getBytes()), "test");
092             }
093         });
094         vefb.afterPropertiesSet();
095         assertTrue(vefb.getObject() instanceof VelocityEngine);
096         VelocityEngine ve = (VelocityEngine) vefb.getObject();
097         assertEquals("test", VelocityEngineUtils.mergeTemplateIntoString(ve, "test", new HashMap()));
098     }
099 
100     public void testVelocityConfigurer() throws IOException, 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();
Rate106         assertEquals(new File("/mydir").getAbsolutePath(), ve.getProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH));
107     }
108 
109 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
80 0% of 1
106 0% of 0