1 16 17 package org.springframework.web.servlet.view.freemarker; 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.Properties ; 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 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 props = new Properties (); 47 props.setProperty("myprop", "/mydir"); 48 fcfb.setFreemarkerSettings(props); 49 try { 50 fcfb.afterPropertiesSet(); 51 fail("Should have thrown IOException"); 52 } 53 catch (IOException ex) { 54 } 56 } 57 58 public void testVelocityEngineFactoryBeanWithResourceLoaderPath() throws IOException , TemplateException { 59 final File [] files = new File [1]; 60 FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean() { 61 protected Configuration newConfiguration() { 62 return new Configuration() { 63 public void setDirectoryForTemplateLoading(File file) throws IOException { 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 ("/mydir").getPath(), files[0].getPath()); 73 } 74 75 public void testFreemarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath() 76 throws IOException , TemplateException { 77 FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean(); 78 fcfb.setTemplateLoaderPath("file:/mydir"); 79 Properties settings = new Properties (); 80 settings.setProperty("localized_lookup", "false"); 81 fcfb.setFreemarkerSettings(settings); 82 fcfb.setResourceLoader(new ResourceLoader() { 83 public Resource getResource(String location) { 84 if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) { 85 throw new IllegalArgumentException (location); 86 } 87 return new InputStreamResource(new ByteArrayInputStream ("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 ())); 95 } 96 97 public void testFreemarkerConfigurer() throws IOException , TemplateException { 98 final File [] files = new File [1]; 99 FreeMarkerConfigurer fc = new FreeMarkerConfigurer() { 100 protected Configuration newConfiguration() { 101 return new Configuration() { 102 public void setDirectoryForTemplateLoading(File file) throws IOException { 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 ("/mydir").getPath(), files[0].getPath()); 112 } 113 114 } 115 | Popular Tags |