KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > freemarker > FreeMarkerMacroTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.view.freemarker;
18
19 import java.io.StringWriter JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
45  * @author Darren Davison
46  * @since 25.01.2005
47  */

48 public class FreeMarkerMacroTests extends TestCase {
49
50     private static final String JavaDoc TEMPLATE_FILE = "test.ftl";
51
52
53     private StaticWebApplicationContext wac;
54
55     private HttpServletRequest JavaDoc request;
56
57     private HttpServletResponse JavaDoc expectedResponse;
58
59     private FreeMarkerConfigurer fc;
60
61
62     public void setUp() throws Exception JavaDoc {
63         wac = new StaticWebApplicationContext();
64         wac.setServletContext(new MockServletContext());
65
66         //final Template expectedTemplate = new Template();
67
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 JavaDoc {
82         FreeMarkerView fv = new FreeMarkerView() {
83             protected void processTemplate(Template template, Map JavaDoc model, HttpServletResponse JavaDoc 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 JavaDoc model = new HashMap JavaDoc();
96         model.put("tb", new TestBean("juergen", 99));
97         fv.render(model, request, expectedResponse);
98     }
99
100     public void testSpringMacroRequestContextAttributeUsed() {
101         final String JavaDoc helperTool = "wrongType";
102
103         FreeMarkerView fv = new FreeMarkerView() {
104             protected void processTemplate(Template template, Map JavaDoc model, HttpServletResponse JavaDoc response) {
105                 fail();
106             }
107         };
108         fv.setUrl(TEMPLATE_FILE);
109         fv.setApplicationContext(wac);
110         fv.setExposeSpringMacroHelpers(true);
111
112         Map JavaDoc model = new HashMap JavaDoc();
113         model.put(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, helperTool);
114
115         try {
116             fv.render(model, request, expectedResponse);
117         }
118         catch (Exception JavaDoc ex) {
119             assertTrue(ex instanceof ServletException JavaDoc);
120             assertTrue(ex.getMessage().indexOf(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) > -1);
121         }
122     }
123
124     public void testAllMacros() throws Exception JavaDoc {
125         DummyMacroRequestContext rc = new DummyMacroRequestContext();
126         HashMap JavaDoc msgMap = new HashMap JavaDoc();
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 JavaDoc names = new HashMap JavaDoc();
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 JavaDoc model = new HashMap JavaDoc();
142         model.put("command", tb);
143         model.put("springMacroRequestContext", rc);
144         model.put("nameOptionMap", names);
145
146         StringWriter JavaDoc sw = new StringWriter JavaDoc();
147         Template t = config.getTemplate("test.ftl");
148         t.process(model, sw);
149
150         // tokenize output and ignore whitespace
151
String JavaDoc output = sw.getBuffer().toString();
152         String JavaDoc[] 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             //TODO verify remaining output (fix whitespace)
165
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