KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > internal > http > MockServletConfig


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit.internal.http;
8
9
10 import java.util.Enumeration JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import javax.servlet.ServletConfig JavaDoc;
16 import javax.servlet.ServletContext JavaDoc;
17
18
19 /**
20  * This is a Mock ServletConfig object. This object can be
21  * used to test the initialization of Servlets in the same
22  * manner that the servlet container would create them.
23  * This class is also setup with a MockServletContext
24  * object which can be populated with init parameters.
25  * This ServletContext can be retrieved using the
26  * {@link #getServletContext() getServletContext()} method.
27  *
28  * @author Brian Pontarelli
29  */

30 public class MockServletConfig implements ServletConfig JavaDoc {
31
32     String JavaDoc servletName;
33     Map JavaDoc initParameters;
34     ServletContext JavaDoc context;
35
36
37     /**
38      * Constructs a new <code>MockServletConfig</code> that can be passed during
39      * testing to a new Servlet instance.
40      */

41     public MockServletConfig() {
42         this.context = new MockServletContext();
43         this.initParameters = new HashMap JavaDoc();
44     }
45
46     /**
47      * Constructs a new <code>MockServletConfig</code>, with the given servlet
48      * name, that can be passed during testing to a new Servlet instance.
49      *
50      * @param servletName The name of the servlet whose config this is
51      */

52     public MockServletConfig(String JavaDoc servletName) {
53         this.initParameters = new HashMap JavaDoc();
54         this.context = new MockServletContext();
55         this.servletName = servletName;
56     }
57
58     /**
59      * Constructs a new <code>MockServletConfig</code>, with the given
60      * servlet context, that can be passed during testing to a new Servlet
61      * instance.
62      *
63      * @param context The ServletContext to associate with this servlet
64      * config
65      */

66     public MockServletConfig(ServletContext JavaDoc context) {
67         assert (context != null) : "context == null";
68
69         this.initParameters = new HashMap JavaDoc();
70         this.context = context;
71     }
72
73     /**
74      * Constructs a new <code>MockServletConfig</code>, with the given
75      * servlet context and servlet name, that can be passed during testing to a
76      * new Servlet instance.
77      *
78      * @param servletName The name of the servlet whose config this is
79      * @param context The ServletContext to associate with this servlet
80      * config
81      */

82     public MockServletConfig(String JavaDoc servletName, ServletContext JavaDoc context) {
83         assert (context != null) : "context == null";
84
85         this.initParameters = new HashMap JavaDoc();
86         this.context = context;
87         this.servletName = servletName;
88     }
89
90
91     /**
92      * Returns the servlet name that was setup during construction or using the
93      * {@link #setServletName(String) setServletName()} method.
94      *
95      * @return The Servlet's name
96      * @see javax.servlet.ServletConfig#getServletName()
97      */

98     public String JavaDoc getServletName() {
99         return servletName;
100     }
101
102     /**
103      * Sets the name of the servlet that this config is for
104      *
105      * @param servletName The name of the servlet
106      */

107     public void setServletName(String JavaDoc servletName) {
108         this.servletName = servletName;
109     }
110
111     /**
112      * Returns the ServletContext for this config. This may be a new
113      * MockServletContext instance for each instance of this class unless
114      * the constructor which takes a ServletContext as a parameter was used.
115      *
116      * @return The ServletContext and never null
117      * @see javax.servlet.ServletConfig#getServletContext()
118      */

119     public ServletContext JavaDoc getServletContext() {
120         return context;
121     }
122
123     /**
124      * @see javax.servlet.ServletConfig#getInitParameter(String)
125      */

126     public String JavaDoc getInitParameter(String JavaDoc name) {
127         return (String JavaDoc) initParameters.get(name);
128     }
129
130     /**
131      * @see javax.servlet.ServletConfig#getInitParameterNames()
132      */

133     public Enumeration JavaDoc getInitParameterNames() {
134         final Iterator JavaDoc iter = initParameters.keySet().iterator();
135         return new Enumeration JavaDoc() {
136             public boolean hasMoreElements() {
137                 return iter.hasNext();
138             }
139             public java.lang.Object JavaDoc nextElement() {
140                 return iter.next();
141             }
142         };
143     }
144 }
145
Popular Tags