KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > mock > MockServletContext


1 /*
2  * $Id: MockServletContext.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 package org.apache.struts.mock;
21
22
23 import java.io.InputStream JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.servlet.RequestDispatcher JavaDoc;
29 import javax.servlet.Servlet JavaDoc;
30 import javax.servlet.ServletContext JavaDoc;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34
35 /**
36  * <p>Mock <strong>ServletContext</strong> object for low-level unit tests
37  * of Struts controller components. Coarser grained tests should be
38  * implemented in terms of the Cactus framework, instead of the mock
39  * object classes.</p>
40  *
41  * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
42  * create unit tests is provided, plus additional methods to configure this
43  * object as necessary. Methods for unsupported operations will throw
44  * <code>UnsupportedOperationException</code>.</p>
45  *
46  * <p><strong>WARNING</strong> - Because unit tests operate in a single
47  * threaded environment, no synchronization is performed.</p>
48  *
49  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
50  */

51
52 public class MockServletContext implements ServletContext JavaDoc {
53
54
55
56     // ----------------------------------------------------- Instance Variables
57

58
59     /**
60      * The set of servlet context attributes.
61      */

62     protected HashMap JavaDoc attributes = new HashMap JavaDoc();
63
64
65     /**
66      * Default destination for <code>log()</code> output.
67      */

68     protected Log log = LogFactory.getLog(MockServletContext.class);
69
70
71     /**
72      * The set of context initialization parameters.
73      */

74     protected HashMap JavaDoc parameters = new HashMap JavaDoc();
75
76
77     // --------------------------------------------------------- Public Methods
78

79
80     public void addInitParameter(String JavaDoc name, String JavaDoc value) {
81         parameters.put(name, value);
82     }
83
84
85     public void setLog(Log log) {
86         this.log = log;
87     }
88
89
90
91     // ------------------------------------------------- ServletContext Methods
92

93
94     public Object JavaDoc getAttribute(String JavaDoc name) {
95         return (attributes.get(name));
96     }
97
98
99     public Enumeration JavaDoc getAttributeNames() {
100         return (new MockEnumeration(attributes.keySet().iterator()));
101     }
102
103
104     public ServletContext JavaDoc getContext(String JavaDoc uripath) {
105         throw new UnsupportedOperationException JavaDoc();
106     }
107
108
109     public String JavaDoc getInitParameter(String JavaDoc name) {
110         return ((String JavaDoc) parameters.get(name));
111     }
112
113
114     public Enumeration JavaDoc getInitParameterNames() {
115         return (new MockEnumeration(parameters.keySet().iterator()));
116     }
117
118
119     public int getMajorVersion() {
120         return (2);
121     }
122
123
124     public String JavaDoc getMimeType(String JavaDoc file) {
125         throw new UnsupportedOperationException JavaDoc();
126     }
127
128
129     public int getMinorVersion() {
130         return (3);
131     }
132
133
134     public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc name) {
135         throw new UnsupportedOperationException JavaDoc();
136     }
137
138
139     public String JavaDoc getRealPath(String JavaDoc path) {
140         throw new UnsupportedOperationException JavaDoc();
141     }
142
143
144     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
145         throw new UnsupportedOperationException JavaDoc();
146     }
147
148
149     public URL JavaDoc getResource(String JavaDoc path) {
150       return this.getClass().getResource(path);
151         //throw new UnsupportedOperationException();
152
}
153
154
155     public InputStream JavaDoc getResourceAsStream(String JavaDoc path) {
156       return this.getClass().getResourceAsStream(path);
157         //throw new UnsupportedOperationException();
158
}
159
160
161     public Set JavaDoc getResourcePaths(String JavaDoc path) {
162         throw new UnsupportedOperationException JavaDoc();
163     }
164
165
166     public String JavaDoc getServerInfo() {
167         return ("MockServletContext/$Version$");
168     }
169
170
171     public Servlet JavaDoc getServlet(String JavaDoc name) {
172         throw new UnsupportedOperationException JavaDoc();
173     }
174
175
176     public String JavaDoc getServletContextName() {
177         return (getServerInfo());
178     }
179
180
181     public Enumeration JavaDoc getServletNames() {
182         throw new UnsupportedOperationException JavaDoc();
183     }
184
185
186     public Enumeration JavaDoc getServlets() {
187         throw new UnsupportedOperationException JavaDoc();
188     }
189
190
191     public void log(Exception JavaDoc exception, String JavaDoc message) {
192         log(message, exception);
193     }
194
195
196     public void log(String JavaDoc message) {
197         log.info(message);
198     }
199
200
201     public void log(String JavaDoc message, Throwable JavaDoc throwable) {
202         log.error(message, throwable);
203     }
204
205
206     public void removeAttribute(String JavaDoc name) {
207         attributes.remove(name);
208     }
209
210
211     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
212         if (value == null) {
213             attributes.remove(name);
214         } else {
215             attributes.put(name, value);
216         }
217     }
218
219
220
221 }
222
Popular Tags