KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > velocity > VelocityMacroTests


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.velocity;
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 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 /**
48  * @author Darren Davison
49  * @since 18.06.2004
50  */

51 public class VelocityMacroTests extends TestCase {
52
53     private static final String JavaDoc TEMPLATE_FILE = "test.vm";
54
55
56     private StaticWebApplicationContext wac;
57
58     private HttpServletRequest JavaDoc request;
59
60     private HttpServletResponse JavaDoc expectedResponse;
61
62
63     public void setUp() throws Exception JavaDoc {
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 JavaDoc {
84         VelocityView vv = new VelocityView() {
85             protected void mergeTemplate(Template template, Context context, HttpServletResponse JavaDoc 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 JavaDoc model = new HashMap JavaDoc();
98         model.put("tb", new TestBean("juergen", 99));
99         vv.render(model, request, expectedResponse);
100     }
101
102     public void testSpringMacroRequestContextAttributeUsed() {
103         final String JavaDoc helperTool = "wrongType";
104
105         VelocityView vv = new VelocityView() {
106             protected void mergeTemplate(Template template, Context context, HttpServletResponse JavaDoc response) {
107                 fail();
108             }
109         };
110         vv.setUrl(TEMPLATE_FILE);
111         vv.setApplicationContext(wac);
112         vv.setExposeSpringMacroHelpers(true);
113
114         Map JavaDoc model = new HashMap JavaDoc();
115         model.put(VelocityView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, helperTool);
116
117         try {
118             vv.render(model, request, expectedResponse);
119         }
120         catch (Exception JavaDoc ex) {
121             assertTrue(ex instanceof ServletException JavaDoc);
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 JavaDoc msgMap = new HashMap JavaDoc();
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 JavaDoc names = new HashMap JavaDoc();
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 JavaDoc sw = new StringWriter JavaDoc();
152             ve.mergeTemplate("org/springframework/web/servlet/view/velocity/test.vm", "UTF-8", context, sw);
153             // tokenize output and ignore whitespace
154
String JavaDoc output = sw.getBuffer().toString();
155
156             String JavaDoc[] tokens = StringUtils.tokenizeToStringArray(output, "\t\n");
157
158             //for (int i=0; i<tokens.length; i++) System.out.println(tokens[i]);
159

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                 //TODO verify remaining output (fix whitespace)
171
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 JavaDoc e) {
176             fail();
177         }
178     }
179 }
180
Popular Tags