Source Code Cross Referenced for MockHttpServletResponse.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.OutputStreamWriter;
005:        import java.io.PrintWriter;
006:        import java.io.UnsupportedEncodingException;
007:        import java.text.SimpleDateFormat;
008:        import java.util.ArrayList;
009:        import java.util.Date;
010:        import java.util.Enumeration;
011:        import java.util.List;
012:        import java.util.Locale;
013:        import java.util.Map;
014:        import java.util.TimeZone;
015:        import java.util.Vector;
016:
017:        import javax.servlet.ServletOutputStream;
018:        import javax.servlet.http.Cookie;
019:        import javax.servlet.http.HttpServletResponse;
020:
021:        import com.mockrunner.base.NestedApplicationException;
022:        import com.mockrunner.util.common.CaseAwareMap;
023:
024:        /**
025:         * Mock implementation of <code>HttpServletResponse</code>.
026:         */
027:        public class MockHttpServletResponse implements  HttpServletResponse {
028:            private PrintWriter writer;
029:            private MockServletOutputStream outputStream;
030:            private Map headers;
031:            private Locale locale;
032:            private String characterEncoding;
033:            private int bufferSize;
034:            private boolean wasErrorSent;
035:            private boolean wasRedirectSent;
036:            private int errorCode;
037:            private int statusCode;
038:            private List cookies;
039:
040:            public MockHttpServletResponse() {
041:                resetAll();
042:            }
043:
044:            /**
045:             * Resets the state of this object to the default values
046:             */
047:            public void resetAll() {
048:                headers = new CaseAwareMap();
049:                characterEncoding = "ISO-8859-1";
050:                bufferSize = 8192;
051:                wasErrorSent = false;
052:                wasRedirectSent = false;
053:                errorCode = SC_OK;
054:                statusCode = SC_OK;
055:                cookies = new ArrayList();
056:                outputStream = new MockServletOutputStream(characterEncoding);
057:                try {
058:                    writer = new PrintWriter(new OutputStreamWriter(
059:                            outputStream, characterEncoding), true);
060:                } catch (UnsupportedEncodingException exc) {
061:                    throw new NestedApplicationException(exc);
062:                }
063:            }
064:
065:            public String encodeURL(String url) {
066:                return url;
067:            }
068:
069:            public String encodeRedirectUrl(String url) {
070:                return url;
071:            }
072:
073:            public String encodeRedirectURL(String url) {
074:                return url;
075:            }
076:
077:            public String encodeUrl(String url) {
078:                return url;
079:            }
080:
081:            public PrintWriter getWriter() throws IOException {
082:                return writer;
083:            }
084:
085:            public ServletOutputStream getOutputStream() throws IOException {
086:                return outputStream;
087:            }
088:
089:            public String getOutputStreamContent() {
090:                return outputStream.getContent();
091:            }
092:
093:            public void addCookie(Cookie cookie) {
094:                cookies.add(cookie);
095:            }
096:
097:            public void addDateHeader(String key, long date) {
098:                addHeader(key, getDateString(date));
099:            }
100:
101:            public void addHeader(String key, String value) {
102:                List valueList = (List) headers.get(key);
103:                if (null == valueList) {
104:                    valueList = new ArrayList();
105:                    headers.put(key, valueList);
106:                }
107:                valueList.add(value);
108:            }
109:
110:            public void addIntHeader(String key, int value) {
111:                String stringValue = new Integer(value).toString();
112:                addHeader(key, stringValue);
113:            }
114:
115:            public boolean containsHeader(String key) {
116:                return headers.containsKey(key);
117:            }
118:
119:            public void sendError(int code, String message) throws IOException {
120:                errorCode = code;
121:                wasErrorSent = true;
122:            }
123:
124:            public void sendError(int code) throws IOException {
125:                errorCode = code;
126:                wasErrorSent = true;
127:            }
128:
129:            public void sendRedirect(String location) throws IOException {
130:                setHeader("Location", location);
131:                wasRedirectSent = true;
132:            }
133:
134:            public void setDateHeader(String key, long date) {
135:                setHeader(key, getDateString(date));
136:            }
137:
138:            public void setHeader(String key, String value) {
139:                List valueList = new ArrayList();
140:                headers.put(key, valueList);
141:                valueList.add(value);
142:            }
143:
144:            public void setIntHeader(String key, int value) {
145:                String stringValue = new Integer(value).toString();
146:                setHeader(key, stringValue);
147:            }
148:
149:            public void setStatus(int code, String message) {
150:                statusCode = code;
151:            }
152:
153:            public void setStatus(int code) {
154:                statusCode = code;
155:            }
156:
157:            public void flushBuffer() throws IOException {
158:                writer.flush();
159:                outputStream.flush();
160:            }
161:
162:            public int getBufferSize() {
163:                return bufferSize;
164:            }
165:
166:            public String getCharacterEncoding() {
167:                return characterEncoding;
168:            }
169:
170:            public void setCharacterEncoding(String encoding) {
171:                characterEncoding = encoding;
172:                outputStream.setEncoding(encoding);
173:            }
174:
175:            public Locale getLocale() {
176:                return locale;
177:            }
178:
179:            public void setLocale(Locale locale) {
180:                this .locale = locale;
181:            }
182:
183:            public boolean isCommitted() {
184:                return false;
185:            }
186:
187:            public void reset() {
188:                errorCode = SC_OK;
189:                statusCode = SC_OK;
190:                clearHeaders();
191:                resetBuffer();
192:            }
193:
194:            public void resetBuffer() {
195:                outputStream.clearContent();
196:            }
197:
198:            public void clearHeaders() {
199:                headers.clear();
200:            }
201:
202:            public void setBufferSize(int size) {
203:                bufferSize = size;
204:            }
205:
206:            public void setContentLength(int length) {
207:                setIntHeader("Content-Length", length);
208:            }
209:
210:            public String getContentType() {
211:                return getHeader("Content-Type");
212:            }
213:
214:            public void setContentType(String type) {
215:                setHeader("Content-Type", type);
216:            }
217:
218:            public Enumeration getHeaderNames() {
219:                return new Vector(headers.keySet()).elements();
220:            }
221:
222:            public List getHeaderList(String key) {
223:                return (List) headers.get(key);
224:            }
225:
226:            public String getHeader(String key) {
227:                List list = getHeaderList(key);
228:                if (null == list || 0 == list.size())
229:                    return null;
230:                return (String) list.get(0);
231:            }
232:
233:            public int getStatusCode() {
234:                return statusCode;
235:            }
236:
237:            public int getErrorCode() {
238:                return errorCode;
239:            }
240:
241:            public List getCookies() {
242:                return cookies;
243:            }
244:
245:            public boolean wasErrorSent() {
246:                return wasErrorSent;
247:            }
248:
249:            public boolean wasRedirectSent() {
250:                return wasRedirectSent;
251:            }
252:
253:            private String getDateString(long date) {
254:                Date dateValue = new Date(date);
255:                SimpleDateFormat dateFormat = new SimpleDateFormat(
256:                        WebConstants.DATE_FORMAT_HEADER, Locale.US);
257:                dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
258:                return dateFormat.format(dateValue);
259:            }
260:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.