001 package com.mockrunner.mock.web;
002
003 import java.io.ByteArrayInputStream;
004 import java.io.InputStream;
005 import java.net.MalformedURLException;
006 import java.net.URL;
007 import java.util.ArrayList;
008 import java.util.Collection;
009 import java.util.Collections;
010 import java.util.Enumeration;
011 import java.util.HashMap;
012 import java.util.HashSet;
013 import java.util.List;
014 import java.util.Map;
015 import java.util.Set;
016 import java.util.Vector;
017
018 import javax.servlet.RequestDispatcher;
019 import javax.servlet.Servlet;
020 import javax.servlet.ServletContext;
021 import javax.servlet.ServletContextAttributeEvent;
022 import javax.servlet.ServletContextAttributeListener;
023 import javax.servlet.ServletException;
024
025 import com.mockrunner.util.common.StreamUtil;
026
027 /**
028 * Mock implementation of <code>ServletContext</code>.
029 */
030 public class MockServletContext implements ServletContext
031 {
032 private Map attributes;
033 private Map requestDispatchers;
034 private Map contexts;
035 private Map initParameters;
036 private Map mimeTypes;
037 private Map realPaths;
038 private Map resources;
039 private Map resourcePaths;
040 private Map resourceStreams;
041 private String servletContextName;
042 private List attributeListener;
043
044 public MockServletContext()
045 {
046 resetAll();
047 }
048
049 /**
050 * Resets the state of this object to the default values
051 */
052 public synchronized void resetAll()
053 {
054 attributes = new HashMap();
055 requestDispatchers = new HashMap();
056 contexts = new HashMap();
057 initParameters = new HashMap();
058 mimeTypes = new HashMap();
059 realPaths = new HashMap();
060 resources = new HashMap();
061 resourcePaths = new HashMap();
062 resourceStreams = new HashMap();
063 attributeListener = new ArrayList();
064 }
065
066 public synchronized void addAttributeListener(ServletContextAttributeListener listener)
067 {
068 attributeListener.add(listener);
069 }
070
071 public synchronized void clearAttributes()
072 {
073 attributes.clear();
074 }
075
076 public synchronized Object getAttribute(String key)
077 {
078 return attributes.get(key);
079 }
080
081 public synchronized Enumeration getAttributeNames()
082 {
083 Vector attKeys = new Vector(attributes.keySet());
084 return attKeys.elements();
085 }
086
087 public synchronized void removeAttribute(String key)
088 {
089 Object value = attributes.get(key);
090 attributes.remove(key);
091 if(null != value)
092 {
093 callAttributeListenersRemovedMethod(key, value);
094 }
095 }
096
097 public synchronized void setAttribute(String key, Object value)
098 {
099 Object oldValue = attributes.get(key);
100 if(null == value)
101 {
102 attributes.remove(key);
103 }
104 else
105 {
106 attributes.put(key, value);
107 }
108 handleAttributeListenerCalls(key, value, oldValue);
109 }
110
111 public synchronized RequestDispatcher getNamedDispatcher(String name)
112 {
113 return getRequestDispatcher(name);
114 }
115
116 public synchronized RequestDispatcher getRequestDispatcher(String path)
117 {
118 RequestDispatcher dispatcher = (RequestDispatcher)requestDispatchers.get(path);
119 if(null == dispatcher)
120 {
121 dispatcher = new MockRequestDispatcher();
122 setRequestDispatcher(path, dispatcher);
123 }
124 return dispatcher;
125 }
126
127 /**
128 * Returns the map of <code>RequestDispatcher</code> objects. The specified path
129 * maps to the corresponding <code>RequestDispatcher</code> object.
130 * @return the map of <code>RequestDispatcher</code> objects
131 */
132 public synchronized Map getRequestDispatcherMap()
133 {
134 return Collections.unmodifiableMap(requestDispatchers);
135 }
136
137 /**
138 * Clears the map of <code>RequestDispatcher</code> objects.
139 */
140 public synchronized void clearRequestDispatcherMap()
141 {
142 requestDispatchers.clear();
143 }
144
145 /**
146 * Sets a <code>RequestDispatcher</code> that will be returned when calling
147 * {@link #getRequestDispatcher} or {@link #getNamedDispatcher}
148 * with the specified path or name.
149 * If no <code>RequestDispatcher</code>
150 * is set for the specified path, {@link #getRequestDispatcher} and
151 * {@link #getNamedDispatcher} automatically create a new one.
152 * @param path the path for the <code>RequestDispatcher</code>
153 * @param dispatcher the <code>RequestDispatcher</code> object
154 */
155 public synchronized void setRequestDispatcher(String path, RequestDispatcher dispatcher)
156 {
157 if(dispatcher instanceof MockRequestDispatcher)
158 {
159 ((MockRequestDispatcher)dispatcher).setPath(path);
160 }
161 requestDispatchers.put(path, dispatcher);
162 }
163
164 public synchronized ServletContext getContext(String url)
165 {
166 return (ServletContext)contexts.get(url);
167 }
168
169 public synchronized void setContext(String url, ServletContext context)
170 {
171 contexts.put(url, context);
172 }
173
174 public synchronized void clearInitParameters()
175 {
176 initParameters.clear();
177 }
178
179 public synchronized String getInitParameter(String name)
180 {
181 return (String)initParameters.get(name);
182 }
183
184 public synchronized void setInitParameter(String name, String value)
185 {
186 initParameters.put(name, value);
187 }
188
189 public synchronized void setInitParameters(Map parameters)
190 {
191 initParameters.putAll(parameters);
192 }
193
194 public synchronized Enumeration getInitParameterNames()
195 {
196 return new Vector(initParameters.keySet()).elements();
197 }
198
199 public synchronized int getMajorVersion()
200 {
201 return 2;
202 }
203
204 public synchronized int getMinorVersion()
205 {
206 return 3;
207 }
208
209 public synchronized String getMimeType(String file)
210 {
211 return (String)mimeTypes.get(file);
212 }
213
214 public synchronized void setMimeType(String file, String type)
215 {
216 mimeTypes.put(file, type);
217 }
218
219 public synchronized String getRealPath(String path)
220 {
221 return (String)realPaths.get(path);
222 }
223
224 public synchronized void setRealPath(String path, String realPath)
225 {
226 realPaths.put(path, realPath);
227 }
228
229 public synchronized URL getResource(String path) throws MalformedURLException
230 {
231 return (URL)resources.get(path);
232 }
233
234 public synchronized void setResource(String path, URL url)
235 {
236 resources.put(path, url);
237 }
238
239 public synchronized InputStream getResourceAsStream(String path)
240 {
241 byte[] data = (byte[])resourceStreams.get(path);
242 if(null == data) return null;
243 return new ByteArrayInputStream(data);
244 }
245
246 public synchronized void setResourceAsStream(String path, InputStream inputStream)
247 {
248 setResourceAsStream(path, StreamUtil.getStreamAsByteArray(inputStream));
249 }
250
251 public synchronized void setResourceAsStream(String path, byte[] data)
252 {
253 byte[] copy = (byte[])data.clone();
254 resourceStreams.put(path, copy);
255 }
256
257 public synchronized Set getResourcePaths(String path)
258 {
259 Set set = (Set)resourcePaths.get(path);
260 if(null == set) return null;
261 return Collections.unmodifiableSet(set);
262 }
263
264 public synchronized void addResourcePaths(String path, Collection pathes)
265 {
266 Set set = (Set)resourcePaths.get(path);
267 if(null == set)
268 {
269 set = new HashSet();
270 resourcePaths.put(path, set);
271 }
272 set.addAll(pathes);
273 }
274
275 public synchronized void addResourcePath(String path, String resourcePath)
276 {
277 ArrayList list = new ArrayList();
278 list.add(resourcePath);
279 addResourcePaths(path, list);
280 }
281
282 public synchronized String getServerInfo()
283 {
284 return "Mockrunner Server";
285 }
286
287 public synchronized Servlet getServlet(String arg0) throws ServletException
288 {
289 return null;
290 }
291
292 public synchronized String getServletContextName()
293 {
294 return servletContextName;
295 }
296
297 public synchronized void setServletContextName(String servletContextName)
298 {
299 this.servletContextName = servletContextName;
300 }
301
302 public synchronized Enumeration getServletNames()
303 {
304 return new Vector().elements();
305 }
306
307 public synchronized Enumeration getServlets()
308 {
309 return new Vector().elements();
310 }
311
312 public synchronized void log(Exception exc, String message)
313 {
314
315 }
316
317 public synchronized void log(String message, Throwable exc)
318 {
319
320 }
321
322 public synchronized void log(String message)
323 {
324
325 }
326
327 private synchronized void handleAttributeListenerCalls(String key, Object value, Object oldValue)
328 {
329 if(null != oldValue)
330 {
331 if(value != null)
332 {
333 callAttributeListenersReplacedMethod(key, oldValue);
334 }
335 else
336 {
337 callAttributeListenersRemovedMethod(key, oldValue);
338 }
339 }
340 else
341 {
342 if(value != null)
343 {
344 callAttributeListenersAddedMethod(key, value);
345 }
346
347 }
348 }
349
350 private synchronized void callAttributeListenersAddedMethod(String key, Object value)
351 {
352 for(int ii = 0; ii < attributeListener.size(); ii++)
353 {
354 ServletContextAttributeEvent event = new ServletContextAttributeEvent(this, key, value);
355 ((ServletContextAttributeListener)attributeListener.get(ii)).attributeAdded(event);
356 }
357 }
358
359 private synchronized void callAttributeListenersReplacedMethod(String key, Object value)
360 {
361 for(int ii = 0; ii < attributeListener.size(); ii++)
362 {
363 ServletContextAttributeEvent event = new ServletContextAttributeEvent(this, key, value);
364 ((ServletContextAttributeListener)attributeListener.get(ii)).attributeReplaced(event);
365 }
366 }
367
368 private synchronized void callAttributeListenersRemovedMethod(String key, Object value)
369 {
370 for(int ii = 0; ii < attributeListener.size(); ii++)
371 {
372 ServletContextAttributeEvent event = new ServletContextAttributeEvent(this, key, value);
373 ((ServletContextAttributeListener)attributeListener.get(ii)).attributeRemoved(event);
374 }
375 }
376 }