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