Source Code Cross Referenced for TestHttpResponse.java in  » Testing » jakarta-cactus » org » apache » cactus » sample » servlet » unit » 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 » jakarta cactus » org.apache.cactus.sample.servlet.unit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * ========================================================================
003:         * 
004:         * Copyright 2001-2003 The Apache Software Foundation.
005:         *
006:         * Licensed under the Apache License, Version 2.0 (the "License");
007:         * you may not use this file except in compliance with the License.
008:         * You may obtain a copy of the License at
009:         * 
010:         *   http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing, software
013:         * distributed under the License is distributed on an "AS IS" BASIS,
014:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015:         * See the License for the specific language governing permissions and
016:         * limitations under the License.
017:         * 
018:         * ========================================================================
019:         */
020:        package org.apache.cactus.sample.servlet.unit;
021:
022:        import java.io.BufferedReader;
023:        import java.io.DataInputStream;
024:        import java.io.IOException;
025:        import java.io.InputStreamReader;
026:        import java.io.PrintWriter;
027:
028:        import javax.servlet.http.HttpServletResponse;
029:
030:        import org.apache.cactus.ServletTestCase;
031:        import org.apache.cactus.WebRequest;
032:        import org.apache.cactus.WebResponse;
033:
034:        /**
035:         * Tests that writes to the HTTP response.
036:         *
037:         * @version $Id: TestHttpResponse.java 239010 2004-06-19 15:10:53Z vmassol $
038:         */
039:        public class TestHttpResponse extends ServletTestCase {
040:            /**
041:             * Verify that it is possible to write to the servlet output stream.
042:             * 
043:             * @exception IOException on test failure
044:             */
045:            public void testWriteOutputStream() throws IOException {
046:                PrintWriter pw = response.getWriter();
047:
048:                pw.println("should not result in an error");
049:            }
050:
051:            /**
052:             * Verify that it is possible to write to the servlet output stream.
053:             *
054:             * @param theResponse the response from the server side.
055:             * 
056:             * @exception IOException on test failure
057:             */
058:            public void endWriteOutputStream(WebResponse theResponse)
059:                    throws IOException {
060:                BufferedReader br = new BufferedReader(new InputStreamReader(
061:                        new DataInputStream(theResponse.getConnection()
062:                                .getInputStream())));
063:
064:                assertEquals("should not result in an error", br.readLine());
065:            }
066:
067:            //-------------------------------------------------------------------------
068:
069:            /**
070:             * Verify that the <code>WebResponse.getText()</code> method works.
071:             * 
072:             * @exception IOException on test failure
073:             */
074:            public void testGetResponseAsText() throws IOException {
075:                PrintWriter pw = response.getWriter();
076:
077:                // Note: Ideally we could also test multi line to verify that end 
078:                // of lines are correctly handled. However, the different containers
079:                // handle end of lines differently (some return "\r\n" - Windows
080:                // style, others return "\n" - Unix style).
081:                pw.print("<html><head/><body>A GET request</body></html>");
082:            }
083:
084:            /**
085:             * Verify that the <code>WebResponse.getText()</code> method works.
086:             *
087:             * @param theResponse the response from the server side.
088:             * 
089:             * @exception IOException on test failure
090:             */
091:            public void endGetResponseAsText(WebResponse theResponse)
092:                    throws IOException {
093:                String expected = "<html><head/><body>A GET request</body></html>";
094:
095:                String result = theResponse.getText();
096:
097:                assertEquals(expected, result);
098:            }
099:
100:            //-------------------------------------------------------------------------
101:
102:            /**
103:             * Verify that the <code>getTextAsArray()</code> method
104:             * works with output text sent on multiple lines. We also verify that
105:             * we can call it several times with the same result.
106:             * 
107:             * @exception IOException on test failure
108:             */
109:            public void testGetResponseAsStringArrayMultiLines()
110:                    throws IOException {
111:                PrintWriter pw = response.getWriter();
112:
113:                response.setContentType("text/html");
114:                pw.println("<html><head/>");
115:                pw.println("<body>A GET request</body>");
116:                pw.println("</html>");
117:            }
118:
119:            /**
120:             * Verify that the <code>getTextAsArray()</code> method
121:             * works with output text sent on multiple lines. We also verify that
122:             * we can call it several times with the same result.
123:             *
124:             * @param theResponse the response from the server side.
125:             * 
126:             * @exception IOException on test failure
127:             */
128:            public void endGetResponseAsStringArrayMultiLines(
129:                    WebResponse theResponse) throws IOException {
130:                String[] results1 = theResponse.getTextAsArray();
131:                String[] results2 = theResponse.getTextAsArray();
132:
133:                assertTrue(
134:                        "Should have returned 3 lines of text but returned ["
135:                                + results1.length + "]", results1.length == 3);
136:                assertEquals("<html><head/>", results1[0]);
137:                assertEquals("<body>A GET request</body>", results1[1]);
138:                assertEquals("</html>", results1[2]);
139:
140:                assertTrue(
141:                        "Should have returned 3 lines of text but returned ["
142:                                + results2.length + "]", results2.length == 3);
143:                assertEquals("<html><head/>", results2[0]);
144:                assertEquals("<body>A GET request</body>", results2[1]);
145:                assertEquals("</html>", results2[2]);
146:            }
147:
148:            //-------------------------------------------------------------------------
149:
150:            /**
151:             * Verify that a test case can get the request body by calling
152:             * <code>HttpServletRequest.getReader()</code>. In other words, verify that
153:             * internal parameters that Cactus passes from its client side to the
154:             * server do not affect this ability.
155:             *
156:             * @param theRequest the request object that serves to initialize the
157:             *                   HTTP connection to the server redirector.
158:             */
159:            public void beginGetReader(WebRequest theRequest) {
160:                theRequest.addParameter("test", "something",
161:                        WebRequest.POST_METHOD);
162:            }
163:
164:            /**
165:             * Verify that a test case can get the request body by calling
166:             * <code>HttpServletRequest.getReader()</code>. In other words, verify that
167:             * internal parameters that Cactus passes from its client side to the
168:             * server do not affect this ability.
169:             * 
170:             * @exception Exception on test failure
171:             */
172:            public void testGetReader() throws Exception {
173:                String buffer;
174:                StringBuffer body = new StringBuffer();
175:
176:                BufferedReader reader = request.getReader();
177:
178:                while ((buffer = reader.readLine()) != null) {
179:                    body.append(buffer);
180:                }
181:
182:                assertEquals("test=something", body.toString());
183:            }
184:
185:            //-------------------------------------------------------------------------
186:
187:            /**
188:             * Verify we can set and retrieve the content type.
189:             * 
190:             * @throws Exception If an unexpected error occurs
191:             */
192:            public void testSetContentType() throws Exception {
193:                // Note: We also specify the charset so that we are sure to known the
194:                // full content type string that will be returned on the client side.
195:                // Indeed, some containers will specify a charset even if we don't
196:                // specify one in the call to setContentType. This is normal and in
197:                // accordance with RFC2616, section 3.4.1.
198:                response.setContentType("text/xml;charset=ISO-8859-1");
199:
200:                // Although we don't assert the written content, this is needed to make
201:                // the test succeed on some versions of Orion. If the content is left 
202:                // empty, Orion will somehow reset the content-type to text/html. Sigh.
203:                PrintWriter pw = response.getWriter();
204:                pw.println("<?xml version=\"1.0\"?>");
205:                pw.println("<test></test>");
206:            }
207:
208:            /**
209:             * Verify we can set and retrieve the content type.
210:             * 
211:             * @param theResponse the response from the server side.
212:             */
213:            public void endSetContentType(WebResponse theResponse) {
214:                assertEquals("text/xml;charset=ISO-8859-1", theResponse
215:                        .getConnection().getContentType());
216:            }
217:
218:            //-------------------------------------------------------------------------
219:
220:            /**
221:             * Verify that we can assert HTTP status code when it is a redirect and
222:             * that the client side of Cactus does not follow the redirect.
223:             * 
224:             * @exception IOException on test failure
225:             */
226:            public void testRedirect() throws IOException {
227:                response.sendRedirect("http://jakarta.apache.org");
228:            }
229:
230:            /**
231:             * Verify that we can assert HTTP status code when it is a redirect and
232:             * that the client side of Cactus does not follow the redirect.
233:             *
234:             * @param theResponse the response from the server side.
235:             * 
236:             * @exception IOException on test failure
237:             */
238:            public void endRedirect(WebResponse theResponse) throws IOException {
239:                assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY,
240:                        theResponse.getStatusCode());
241:            }
242:
243:            //-------------------------------------------------------------------------
244:
245:            /**
246:             * Verify that we can assert HTTP status code when it is an error
247:             * status code (> 400).
248:             *
249:             * Note: HttpURLConnection will return a FileNotFoundException if the
250:             * status code is > 400 and the request does not end with a "/" !
251:             */
252:            public void testStatusCode() {
253:                response
254:                        .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
255:            }
256:
257:            /**
258:             * Verify that we can assert HTTP status code when it is an error
259:             * status code (> 400).
260:             *
261:             * Note: HttpURLConnection will return a FileNotFoundException if the
262:             * status code is > 400 and the request does not end with a "/" !
263:             *
264:             * @param theResponse the response from the server side.
265:             * 
266:             * @exception IOException on test failure
267:             */
268:            public void endStatusCode(WebResponse theResponse)
269:                    throws IOException {
270:                assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
271:                        theResponse.getStatusCode());
272:            }
273:
274:            //-------------------------------------------------------------------------
275:
276:            /**
277:             * Verify that we can return a NO_CONTENT response.
278:             */
279:            public void testNoContentResponseCode() {
280:                response.setStatus(HttpServletResponse.SC_NO_CONTENT);
281:            }
282:
283:            /**
284:             * Verify that we can return a NO_CONTENT response.
285:             *
286:             * @param theResponse the response from the server side.
287:             */
288:            public void endNoContentResponseCode(WebResponse theResponse) {
289:                assertEquals(theResponse.getStatusCode(), 204);
290:            }
291:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.