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