KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > web > MockServletConfig


1 /*
2  * Copyright 2002-2006 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.Enumeration JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import javax.servlet.ServletConfig JavaDoc;
23 import javax.servlet.ServletContext JavaDoc;
24
25 import org.springframework.util.Assert;
26
27 /**
28  * Mock implementation of the {@link javax.servlet.ServletConfig} interface.
29  *
30  * <p>Used for testing the web framework; typically not necessary for
31  * testing application controllers.
32  *
33  * @author Rod Johnson
34  * @author Juergen Hoeller
35  * @since 1.0.2
36  */

37 public class MockServletConfig implements ServletConfig JavaDoc {
38
39     private final ServletContext JavaDoc servletContext;
40
41     private final String JavaDoc servletName;
42
43     private final Properties JavaDoc initParameters = new Properties JavaDoc();
44
45
46     /**
47      * Create a new MockServletConfig with a default {@link MockServletContext}.
48      */

49     public MockServletConfig() {
50         this(null, "");
51     }
52
53     /**
54      * Create a new MockServletConfig with a default {@link MockServletContext}.
55      * @param servletName the name of the servlet
56      */

57     public MockServletConfig(String JavaDoc servletName) {
58         this(null, servletName);
59     }
60
61     /**
62      * Create a new MockServletConfig.
63      * @param servletContext the ServletContext that the servlet runs in
64      */

65     public MockServletConfig(ServletContext JavaDoc servletContext) {
66         this(servletContext, "");
67     }
68
69     /**
70      * Create a new MockServletConfig.
71      * @param servletContext the ServletContext that the servlet runs in
72      * @param servletName the name of the servlet
73      */

74     public MockServletConfig(ServletContext JavaDoc servletContext, String JavaDoc servletName) {
75         this.servletContext = (servletContext != null ? servletContext : new MockServletContext());
76         this.servletName = servletName;
77     }
78
79
80     public String JavaDoc getServletName() {
81         return servletName;
82     }
83
84     public ServletContext JavaDoc getServletContext() {
85         return servletContext;
86     }
87
88     public void addInitParameter(String JavaDoc name, String JavaDoc value) {
89         Assert.notNull(name, "Parameter name must not be null");
90         this.initParameters.setProperty(name, value);
91     }
92
93     public String JavaDoc getInitParameter(String JavaDoc name) {
94         Assert.notNull(name, "Parameter name must not be null");
95         return this.initParameters.getProperty(name);
96     }
97
98     public Enumeration JavaDoc getInitParameterNames() {
99         return this.initParameters.keys();
100     }
101
102 }
103
Popular Tags