Source Code Cross Referenced for MockPageContext.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.IOException;
004:        import java.io.Writer;
005:        import java.util.Enumeration;
006:        import java.util.HashMap;
007:        import java.util.Iterator;
008:        import java.util.NoSuchElementException;
009:        import java.util.Stack;
010:
011:        //import javax.el.ELContext;
012:        import javax.servlet.RequestDispatcher;
013:        import javax.servlet.Servlet;
014:        import javax.servlet.ServletConfig;
015:        import javax.servlet.ServletContext;
016:        import javax.servlet.ServletException;
017:        import javax.servlet.ServletRequest;
018:        import javax.servlet.ServletResponse;
019:        import javax.servlet.http.HttpServletRequest;
020:        import javax.servlet.http.HttpSession;
021:        import javax.servlet.jsp.JspWriter;
022:        import javax.servlet.jsp.PageContext; //import javax.servlet.jsp.el.ExpressionEvaluator;
023:        //import javax.servlet.jsp.el.VariableResolver;
024:        import javax.servlet.jsp.tagext.BodyContent;
025:
026:        /**
027:         * Mock implementation of <code>PageContext</code>.
028:         * Please note that EL support using the
029:         * the <b>Unified Expression Language</b> API is only available,
030:         * if the {@link JasperJspFactory} is configured as the default
031:         * <code>JspFactory</code>. By default, {@link #getELContext}
032:         * returns <code>null</code>.
033:         */
034:        //Some methods of this class were copied from org.apache.struts.mock.MockPageContext
035:        //and modified
036:        public class MockPageContext extends PageContext {
037:            protected ServletConfig config;
038:            protected ServletRequest request;
039:            protected ServletResponse response;
040:            private JspWriter jspWriter;
041:            private Stack outStack;
042:            private Exception exception;
043:            private Object page;
044:            private HashMap attributes;
045:
046:            //private ExpressionEvaluator evaluator;
047:            //private VariableResolver resolver;
048:            //private ELContext elContext;
049:
050:            public MockPageContext() {
051:                this (null, null, null);
052:            }
053:
054:            public MockPageContext(ServletConfig config,
055:                    ServletRequest request, ServletResponse response) {
056:                this .config = config;
057:                this .request = request;
058:                this .response = response;
059:                jspWriter = new MockJspWriter();
060:                outStack = new Stack();
061:                attributes = new HashMap();
062:                //evaluator = new MockExpressionEvaluator();
063:                //resolver = new MockVariableResolver();
064:            }
065:
066:            /**
067:             * This method allows to set custom implementations
068:             * of <code>JspWriter</code>. Per default, {@link MockJspWriter}
069:             * is used.
070:             * @param jspWriter the <code>JspWriter</code>
071:             */
072:            public void setJspWriter(JspWriter jspWriter) {
073:                this .jspWriter = jspWriter;
074:            }
075:
076:            public void setPage(Object page) {
077:                this .page = page;
078:            }
079:
080:            /**
081:             * Sets the <code>ServletConfig</code>.
082:             * @param config the <code>ServletConfig</code>
083:             */
084:            public void setServletConfig(ServletConfig config) {
085:                this .config = config;
086:            }
087:
088:            /**
089:             * Sets the <code>ServletRequest</code>.
090:             * @param request the <code>ServletRequest</code>
091:             */
092:            public void setServletRequest(ServletRequest request) {
093:                this .request = request;
094:            }
095:
096:            /**
097:             * Sets the <code>ServletResponse</code>.
098:             * @param response the <code>ServletResponse</code>
099:             */
100:            public void setServletResponse(ServletResponse response) {
101:                this .response = response;
102:            }
103:
104:            public void setException(Exception exception) {
105:                this .exception = exception;
106:            }
107:
108:            public Object findAttribute(String name) {
109:                Object value = getAttribute(name, PageContext.PAGE_SCOPE);
110:                if (value == null) {
111:                    value = getAttribute(name, PageContext.REQUEST_SCOPE);
112:                }
113:                if (value == null) {
114:                    value = getAttribute(name, PageContext.SESSION_SCOPE);
115:                }
116:                if (value == null) {
117:                    value = getAttribute(name, PageContext.APPLICATION_SCOPE);
118:                }
119:                return value;
120:            }
121:
122:            public Object getAttribute(String name) {
123:                return getAttribute(name, PageContext.PAGE_SCOPE);
124:            }
125:
126:            public Object getAttribute(String name, int scope) {
127:                if (scope == PageContext.PAGE_SCOPE) {
128:                    return attributes.get(name);
129:                } else if (scope == PageContext.REQUEST_SCOPE) {
130:                    if (null == request)
131:                        return null;
132:                    return request.getAttribute(name);
133:                } else if (scope == PageContext.SESSION_SCOPE) {
134:                    if (null == getSession())
135:                        return null;
136:                    return getSession().getAttribute(name);
137:                } else if (scope == PageContext.APPLICATION_SCOPE) {
138:                    if (null == getServletContext())
139:                        return null;
140:                    return getServletContext().getAttribute(name);
141:                } else {
142:                    throw new IllegalArgumentException("Invalid scope " + scope);
143:                }
144:            }
145:
146:            public void removeAttribute(String name) {
147:                int scope = getAttributesScope(name);
148:                if (scope != 0) {
149:                    removeAttribute(name, scope);
150:                }
151:            }
152:
153:            public void removeAttribute(String name, int scope) {
154:                if (scope == PageContext.PAGE_SCOPE) {
155:                    attributes.remove(name);
156:                } else if (scope == PageContext.REQUEST_SCOPE) {
157:                    if (request != null) {
158:                        request.removeAttribute(name);
159:                    }
160:                } else if (scope == PageContext.SESSION_SCOPE) {
161:                    if (getSession() != null) {
162:                        getSession().removeAttribute(name);
163:                    }
164:                } else if (scope == PageContext.APPLICATION_SCOPE) {
165:                    if (getServletContext() != null) {
166:                        getServletContext().removeAttribute(name);
167:                    }
168:                } else {
169:                    throw new IllegalArgumentException("Invalid scope " + scope);
170:                }
171:            }
172:
173:            public void setAttribute(String name, Object value) {
174:                setAttribute(name, value, PageContext.PAGE_SCOPE);
175:            }
176:
177:            public void setAttribute(String name, Object value, int scope) {
178:                if (scope == PageContext.PAGE_SCOPE) {
179:                    attributes.put(name, value);
180:                } else if (scope == PageContext.REQUEST_SCOPE) {
181:                    if (request != null) {
182:                        request.setAttribute(name, value);
183:                    }
184:                } else if (scope == PageContext.SESSION_SCOPE) {
185:                    if (getSession() != null) {
186:                        getSession().setAttribute(name, value);
187:                    }
188:                } else if (scope == PageContext.APPLICATION_SCOPE) {
189:                    if (getServletContext() != null) {
190:                        getServletContext().setAttribute(name, value);
191:                    }
192:                } else {
193:                    throw new IllegalArgumentException("Invalid scope " + scope);
194:                }
195:            }
196:
197:            public int getAttributesScope(String name) {
198:                if (getAttribute(name, PageContext.PAGE_SCOPE) != null) {
199:                    return PageContext.PAGE_SCOPE;
200:                } else if (getAttribute(name, PageContext.REQUEST_SCOPE) != null) {
201:                    return PageContext.REQUEST_SCOPE;
202:                } else if (getAttribute(name, PageContext.SESSION_SCOPE) != null) {
203:                    return PageContext.SESSION_SCOPE;
204:                } else if (getAttribute(name, PageContext.APPLICATION_SCOPE) != null) {
205:                    return PageContext.APPLICATION_SCOPE;
206:                }
207:                return 0;
208:            }
209:
210:            public Enumeration getAttributeNamesInScope(int scope) {
211:                if (scope == PageContext.PAGE_SCOPE) {
212:                    return new WrappedEnumeration(attributes.keySet()
213:                            .iterator());
214:                } else if (scope == PageContext.REQUEST_SCOPE) {
215:                    if (request == null)
216:                        return new NullEnumeration();
217:                    return request.getAttributeNames();
218:                } else if (scope == PageContext.SESSION_SCOPE) {
219:                    if (getSession() == null)
220:                        return new NullEnumeration();
221:                    return getSession().getAttributeNames();
222:                } else if (scope == PageContext.APPLICATION_SCOPE) {
223:                    if (getServletContext() == null)
224:                        return new NullEnumeration();
225:                    return getServletContext().getAttributeNames();
226:                } else {
227:                    throw new IllegalArgumentException("Invalid scope " + scope);
228:                }
229:            }
230:
231:            public JspWriter getOut() {
232:                return jspWriter;
233:            }
234:
235:            public Exception getException() {
236:                return exception;
237:            }
238:
239:            public Object getPage() {
240:                return page;
241:            }
242:
243:            public ServletRequest getRequest() {
244:                return request;
245:            }
246:
247:            public ServletResponse getResponse() {
248:                return response;
249:            }
250:
251:            public ServletConfig getServletConfig() {
252:                return config;
253:            }
254:
255:            public ServletContext getServletContext() {
256:                if (null == config)
257:                    return null;
258:                return config.getServletContext();
259:            }
260:
261:            public HttpSession getSession() {
262:                if (null == request)
263:                    return null;
264:                return ((HttpServletRequest) request).getSession();
265:            }
266:
267:            public void handlePageException(Exception exc) {
268:
269:            }
270:
271:            public void handlePageException(Throwable thr) {
272:
273:            }
274:
275:            public void forward(String path) throws ServletException,
276:                    IOException {
277:                if (null != request) {
278:                    RequestDispatcher dispatcher = request
279:                            .getRequestDispatcher(path);
280:                    if (null != dispatcher) {
281:                        dispatcher.forward(request, response);
282:                    }
283:                }
284:            }
285:
286:            public void include(String path) throws ServletException,
287:                    IOException {
288:                if (null != request) {
289:                    RequestDispatcher dispatcher = request
290:                            .getRequestDispatcher(path);
291:                    if (null != dispatcher) {
292:                        dispatcher.include(request, response);
293:                    }
294:                }
295:            }
296:
297:            public void include(String path, boolean flush)
298:                    throws ServletException, IOException {
299:                if (flush) {
300:                    jspWriter.flush();
301:                }
302:                include(path);
303:            }
304:
305:            public void initialize(Servlet servlet, ServletRequest request,
306:                    ServletResponse response, String errorPageURL,
307:                    boolean needsSession, int bufferSize, boolean autoFlush) {
308:                this .config = servlet.getServletConfig();
309:                this .request = request;
310:                this .response = response;
311:                jspWriter = new MockJspWriter();
312:                outStack = new Stack();
313:                attributes = new HashMap();
314:            }
315:
316:            public JspWriter popBody() {
317:                jspWriter = (JspWriter) outStack.pop();
318:                return jspWriter;
319:            }
320:
321:            public BodyContent pushBody() {
322:                outStack.push(jspWriter);
323:                jspWriter = new MockBodyContent(jspWriter);
324:                return (BodyContent) jspWriter;
325:            }
326:
327:            public JspWriter pushBody(Writer writer) {
328:                outStack.push(jspWriter);
329:                jspWriter = new MockJspWriter(writer);
330:                return jspWriter;
331:            }
332:
333:            public void release() {
334:                jspWriter = new MockJspWriter();
335:                outStack = new Stack();
336:            }
337:
338:            /**
339:             * Sets the expression evaluator. The default expression evaluator
340:             * is {@link MockExpressionEvaluator}.
341:             * @param evaluator the <code>ExpressionEvaluator</code>
342:             */
343:            /*public void setExpressionEvaluator(ExpressionEvaluator evaluator)
344:            {
345:                this.evaluator = evaluator;
346:            }*/
347:
348:            /**
349:             * Sets the variable resolver. The default variable resolver
350:             * is {@link MockVariableResolver}.
351:             * @param resolver the <code>VariableResolver</code>
352:             */
353:            /*public void setVariableResolver(VariableResolver resolver)
354:            {
355:                this.resolver = resolver;
356:            }*/
357:
358:            /**
359:             * Sets the <code>ELContext</code>. There is no default
360:             * <code>ELContext</code>. Configure the {@link JasperJspFactory}
361:             * to set one. 
362:             * @param elContext the <code>ELContext</code>
363:             */
364:            /*public void setELContext(ELContext elContext)
365:            {
366:                this.elContext = elContext;
367:            }*/
368:
369:            /*public ExpressionEvaluator getExpressionEvaluator()
370:            {
371:                return evaluator;
372:            }*/
373:
374:            /*public VariableResolver getVariableResolver()
375:            {
376:                return resolver;
377:            }*/
378:
379:            /*public ELContext getELContext()
380:            {
381:                return elContext;
382:            }*/
383:
384:            private class NullEnumeration implements  Enumeration {
385:                public boolean hasMoreElements() {
386:                    return false;
387:                }
388:
389:                public Object nextElement() {
390:                    throw new NoSuchElementException();
391:                }
392:            }
393:
394:            private class WrappedEnumeration implements  Enumeration {
395:                private Iterator iterator;
396:
397:                public WrappedEnumeration(Iterator iterator) {
398:                    this .iterator = iterator;
399:                }
400:
401:                public boolean hasMoreElements() {
402:                    return iterator.hasNext();
403:                }
404:
405:                public Object nextElement() {
406:                    return iterator.next();
407:                }
408:            }
409:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.