1 16 17 package org.springframework.web.servlet.view.velocity; 18 19 import java.io.StringWriter ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import junit.framework.TestCase; 28 29 import org.apache.velocity.Template; 30 import org.apache.velocity.VelocityContext; 31 import org.apache.velocity.app.VelocityEngine; 32 import org.apache.velocity.context.Context; 33 34 import org.springframework.beans.TestBean; 35 import org.springframework.mock.web.MockHttpServletRequest; 36 import org.springframework.mock.web.MockHttpServletResponse; 37 import org.springframework.mock.web.MockServletContext; 38 import org.springframework.util.StringUtils; 39 import org.springframework.web.context.support.StaticWebApplicationContext; 40 import org.springframework.web.servlet.DispatcherServlet; 41 import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver; 42 import org.springframework.web.servlet.support.BindStatus; 43 import org.springframework.web.servlet.support.RequestContext; 44 import org.springframework.web.servlet.theme.FixedThemeResolver; 45 import org.springframework.web.servlet.view.DummyMacroRequestContext; 46 47 51 public class VelocityMacroTests extends TestCase { 52 53 private static final String TEMPLATE_FILE = "test.vm"; 54 55 56 private StaticWebApplicationContext wac; 57 58 private HttpServletRequest request; 59 60 private HttpServletResponse expectedResponse; 61 62 63 public void setUp() throws Exception { 64 wac = new StaticWebApplicationContext(); 65 wac.setServletContext(new MockServletContext()); 66 67 final Template expectedTemplate = new Template(); 68 VelocityConfig vc = new VelocityConfig() { 69 public VelocityEngine getVelocityEngine() { 70 return new TestVelocityEngine(TEMPLATE_FILE, expectedTemplate); 71 } 72 }; 73 wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc); 74 wac.refresh(); 75 76 request = new MockHttpServletRequest(); 77 request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); 78 request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); 79 request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver()); 80 expectedResponse = new MockHttpServletResponse(); 81 } 82 83 public void testExposeSpringMacroHelpers() throws Exception { 84 VelocityView vv = new VelocityView() { 85 protected void mergeTemplate(Template template, Context context, HttpServletResponse response) { 86 assertTrue(context.get(VelocityView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) instanceof RequestContext); 87 RequestContext rc = (RequestContext) context.get(VelocityView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE); 88 BindStatus status = rc.getBindStatus("tb.name"); 89 assertEquals("name", status.getExpression()); 90 assertEquals("juergen", status.getValue()); 91 } 92 }; 93 vv.setUrl(TEMPLATE_FILE); 94 vv.setApplicationContext(wac); 95 vv.setExposeSpringMacroHelpers(true); 96 97 Map model = new HashMap (); 98 model.put("tb", new TestBean("juergen", 99)); 99 vv.render(model, request, expectedResponse); 100 } 101 102 public void testSpringMacroRequestContextAttributeUsed() { 103 final String helperTool = "wrongType"; 104 105 VelocityView vv = new VelocityView() { 106 protected void mergeTemplate(Template template, Context context, HttpServletResponse response) { 107 fail(); 108 } 109 }; 110 vv.setUrl(TEMPLATE_FILE); 111 vv.setApplicationContext(wac); 112 vv.setExposeSpringMacroHelpers(true); 113 114 Map model = new HashMap (); 115 model.put(VelocityView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, helperTool); 116 117 try { 118 vv.render(model, request, expectedResponse); 119 } 120 catch (Exception ex) { 121 assertTrue(ex instanceof ServletException ); 122 assertTrue(ex.getMessage().indexOf(VelocityView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) > -1); 123 } 124 } 125 126 public void testAllMacros() { 127 DummyMacroRequestContext rc = new DummyMacroRequestContext(); 128 HashMap msgMap = new HashMap (); 129 msgMap.put("hello", "Howdy"); 130 msgMap.put("world", "Mundo"); 131 rc.setMsgMap(msgMap); 132 rc.setContextPath("/springtest"); 133 134 TestBean tb = new TestBean("Darren", 99); 135 rc.setCommand(tb); 136 137 HashMap names = new HashMap (); 138 names.put("Darren", "Darren Davison"); 139 names.put("John", "John Doe"); 140 names.put("Fred", "Fred Bloggs"); 141 142 try { 143 VelocityConfigurer vc = new VelocityConfigurer(); 144 vc.setPreferFileSystemAccess(false); 145 VelocityEngine ve = vc.createVelocityEngine(); 146 VelocityContext context = new VelocityContext(); 147 context.put("command", tb); 148 context.put("springMacroRequestContext", rc); 149 context.put("nameOptionMap", names); 150 151 StringWriter sw = new StringWriter (); 152 ve.mergeTemplate("org/springframework/web/servlet/view/velocity/test.vm", "UTF-8", context, sw); 153 String output = sw.getBuffer().toString(); 155 156 String [] tokens = StringUtils.tokenizeToStringArray(output, "\t\n"); 157 158 160 for (int i = 0; i < tokens.length; i++) { 161 if (tokens[i].equals("NAME")) assertEquals("Darren", tokens[i + 1]); 162 if (tokens[i].equals("AGE")) assertEquals("99", tokens[i + 1]); 163 if (tokens[i].equals("MESSAGE")) assertEquals("Howdy Mundo", tokens[i + 1]); 164 if (tokens[i].equals("DEFAULTMESSAGE")) assertEquals("hi planet", tokens[i + 1]); 165 if (tokens[i].equals("URL")) assertEquals("/springtest/aftercontext.html", tokens[i + 1]); 166 if (tokens[i].equals("FORM1")) assertEquals("<input type=\"text\" name=\"name\" value=\"Darren\" >", tokens[i + 1]); 167 if (tokens[i].equals("FORM2")) assertEquals("<input type=\"text\" name=\"name\" value=\"Darren\" class=\"myCssClass\">", tokens[i + 1]); 168 if (tokens[i].equals("FORM3")) assertEquals("<textarea name=\"name\" >Darren</textarea>", tokens[i + 1]); 169 if (tokens[i].equals("FORM4")) assertEquals("<textarea name=\"name\" rows=10 cols=30>Darren</textarea>", tokens[i + 1]); 170 if (tokens[i].equals("FORM9")) assertEquals("<input type=\"password\" name=\"name\" value=\"\" >", tokens[i + 1]); 172 if (tokens[i].equals("FORM10")) assertEquals("<input type=\"hidden\" name=\"name\" value=\"Darren\" >", tokens[i + 1]); 173 } 174 } 175 catch (Exception e) { 176 fail(); 177 } 178 } 179 } 180 | Popular Tags |