001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.mock.web;
018
019import java.util.Collections;
020import java.util.Enumeration;
021import java.util.LinkedHashMap;
022import java.util.Map;
023
024import javax.servlet.FilterConfig;
025import javax.servlet.ServletContext;
026
027import org.springframework.lang.Nullable;
028import org.springframework.util.Assert;
029
030/**
031 * Mock implementation of the {@link javax.servlet.FilterConfig} interface.
032 *
033 * <p>Used for testing the web framework; also useful for testing
034 * custom {@link javax.servlet.Filter} implementations.
035 *
036 * @author Juergen Hoeller
037 * @since 1.0.2
038 * @see MockFilterChain
039 * @see PassThroughFilterChain
040 */
041public class MockFilterConfig implements FilterConfig {
042
043        private final ServletContext servletContext;
044
045        private final String filterName;
046
047        private final Map<String, String> initParameters = new LinkedHashMap<>();
048
049
050        /**
051         * Create a new MockFilterConfig with a default {@link MockServletContext}.
052         */
053        public MockFilterConfig() {
054                this(null, "");
055        }
056
057        /**
058         * Create a new MockFilterConfig with a default {@link MockServletContext}.
059         * @param filterName the name of the filter
060         */
061        public MockFilterConfig(String filterName) {
062                this(null, filterName);
063        }
064
065        /**
066         * Create a new MockFilterConfig.
067         * @param servletContext the ServletContext that the servlet runs in
068         */
069        public MockFilterConfig(@Nullable ServletContext servletContext) {
070                this(servletContext, "");
071        }
072
073        /**
074         * Create a new MockFilterConfig.
075         * @param servletContext the ServletContext that the servlet runs in
076         * @param filterName the name of the filter
077         */
078        public MockFilterConfig(@Nullable ServletContext servletContext, String filterName) {
079                this.servletContext = (servletContext != null ? servletContext : new MockServletContext());
080                this.filterName = filterName;
081        }
082
083
084        @Override
085        public String getFilterName() {
086                return this.filterName;
087        }
088
089        @Override
090        public ServletContext getServletContext() {
091                return this.servletContext;
092        }
093
094        public void addInitParameter(String name, String value) {
095                Assert.notNull(name, "Parameter name must not be null");
096                this.initParameters.put(name, value);
097        }
098
099        @Override
100        public String getInitParameter(String name) {
101                Assert.notNull(name, "Parameter name must not be null");
102                return this.initParameters.get(name);
103        }
104
105        @Override
106        public Enumeration<String> getInitParameterNames() {
107                return Collections.enumeration(this.initParameters.keySet());
108        }
109
110}