View Javadoc

1   /*
2    * $Id: MockServletConfig.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 1999-2004 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  package org.apache.struts.mock;
19  
20  import javax.servlet.ServletConfig;
21  import javax.servlet.ServletContext;
22  
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  
26  /***
27   * <p>Mock <strong>ServletConfig</strong> object for low-level unit tests of
28   * Struts controller components.  Coarser grained tests should be implemented
29   * in terms of the Cactus framework, instead of the mock object classes.</p>
30   *
31   * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
32   * create unit tests is provided, plus additional methods to configure this
33   * object as necessary.  Methods for unsupported operations will throw
34   * <code>UnsupportedOperationException</code>.</p>
35   *
36   * <p><strong>WARNING</strong> - Because unit tests operate in a single
37   * threaded environment, no synchronization is performed.</p>
38   *
39   * @version $Rev: 421119 $ $Date: 2005-05-07 12:11:38 -0400 (Sat, 07 May 2005)
40   *          $
41   */
42  public class MockServletConfig implements ServletConfig {
43      // ----------------------------------------------------- Instance Variables
44      protected ServletContext context = null;
45      protected HashMap parameters = new HashMap();
46  
47      // ----------------------------------------------------------- Constructors
48      public MockServletConfig() {
49          super();
50      }
51  
52      public MockServletConfig(ServletContext context) {
53          super();
54          setServletContext(context);
55      }
56  
57      // --------------------------------------------------------- Public Methods
58      public void addInitParameter(String name, String value) {
59          parameters.put(name, value);
60      }
61  
62      public void setServletContext(ServletContext context) {
63          this.context = context;
64      }
65  
66      // ------------------------------------------------- ServletContext Methods
67      public String getInitParameter(String name) {
68          return ((String) parameters.get(name));
69      }
70  
71      public Enumeration getInitParameterNames() {
72          return (new MockEnumeration(parameters.keySet().iterator()));
73      }
74  
75      public ServletContext getServletContext() {
76          return (this.context);
77      }
78  
79      public String getServletName() {
80          return ("action");
81      }
82  }