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 org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.apache.pluto.testsuite.TestResult;
022: import org.apache.pluto.testsuite.TestUtils;
023:
024: import java.io.IOException;
025: import java.util.Arrays;
026: import java.util.Map;
027:
028: import javax.portlet.PortletContext;
029: import javax.portlet.PortletException;
030: import javax.portlet.PortletRequest;
031: import javax.portlet.PortletRequestDispatcher;
032: import javax.portlet.PortletResponse;
033: import javax.portlet.RenderRequest;
034: import javax.portlet.RenderResponse;
035: import javax.servlet.GenericServlet;
036: import javax.servlet.ServletException;
037: import javax.servlet.ServletRequest;
038: import javax.servlet.ServletResponse;
039:
040: /**
041: *
042: * @version 1.0
043: * @since Mar 9, 2005
044: */
045: public class DispatcherRenderParameterTest extends
046: AbstractReflectivePortletTest {
047:
048: /** Internal logger. */
049: private static final Log LOG = LogFactory
050: .getLog(DispatcherRenderParameterTest.class);
051:
052: // Static Final Constants --------------------------------------------------
053:
054: /** The path to the companion servlet. */
055: private static final String SERVLET_PATH = "/test/DispatcherRenderParameterTest_Servlet";
056:
057: private static final String KEY_TARGET = "target";
058:
059: private static final String TARGET_PARAMS = "testParams";
060: private static final String TARGET_SAME_NAME_PARAM = "testSameNameParam";
061: private static final String TARGET_ADDED_SAME_NAME_PARAM = "testAddedSameNameParam";
062: private static final String TARGET_INVALID_PARAMS = "testInvalidParams";
063:
064: private static final String KEY_RENDER = "renderParamKey";
065: private static final String VALUE_RENDER = "renderParamValue";
066: private static final String VALUE_ADDED1 = "addedParamValue1";
067: private static final String VALUE_ADDED2 = "addedParamValue2";
068:
069: private static final String KEY_A = "includedTestKeyA";
070: private static final String VALUE_A = "includedTestValueA";
071:
072: private static final String KEY_B = "includedTestKeyB";
073: private static final String VALUE_B = "includedTestValueB";
074:
075: private static final String KEY_C = "includedTestKeyC";
076: private static final String VALUE_C1 = "valueOneOfKeyC";
077: private static final String VALUE_C2 = "valueTwoOfKeyC";
078: private static final String VALUE_C3 = "valueThreeOfKeyC";
079:
080: public static final String RESULT_KEY = DispatcherRenderParameterTest.class
081: .getName()
082: + ".RESULT_KEY";
083:
084: // AbstractReflectivePortletTest Impl --------------------------------------
085:
086: /**
087: * Overwrites <code>super.getRenderParameters(..)</code> to set the
088: * test-specific render parameter in the render URL.
089: */
090: public Map getRenderParameters(PortletRequest request) {
091: Map parameterMap = super .getRenderParameters(request);
092: parameterMap.put(KEY_RENDER, new String[] { VALUE_RENDER });
093: return parameterMap;
094: }
095:
096: // Test Methods ------------------------------------------------------------
097:
098: protected TestResult checkParameters(PortletContext context,
099: PortletRequest request, PortletResponse response)
100: throws IOException, PortletException {
101:
102: // Dispatch to the companion servlet: call checkParameters().
103: StringBuffer buffer = new StringBuffer();
104: buffer.append(SERVLET_PATH).append("?").append(KEY_TARGET)
105: .append("=").append(TARGET_PARAMS).append("&").append(
106: KEY_A).append("=").append(VALUE_A).append("&")
107: .append(KEY_B).append("=").append(VALUE_B);
108:
109: if (LOG.isDebugEnabled()) {
110: LOG.debug("Dispatching to: " + buffer.toString());
111: }
112: PortletRequestDispatcher dispatcher = context
113: .getRequestDispatcher(buffer.toString());
114: dispatcher.include((RenderRequest) request,
115: (RenderResponse) response);
116:
117: // Retrieve test result returned by the companion servlet.
118: TestResult result = (TestResult) request
119: .getAttribute(RESULT_KEY);
120: request.removeAttribute(RESULT_KEY);
121: return result;
122: }
123:
124: protected TestResult checkSameNameParameter(PortletContext context,
125: PortletRequest request, PortletResponse response)
126: throws IOException, PortletException {
127:
128: // Dispatch to the companion servlet: call checkSameNameParameter().
129: StringBuffer buffer = new StringBuffer();
130: buffer.append(SERVLET_PATH).append("?").append(KEY_TARGET)
131: .append("=").append(TARGET_SAME_NAME_PARAM).append("&")
132: .append(KEY_C).append("=").append(VALUE_C1).append("&")
133: .append(KEY_C).append("=").append(VALUE_C2).append("&")
134: .append(KEY_C).append("=").append(VALUE_C3);
135:
136: if (LOG.isDebugEnabled()) {
137: LOG.debug("Dispatching to: " + buffer.toString());
138: }
139: PortletRequestDispatcher dispatcher = context
140: .getRequestDispatcher(buffer.toString());
141: dispatcher.include((RenderRequest) request,
142: (RenderResponse) response);
143:
144: // Retrieve test result returned by the companion servlet.
145: TestResult result = (TestResult) request
146: .getAttribute(RESULT_KEY);
147: request.removeAttribute(RESULT_KEY);
148: return result;
149: }
150:
151: protected TestResult checkAddedSameNameParameter(
152: PortletContext context, PortletRequest request,
153: PortletResponse response) throws IOException,
154: PortletException {
155: // Dispatch to the companion servlet: call checkAddedSameNameParameter().
156: StringBuffer buffer = new StringBuffer();
157: buffer.append(SERVLET_PATH).append("?").append(KEY_TARGET)
158: .append("=").append(TARGET_ADDED_SAME_NAME_PARAM)
159: .append("&").append(KEY_RENDER).append("=").append(
160: VALUE_ADDED1).append("&").append(KEY_RENDER)
161: .append("=").append(VALUE_ADDED2);
162:
163: if (LOG.isDebugEnabled()) {
164: LOG.debug("Dispatching to: " + buffer.toString());
165: }
166: PortletRequestDispatcher dispatcher = context
167: .getRequestDispatcher(buffer.toString());
168: dispatcher.include((RenderRequest) request,
169: (RenderResponse) response);
170:
171: // Retrieve test result returned by the companion servlet.
172: TestResult result = (TestResult) request
173: .getAttribute(RESULT_KEY);
174: request.removeAttribute(RESULT_KEY);
175: return result;
176: }
177:
178: protected TestResult checkInvalidParameters(PortletContext context,
179: PortletRequest request, PortletResponse response)
180: throws IOException, PortletException {
181:
182: // Dispatch to the companion servlet: call checkInvalidParameters().
183: StringBuffer buffer = new StringBuffer();
184: buffer.append(SERVLET_PATH).append("?").append(KEY_TARGET)
185: .append("=").append(TARGET_INVALID_PARAMS).append("&")
186: .append(KEY_A).append("&").append(KEY_B).append("=")
187: .append(VALUE_B).append("&").append(KEY_C).append("=");
188: if (LOG.isDebugEnabled()) {
189: LOG.debug("Dispatching to: " + buffer.toString());
190: }
191: PortletRequestDispatcher dispatcher = context
192: .getRequestDispatcher(buffer.toString());
193: dispatcher.include((RenderRequest) request,
194: (RenderResponse) response);
195:
196: // Retrieve test result returned by the companion servlet.
197: TestResult result = (TestResult) request
198: .getAttribute(RESULT_KEY);
199: request.removeAttribute(RESULT_KEY);
200: return result;
201: }
202:
203: // Nested Companion Servlet Class ------------------------------------------
204:
205: /**
206: * Nested static companion servlet class.
207: */
208: public static class CompanionServlet extends GenericServlet {
209:
210: // GenericServlet Impl -------------------------------------------------
211:
212: public String getServletInfo() {
213: return getClass().getName();
214: }
215:
216: /**
217: * Services the servlet request dispatched from the test portlet.
218: * This method checks the 'target' parameter to determine which test
219: * to run, and saves the test result in the request scope, which will
220: * be retrieved by the test portlet.
221: * @param request the incoming servlet request.
222: * @param response the incoming servlet response.
223: */
224: public void service(ServletRequest request,
225: ServletResponse response) throws ServletException,
226: IOException {
227: TestResult result = null;
228: String target = request.getParameter(KEY_TARGET);
229: if (TARGET_PARAMS.equals(target)) {
230: result = checkParameters(request);
231: } else if (TARGET_SAME_NAME_PARAM.equals(target)) {
232: result = checkSameNameParameter(request);
233: } else if (TARGET_ADDED_SAME_NAME_PARAM.equals(target)) {
234: result = checkAddedSameNameParameter(request);
235: } else if (TARGET_INVALID_PARAMS.equals(target)) {
236: result = checkInvalidParameters(request);
237: } else {
238: result = failOnUnknownTarget(request);
239: }
240: request.setAttribute(RESULT_KEY, result);
241: }
242:
243: // Private Methods -----------------------------------------------------
244:
245: /**
246: * Check that parameters A and B are available in the dispatching
247: * request.
248: * @param request the servlet request.
249: */
250: private TestResult checkParameters(ServletRequest request) {
251: TestResult result = new TestResult();
252: result
253: .setDescription("Ensure query parameters added during "
254: + "dispatching are attached to the request.");
255: String valueA = request.getParameter(KEY_A);
256: String valueB = request.getParameter(KEY_B);
257: if (VALUE_A.equals(valueA) && VALUE_B.equals(valueB)) {
258: result.setReturnCode(TestResult.PASSED);
259: } else if (!VALUE_A.equals(valueA)) {
260: TestUtils.failOnAssertion("parameter", valueA, VALUE_A,
261: result);
262: } else {
263: TestUtils.failOnAssertion("parameter", valueB, VALUE_B,
264: result);
265: }
266: return result;
267: }
268:
269: /**
270: * Check that parameter C has three values.
271: * @param request the servlet reqeust.
272: */
273: private TestResult checkSameNameParameter(ServletRequest request) {
274: TestResult result = new TestResult();
275: result
276: .setDescription("Ensure query parameters with the same name "
277: + "added during dispatching are attached to the request.");
278: String[] values = request.getParameterValues(KEY_C);
279: String[] expected = new String[] { VALUE_C1, VALUE_C2,
280: VALUE_C3, };
281: if (Arrays.equals(values, expected)) {
282: result.setReturnCode(TestResult.PASSED);
283: } else {
284: TestUtils.failOnAssertion("parameter", values,
285: expected, result);
286: }
287: return result;
288: }
289:
290: /**
291: * Check that parameter RENDER has three values: one is the render
292: * parameter, while the other two are appended in the dispatch URI.
293: * @param request the servlet reqeust.
294: */
295: private TestResult checkAddedSameNameParameter(
296: ServletRequest request) {
297: TestResult result = new TestResult();
298: result
299: .setDescription("Ensure query parameters with the same name "
300: + "added during dispatching are attached to the request "
301: + "as well as render parameters.");
302: String[] values = request.getParameterValues(KEY_RENDER);
303: String[] expected = new String[] { VALUE_ADDED1,
304: VALUE_ADDED2, VALUE_RENDER, };
305: if (Arrays.equals(values, expected)) {
306: result.setReturnCode(TestResult.PASSED);
307: } else {
308: TestUtils.failOnAssertion("parameter", values,
309: expected, result);
310: }
311: return result;
312: }
313:
314: /**
315: * Check that invalid parameter A is ignored, parameter B is attached
316: * to the dispatching request with the correct value, and parameter C
317: * is attached to the dispatching request with an empty string.
318: * @param request the servlet request.
319: */
320: private TestResult checkInvalidParameters(ServletRequest request) {
321: TestResult result = new TestResult();
322: result
323: .setDescription("Ensure invalid query parameters added "
324: + "during dispatching are ignored.");
325: String valueA = request.getParameter(KEY_A);
326: String valueB = request.getParameter(KEY_B);
327: String valueC = request.getParameter(KEY_C);
328: if (valueA == null && VALUE_B.equals(valueB)
329: && "".equals(valueC)) {
330: result.setReturnCode(TestResult.PASSED);
331: } else if (valueA != null) {
332: TestUtils.failOnAssertion("parameter", valueA, null,
333: result);
334: } else if (!VALUE_B.equals(valueB)) {
335: TestUtils.failOnAssertion("parameter", valueB, VALUE_B,
336: result);
337: } else {
338: TestUtils.failOnAssertion("parameter", valueC, "",
339: result);
340: }
341: return result;
342: }
343:
344: private TestResult failOnUnknownTarget(ServletRequest request) {
345: TestResult result = new TestResult();
346: result.setReturnCode(TestResult.FAILED);
347: result
348: .setResultMessage("Unable to perform test for parameter "
349: + KEY_TARGET
350: + ": "
351: + request.getParameter(KEY_TARGET));
352: return result;
353: }
354:
355: }
356:
357: }
|