KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > ViewResolverTests


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;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import javax.servlet.jsp.jstl.core.Config;
29 import javax.servlet.jsp.jstl.fmt.LocalizationContext;
30
31 import junit.framework.TestCase;
32
33 import org.springframework.beans.MutablePropertyValues;
34 import org.springframework.beans.PropertyValue;
35 import org.springframework.beans.TestBean;
36 import org.springframework.beans.factory.BeanDefinitionStoreException;
37 import org.springframework.context.ApplicationContextException;
38 import org.springframework.core.io.ClassPathResource;
39 import org.springframework.core.io.Resource;
40 import org.springframework.mock.web.MockHttpServletRequest;
41 import org.springframework.mock.web.MockHttpServletResponse;
42 import org.springframework.mock.web.MockServletContext;
43 import org.springframework.web.context.support.ServletContextResource;
44 import org.springframework.web.context.support.StaticWebApplicationContext;
45 import org.springframework.web.servlet.DispatcherServlet;
46 import org.springframework.web.servlet.View;
47 import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
48 import org.springframework.web.servlet.support.JstlUtils;
49 import org.springframework.web.servlet.support.RequestContext;
50 import org.springframework.web.servlet.theme.FixedThemeResolver;
51
52 /**
53  * @author Juergen Hoeller
54  * @since 18.06.2003
55  */

56 public class ViewResolverTests extends TestCase {
57
58     public void testBeanNameViewResolver() throws ServletException JavaDoc {
59         StaticWebApplicationContext wac = new StaticWebApplicationContext();
60         wac.setServletContext(new MockServletContext());
61         MutablePropertyValues pvs1 = new MutablePropertyValues();
62         pvs1.addPropertyValue(new PropertyValue("url", "/example1.jsp"));
63         wac.registerSingleton("example1", InternalResourceView.class, pvs1);
64         MutablePropertyValues pvs2 = new MutablePropertyValues();
65         pvs2.addPropertyValue(new PropertyValue("url", "/example2.jsp"));
66         wac.registerSingleton("example2", JstlView.class, pvs2);
67         BeanNameViewResolver vr = new BeanNameViewResolver();
68         vr.setApplicationContext(wac);
69         wac.refresh();
70
71         View view = vr.resolveViewName("example1", Locale.getDefault());
72         assertEquals("Correct view class", InternalResourceView.class, view.getClass());
73         assertEquals("Correct URL", "/example1.jsp", ((InternalResourceView) view).getUrl());
74
75         view = vr.resolveViewName("example2", Locale.getDefault());
76         assertEquals("Correct view class", JstlView.class, view.getClass());
77         assertEquals("Correct URL", "/example2.jsp", ((JstlView) view).getUrl());
78     }
79
80     public void testUrlBasedViewResolverWithNullViewClass() {
81         UrlBasedViewResolver resolver = new UrlBasedViewResolver();
82         try {
83             resolver.setViewClass(null);
84             fail("Should have thrown IllegalArgumentException");
85         }
86         catch (IllegalArgumentException JavaDoc ex) {
87             // expected
88
}
89     }
90
91     public void testUrlBasedViewResolverWithoutPrefixes() throws Exception JavaDoc {
92         UrlBasedViewResolver vr = new UrlBasedViewResolver();
93         vr.setViewClass(InternalResourceView.class);
94         doTestUrlBasedViewResolverWithoutPrefixes(vr);
95     }
96
97     public void testUrlBasedViewResolverWithPrefixes() throws Exception JavaDoc {
98         UrlBasedViewResolver vr = new UrlBasedViewResolver();
99         vr.setViewClass(InternalResourceView.class);
100         doTestUrlBasedViewResolverWithPrefixes(vr);
101     }
102
103     public void testInternalResourceViewResolverWithoutPrefixes() throws Exception JavaDoc {
104         doTestUrlBasedViewResolverWithoutPrefixes(new InternalResourceViewResolver());
105     }
106
107     public void testInternalResourceViewResolverWithPrefixes() throws Exception JavaDoc {
108         doTestUrlBasedViewResolverWithPrefixes(new InternalResourceViewResolver());
109     }
110
111     private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception JavaDoc {
112         StaticWebApplicationContext wac = new StaticWebApplicationContext();
113         wac.setServletContext(new MockServletContext());
114         wac.refresh();
115         vr.setApplicationContext(wac);
116         vr.setContentType("myContentType");
117         vr.setRequestContextAttribute("rc");
118
119         View view = vr.resolveViewName("example1", Locale.getDefault());
120         assertEquals("Correct view class", InternalResourceView.class, view.getClass());
121         assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
122         assertEquals("Correct contentType", "myContentType", ((InternalResourceView) view).getContentType());
123
124         view = vr.resolveViewName("example2", Locale.getDefault());
125         assertEquals("Correct view class", InternalResourceView.class, view.getClass());
126         assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
127         assertEquals("Correct contentType", "myContentType", ((InternalResourceView) view).getContentType());
128
129         HttpServletRequest JavaDoc request = new MockHttpServletRequest(wac.getServletContext());
130         HttpServletResponse JavaDoc response = new MockHttpServletResponse();
131         request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
132         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
133         request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
134         Map JavaDoc model = new HashMap JavaDoc();
135         TestBean tb = new TestBean();
136         model.put("tb", tb);
137         view.render(model, request, response);
138         assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
139         assertTrue("Correct rc attribute", request.getAttribute("rc") instanceof RequestContext);
140
141         view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
142         assertEquals("Correct view class", RedirectView.class, view.getClass());
143         assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl());
144
145         view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
146         assertEquals("Correct view class", InternalResourceView.class, view.getClass());
147         assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
148     }
149
150     private void doTestUrlBasedViewResolverWithPrefixes(UrlBasedViewResolver vr) throws Exception JavaDoc {
151         StaticWebApplicationContext wac = new StaticWebApplicationContext();
152         wac.setServletContext(new MockServletContext());
153         wac.refresh();
154         vr.setPrefix("/WEB-INF/");
155         vr.setSuffix(".jsp");
156         vr.setApplicationContext(wac);
157
158         View view = vr.resolveViewName("example1", Locale.getDefault());
159         assertEquals("Correct view class", InternalResourceView.class, view.getClass());
160         assertEquals("Correct URL", "/WEB-INF/example1.jsp", ((InternalResourceView) view).getUrl());
161
162         view = vr.resolveViewName("example2", Locale.getDefault());
163         assertEquals("Correct view class", InternalResourceView.class, view.getClass());
164         assertEquals("Correct URL", "/WEB-INF/example2.jsp", ((InternalResourceView) view).getUrl());
165
166         view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
167         assertEquals("Correct view class", RedirectView.class, view.getClass());
168         assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl());
169
170         view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
171         assertEquals("Correct view class", InternalResourceView.class, view.getClass());
172         assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
173     }
174
175     public void testInternalResourceViewResolverWithAttributes() throws Exception JavaDoc {
176         MockServletContext sc = new MockServletContext();
177         StaticWebApplicationContext wac = new StaticWebApplicationContext();
178         wac.setServletContext(sc);
179         wac.refresh();
180         InternalResourceViewResolver vr = new InternalResourceViewResolver();
181         Properties JavaDoc props = new Properties JavaDoc();
182         props.setProperty("key1", "value1");
183         vr.setAttributes(props);
184         Map JavaDoc map = new HashMap JavaDoc();
185         map.put("key2", new Integer JavaDoc(2));
186         vr.setAttributesMap(map);
187         vr.setApplicationContext(wac);
188
189         View view = vr.resolveViewName("example1", Locale.getDefault());
190         assertEquals("Correct view class", InternalResourceView.class, view.getClass());
191         assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
192         Map JavaDoc attributes = ((InternalResourceView) view).getStaticAttributes();
193         assertEquals("value1", attributes.get("key1"));
194         assertEquals(new Integer JavaDoc(2), attributes.get("key2"));
195
196         view = vr.resolveViewName("example2", Locale.getDefault());
197         assertEquals("Correct view class", InternalResourceView.class, view.getClass());
198         assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
199         attributes = ((InternalResourceView) view).getStaticAttributes();
200         assertEquals("value1", attributes.get("key1"));
201         assertEquals(new Integer JavaDoc(2), attributes.get("key2"));
202
203         MockHttpServletRequest request = new MockHttpServletRequest(sc);
204         HttpServletResponse JavaDoc response = new MockHttpServletResponse();
205         request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
206         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
207         Map JavaDoc model = new HashMap JavaDoc();
208         TestBean tb = new TestBean();
209         model.put("tb", tb);
210         view.render(model, request, response);
211
212         assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
213         assertTrue("Correct rc attribute", request.getAttribute("rc") == null);
214         assertEquals("value1", request.getAttribute("key1"));
215         assertEquals(new Integer JavaDoc(2), request.getAttribute("key2"));
216     }
217
218     public void testInternalResourceViewResolverWithJstl() throws Exception JavaDoc {
219         Locale JavaDoc locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.ENGLISH;
220
221         MockServletContext sc = new MockServletContext();
222         StaticWebApplicationContext wac = new StaticWebApplicationContext();
223         wac.setServletContext(sc);
224         wac.addMessage("code1", locale, "messageX");
225         wac.refresh();
226         InternalResourceViewResolver vr = new InternalResourceViewResolver();
227         vr.setViewClass(JstlView.class);
228         vr.setApplicationContext(wac);
229
230         View view = vr.resolveViewName("example1", Locale.getDefault());
231         assertEquals("Correct view class", JstlView.class, view.getClass());
232         assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());
233
234         view = vr.resolveViewName("example2", Locale.getDefault());
235         assertEquals("Correct view class", JstlView.class, view.getClass());
236         assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());
237
238         MockHttpServletRequest request = new MockHttpServletRequest(sc);
239         request.addPreferredLocale(locale);
240         HttpServletResponse JavaDoc response = new MockHttpServletResponse();
241         request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
242         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
243         Map JavaDoc model = new HashMap JavaDoc();
244         TestBean tb = new TestBean();
245         model.put("tb", tb);
246         view.render(model, request, response);
247
248         assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
249         assertTrue("Correct rc attribute", request.getAttribute("rc") == null);
250         assertTrue("Correct JSTL attributes",
251                 request.getAttribute(Config.FMT_LOCALIZATION_CONTEXT) instanceof LocalizationContext);
252         assertTrue("Correct JSTL attributes",
253                 locale.equals(request.getAttribute(Config.FMT_LOCALE)));
254         assertTrue("Correct JSTL attributes",
255                 request.getAttribute(Config.FMT_LOCALIZATION_CONTEXT + JstlUtils.REQUEST_SCOPE_SUFFIX) instanceof LocalizationContext);
256         assertTrue("Correct JSTL attributes",
257                 locale.equals(request.getAttribute(Config.FMT_LOCALE + JstlUtils.REQUEST_SCOPE_SUFFIX)));
258
259         LocalizationContext lc = (LocalizationContext) request.getAttribute(Config.FMT_LOCALIZATION_CONTEXT);
260         assertEquals("messageX", lc.getResourceBundle().getString("code1"));
261     }
262
263     public void testInternalResourceViewResolverWithJstlAndContextParam() throws Exception JavaDoc {
264         Locale JavaDoc locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.ENGLISH;
265
266         MockServletContext sc = new MockServletContext();
267         sc.addInitParameter(Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/context/WEB-INF/context-messages");
268         StaticWebApplicationContext wac = new StaticWebApplicationContext();
269         wac.setServletContext(sc);
270         wac.addMessage("code1", locale, "messageX");
271         wac.refresh();
272         InternalResourceViewResolver vr = new InternalResourceViewResolver();
273         vr.setViewClass(JstlView.class);
274         vr.setApplicationContext(wac);
275
276         View view = vr.resolveViewName("example1", Locale.getDefault());
277         assertEquals("Correct view class", JstlView.class, view.getClass());
278         assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());
279
280         view = vr.resolveViewName("example2", Locale.getDefault());
281         assertEquals("Correct view class", JstlView.class, view.getClass());
282         assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());
283
284         MockHttpServletRequest request = new MockHttpServletRequest(sc);
285         request.addPreferredLocale(locale);
286         HttpServletResponse JavaDoc response = new MockHttpServletResponse();
287         request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
288         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
289         Map JavaDoc model = new HashMap JavaDoc();
290         TestBean tb = new TestBean();
291         model.put("tb", tb);
292         view.render(model, request, response);
293
294         assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
295         assertTrue("Correct rc attribute", request.getAttribute("rc") == null);
296         assertTrue("Correct JSTL attributes",
297                 request.getAttribute(Config.FMT_LOCALIZATION_CONTEXT) instanceof LocalizationContext);
298         assertTrue("Correct JSTL attributes",
299                 locale.equals(request.getAttribute(Config.FMT_LOCALE)));
300         assertTrue("Correct JSTL attributes",
301                 request.getAttribute(Config.FMT_LOCALIZATION_CONTEXT + JstlUtils.REQUEST_SCOPE_SUFFIX) instanceof LocalizationContext);
302         assertTrue("Correct JSTL attributes",
303                 locale.equals(request.getAttribute(Config.FMT_LOCALE + JstlUtils.REQUEST_SCOPE_SUFFIX)));
304
305         LocalizationContext lc = (LocalizationContext) request.getAttribute(Config.FMT_LOCALIZATION_CONTEXT);
306         assertEquals("message1", lc.getResourceBundle().getString("code1"));
307         assertEquals("message2", lc.getResourceBundle().getString("code2"));
308     }
309
310     public void testXmlViewResolver() throws Exception JavaDoc {
311         StaticWebApplicationContext wac = new StaticWebApplicationContext();
312         wac.registerSingleton("testBean", TestBean.class);
313         wac.setServletContext(new MockServletContext());
314         wac.refresh();
315         TestBean testBean = (TestBean) wac.getBean("testBean");
316         XmlViewResolver vr = new XmlViewResolver();
317         vr.setLocation(new ClassPathResource("org/springframework/web/servlet/view/views.xml"));
318         vr.setApplicationContext(wac);
319
320         View view1 = vr.resolveViewName("example1", Locale.getDefault());
321         assertTrue("Correct view class", TestView.class.equals(view1.getClass()));
322         assertTrue("Correct URL", "/example1.jsp".equals(((InternalResourceView) view1).getUrl()));
323
324         View view2 = vr.resolveViewName("example2", Locale.getDefault());
325         assertTrue("Correct view class", JstlView.class.equals(view2.getClass()));
326         assertTrue("Correct URL", "/example2new.jsp".equals(((InternalResourceView) view2).getUrl()));
327
328         ServletContext JavaDoc sc = new MockServletContext();
329         Map JavaDoc model = new HashMap JavaDoc();
330         TestBean tb = new TestBean();
331         model.put("tb", tb);
332
333         HttpServletRequest JavaDoc request = new MockHttpServletRequest(sc);
334         HttpServletResponse JavaDoc response = new MockHttpServletResponse();
335         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
336         request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
337         view1.render(model, request, response);
338         assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
339         assertTrue("Correct test1 attribute", "testvalue1".equals(request.getAttribute("test1")));
340         assertTrue("Correct test2 attribute", testBean.equals(request.getAttribute("test2")));
341
342         request = new MockHttpServletRequest(sc);
343         response = new MockHttpServletResponse();
344         request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
345         request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
346         view2.render(model, request, response);
347         assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
348         assertTrue("Correct test1 attribute", "testvalue1".equals(request.getAttribute("test1")));
349         assertTrue("Correct test2 attribute", "testvalue2".equals(request.getAttribute("test2")));
350     }
351
352     public void testXmlViewResolverDefaultLocation() {
353         StaticWebApplicationContext wac = new StaticWebApplicationContext() {
354             protected Resource getResourceByPath(String JavaDoc path) {
355                 assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path));
356                 return super.getResourceByPath(path);
357             }
358         };
359         wac.setServletContext(new MockServletContext());
360         wac.refresh();
361         XmlViewResolver vr = new XmlViewResolver();
362         try {
363             vr.setApplicationContext(wac);
364             fail("Should have thrown BeanDefinitionStoreException");
365         }
366         catch (BeanDefinitionStoreException ex) {
367             // expected
368
}
369     }
370
371     public void testXmlViewResolverWithoutCache() throws Exception JavaDoc {
372         StaticWebApplicationContext wac = new StaticWebApplicationContext() {
373             protected Resource getResourceByPath(String JavaDoc path) {
374                 assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path));
375                 return super.getResourceByPath(path);
376             }
377         };
378         wac.setServletContext(new MockServletContext());
379         wac.refresh();
380         XmlViewResolver vr = new XmlViewResolver();
381         vr.setCache(false);
382         try {
383             vr.setApplicationContext(wac);
384         }
385         catch (ApplicationContextException ex) {
386             fail("Should not have thrown ApplicationContextException: " + ex.getMessage());
387         }
388         try {
389             vr.resolveViewName("example1", Locale.getDefault());
390             fail("Should have thrown BeanDefinitionStoreException");
391         }
392         catch (BeanDefinitionStoreException ex) {
393             // expected
394
}
395     }
396
397     public void testCacheRemoval() throws Exception JavaDoc {
398         StaticWebApplicationContext wac = new StaticWebApplicationContext();
399         wac.setServletContext(new MockServletContext());
400         wac.refresh();
401         InternalResourceViewResolver vr = new InternalResourceViewResolver();
402         vr.setViewClass(JstlView.class);
403         vr.setApplicationContext(wac);
404     
405         View view = vr.resolveViewName("example1", Locale.getDefault());
406         View cached = vr.resolveViewName("example1", Locale.getDefault());
407         if (view != cached) {
408             fail("Caching doesn't work");
409         }
410         
411         vr.removeFromCache("example1", Locale.getDefault());
412         cached = vr.resolveViewName("example1", Locale.getDefault());
413         if (view == cached) {
414             // the chance of having the same reference (hashCode) twice if negligable).
415
fail("View wasn't removed from cache");
416         }
417     }
418
419
420     public static class TestView extends InternalResourceView {
421
422         public void setLocation(Resource location) {
423             if (!(location instanceof ServletContextResource)) {
424                 throw new IllegalArgumentException JavaDoc("Expecting ClassPathResource, not " + location.getClass().getName());
425             }
426         }
427     }
428
429 }
430
Popular Tags