1 16 17 package org.springframework.web.servlet.view.freemarker; 18 19 import java.io.IOException ; 20 import java.io.StringReader ; 21 import java.io.Writer ; 22 import java.util.HashMap ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 26 import javax.servlet.http.HttpServletResponse ; 27 28 import freemarker.template.Configuration; 29 import freemarker.template.Template; 30 import freemarker.template.TemplateException; 31 import junit.framework.TestCase; 32 import org.easymock.MockControl; 33 34 import org.springframework.context.ApplicationContextException; 35 import org.springframework.mock.web.MockHttpServletRequest; 36 import org.springframework.mock.web.MockHttpServletResponse; 37 import org.springframework.web.context.WebApplicationContext; 38 import org.springframework.web.servlet.DispatcherServlet; 39 import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver; 40 41 45 public class FreeMarkerViewTests extends TestCase { 46 47 public void testNoFreemarkerConfig() { 48 FreeMarkerView fv = new FreeMarkerView(); 49 50 MockControl wmc = MockControl.createControl(WebApplicationContext.class); 51 WebApplicationContext wac = (WebApplicationContext) wmc.getMock(); 52 wac.getBeansOfType(FreeMarkerConfig.class, true, false); 53 wmc.setReturnValue(new HashMap ()); 54 wac.getParentBeanFactory(); 55 wmc.setReturnValue(null); 56 wmc.replay(); 57 58 fv.setUrl("anythingButNull"); 59 try { 60 fv.setApplicationContext(wac); 61 fail(); 62 } 63 catch (ApplicationContextException ex) { 64 assertTrue(ex.getMessage().indexOf("FreeMarkerConfig") != -1); 66 } 67 68 wmc.verify(); 69 } 70 71 public void testNoTemplateName() throws Exception { 72 FreeMarkerView fv = new FreeMarkerView(); 73 try { 74 fv.afterPropertiesSet(); 75 fail("Should have thrown IllegalArgumentException"); 76 } 77 catch (IllegalArgumentException ex) { 78 assertTrue(ex.getMessage().indexOf("url") != -1); 80 } 81 } 82 83 public void testValidTemplateName() throws Exception { 84 FreeMarkerView fv = new FreeMarkerView(); 85 86 MockControl wmc = MockControl.createControl(WebApplicationContext.class); 87 WebApplicationContext wac = (WebApplicationContext) wmc.getMock(); 88 wac.getBeansOfType(FreeMarkerConfig.class, true, false); 89 Map configs = new HashMap (); 90 FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); 91 configurer.setConfiguration(new TestConfiguration()); 92 configs.put("freemarkerConfig", configurer); 93 wmc.setReturnValue(configs); 94 wac.getParentBeanFactory(); 95 wmc.setReturnValue(null); 96 wmc.replay(); 97 98 fv.setUrl("templateName"); 99 fv.setApplicationContext(wac); 100 101 MockHttpServletRequest request = new MockHttpServletRequest(); 102 request.addPreferredLocale(Locale.US); 103 request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); 104 HttpServletResponse response = new MockHttpServletResponse(); 105 106 Map model = new HashMap (); 107 model.put("myattr", "myvalue"); 108 fv.render(model, request, response); 109 110 wmc.verify(); 111 } 112 113 114 private class TestConfiguration extends Configuration { 115 116 public Template getTemplate(String name, final Locale locale) throws IOException { 117 assertEquals("templateName", name); 118 return new Template(name, new StringReader ("test")) { 119 public void process(Object model, Writer writer) throws TemplateException, IOException { 120 assertEquals(Locale.US, locale); 121 assertTrue(model instanceof Map ); 122 Map modelMap = (Map ) model; 123 assertEquals("myvalue", modelMap.get("myattr")); 124 } 125 }; 126 } 127 128 } 129 130 } 131 | Popular Tags |