View Javadoc

1   /*
2    * $Id: MockMultipartRequestHandler.java 421117 2006-07-12 04:35:47Z wsmoak $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.struts.mock;
20  
21  import java.util.Enumeration;
22  import java.util.Hashtable;
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import org.apache.struts.action.ActionServlet;
26  import org.apache.struts.action.ActionMapping;
27  import org.apache.struts.upload.MultipartRequestHandler;
28  
29  /***
30   * <p>Mock <strong>MultipartRequestHandler</strong> object for unit tests.</p>
31   *
32   * @version $Rev: 421117 $
33   */
34  public class MockMultipartRequestHandler implements MultipartRequestHandler {
35  
36      /*** mock ActionServlet instance. */
37      private ActionServlet servlet;
38  
39      /*** mock ActionMapping instance. */
40      private ActionMapping mapping = new ActionMapping();
41  
42      /*** request elements. */
43      private Hashtable elements;
44  
45      /***
46       * Convienience method to set a reference to a mock
47       * ActionServlet instance.
48       * @param servlet Mock servlet instance.
49       */
50      public void setServlet(ActionServlet servlet) {
51          this.servlet = servlet;
52      }
53  
54      /***
55       * Convienience method to set a reference to a mock
56       * ActionMapping instance.
57       * @param mapping Mock action mapping instance.
58       */
59      public void setMapping(ActionMapping mapping) {
60          this.mapping = mapping;
61      }
62  
63      /***
64       * Get the mock ActionServlet instance.
65       * @return The mock servlet instance.
66       */
67      public ActionServlet getServlet() {
68          return this.servlet;
69      }
70  
71      /***
72       * Get the ActionMapping instance for this mock request.
73       * @return The mock action mapping instance.
74       */
75      public ActionMapping getMapping() {
76          return this.mapping;
77      }
78  
79      /***
80        * <p>Mock parsing of the ServletInputStream.</p>
81        *
82        * <p>Constructs a <code>Hashtable</code> of elements
83        *    from the HttpServletRequest's parameters - no
84        *    <code>FormFile</code> elements are created.</p>
85        * @param request Mock request instance.
86        * @throws ServletException If there is a problem with
87        * processing the request.
88        */
89      public void handleRequest(HttpServletRequest request) throws ServletException {
90          elements = new Hashtable();
91          Enumeration enumer = request.getParameterNames();
92          while (enumer.hasMoreElements()) {
93              String key = enumer.nextElement().toString();
94              elements.put(key, request.getParameter(key));
95          }
96      }
97  
98      /***
99       * This method is called on to retrieve all the text
100      * input elements of the request.
101      *
102      * @return A Hashtable where the keys and values are the names and
103      *  values of the request input parameters
104      */
105     public Hashtable getTextElements() {
106         return this.elements;
107     }
108 
109     /***
110      * <p>This method is called on to retrieve all the FormFile
111      * input elements of the request.</p>
112      *
113      * @return This mock implementation returns an empty
114      *    <code>Hashtable</code>
115      */
116     public Hashtable getFileElements() {
117         return new Hashtable();
118     }
119 
120     /***
121      * This method returns all elements of a multipart request.
122      * @return This mock implementation returns a Hashtable where
123      *   the keys are input names and values are either Strings
124      *   (no FormFile elements)
125      */
126     public Hashtable getAllElements() {
127         return this.elements;
128     }
129 
130     /***
131      * Mock <code>rollback()</code> method does nothing.
132      */
133     public void rollback() {
134         // ignore
135     }
136 
137     /***
138      * Mock <code>finish()</code> method does nothing.
139      */
140     public void finish() {
141         // ignore
142     }
143 
144 }