1 /*
2  * Copyright 2002-2009 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.springframework.mock.web;
18 
19 import java.util.Collections;
20 import java.util.Enumeration;
21 import java.util.LinkedHashMap;
22 import java.util.Map;
23 import javax.servlet.FilterConfig;
24 import javax.servlet.ServletContext;
25 
26 import org.springframework.util.Assert;
27 
28 /**
29  * Mock implementation of the {@link javax.servlet.FilterConfig} interface.
30  *
31  * <p>Used for testing the web framework; also useful for testing
32  * custom {@link javax.servlet.Filter} implementations.
33  *
34  * @author Juergen Hoeller
35  * @since 1.0.2
36  * @see MockFilterChain
37  * @see PassThroughFilterChain
38  */
39 public class MockFilterConfig implements FilterConfig {
40 
41 	private final ServletContext servletContext;
42 
43 	private final String filterName;
44 
45 	private final Map<String, String> initParameters = new LinkedHashMap<String, String>();
46 
47 
48 	/**
49 	 * Create a new MockFilterConfig with a default {@link MockServletContext}.
50 	 */
MockFilterConfig()51 	public MockFilterConfig() {
52 		this(null, "");
53 	}
54 
55 	/**
56 	 * Create a new MockFilterConfig with a default {@link MockServletContext}.
57 	 * @param filterName the name of the filter
58 	 */
MockFilterConfig(String filterName)59 	public MockFilterConfig(String filterName) {
60 		this(null, filterName);
61 	}
62 
63 	/**
64 	 * Create a new MockFilterConfig.
65 	 * @param servletContext the ServletContext that the servlet runs in
66 	 */
MockFilterConfig(ServletContext servletContext)67 	public MockFilterConfig(ServletContext servletContext) {
68 		this(servletContext, "");
69 	}
70 
71 	/**
72 	 * Create a new MockFilterConfig.
73 	 * @param servletContext the ServletContext that the servlet runs in
74 	 * @param filterName the name of the filter
75 	 */
MockFilterConfig(ServletContext servletContext, String filterName)76 	public MockFilterConfig(ServletContext servletContext, String filterName) {
77 		this.servletContext = (servletContext != null ? servletContext : new MockServletContext());
78 		this.filterName = filterName;
79 	}
80 
81 
getFilterName()82 	public String getFilterName() {
83 		return filterName;
84 	}
85 
getServletContext()86 	public ServletContext getServletContext() {
87 		return servletContext;
88 	}
89 
addInitParameter(String name, String value)90 	public void addInitParameter(String name, String value) {
91 		Assert.notNull(name, "Parameter name must not be null");
92 		this.initParameters.put(name, value);
93 	}
94 
getInitParameter(String name)95 	public String getInitParameter(String name) {
96 		Assert.notNull(name, "Parameter name must not be null");
97 		return this.initParameters.get(name);
98 	}
99 
getInitParameterNames()100 	public Enumeration<String> getInitParameterNames() {
101 		return Collections.enumeration(this.initParameters.keySet());
102 	}
103 
104 }
105