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 */
017package org.apache.commons.chain.web.jakarta.servlet;
018
019import java.util.Enumeration;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.commons.chain.web.MockEnumeration;
024
025import jakarta.servlet.ServletConfig;
026import jakarta.servlet.ServletContext;
027
028/**
029 * Mock {@link ServletConfig} implementation.
030 */
031public class MockServletConfig implements ServletConfig {
032    private final String servletName;
033    private final ServletContext servletContext;
034    private final Map<String, String> 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    @Override
070    public String getInitParameter(String name) {
071        return parameters.get(name);
072    }
073
074    /**
075     * Get the init parameter names.
076     *
077     * @return the set of parameter names
078     */
079    @Override
080    public Enumeration<String> getInitParameterNames() {
081        return new MockEnumeration<>(parameters.keySet().iterator());
082    }
083
084    /**
085     * Get the servlet context.
086     *
087     * @return the servlet context
088     */
089    @Override
090    public ServletContext getServletContext() {
091        return servletContext;
092    }
093
094    /**
095     * Return the servlet name.
096     *
097     * @return The servlet name
098     */
099    @Override
100    public String getServletName() {
101        return servletName;
102    }
103
104    /**
105     * Set a specified init parameter.
106     *
107     * @param name parameter name
108     * @param value the parameter value
109     */
110    public void setInitParameter(String name, String value) {
111        parameters.put(name, value);
112    }
113}