1 16 17 package org.springframework.web.servlet.view.freemarker; 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 freemarker.template.Configuration; 28 import freemarker.template.Template; 29 import junit.framework.TestCase; 30 31 import org.springframework.beans.TestBean; 32 import org.springframework.mock.web.MockHttpServletRequest; 33 import org.springframework.mock.web.MockHttpServletResponse; 34 import org.springframework.mock.web.MockServletContext; 35 import org.springframework.util.StringUtils; 36 import org.springframework.web.context.support.StaticWebApplicationContext; 37 import org.springframework.web.servlet.DispatcherServlet; 38 import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver; 39 import org.springframework.web.servlet.support.BindStatus; 40 import org.springframework.web.servlet.support.RequestContext; 41 import org.springframework.web.servlet.theme.FixedThemeResolver; 42 import org.springframework.web.servlet.view.DummyMacroRequestContext; 43 44 48 public class FreeMarkerMacroTests extends TestCase { 49 50 private static final String TEMPLATE_FILE = "test.ftl"; 51 52 53 private StaticWebApplicationContext wac; 54 55 private HttpServletRequest request; 56 57 private HttpServletResponse expectedResponse; 58 59 private FreeMarkerConfigurer fc; 60 61 62 public void setUp() throws Exception { 63 wac = new StaticWebApplicationContext(); 64 wac.setServletContext(new MockServletContext()); 65 66 fc = new FreeMarkerConfigurer(); 68 fc.setConfiguration(fc.createConfiguration()); 69 fc.setPreferFileSystemAccess(false); 70 71 wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc); 72 wac.refresh(); 73 74 request = new MockHttpServletRequest(); 75 request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); 76 request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); 77 request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver()); 78 expectedResponse = new MockHttpServletResponse(); 79 } 80 81 public void testExposeSpringMacroHelpers() throws Exception { 82 FreeMarkerView fv = new FreeMarkerView() { 83 protected void processTemplate(Template template, Map model, HttpServletResponse response) { 84 assertTrue(model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) instanceof RequestContext); 85 RequestContext rc = (RequestContext) model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE); 86 BindStatus status = rc.getBindStatus("tb.name"); 87 assertEquals("name", status.getExpression()); 88 assertEquals("juergen", status.getValue()); 89 } 90 }; 91 fv.setUrl(TEMPLATE_FILE); 92 fv.setApplicationContext(wac); 93 fv.setExposeSpringMacroHelpers(true); 94 95 Map model = new HashMap (); 96 model.put("tb", new TestBean("juergen", 99)); 97 fv.render(model, request, expectedResponse); 98 } 99 100 public void testSpringMacroRequestContextAttributeUsed() { 101 final String helperTool = "wrongType"; 102 103 FreeMarkerView fv = new FreeMarkerView() { 104 protected void processTemplate(Template template, Map model, HttpServletResponse response) { 105 fail(); 106 } 107 }; 108 fv.setUrl(TEMPLATE_FILE); 109 fv.setApplicationContext(wac); 110 fv.setExposeSpringMacroHelpers(true); 111 112 Map model = new HashMap (); 113 model.put(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, helperTool); 114 115 try { 116 fv.render(model, request, expectedResponse); 117 } 118 catch (Exception ex) { 119 assertTrue(ex instanceof ServletException ); 120 assertTrue(ex.getMessage().indexOf(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) > -1); 121 } 122 } 123 124 public void testAllMacros() throws Exception { 125 DummyMacroRequestContext rc = new DummyMacroRequestContext(); 126 HashMap msgMap = new HashMap (); 127 msgMap.put("hello", "Howdy"); 128 msgMap.put("world", "Mundo"); 129 rc.setMsgMap(msgMap); 130 rc.setContextPath("/springtest"); 131 132 TestBean tb = new TestBean("Darren", 99); 133 rc.setCommand(tb); 134 135 HashMap names = new HashMap (); 136 names.put("Darren", "Darren Davison"); 137 names.put("John", "John Doe"); 138 names.put("Fred", "Fred Bloggs"); 139 140 Configuration config = fc.getConfiguration(); 141 Map model = new HashMap (); 142 model.put("command", tb); 143 model.put("springMacroRequestContext", rc); 144 model.put("nameOptionMap", names); 145 146 StringWriter sw = new StringWriter (); 147 Template t = config.getTemplate("test.ftl"); 148 t.process(model, sw); 149 150 String output = sw.getBuffer().toString(); 152 String [] tokens = StringUtils.tokenizeToStringArray(output, "\t\n"); 153 154 for (int i = 0; i < tokens.length; i++) { 155 if (tokens[i].equals("NAME")) assertEquals("Darren", tokens[i + 1]); 156 if (tokens[i].equals("AGE")) assertEquals("99", tokens[i + 1]); 157 if (tokens[i].equals("MESSAGE")) assertEquals("Howdy Mundo", tokens[i + 1]); 158 if (tokens[i].equals("DEFAULTMESSAGE")) assertEquals("hi planet", tokens[i + 1]); 159 if (tokens[i].equals("URL")) assertEquals("/springtest/aftercontext.html", tokens[i + 1]); 160 if (tokens[i].equals("FORM1")) assertEquals("<input type=\"text\" name=\"name\" value=\"Darren\"", tokens[i + 1]); 161 if (tokens[i].equals("FORM2")) assertEquals("<input type=\"text\" name=\"name\" value=\"Darren\" class=\"myCssClass\"", tokens[i + 1]); 162 if (tokens[i].equals("FORM3")) assertEquals("<textarea name=\"name\" >Darren</textarea>", tokens[i + 1]); 163 if (tokens[i].equals("FORM4")) assertEquals("<textarea name=\"name\" rows=10 cols=30>Darren</textarea>", tokens[i + 1]); 164 if (tokens[i].equals("FORM9")) assertEquals("<input type=\"password\" name=\"name\" value=\"\"", tokens[i + 1]); 166 if (tokens[i].equals("FORM10")) assertEquals("<input type=\"hidden\" name=\"name\" value=\"Darren\"", tokens[i + 1]); 167 if (tokens[i].equals("FORM11")) assertEquals("<input type=\"text\" name=\"name\" value=\"Darren\"", tokens[i + 1]); 168 if (tokens[i].equals("FORM12")) assertEquals("<input type=\"hidden\" name=\"name\" value=\"Darren\"", tokens[i + 1]); 169 if (tokens[i].equals("FORM13")) assertEquals("<input type=\"password\" name=\"name\" value=\"\"", tokens[i + 1]); 170 } 171 } 172 173 } 174 | Popular Tags |