001: /*
002: * $Id: MockMultipartRequestHandler.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.mock;
023:
024: import java.util.Enumeration;
025: import java.util.Hashtable;
026: import javax.servlet.ServletException;
027: import javax.servlet.http.HttpServletRequest;
028: import org.apache.struts.action.ActionServlet;
029: import org.apache.struts.action.ActionMapping;
030: import org.apache.struts.upload.MultipartRequestHandler;
031:
032: /**
033: * <p>Mock <strong>MultipartRequestHandler</strong> object for unit tests.</p>
034: *
035: * @version $Rev: 471754 $
036: */
037: public class MockMultipartRequestHandler implements
038: MultipartRequestHandler {
039:
040: /** mock ActionServlet instance. */
041: private ActionServlet servlet;
042:
043: /** mock ActionMapping instance. */
044: private ActionMapping mapping = new ActionMapping();
045:
046: /** request elements. */
047: private Hashtable elements;
048:
049: /**
050: * Convienience method to set a reference to a mock
051: * ActionServlet instance.
052: * @param servlet Mock servlet instance.
053: */
054: public void setServlet(ActionServlet servlet) {
055: this .servlet = servlet;
056: }
057:
058: /**
059: * Convienience method to set a reference to a mock
060: * ActionMapping instance.
061: * @param mapping Mock action mapping instance.
062: */
063: public void setMapping(ActionMapping mapping) {
064: this .mapping = mapping;
065: }
066:
067: /**
068: * Get the mock ActionServlet instance.
069: * @return The mock servlet instance.
070: */
071: public ActionServlet getServlet() {
072: return this .servlet;
073: }
074:
075: /**
076: * Get the ActionMapping instance for this mock request.
077: * @return The mock action mapping instance.
078: */
079: public ActionMapping getMapping() {
080: return this .mapping;
081: }
082:
083: /**
084: * <p>Mock parsing of the ServletInputStream.</p>
085: *
086: * <p>Constructs a <code>Hashtable</code> of elements
087: * from the HttpServletRequest's parameters - no
088: * <code>FormFile</code> elements are created.</p>
089: * @param request Mock request instance.
090: * @throws ServletException If there is a problem with
091: * processing the request.
092: */
093: public void handleRequest(HttpServletRequest request)
094: throws ServletException {
095: elements = new Hashtable();
096: Enumeration enumer = request.getParameterNames();
097: while (enumer.hasMoreElements()) {
098: String key = enumer.nextElement().toString();
099: elements.put(key, request.getParameter(key));
100: }
101: }
102:
103: /**
104: * This method is called on to retrieve all the text
105: * input elements of the request.
106: *
107: * @return A Hashtable where the keys and values are the names and
108: * values of the request input parameters
109: */
110: public Hashtable getTextElements() {
111: return this .elements;
112: }
113:
114: /**
115: * <p>This method is called on to retrieve all the FormFile
116: * input elements of the request.</p>
117: *
118: * @return This mock implementation returns an empty
119: * <code>Hashtable</code>
120: */
121: public Hashtable getFileElements() {
122: return new Hashtable();
123: }
124:
125: /**
126: * This method returns all elements of a multipart request.
127: * @return This mock implementation returns a Hashtable where
128: * the keys are input names and values are either Strings
129: * (no FormFile elements)
130: */
131: public Hashtable getAllElements() {
132: return this .elements;
133: }
134:
135: /**
136: * Mock <code>rollback()</code> method does nothing.
137: */
138: public void rollback() {
139: // ignore
140: }
141:
142: /**
143: * Mock <code>finish()</code> method does nothing.
144: */
145: public void finish() {
146: // ignore
147: }
148:
149: }
|