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.commons.chain.web.servlet;
018    
019    import java.util.Enumeration;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import javax.servlet.ServletConfig;
024    import javax.servlet.ServletContext;
025    
026    import org.apache.commons.chain.web.MockEnumeration;
027    
028    /**
029     * Mock {@link ServletConfig} implementation.
030     */
031    public class MockServletConfig implements ServletConfig {
032        private final String servletName;
033        private final ServletContext servletContext;
034        private final Map parameters = new HashMap();
035    
036        /**
037         * Default Constructor.
038         */
039        public MockServletConfig() {
040            this("unspecified", new MockServletContext());
041        }
042    
043        /**
044         * Construct an instance with the specified name.
045         *
046         * @param servletName the servlet name
047         */
048        public MockServletConfig(String servletName) {
049            this(servletName, new MockServletContext());
050        }
051    
052        /**
053         * Construct an instance with the specified name and context.
054         *
055         * @param servletName the servlet name
056         * @param servletContext the servlet context
057         */
058        public MockServletConfig(String servletName, ServletContext servletContext) {
059            this.servletName = servletName;
060            this.servletContext = servletContext;
061        }
062    
063        /**
064         * Get a specified init parameter.
065         *
066         * @param name parameter name
067         * @return the parameter value
068         */
069        public String getInitParameter(String name) {
070            return (String)parameters.get(name);
071        }
072    
073        /**
074         * Get the init parameter names.
075         *
076         * @return the set of parameter names
077         */
078        public Enumeration getInitParameterNames() {
079            return (new MockEnumeration(parameters.keySet().iterator()));
080        }
081    
082        /**
083         * Get the servlet context.
084         *
085         * @return the servlet context
086         */
087        public ServletContext getServletContext() {
088            return servletContext;
089        }
090    
091        /**
092         * Return the servlet name.
093         *
094         * @return The servlet name
095         */
096        public String getServletName() {
097            return servletName;
098        }
099    
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    }