KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > tiles > TilesViewTests


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.tiles;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import javax.servlet.jsp.jstl.core.Config;
23 import javax.servlet.jsp.jstl.fmt.LocalizationContext;
24
25 import junit.framework.TestCase;
26 import org.apache.struts.taglib.tiles.ComponentConstants;
27 import org.apache.struts.tiles.ComponentContext;
28 import org.apache.struts.tiles.PathAttribute;
29
30 import org.springframework.mock.web.MockHttpServletRequest;
31 import org.springframework.mock.web.MockHttpServletResponse;
32 import org.springframework.mock.web.MockServletContext;
33 import org.springframework.web.context.WebApplicationContext;
34 import org.springframework.web.context.support.StaticWebApplicationContext;
35 import org.springframework.web.servlet.DispatcherServlet;
36 import org.springframework.web.servlet.View;
37 import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
38 import org.springframework.web.servlet.support.JstlUtils;
39 import org.springframework.web.servlet.view.InternalResourceViewResolver;
40
41 /**
42  * @author Alef Arendsen
43  * @author Juergen Hoeller
44  */

45 public class TilesViewTests extends TestCase {
46
47     protected StaticWebApplicationContext prepareWebApplicationContext() throws Exception JavaDoc {
48         StaticWebApplicationContext wac = new StaticWebApplicationContext();
49         MockServletContext sc = new MockServletContext("/org/springframework/web/servlet/view/tiles/");
50         wac.setServletContext(sc);
51         wac.refresh();
52
53         TilesConfigurer tc = new TilesConfigurer();
54         tc.setDefinitions(new String JavaDoc[] {"tiles-test.xml"});
55         tc.setValidateDefinitions(true);
56         tc.setApplicationContext(wac);
57         tc.afterPropertiesSet();
58
59         return wac;
60     }
61
62     public void testTilesView() throws Exception JavaDoc {
63         WebApplicationContext wac = prepareWebApplicationContext();
64
65         InternalResourceViewResolver irvr = new InternalResourceViewResolver();
66         irvr.setApplicationContext(wac);
67         irvr.setViewClass(TilesView.class);
68         View view = irvr.resolveViewName("testTile", new Locale JavaDoc("nl", ""));
69
70         MockHttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
71         MockHttpServletResponse response = new MockHttpServletResponse();
72         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
73
74         view.render(new HashMap JavaDoc(), request, response);
75         assertEquals("/WEB-INF/jsp/layout.jsp", response.getForwardedUrl());
76         ComponentContext cc = (ComponentContext) request.getAttribute(ComponentConstants.COMPONENT_CONTEXT);
77         assertNotNull(cc);
78         PathAttribute attr = (PathAttribute) cc.getAttribute("content");
79         assertEquals("/WEB-INF/jsp/content.jsp", attr.getValue());
80
81         view.render(new HashMap JavaDoc(), request, response);
82         assertEquals("/WEB-INF/jsp/layout.jsp", response.getForwardedUrl());
83         cc = (ComponentContext) request.getAttribute(ComponentConstants.COMPONENT_CONTEXT);
84         assertNotNull(cc);
85         attr = (PathAttribute) cc.getAttribute("content");
86         assertEquals("/WEB-INF/jsp/content.jsp", attr.getValue());
87     }
88
89     public void testTilesJstlView() throws Exception JavaDoc {
90         StaticWebApplicationContext wac = prepareWebApplicationContext();
91
92         InternalResourceViewResolver irvr = new InternalResourceViewResolver();
93         irvr.setApplicationContext(wac);
94         irvr.setViewClass(TilesJstlView.class);
95         View view = irvr.resolveViewName("testTile", new Locale JavaDoc("nl", ""));
96
97         MockHttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
98         MockHttpServletResponse response = new MockHttpServletResponse();
99         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
100         wac.addMessage("code1", request.getLocale(), "messageX");
101         view.render(new HashMap JavaDoc(), request, response);
102
103         assertEquals("/WEB-INF/jsp/layout.jsp", response.getForwardedUrl());
104         ComponentContext cc = (ComponentContext) request.getAttribute(ComponentConstants.COMPONENT_CONTEXT);
105         assertNotNull(cc);
106         PathAttribute attr = (PathAttribute) cc.getAttribute("content");
107         assertEquals("/WEB-INF/jsp/content.jsp", attr.getValue());
108
109         assertTrue("Correct JSTL attributes",
110                 request.getAttribute(Config.FMT_LOCALIZATION_CONTEXT) instanceof LocalizationContext);
111         assertTrue("Correct JSTL attributes",
112                 request.getLocale().equals(request.getAttribute(Config.FMT_LOCALE)));
113         assertTrue("Correct JSTL attributes",
114                 request.getAttribute(Config.FMT_LOCALIZATION_CONTEXT + JstlUtils.REQUEST_SCOPE_SUFFIX) instanceof LocalizationContext);
115         assertTrue("Correct JSTL attributes",
116                 request.getLocale().equals(request.getAttribute(Config.FMT_LOCALE + JstlUtils.REQUEST_SCOPE_SUFFIX)));
117
118         LocalizationContext lc = (LocalizationContext) request.getAttribute(Config.FMT_LOCALIZATION_CONTEXT);
119         assertEquals("messageX", lc.getResourceBundle().getString("code1"));
120     }
121
122     public void testTilesJstlViewWithContextParam() throws Exception JavaDoc {
123         StaticWebApplicationContext wac = prepareWebApplicationContext();
124         ((MockServletContext) wac.getServletContext()).addInitParameter(
125                 Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/context/WEB-INF/context-messages");
126
127         InternalResourceViewResolver irvr = new InternalResourceViewResolver();
128         irvr.setApplicationContext(wac);
129         irvr.setViewClass(TilesJstlView.class);
130         View view = irvr.resolveViewName("testTile", new Locale JavaDoc("nl", ""));
131
132         MockHttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
133         MockHttpServletResponse response = new MockHttpServletResponse();
134         wac.addMessage("code1", request.getLocale(), "messageX");
135         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
136
137         view.render(new HashMap JavaDoc(), request, response);
138         assertEquals("/WEB-INF/jsp/layout.jsp", response.getForwardedUrl());
139         ComponentContext cc = (ComponentContext) request.getAttribute(ComponentConstants.COMPONENT_CONTEXT);
140         assertNotNull(cc);
141         PathAttribute attr = (PathAttribute) cc.getAttribute("content");
142         assertEquals("/WEB-INF/jsp/content.jsp", attr.getValue());
143
144         assertTrue("Correct JSTL attributes",
145                 request.getAttribute(Config.FMT_LOCALIZATION_CONTEXT) instanceof LocalizationContext);
146         assertTrue("Correct JSTL attributes",
147                 request.getLocale().equals(request.getAttribute(Config.FMT_LOCALE)));
148         assertTrue("Correct JSTL attributes",
149                 request.getAttribute(Config.FMT_LOCALIZATION_CONTEXT + JstlUtils.REQUEST_SCOPE_SUFFIX) instanceof LocalizationContext);
150         assertTrue("Correct JSTL attributes",
151                 request.getLocale().equals(request.getAttribute(Config.FMT_LOCALE + JstlUtils.REQUEST_SCOPE_SUFFIX)));
152
153         LocalizationContext lc = (LocalizationContext) request.getAttribute(Config.FMT_LOCALIZATION_CONTEXT);
154         assertEquals("message1", lc.getResourceBundle().getString("code1"));
155         assertEquals("message2", lc.getResourceBundle().getString("code2"));
156     }
157
158     public void testTilesViewWithController() throws Exception JavaDoc {
159         WebApplicationContext wac = prepareWebApplicationContext();
160
161         InternalResourceViewResolver irvr = new InternalResourceViewResolver();
162         irvr.setApplicationContext(wac);
163         irvr.setViewClass(TilesView.class);
164         View view = irvr.resolveViewName("testTileWithController", new Locale JavaDoc("nl", ""));
165
166         MockHttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
167         MockHttpServletResponse response = new MockHttpServletResponse();
168         request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
169         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
170         view.render(new HashMap JavaDoc(), request, response);
171         assertEquals("/WEB-INF/jsp/layout.jsp", response.getForwardedUrl());
172         ComponentContext cc = (ComponentContext) request.getAttribute(ComponentConstants.COMPONENT_CONTEXT);
173         assertNotNull(cc);
174         PathAttribute attr = (PathAttribute) cc.getAttribute("content");
175         assertEquals("/WEB-INF/jsp/otherContent.jsp", attr.getValue());
176         assertEquals("testVal", request.getAttribute("testAttr"));
177
178         view.render(new HashMap JavaDoc(), request, response);
179         assertEquals("/WEB-INF/jsp/layout.jsp", response.getForwardedUrl());
180         cc = (ComponentContext) request.getAttribute(ComponentConstants.COMPONENT_CONTEXT);
181         assertNotNull(cc);
182         attr = (PathAttribute) cc.getAttribute("content");
183         assertEquals("/WEB-INF/jsp/otherContent.jsp", attr.getValue());
184         assertEquals("testVal", request.getAttribute("testAttr"));
185     }
186
187 }
188
Popular Tags