KickJava   Java API By Example, From Geeks To Geeks.

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


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.Locale JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.MissingResourceException JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.springframework.beans.factory.BeanIsAbstractException;
29 import org.springframework.core.io.Resource;
30 import org.springframework.mock.web.MockServletContext;
31 import org.springframework.web.context.support.ServletContextResource;
32 import org.springframework.web.context.support.StaticWebApplicationContext;
33 import org.springframework.web.servlet.View;
34
35 /**
36  * @author Rod Johnson
37  * @author Juergen Hoeller
38  */

39 public class ResourceBundleViewResolverTests extends TestCase {
40
41     /** Comes from this package */
42     private static String JavaDoc PROPS_FILE = "org.springframework.web.servlet.view.testviews";
43
44     private ResourceBundleViewResolver rb;
45     
46     private StaticWebApplicationContext wac;
47     
48     public ResourceBundleViewResolverTests() {
49         rb = new ResourceBundleViewResolver();
50         rb.setBasename(PROPS_FILE);
51         rb.setCache(getCache());
52         rb.setDefaultParentView("testParent");
53
54         wac = new StaticWebApplicationContext();
55         wac.setServletContext(new MockServletContext());
56         wac.refresh();
57
58         // This will be propagated to views, so we need it
59
rb.setApplicationContext(wac);
60     }
61
62     /**
63      * Not a constant: allows overrides.
64      * Controls whether to cache views.
65      */

66     protected boolean getCache() {
67         return true;
68     }
69
70     public void testParentsAreAbstract() throws Exception JavaDoc {
71         try {
72             View v = rb.resolveViewName("debug.Parent", Locale.ENGLISH);
73             fail("Should have thrown BeanIsAbstractException");
74         }
75         catch (BeanIsAbstractException ex) {
76             // expected
77
}
78         try {
79             View v = rb.resolveViewName("testParent", Locale.ENGLISH);
80             fail("Should have thrown BeanIsAbstractException");
81         }
82         catch (BeanIsAbstractException ex) {
83             // expected
84
}
85     }
86
87     public void testDebugViewEnglish() throws Exception JavaDoc {
88         View v = rb.resolveViewName("debugView", Locale.ENGLISH);
89         assertTrue("debugView must be of type InternalResourceView", v instanceof InternalResourceView);
90         InternalResourceView jv = (InternalResourceView) v;
91         assertTrue("debugView must have correct URL", "jsp/debug/debug.jsp".equals(jv.getUrl()));
92
93         Map JavaDoc m = jv.getStaticAttributes();
94         assertTrue("Must have 2 static attributes, not " + m.size(), m.size() == 2);
95         assertTrue("attribute foo = bar, not '" + m.get("foo") + "'", m.get("foo").equals("bar"));
96         assertTrue("attribute postcode = SE10 9JY", m.get("postcode").equals("SE10 9JY"));
97
98         assertTrue("Correct default content type", jv.getContentType().equals("text/html; charset=ISO-8859-1"));
99         assertTrue("WebAppContext was set on view", jv.getApplicationContext() != null);
100         assertTrue("WebAppContext was correct", jv.getApplicationContext().getParent().equals(wac));
101     }
102
103     public void testDebugViewFrench() throws Exception JavaDoc {
104         View v = rb.resolveViewName("debugView", Locale.FRENCH);
105         assertTrue("French debugView must be of type InternalResourceView", v instanceof InternalResourceView);
106         InternalResourceView jv = (InternalResourceView) v;
107         assertTrue("French debugView must have correct URL", "jsp/debug/deboug.jsp".equals(jv.getUrl()));
108         assertTrue(
109             "Correct overridden (XML) content type, not '" + jv.getContentType() + "'",
110             jv.getContentType().equals("text/xml; charset=ISO-8859-1"));
111     }
112
113     public void testNoSuchViewEnglish() throws Exception JavaDoc {
114         View v = rb.resolveViewName("xxxxxxweorqiwuopeir", Locale.ENGLISH);
115         assertTrue(v == null);
116     }
117
118     public void testOnSetContextCalledOnce() throws Exception JavaDoc {
119         TestView tv = (TestView) rb.resolveViewName("test", Locale.ENGLISH);
120         tv = (TestView) rb.resolveViewName("test", Locale.ENGLISH);
121         tv = (TestView) rb.resolveViewName("test", Locale.ENGLISH);
122         assertTrue("test has correct name", "test".equals(tv.getBeanName()));
123         assertTrue("test should have been initialized once, not " + tv.initCount + " times", tv.initCount == 1);
124     }
125     
126     public void testNoSuchBasename() throws Exception JavaDoc {
127         try {
128             rb.setBasename("weoriwoierqupowiuer");
129             View v = rb.resolveViewName("debugView", Locale.ENGLISH);
130             fail("No such basename: all requests should fail with exception");
131         }
132         catch (MissingResourceException JavaDoc ex) {
133             // OK
134
}
135     }
136
137
138     public static class TestView extends AbstractView {
139
140         public int initCount;
141
142         public void setLocation(Resource location) {
143             if (!(location instanceof ServletContextResource)) {
144                 throw new IllegalArgumentException JavaDoc("Expecting ServletContextResource, not " + location.getClass().getName());
145             }
146         }
147
148         protected void renderMergedOutputModel(Map JavaDoc model, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
149         }
150
151         protected void initApplicationContext() {
152             ++initCount;
153         }
154     }
155
156 }
157
Popular Tags