1 16 17 package org.springframework.web.servlet.view.velocity; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import java.util.Properties ; 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 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 props = new Properties (); 47 props.setProperty("myprop", "/mydir"); 48 vefb.setVelocityProperties(props); 49 try { 50 vefb.afterPropertiesSet(); 51 fail("Should have thrown IOException"); 52 } 53 catch (IOException ex) { 54 } 56 } 57 58 public void testVelocityEngineFactoryBeanWithVelocityProperties() throws VelocityException, IOException { 59 VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean(); 60 Properties props = new Properties (); 61 props.setProperty("myprop", "/mydir"); 62 vefb.setVelocityProperties(props); 63 Object value = new Object (); 64 Map map = new HashMap (); 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 , 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 ("/mydir").getAbsolutePath(), ve.getProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH)); 81 } 82 83 public void testVelocityEngineFactoryBeanWithNonFileResourceLoaderPath() throws Exception { 84 VelocityEngineFactoryBean vefb = new VelocityEngineFactoryBean(); 85 vefb.setResourceLoaderPath("file:/mydir"); 86 vefb.setResourceLoader(new ResourceLoader() { 87 public Resource getResource(String location) { 88 if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) { 89 throw new IllegalArgumentException (location); 90 } 91 return new InputStreamResource(new ByteArrayInputStream ("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 ())); 98 } 99 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(); 106 assertEquals(new File ("/mydir").getAbsolutePath(), ve.getProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH)); 107 } 108 109 } 110 | Popular Tags |