Source Code Cross Referenced for MockServletContext.java in  » Testing » mockrunner-0.4 » com » mockrunner » mock » web » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » mockrunner 0.4 » com.mockrunner.mock.web 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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