View Javadoc

1   /*
2    * $Id: ServletTilesRequestContextTest.java 600665 2007-12-03 20:25:44Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.tiles.servlet.context;
23  
24  import java.io.IOException;
25  import java.util.Map;
26  
27  import javax.servlet.ServletContext;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.shale.test.mock.MockHttpServletRequest;
32  import org.apache.shale.test.mock.MockHttpServletResponse;
33  import org.apache.shale.test.mock.MockHttpSession;
34  import org.apache.shale.test.mock.MockServletContext;
35  import org.apache.tiles.TilesApplicationContext;
36  import org.apache.tiles.context.TilesRequestContext;
37  
38  import junit.framework.TestCase;
39  
40  /***
41   * @version $Rev: 600665 $ $Date: 2007-12-03 21:25:44 +0100 (Mon, 03 Dec 2007) $
42   */
43  public class ServletTilesRequestContextTest extends TestCase {
44  
45      /***
46       * Test path to check forward and include.
47       */
48      private static final String TEST_PATH = "testPath.jsp";
49  
50      /***
51       * The request context.
52       */
53      private TilesRequestContext context;
54  
55      /***
56       * The servlet context.
57       */
58      private MockServletContext servletContext;
59  
60      /*** {@inheritDoc} */
61      @Override
62      protected void setUp() throws Exception {
63          super.setUp();
64          servletContext = new MockServletContext();
65          servletContext
66                  .addInitParameter("initParameter1", "initParameterValue1");
67          MockHttpSession session = new MockHttpSession(servletContext);
68          MockHttpServletRequest request = new MockHttpServletRequest(session);
69          MockHttpServletResponse response = new MockHttpServletResponse();
70          request.addHeader("Content-Type", "text/html");
71          request.addParameter("myParam", "value1");
72          request.addParameter("myParam", "value2");
73  
74          context = new ServletTilesRequestContext(servletContext, request,
75                  response);
76  
77          Map<String, Object> requestScope = context.getRequestScope();
78          requestScope.put("attribute1", "value1");
79          requestScope.put("attribute2", "value2");
80  
81          Map<String, Object> sessionScope = context.getSessionScope();
82          sessionScope.put("sessionAttribute1", "sessionValue1");
83          sessionScope.put("sessionAttribute2", "sessionValue2");
84  
85          Map<String, Object> applicationScope = ((TilesApplicationContext) context)
86                  .getApplicationScope();
87          applicationScope.put("applicationAttribute1", "applicationValue1");
88          applicationScope.put("applicationAttribute2", "applicationValue2");
89      }
90  
91      /***
92       * Tests getting the header.
93       */
94      public void testGetHeader() {
95          Map<String, String> map = context.getHeader();
96          assertTrue("The header does not contain a set value", "text/html"
97                  .equals(map.get("Content-Type")));
98          doTestReadMap(map, String.class, String.class, "header map");
99      }
100 
101     /***
102      * Tests getting the header value.
103      */
104     public void testGetHeaderValues() {
105         Map<String, String[]> map = context.getHeaderValues();
106         String[] array = map.get("Content-Type");
107         assertTrue("The header does not contain a set value", array.length == 1
108                 && "text/html".equals(array[0]));
109         doTestReadMap(map, String.class, String[].class, "header values map");
110     }
111 
112     /***
113      * Tests getting the parameters.
114      */
115     public void testGetParam() {
116         Map<String, String> map = context.getParam();
117         assertTrue("The parameters do not contain a set value", "value1"
118                 .equals(map.get("myParam"))
119                 || "value2".equals(map.get("myParam")));
120         doTestReadMap(map, String.class, String.class, "parameter map");
121     }
122 
123     /***
124      * Tests getting the parameter values.
125      */
126     public void testGetParamValues() {
127         Map<String, String[]> map = context.getParamValues();
128         String[] array = map.get("myParam");
129         assertTrue(
130                 "The parameters not contain a set value",
131                 array.length == 2
132                         && (("value1".equals(array[0]) && "value2"
133                                 .equals(array[1])) || ("value1"
134                                 .equals(array[1]) && "value2".equals(array[0]))));
135         doTestReadMap(map, String.class, String[].class, "parameter values map");
136     }
137 
138     /***
139      * Tests getting request scope attributes.
140      */
141     public void testGetRequestScope() {
142         Map<String, Object> map = context.getRequestScope();
143         assertTrue("The request scope does not contain a set value", "value1"
144                 .equals(map.get("attribute1")));
145         assertTrue("The request scope does not contain a set value", "value2"
146                 .equals(map.get("attribute2")));
147         doTestReadMap(map, String.class, Object.class, "request scope map");
148     }
149 
150     /***
151      * Tests getting session scope attributes.
152      */
153     public void testGetSessionScope() {
154         Map<String, Object> map = context.getSessionScope();
155         assertTrue("The session scope does not contain a set value",
156                 "sessionValue1".equals(map.get("sessionAttribute1")));
157         assertTrue("The session scope does not contain a set value",
158                 "sessionValue2".equals(map.get("sessionAttribute2")));
159         doTestReadMap(map, String.class, Object.class, "session scope map");
160     }
161 
162     /***
163      * Tests getting application scope attributes.
164      */
165     public void testGetApplicationScope() {
166         Map<String, Object> map = ((TilesApplicationContext) context)
167                 .getApplicationScope();
168         assertTrue("The application scope does not contain a set value",
169                 "applicationValue1".equals(map.get("applicationAttribute1")));
170         assertTrue("The application scope does not contain a set value",
171                 "applicationValue2".equals(map.get("applicationAttribute2")));
172         doTestReadMap(map, String.class, Object.class, "application scope map");
173     }
174 
175     /***
176      * Tests getting init parameters.
177      */
178     public void testGetInitParams() {
179         Map<String, String> map = ((TilesApplicationContext) context)
180                 .getInitParams();
181         assertTrue("The init parameters do not contain a set value",
182                 "initParameterValue1".equals(map.get("initParameter1")));
183         doTestReadMap(map, String.class, String.class,
184                 "init parameters scope map");
185     }
186 
187     /***
188      * Tests the forced inclusion in the request.
189      *
190      * @throws IOException If something goes wrong.
191      */
192     public void testForceInclude() throws IOException {
193         MockHttpServletRequest request = new MockHttpServletRequest();
194         MockHttpServletResponse response = new CommitSupportMockHttpServletResponse();
195         MockServletTilesRequestContext context = new MockServletTilesRequestContext(
196                 servletContext, request, response);
197         context.dispatch(TEST_PATH);
198         assertEquals("Forward has not been called", 1, context.getForwardCount());
199         assertEquals("Include has been called", 0, context.getIncludeCount());
200         assertFalse("Force include has been incorrectly set.", ServletUtil
201                 .isForceInclude(request));
202         ServletUtil.setForceInclude(request, true);
203         context.dispatch(TEST_PATH);
204         assertEquals("Forward has been called", 1, context.getForwardCount());
205         assertEquals("Include has not been called", 1, context.getIncludeCount());
206     }
207 
208     /***
209      * Tests a generic map.
210      *
211      * @param <K> The key type.
212      * @param <V> The value type.
213      * @param currentMap The map to check.
214      * @param keyClass The key class.
215      * @param valueClass The value class.
216      * @param mapName The name of the map to test (for messages).
217      */
218     private <K, V> void doTestReadMap(Map<K, V> currentMap, Class<K> keyClass,
219             Class<V> valueClass, String mapName) {
220         int size1, size2;
221         size1 = currentMap.keySet().size();
222         size2 = currentMap.entrySet().size();
223         assertEquals("The map" + mapName
224                 + " has keySet and entrySet of different size", size1, size2);
225         for (K key : currentMap.keySet()) {
226             assertTrue("The key is not of class" + keyClass.getName(), keyClass
227                     .isInstance(key));
228             V value = currentMap.get(key);
229             assertTrue("The value is not of class" + valueClass.getName(),
230                     valueClass.isInstance(value));
231             assertTrue("The map " + mapName
232                     + " does not return the correct value for 'containsValue'",
233                     currentMap.containsValue(value));
234         }
235     }
236 
237     /***
238      * Extends {@link MockHttpServletResponse} to override
239      * {@link MockHttpServletResponse#isCommitted()} method.
240      */
241     private static class CommitSupportMockHttpServletResponse extends
242             MockHttpServletResponse {
243 
244         /*** {@inheritDoc} */
245         @Override
246         public boolean isCommitted() {
247             return false;
248         }
249     }
250 
251     /***
252      * Extends {@link ServletTilesRequestContext} to check forward and include.
253      */
254     private static class MockServletTilesRequestContext extends
255             ServletTilesRequestContext {
256 
257         /***
258          * The number of times that forward has been called.
259          */
260         private int forwardCount = 0;
261 
262         /***
263          * The number of times that include has been called.
264          */
265         private int includeCount = 0;
266 
267         /***
268          * Constructor.
269          *
270          * @param servletContext The servlet context.
271          * @param request The request.
272          * @param response The response.
273          */
274         public MockServletTilesRequestContext(ServletContext servletContext,
275                 HttpServletRequest request, HttpServletResponse response) {
276             super(servletContext, request, response);
277         }
278 
279         /*** {@inheritDoc} */
280         @Override
281         protected void forward(String path) throws IOException {
282             forwardCount++;
283         }
284 
285         /*** {@inheritDoc} */
286         @Override
287         public void include(String path) throws IOException {
288             includeCount++;
289         }
290 
291         /***
292          * Returns the forward count.
293          *
294          * @return The forward count.
295          */
296         public int getForwardCount() {
297             return forwardCount;
298         }
299 
300         /***
301          * Returns the include count.
302          *
303          * @return The include count.
304          */
305         public int getIncludeCount() {
306             return includeCount;
307         }
308     }
309 }