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