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.ServletConfig;
24 import javax.servlet.ServletContext;
25 
26 import org.springframework.util.Assert;
27 
28 /**
29  * Mock implementation of the {@link javax.servlet.ServletConfig} interface.
30  *
31  * <p>Used for testing the web framework; typically not necessary for
32  * testing application controllers.
33  *
34  * @author Rod Johnson
35  * @author Juergen Hoeller
36  * @since 1.0.2
37  */
38 public class MockServletConfig implements ServletConfig {
39 
40 	private final ServletContext servletContext;
41 
42 	private final String servletName;
43 
44 	private final Map<String, String> initParameters = new LinkedHashMap<String, String>();
45 
46 
47 	/**
48 	 * Create a new MockServletConfig with a default {@link MockServletContext}.
49 	 */
MockServletConfig()50 	public MockServletConfig() {
51 		this(null, "");
52 	}
53 
54 	/**
55 	 * Create a new MockServletConfig with a default {@link MockServletContext}.
56 	 * @param servletName the name of the servlet
57 	 */
MockServletConfig(String servletName)58 	public MockServletConfig(String servletName) {
59 		this(null, servletName);
60 	}
61 
62 	/**
63 	 * Create a new MockServletConfig.
64 	 * @param servletContext the ServletContext that the servlet runs in
65 	 */
MockServletConfig(ServletContext servletContext)66 	public MockServletConfig(ServletContext servletContext) {
67 		this(servletContext, "");
68 	}
69 
70 	/**
71 	 * Create a new MockServletConfig.
72 	 * @param servletContext the ServletContext that the servlet runs in
73 	 * @param servletName the name of the servlet
74 	 */
MockServletConfig(ServletContext servletContext, String servletName)75 	public MockServletConfig(ServletContext servletContext, String servletName) {
76 		this.servletContext = (servletContext != null ? servletContext : new MockServletContext());
77 		this.servletName = servletName;
78 	}
79 
80 
getServletName()81 	public String getServletName() {
82 		return this.servletName;
83 	}
84 
getServletContext()85 	public ServletContext getServletContext() {
86 		return this.servletContext;
87 	}
88 
addInitParameter(String name, String value)89 	public void addInitParameter(String name, String value) {
90 		Assert.notNull(name, "Parameter name must not be null");
91 		this.initParameters.put(name, value);
92 	}
93 
getInitParameter(String name)94 	public String getInitParameter(String name) {
95 		Assert.notNull(name, "Parameter name must not be null");
96 		return this.initParameters.get(name);
97 	}
98 
getInitParameterNames()99 	public Enumeration<String> getInitParameterNames() {
100 		return Collections.enumeration(this.initParameters.keySet());
101 	}
102 
103 }
104