1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.tiles.servlet.context;
23
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import javax.servlet.ServletOutputStream;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import junit.framework.TestCase;
34
35 import org.apache.shale.test.mock.MockHttpServletRequest;
36 import org.apache.shale.test.mock.MockHttpServletResponse;
37 import org.apache.shale.test.mock.MockHttpSession;
38 import org.apache.shale.test.mock.MockServletContext;
39 import org.apache.tiles.TilesApplicationContext;
40 import org.apache.tiles.context.TilesRequestContext;
41 import org.easymock.classextension.EasyMock;
42
43 /***
44 * @version $Rev: 736275 $ $Date: 2009-01-21 10:58:20 +0100 (mer, 21 gen 2009) $
45 */
46 public class ServletTilesRequestContextTest extends TestCase {
47
48 /***
49 * Test path to check forward and include.
50 */
51 private static final String TEST_PATH = "testPath.jsp";
52
53 /***
54 * The request context.
55 */
56 private TilesRequestContext context;
57
58 /***
59 * The servlet context.
60 */
61 private MockServletContext servletContext;
62
63 /***
64 * The Tiles application context.
65 */
66 private TilesApplicationContext applicationContext;
67
68 /*** {@inheritDoc} */
69 @Override
70 protected void setUp() throws Exception {
71 super.setUp();
72 servletContext = new MockServletContext();
73 applicationContext = EasyMock.createMock(TilesApplicationContext.class);
74 Map<String, Object> applicationScope = new HashMap<String, Object>();
75 applicationScope.put("applicationAttribute1", "applicationValue1");
76 applicationScope.put("applicationAttribute2", "applicationValue2");
77 EasyMock.expect(applicationContext.getApplicationScope()).andReturn(
78 applicationScope);
79 Map<String, String> initParams = new HashMap<String, String>();
80 initParams.put("initParameter1", "initParameterValue1");
81 EasyMock.expect(applicationContext.getInitParams()).andReturn(
82 initParams);
83 MockHttpSession session = new MockHttpSession(servletContext);
84 MockHttpServletRequest request = new MockHttpServletRequest(session);
85 MockHttpServletResponse response = new MockHttpServletResponse();
86 request.addHeader("Content-Type", "text/html");
87 request.addParameter("myParam", "value1");
88 request.addParameter("myParam", "value2");
89
90 context = new ServletTilesRequestContext(applicationContext, request,
91 response);
92
93 Map<String, Object> requestScope = context.getRequestScope();
94 requestScope.put("attribute1", "value1");
95 requestScope.put("attribute2", "value2");
96
97 Map<String, Object> sessionScope = context.getSessionScope();
98 sessionScope.put("sessionAttribute1", "sessionValue1");
99 sessionScope.put("sessionAttribute2", "sessionValue2");
100 EasyMock.replay(applicationContext);
101 }
102
103 /***
104 * Tests getting the header.
105 */
106 public void testGetHeader() {
107 Map<String, String> map = context.getHeader();
108 assertTrue("The header does not contain a set value", "text/html"
109 .equals(map.get("Content-Type")));
110 doTestReadMap(map, String.class, String.class, "header map");
111 }
112
113 /***
114 * Tests getting the header value.
115 */
116 public void testGetHeaderValues() {
117 Map<String, String[]> map = context.getHeaderValues();
118 String[] array = map.get("Content-Type");
119 assertTrue("The header does not contain a set value", array.length == 1
120 && "text/html".equals(array[0]));
121 doTestReadMap(map, String.class, String[].class, "header values map");
122 }
123
124 /***
125 * Tests getting the parameters.
126 */
127 public void testGetParam() {
128 Map<String, String> map = context.getParam();
129 assertTrue("The parameters do not contain a set value", "value1"
130 .equals(map.get("myParam"))
131 || "value2".equals(map.get("myParam")));
132 doTestReadMap(map, String.class, String.class, "parameter map");
133 }
134
135 /***
136 * Tests getting the parameter values.
137 */
138 public void testGetParamValues() {
139 Map<String, String[]> map = context.getParamValues();
140 String[] array = map.get("myParam");
141 assertTrue(
142 "The parameters not contain a set value",
143 array.length == 2
144 && (("value1".equals(array[0]) && "value2"
145 .equals(array[1])) || ("value1"
146 .equals(array[1]) && "value2".equals(array[0]))));
147 doTestReadMap(map, String.class, String[].class, "parameter values map");
148 }
149
150 /***
151 * Tests getting request scope attributes.
152 */
153 public void testGetRequestScope() {
154 Map<String, Object> map = context.getRequestScope();
155 assertTrue("The request scope does not contain a set value", "value1"
156 .equals(map.get("attribute1")));
157 assertTrue("The request scope does not contain a set value", "value2"
158 .equals(map.get("attribute2")));
159 doTestReadMap(map, String.class, Object.class, "request scope map");
160 }
161
162 /***
163 * Tests getting session scope attributes.
164 */
165 public void testGetSessionScope() {
166 Map<String, Object> map = context.getSessionScope();
167 assertTrue("The session scope does not contain a set value",
168 "sessionValue1".equals(map.get("sessionAttribute1")));
169 assertTrue("The session scope does not contain a set value",
170 "sessionValue2".equals(map.get("sessionAttribute2")));
171 doTestReadMap(map, String.class, Object.class, "session scope map");
172 }
173
174 /***
175 * Tests {@link ServletTilesRequestContext#getApplicationContext()}.
176 */
177 public void testGetApplicationContext() {
178 assertTrue("The objects are not the same",
179 applicationContext == context.getApplicationContext());
180 }
181
182 /***
183 * Tests getting application scope attributes.
184 */
185 public void testGetApplicationScope() {
186 Map<String, Object> map = ((TilesApplicationContext) context)
187 .getApplicationScope();
188 assertTrue("The application scope does not contain a set value",
189 "applicationValue1".equals(map.get("applicationAttribute1")));
190 assertTrue("The application scope does not contain a set value",
191 "applicationValue2".equals(map.get("applicationAttribute2")));
192 doTestReadMap(map, String.class, Object.class, "application scope map");
193 }
194
195 /***
196 * Tests getting init parameters.
197 */
198 public void testGetInitParams() {
199 Map<String, String> map = ((TilesApplicationContext) context)
200 .getInitParams();
201 assertTrue("The init parameters do not contain a set value",
202 "initParameterValue1".equals(map.get("initParameter1")));
203 doTestReadMap(map, String.class, String.class,
204 "init parameters scope map");
205 }
206
207 /***
208 * Tests {@link ServletTilesRequestContext#getOutputStream()}.
209 *
210 * @throws IOException If something goes wrong.
211 */
212 public void testGetOutputStream() throws IOException {
213 HttpServletRequest request = EasyMock
214 .createMock(HttpServletRequest.class);
215 HttpServletResponse response = EasyMock
216 .createMock(HttpServletResponse.class);
217 TilesApplicationContext applicationContext = EasyMock
218 .createMock(TilesApplicationContext.class);
219 ServletOutputStream os = EasyMock.createMock(ServletOutputStream.class);
220 EasyMock.expect(response.getOutputStream()).andReturn(os);
221 EasyMock.replay(request, response, applicationContext, os);
222 ServletTilesRequestContext requestContext = new ServletTilesRequestContext(
223 applicationContext, request, response);
224 assertEquals(os, requestContext.getOutputStream());
225 EasyMock.verify(request, response, applicationContext, os);
226 }
227
228 /***
229 * Tests {@link ServletTilesRequestContext#getWriter()}.
230 *
231 * @throws IOException If something goes wrong.
232 */
233 public void testGetWriter() throws IOException {
234 HttpServletRequest request = EasyMock
235 .createMock(HttpServletRequest.class);
236 HttpServletResponse response = EasyMock
237 .createMock(HttpServletResponse.class);
238 TilesApplicationContext applicationContext = EasyMock
239 .createMock(TilesApplicationContext.class);
240 PrintWriter writer = EasyMock.createMock(PrintWriter.class);
241 EasyMock.expect(response.getWriter()).andReturn(writer);
242 EasyMock.replay(request, response, applicationContext, writer);
243 ServletTilesRequestContext requestContext = new ServletTilesRequestContext(
244 applicationContext, request, response);
245 assertEquals(writer, requestContext.getWriter());
246 EasyMock.verify(request, response, applicationContext, writer);
247 }
248
249 /***
250 * Tests {@link ServletTilesRequestContext#getPrintWriter()}.
251 *
252 * @throws IOException If something goes wrong.
253 */
254 public void testGetPrintWriter() throws IOException {
255 HttpServletRequest request = EasyMock
256 .createMock(HttpServletRequest.class);
257 HttpServletResponse response = EasyMock
258 .createMock(HttpServletResponse.class);
259 TilesApplicationContext applicationContext = EasyMock
260 .createMock(TilesApplicationContext.class);
261 PrintWriter writer = EasyMock.createMock(PrintWriter.class);
262 EasyMock.expect(response.getWriter()).andReturn(writer);
263 EasyMock.replay(request, response, applicationContext, writer);
264 ServletTilesRequestContext requestContext = new ServletTilesRequestContext(
265 applicationContext, request, response);
266 assertEquals(writer, requestContext.getPrintWriter());
267 EasyMock.verify(request, response, applicationContext, writer);
268 }
269
270 /***
271 * Tests the forced inclusion in the request.
272 *
273 * @throws IOException If something goes wrong.
274 */
275 public void testForceInclude() throws IOException {
276 MockHttpServletRequest request = new MockHttpServletRequest();
277 MockHttpServletResponse response = new CommitSupportMockHttpServletResponse();
278 MockServletTilesRequestContext context = new MockServletTilesRequestContext(
279 applicationContext, request, response);
280 context.dispatch(TEST_PATH);
281 assertEquals("Forward has not been called", 1, context.getForwardCount());
282 assertEquals("Include has been called", 0, context.getIncludeCount());
283 assertFalse("Force include has been incorrectly set.", ServletUtil
284 .isForceInclude(request));
285 ServletUtil.setForceInclude(request, true);
286 context.dispatch(TEST_PATH);
287 assertEquals("Forward has been called", 1, context.getForwardCount());
288 assertEquals("Include has not been called", 1, context.getIncludeCount());
289 }
290
291 /***
292 * Tests a generic map.
293 *
294 * @param <K> The key type.
295 * @param <V> The value type.
296 * @param currentMap The map to check.
297 * @param keyClass The key class.
298 * @param valueClass The value class.
299 * @param mapName The name of the map to test (for messages).
300 */
301 private <K, V> void doTestReadMap(Map<K, V> currentMap, Class<K> keyClass,
302 Class<V> valueClass, String mapName) {
303 int size1, size2;
304 size1 = currentMap.keySet().size();
305 size2 = currentMap.entrySet().size();
306 assertEquals("The map" + mapName
307 + " has keySet and entrySet of different size", size1, size2);
308 for (K key : currentMap.keySet()) {
309 assertTrue("The key is not of class" + keyClass.getName(), keyClass
310 .isInstance(key));
311 V value = currentMap.get(key);
312 assertTrue("The value is not of class" + valueClass.getName(),
313 valueClass.isInstance(value));
314 assertTrue("The map " + mapName
315 + " does not return the correct value for 'containsValue'",
316 currentMap.containsValue(value));
317 }
318 }
319
320 /***
321 * Extends {@link MockHttpServletResponse} to override
322 * {@link MockHttpServletResponse#isCommitted()} method.
323 */
324 private static class CommitSupportMockHttpServletResponse extends
325 MockHttpServletResponse {
326
327 /*** {@inheritDoc} */
328 @Override
329 public boolean isCommitted() {
330 return false;
331 }
332 }
333
334 /***
335 * Extends {@link ServletTilesRequestContext} to check forward and include.
336 */
337 private static class MockServletTilesRequestContext extends
338 ServletTilesRequestContext {
339
340 /***
341 * The number of times that forward has been called.
342 */
343 private int forwardCount = 0;
344
345 /***
346 * The number of times that include has been called.
347 */
348 private int includeCount = 0;
349
350 /***
351 * Constructor.
352 *
353 * @param applicationContext The Tiles application context.
354 * @param request The request.
355 * @param response The response.
356 */
357 public MockServletTilesRequestContext(
358 TilesApplicationContext applicationContext,
359 HttpServletRequest request, HttpServletResponse response) {
360 super(applicationContext, request, response);
361 }
362
363 /*** {@inheritDoc} */
364 @Override
365 protected void forward(String path) throws IOException {
366 forwardCount++;
367 }
368
369 /*** {@inheritDoc} */
370 @Override
371 public void include(String path) throws IOException {
372 includeCount++;
373 }
374
375 /***
376 * Returns the forward count.
377 *
378 * @return The forward count.
379 */
380 public int getForwardCount() {
381 return forwardCount;
382 }
383
384 /***
385 * Returns the include count.
386 *
387 * @return The include count.
388 */
389 public int getIncludeCount() {
390 return includeCount;
391 }
392 }
393 }