KickJava   Java API By Example, From Geeks To Geeks.

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


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

17
18 package org.springframework.web.servlet.view;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import junit.framework.TestCase;
27 import org.easymock.MockControl;
28
29 import org.springframework.mock.web.MockHttpServletResponse;
30 import org.springframework.mock.web.MockRequestDispatcher;
31 import org.springframework.web.context.WebApplicationContext;
32 import org.springframework.web.util.UrlPathHelper;
33
34 /**
35  * @author Rod Johnson
36  * @author Juergen Hoeller
37  */

38 public class InternalResourceViewTests extends TestCase {
39
40     /**
41      * Test that if the url property isn't supplied, view initialization fails.
42      */

43     public void testRejectsNullUrl() throws Exception JavaDoc {
44         InternalResourceView view = new InternalResourceView();
45         try {
46             view.afterPropertiesSet();
47             fail("Should be forced to set URL");
48         }
49         catch (IllegalArgumentException JavaDoc ex) {
50             // expected
51
}
52     }
53     
54     public void testForward() throws Exception JavaDoc {
55         HashMap JavaDoc model = new HashMap JavaDoc();
56         Object JavaDoc obj = new Integer JavaDoc(1);
57         model.put("foo", "bar");
58         model.put("I", obj);
59
60         MockControl wacControl = MockControl.createControl(WebApplicationContext.class);
61         WebApplicationContext wac = (WebApplicationContext) wacControl.getMock();
62         wacControl.replay();
63         
64         String JavaDoc url = "forward-to";
65         
66         MockControl reqControl = MockControl.createControl(HttpServletRequest JavaDoc.class);
67         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) reqControl.getMock();
68         Set JavaDoc keys = model.keySet();
69         for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
70             String JavaDoc key = (String JavaDoc) iter.next();
71             request.setAttribute(key, model.get(key));
72             reqControl.setVoidCallable(1);
73         }
74
75         request.getAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE);
76         reqControl.setReturnValue(null);
77         request.getRequestDispatcher(url);
78         reqControl.setReturnValue(new MockRequestDispatcher(url));
79         reqControl.replay();
80         
81         MockHttpServletResponse response = new MockHttpServletResponse();
82         InternalResourceView v = new InternalResourceView();
83         v.setUrl(url);
84         v.setApplicationContext(wac);
85         
86         // Can now try multiple tests
87
v.render(model, request, response);
88         assertEquals(url, response.getForwardedUrl());
89         wacControl.verify();
90         reqControl.verify();
91     }
92     
93     public void testIncludeOnAttribute() throws Exception JavaDoc {
94         HashMap JavaDoc model = new HashMap JavaDoc();
95         Object JavaDoc obj = new Integer JavaDoc(1);
96         model.put("foo", "bar");
97         model.put("I", obj);
98
99         MockControl wacControl = MockControl.createControl(WebApplicationContext.class);
100         WebApplicationContext wac = (WebApplicationContext) wacControl.getMock();
101         wacControl.replay();
102
103         String JavaDoc url = "forward-to";
104
105         MockControl reqControl = MockControl.createControl(HttpServletRequest JavaDoc.class);
106         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) reqControl.getMock();
107         Set JavaDoc keys = model.keySet();
108         for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
109             String JavaDoc key = (String JavaDoc) iter.next();
110             request.setAttribute(key, model.get(key));
111             reqControl.setVoidCallable(1);
112         }
113
114         request.getAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE);
115         reqControl.setReturnValue("somepath");
116         request.getRequestDispatcher(url);
117         reqControl.setReturnValue(new MockRequestDispatcher(url));
118         reqControl.replay();
119
120         MockHttpServletResponse response = new MockHttpServletResponse();
121         InternalResourceView v = new InternalResourceView();
122         v.setUrl(url);
123         v.setApplicationContext(wac);
124
125         // Can now try multiple tests
126
v.render(model, request, response);
127         assertEquals(url, response.getIncludedUrl());
128         wacControl.verify();
129         reqControl.verify();
130     }
131
132     public void testIncludeOnCommitted() throws Exception JavaDoc {
133         HashMap JavaDoc model = new HashMap JavaDoc();
134         Object JavaDoc obj = new Integer JavaDoc(1);
135         model.put("foo", "bar");
136         model.put("I", obj);
137
138         MockControl wacControl = MockControl.createControl(WebApplicationContext.class);
139         WebApplicationContext wac = (WebApplicationContext) wacControl.getMock();
140         wacControl.replay();
141
142         String JavaDoc url = "forward-to";
143
144         MockControl reqControl = MockControl.createControl(HttpServletRequest JavaDoc.class);
145         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) reqControl.getMock();
146         Set JavaDoc keys = model.keySet();
147         for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
148             String JavaDoc key = (String JavaDoc) iter.next();
149             request.setAttribute(key, model.get(key));
150             reqControl.setVoidCallable(1);
151         }
152
153         request.getAttribute(UrlPathHelper.INCLUDE_URI_REQUEST_ATTRIBUTE);
154         reqControl.setReturnValue(null);
155         request.getRequestDispatcher(url);
156         reqControl.setReturnValue(new MockRequestDispatcher(url));
157         reqControl.replay();
158
159         MockHttpServletResponse response = new MockHttpServletResponse();
160         response.setCommitted(true);
161         InternalResourceView v = new InternalResourceView();
162         v.setUrl(url);
163         v.setApplicationContext(wac);
164
165         // Can now try multiple tests
166
v.render(model, request, response);
167         assertEquals(url, response.getIncludedUrl());
168         wacControl.verify();
169         reqControl.verify();
170     }
171
172 }
173
Popular Tags