001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.testsuite.test;
018:
019: import java.io.IOException;
020:
021: import javax.portlet.PortletContext;
022: import javax.portlet.PortletException;
023: import javax.portlet.PortletRequest;
024: import javax.portlet.PortletRequestDispatcher;
025: import javax.portlet.PortletResponse;
026: import javax.portlet.RenderRequest;
027: import javax.portlet.RenderResponse;
028: import javax.servlet.ServletException;
029: import javax.servlet.http.HttpServlet;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.apache.pluto.testsuite.TestResult;
036: import org.apache.pluto.testsuite.TestUtils;
037:
038: public class DispatcherRequestTest extends
039: AbstractReflectivePortletTest {
040:
041: /** Logger. */
042: private static final Log LOG = LogFactory
043: .getLog(DispatcherRequestTest.class);
044:
045: /** The path to the companion servlet. */
046: private static final String SERVLET_PATH = "/test/DispatcherRequestTest_Servlet";
047:
048: private static final String CHECK_PATH_INFO = "/checkPathInfo";
049: private static final String CHECK_REQUEST_URI = "/checkRequestURI";
050: private static final String CHECK_CONTEXT_PATH = "/checkContextPath";
051: private static final String CHECK_SERVLET_PATH = "/checkServletPath";
052: private static final String CHECK_QUERY_STRING = "/checkQueryString";
053:
054: private static final String GET_REQUEST_URI = "/getRequestURI";
055: private static final String GET_CONTEXT_PATH = "/getContextPath";
056: private static final String GET_SERVLET_PATH = "/getServletPath";
057: private static final String GET_QUERY_STRING = "/getQueryString";
058:
059: /** The query string appended to the dispatch URI. */
060: private static final String QUERY_STRING = "paramName=paramValue";
061:
062: /** The request attribute key which associates the test result. */
063: private static final String RESULT_KEY = DispatcherRequestTest.class
064: .getName()
065: + ".RESULT_KEY";
066:
067: /** The key for the expected request URI. */
068: private static final String EXPECTED_REQUEST_URI = DispatcherRequestTest.class
069: .getName()
070: + ".REQUEST_URI";
071:
072: /** The key for the expected context path. */
073: private static final String EXPECTED_CONTEXT_PATH = DispatcherRequestTest.class
074: .getName()
075: + ".CONTEXT_PATH";
076:
077: // Test Methods ------------------------------------------------------------
078:
079: protected TestResult checkPathInfo(PortletContext context,
080: PortletRequest request, PortletResponse response)
081: throws IOException, PortletException {
082: return doCheckIncludedAttribute(context, request, response,
083: CHECK_PATH_INFO);
084: }
085:
086: protected TestResult checkRequestURI(PortletContext context,
087: PortletRequest request, PortletResponse response)
088: throws IOException, PortletException {
089: return doCheckIncludedAttribute(context, request, response,
090: CHECK_REQUEST_URI);
091: }
092:
093: protected TestResult checkGetRequestURI(PortletContext context,
094: PortletRequest request, PortletResponse response)
095: throws IOException, PortletException {
096: return doCheckIncludedAttribute(context, request, response,
097: GET_REQUEST_URI);
098: }
099:
100: protected TestResult checkContextPath(PortletContext context,
101: PortletRequest request, PortletResponse response)
102: throws IOException, PortletException {
103: return doCheckIncludedAttribute(context, request, response,
104: CHECK_CONTEXT_PATH);
105: }
106:
107: protected TestResult checkGetContextPath(PortletContext context,
108: PortletRequest request, PortletResponse response)
109: throws IOException, PortletException {
110: return doCheckIncludedAttribute(context, request, response,
111: GET_CONTEXT_PATH);
112: }
113:
114: protected TestResult checkServletPath(PortletContext context,
115: PortletRequest request, PortletResponse response)
116: throws IOException, PortletException {
117: return doCheckIncludedAttribute(context, request, response,
118: CHECK_SERVLET_PATH);
119: }
120:
121: protected TestResult checkGetServletPath(PortletContext context,
122: PortletRequest request, PortletResponse response)
123: throws IOException, PortletException {
124: return doCheckIncludedAttribute(context, request, response,
125: GET_SERVLET_PATH);
126: }
127:
128: protected TestResult checkQueryString(PortletContext context,
129: PortletRequest request, PortletResponse response)
130: throws IOException, PortletException {
131: return doCheckIncludedAttribute(context, request, response,
132: CHECK_QUERY_STRING);
133: }
134:
135: protected TestResult checkGetQueryString(PortletContext context,
136: PortletRequest request, PortletResponse response)
137: throws IOException, PortletException {
138: return doCheckIncludedAttribute(context, request, response,
139: GET_QUERY_STRING);
140: }
141:
142: // Private Methods ---------------------------------------------------------
143:
144: private TestResult doCheckIncludedAttribute(PortletContext context,
145: PortletRequest request, PortletResponse response,
146: String pathInfo) throws IOException, PortletException {
147:
148: // Save expected values as request attributes.
149: String contextPath = request.getContextPath();
150: String requestUri = contextPath + SERVLET_PATH + pathInfo;
151: request.setAttribute(EXPECTED_REQUEST_URI, requestUri);
152: request.setAttribute(EXPECTED_CONTEXT_PATH, contextPath);
153:
154: // Dispatch to the companion servlet: call checkParameters().
155: StringBuffer buffer = new StringBuffer();
156: buffer.append(SERVLET_PATH).append(pathInfo).append("?")
157: .append(QUERY_STRING);
158: if (LOG.isDebugEnabled()) {
159: LOG.debug("Dispatching to: " + buffer.toString());
160: }
161: PortletRequestDispatcher dispatcher = context
162: .getRequestDispatcher(buffer.toString());
163: dispatcher.include((RenderRequest) request,
164: (RenderResponse) response);
165:
166: // Retrieve test result returned by the companion servlet.
167: TestResult result = (TestResult) request
168: .getAttribute(RESULT_KEY);
169: request.removeAttribute(RESULT_KEY);
170: request.removeAttribute(EXPECTED_REQUEST_URI);
171: request.removeAttribute(EXPECTED_CONTEXT_PATH);
172: return result;
173: }
174:
175: // Nested Companion Servlet Class ------------------------------------------
176:
177: public static class CompanionServlet extends HttpServlet {
178:
179: private static final String KEY_REQUEST_URI = "javax.servlet.include.request_uri";
180: private static final String KEY_CONTEXT_PATH = "javax.servlet.include.context_path";
181: private static final String KEY_SERVLET_PATH = "javax.servlet.include.servlet_path";
182: private static final String KEY_PATH_INFO = "javax.servlet.include.path_info";
183: private static final String KEY_QUERY_STRING = "javax.servlet.include.query_string";
184:
185: // GenericServlet Impl -------------------------------------------------
186:
187: public String getServletInfo() {
188: return getClass().getName();
189: }
190:
191: /**
192: * This method uses <code>HttpServletRequest.getPathInfo</code> to
193: * retrieve the requested path info, and forwards to the corresponding
194: * check method to perform the test.
195: */
196: public void service(HttpServletRequest request,
197: HttpServletResponse response) throws ServletException,
198: IOException {
199: TestResult result = null;
200: String pathInfo = getPathInfo(request);
201: if (CHECK_PATH_INFO.equals(pathInfo)) {
202: result = checkPathInfo(request, pathInfo);
203: } else if (CHECK_REQUEST_URI.equals(pathInfo)) {
204: result = checkRequestURI(request);
205: } else if (CHECK_CONTEXT_PATH.equals(pathInfo)) {
206: result = checkContextPath(request);
207: } else if (CHECK_SERVLET_PATH.equals(pathInfo)) {
208: result = checkServletPath(request);
209: } else if (CHECK_QUERY_STRING.equals(pathInfo)) {
210: result = checkQueryString(request);
211: } else if (GET_REQUEST_URI.equals(pathInfo)) {
212: result = checkGetRequestURI(request);
213: } else if (GET_CONTEXT_PATH.equals(pathInfo)) {
214: result = checkGetContextPath(request);
215: } else if (GET_SERVLET_PATH.equals(pathInfo)) {
216: result = checkGetServletPath(request);
217: } else if (GET_QUERY_STRING.equals(pathInfo)) {
218: result = checkGetQueryString(request);
219: } else {
220: result = failOnPathInfo(pathInfo);
221: }
222: request.setAttribute(RESULT_KEY, result);
223: }
224:
225: // Private Methods -----------------------------------------------------
226:
227: /**
228: * Retrieves the path info from servlet request by invoking method
229: * <code>HttpServletRequest.getPathInfo()</code>.
230: * @param request the servlet request dispatched from test portlet.
231: * @return the path info retrieved from servlet request.
232: */
233: private String getPathInfo(HttpServletRequest request) {
234: return request.getPathInfo();
235: }
236:
237: private TestResult checkPathInfo(HttpServletRequest request,
238: String expected) {
239: TestResult result = new TestResult();
240: result.setDescription("Ensure that included attribute '"
241: + KEY_PATH_INFO
242: + "' is available in servlet request.");
243: result.setSpecPLT("16.3.1");
244:
245: String pathInfo = (String) request
246: .getAttribute(KEY_PATH_INFO);
247: if (pathInfo != null && pathInfo.equals(expected)) {
248: result.setReturnCode(TestResult.PASSED);
249: } else {
250: TestUtils.failOnAssertion(KEY_PATH_INFO, pathInfo,
251: expected, result);
252: }
253: return result;
254: }
255:
256: private TestResult checkRequestURI(HttpServletRequest request) {
257: TestResult result = new TestResult();
258: result.setDescription("Ensure that included attribute '"
259: + KEY_REQUEST_URI
260: + "' is available in servlet request.");
261: result.setSpecPLT("16.3.1");
262:
263: String expected = (String) request
264: .getAttribute(EXPECTED_REQUEST_URI);
265: String requestURI = (String) request
266: .getAttribute(KEY_REQUEST_URI);
267: if (requestURI != null && requestURI.equals(expected)) {
268: result.setReturnCode(TestResult.PASSED);
269: } else {
270: TestUtils.failOnAssertion(KEY_REQUEST_URI, requestURI,
271: expected, result);
272: }
273: return result;
274: }
275:
276: private TestResult checkGetRequestURI(HttpServletRequest request) {
277: TestResult result = new TestResult();
278: result
279: .setDescription("Ensure that method request.getRequestURI() "
280: + "returns correct value.");
281: result.setSpecPLT("16.3.3");
282:
283: String expected = (String) request
284: .getAttribute(EXPECTED_REQUEST_URI);
285: String requestURI = request.getRequestURI();
286: if (requestURI != null && requestURI.equals(expected)) {
287: result.setReturnCode(TestResult.PASSED);
288: } else {
289: TestUtils.failOnAssertion("request.getRequestURI()",
290: requestURI, expected, result);
291: }
292: return result;
293: }
294:
295: private TestResult checkContextPath(HttpServletRequest request) {
296: TestResult result = new TestResult();
297: result.setDescription("Ensure that included attribute '"
298: + KEY_CONTEXT_PATH
299: + "' is available in servlet request.");
300: result.setSpecPLT("16.3.1");
301:
302: String expected = (String) request
303: .getAttribute(EXPECTED_CONTEXT_PATH);
304: String contextPath = (String) request
305: .getAttribute(KEY_CONTEXT_PATH);
306: if (contextPath != null && contextPath.equals(expected)) {
307: result.setReturnCode(TestResult.PASSED);
308: } else {
309: TestUtils.failOnAssertion(KEY_CONTEXT_PATH,
310: contextPath, expected, result);
311: }
312: return result;
313: }
314:
315: private TestResult checkGetContextPath(
316: HttpServletRequest request) {
317: TestResult result = new TestResult();
318: result
319: .setDescription("Ensure that method request.getContextPath() "
320: + "returns the correct value.");
321: result.setSpecPLT("16.3.3");
322:
323: String expected = (String) request
324: .getAttribute(EXPECTED_CONTEXT_PATH);
325: String contextPath = request.getContextPath();
326: if (contextPath != null && contextPath.equals(expected)) {
327: result.setReturnCode(TestResult.PASSED);
328: } else {
329: TestUtils.failOnAssertion("request.getContextPath()",
330: contextPath, expected, result);
331: }
332: return result;
333: }
334:
335: private TestResult checkServletPath(HttpServletRequest request) {
336: TestResult result = new TestResult();
337: result.setDescription("Ensure that included attribute '"
338: + KEY_SERVLET_PATH
339: + "' is available in servlet request.");
340: result.setSpecPLT("16.3.1");
341:
342: String servletPath = (String) request
343: .getAttribute(KEY_SERVLET_PATH);
344: if (SERVLET_PATH.equals(servletPath)) {
345: result.setReturnCode(TestResult.PASSED);
346: } else {
347: TestUtils.failOnAssertion(KEY_SERVLET_PATH,
348: servletPath, SERVLET_PATH, result);
349: }
350: return result;
351: }
352:
353: private TestResult checkGetServletPath(
354: HttpServletRequest request) {
355: TestResult result = new TestResult();
356: result
357: .setDescription("Ensure that method request.getServletPath() "
358: + "returns the correct value.");
359: result.setSpecPLT("16.3.3");
360:
361: String servletPath = request.getServletPath();
362: if (SERVLET_PATH.equals(servletPath)) {
363: result.setReturnCode(TestResult.PASSED);
364: } else {
365: TestUtils.failOnAssertion("request.getServletPath()",
366: servletPath, SERVLET_PATH, result);
367: }
368: return result;
369: }
370:
371: private TestResult checkQueryString(HttpServletRequest request) {
372: TestResult result = new TestResult();
373: result.setDescription("Ensure that included attribute '"
374: + KEY_QUERY_STRING
375: + "' is available in servlet request.");
376: result.setSpecPLT("16.3.1");
377:
378: String queryString = (String) request
379: .getAttribute(KEY_QUERY_STRING);
380: if (QUERY_STRING.equals(queryString)) {
381: result.setReturnCode(TestResult.PASSED);
382: } else {
383: TestUtils.failOnAssertion(KEY_QUERY_STRING,
384: queryString, QUERY_STRING, result);
385: }
386: return result;
387: }
388:
389: private TestResult checkGetQueryString(
390: HttpServletRequest request) {
391: TestResult result = new TestResult();
392: result
393: .setDescription("Ensure that method request.getQueryString() "
394: + "returns the correct value.");
395: result.setSpecPLT("16.3.3");
396:
397: String queryString = request.getQueryString();
398: if (QUERY_STRING.equals(queryString)) {
399: result.setReturnCode(TestResult.PASSED);
400: } else {
401: TestUtils.failOnAssertion("request.getQueryString()",
402: queryString, QUERY_STRING, result);
403: }
404: return result;
405: }
406:
407: private TestResult failOnPathInfo(String pathInfo) {
408: TestResult result = new TestResult();
409: result.setDescription("Ensure that included attribute '"
410: + KEY_PATH_INFO
411: + "' is available in servlet request.");
412: result.setSpecPLT("16.3.1");
413:
414: String[] expectedPathInfos = new String[] {
415: CHECK_REQUEST_URI, CHECK_CONTEXT_PATH,
416: CHECK_SERVLET_PATH, CHECK_QUERY_STRING, };
417: TestUtils.failOnAssertion(KEY_PATH_INFO,
418: new String[] { pathInfo }, expectedPathInfos,
419: result);
420: return result;
421: }
422:
423: }
424:
425: }
|