1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.chain.web.servlet;
18  
19  import java.util.Enumeration;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.servlet.ServletConfig;
24  import javax.servlet.ServletContext;
25  
26  import org.apache.commons.chain.web.MockEnumeration;
27  
28  /**
29   * Mock {@link ServletConfig} implementation.
30   */
31  public class MockServletConfig implements ServletConfig {
32      private final String servletName;
33      private final ServletContext servletContext;
34      private final Map parameters = new HashMap();
35  
36      /**
37       * Default Constructor.
38       */
39      public MockServletConfig() {
40          this("unspecified", new MockServletContext());
41      }
42  
43      /**
44       * Construct an instance with the specified name.
45       *
46       * @param servletName the servlet name
47       */
48      public MockServletConfig(String servletName) {
49          this(servletName, new MockServletContext());
50      }
51  
52      /**
53       * Construct an instance with the specified name and context.
54       *
55       * @param servletName the servlet name
56       * @param servletContext the servlet context
57       */
58      public MockServletConfig(String servletName, ServletContext servletContext) {
59          this.servletName = servletName;
60          this.servletContext = servletContext;
61      }
62  
63      /**
64       * Get a specified init parameter.
65       *
66       * @param name parameter name
67       * @return the parameter value
68       */
69      public String getInitParameter(String name) {
70          return (String)parameters.get(name);
71      }
72  
73      /**
74       * Get the init parameter names.
75       *
76       * @return the set of parameter names
77       */
78      public Enumeration getInitParameterNames() {
79          return (new MockEnumeration(parameters.keySet().iterator()));
80      }
81  
82      /**
83       * Get the servlet context.
84       *
85       * @return the servlet context
86       */
87      public ServletContext getServletContext() {
88          return servletContext;
89      }
90  
91      /**
92       * Return the servlet name.
93       *
94       * @return The servlet name
95       */
96      public String getServletName() {
97          return servletName;
98      }
99  
100     /**
101      * Set a specified init parameter.
102      *
103      * @param name parameter name
104      * @param value the parameter value
105      */
106     public void setInitParameter(String name, String value) {
107         parameters.put(name, value);
108     }
109 
110 }