001/*
002 * Copyright 2002-2012 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;
023import javax.servlet.FilterConfig;
024import javax.servlet.ServletContext;
025
026import org.springframework.util.Assert;
027
028/**
029 * Mock implementation of the {@link javax.servlet.FilterConfig} interface.
030 *
031 * <p>Used for testing the web framework; also useful for testing
032 * custom {@link javax.servlet.Filter} implementations.
033 *
034 * @author Juergen Hoeller
035 * @since 1.0.2
036 * @see MockFilterChain
037 * @see PassThroughFilterChain
038 */
039public class MockFilterConfig implements FilterConfig {
040
041        private final ServletContext servletContext;
042
043        private final String filterName;
044
045        private final Map<String, String> initParameters = new LinkedHashMap<String, String>();
046
047
048        /**
049         * Create a new MockFilterConfig with a default {@link MockServletContext}.
050         */
051        public MockFilterConfig() {
052                this(null, "");
053        }
054
055        /**
056         * Create a new MockFilterConfig with a default {@link MockServletContext}.
057         * @param filterName the name of the filter
058         */
059        public MockFilterConfig(String filterName) {
060                this(null, filterName);
061        }
062
063        /**
064         * Create a new MockFilterConfig.
065         * @param servletContext the ServletContext that the servlet runs in
066         */
067        public MockFilterConfig(ServletContext servletContext) {
068                this(servletContext, "");
069        }
070
071        /**
072         * Create a new MockFilterConfig.
073         * @param servletContext the ServletContext that the servlet runs in
074         * @param filterName the name of the filter
075         */
076        public MockFilterConfig(ServletContext servletContext, String filterName) {
077                this.servletContext = (servletContext != null ? servletContext : new MockServletContext());
078                this.filterName = filterName;
079        }
080
081
082        @Override
083        public String getFilterName() {
084                return filterName;
085        }
086
087        @Override
088        public ServletContext getServletContext() {
089                return servletContext;
090        }
091
092        public void addInitParameter(String name, String value) {
093                Assert.notNull(name, "Parameter name must not be null");
094                this.initParameters.put(name, value);
095        }
096
097        @Override
098        public String getInitParameter(String name) {
099                Assert.notNull(name, "Parameter name must not be null");
100                return this.initParameters.get(name);
101        }
102
103        @Override
104        public Enumeration<String> getInitParameterNames() {
105                return Collections.enumeration(this.initParameters.keySet());
106        }
107
108}