Source Code Cross Referenced for TestSampleServlet.java in  » Testing » jakarta-cactus » org » apache » cactus » sample » servlet » 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 
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;
021:
022:        import java.io.IOException;
023:
024:        import java.net.URLDecoder;
025:
026:        import java.util.Hashtable;
027:
028:        import org.apache.cactus.Cookie;
029:        import org.apache.cactus.ServletTestCase;
030:        import org.apache.cactus.WebRequest;
031:        import org.apache.cactus.WebResponse;
032:
033:        /**
034:         * Tests of the <code>SampleServlet</code> servlet class.
035:         *
036:         * @version $Id: TestSampleServlet.java 238816 2004-02-29 16:36:46Z vmassol $
037:         */
038:        public class TestSampleServlet extends ServletTestCase {
039:            /**
040:             * Verify that we can assert the servlet output stream.
041:             * 
042:             * @exception IOException on test failure
043:             */
044:            public void testReadServletOutputStream() throws IOException {
045:                SampleServlet servlet = new SampleServlet();
046:
047:                servlet.doGet(request, response);
048:            }
049:
050:            /**
051:             * Verify that we can assert the servlet output stream.
052:             *
053:             * @param theResponse the response from the server side.
054:             * 
055:             * @exception IOException on test failure
056:             */
057:            public void endReadServletOutputStream(WebResponse theResponse)
058:                    throws IOException {
059:                String expected = "<html><head/><body>A GET request</body></html>";
060:                String result = theResponse.getText();
061:
062:                assertEquals(expected, result);
063:            }
064:
065:            //-------------------------------------------------------------------------
066:
067:            /**
068:             * Verify that we can simulate a POST request to a servlet. Note that
069:             * we send a parameter to force a POST.
070:             *
071:             * @param theRequest the request object that serves to initialize the
072:             *                   HTTP connection to the server redirector.
073:             */
074:            public void beginPostMethod(WebRequest theRequest) {
075:                theRequest.addParameter("param", "value",
076:                        WebRequest.POST_METHOD);
077:            }
078:
079:            /**
080:             * Verify that we can simulate a POST request to a servlet. Note that
081:             * we send a parameter to force a POST. Otherwise Cactus will do a GET
082:             * by default.
083:             */
084:            public void testPostMethod() {
085:                SampleServlet servlet = new SampleServlet();
086:
087:                assertEquals("POST", servlet.checkMethod(request));
088:                assertEquals("value", request.getParameter("param"));
089:            }
090:
091:            //-------------------------------------------------------------------------
092:
093:            /**
094:             * Verify that we can simulate a GET request to a servlet. Note: Cactus
095:             * does a GET by default.
096:             */
097:            public void testGetMethod() {
098:                SampleServlet servlet = new SampleServlet();
099:
100:                assertEquals("GET", servlet.checkMethod(request));
101:            }
102:
103:            //-------------------------------------------------------------------------
104:
105:            /**
106:             * Verify that by default the session implicit object is available and can
107:             * be used.
108:             */
109:            public void testSetAttribute() {
110:                SampleServlet servlet = new SampleServlet();
111:
112:                servlet.setSessionVariable(request);
113:
114:                assertNotNull(session);
115:                assertEquals("value_setSessionVariable", session
116:                        .getAttribute("name_setSessionVariable"));
117:            }
118:
119:            /**
120:             * Verify that we can set an attribute in the request.
121:             */
122:            public void testSetRequestAttribute() {
123:                SampleServlet servlet = new SampleServlet();
124:
125:                servlet.setRequestAttribute(request);
126:
127:                assertEquals("value_setRequestAttribute", request
128:                        .getAttribute("name_setRequestAttribute"));
129:            }
130:
131:            //-------------------------------------------------------------------------
132:
133:            /**
134:             * Verify that we can simulate HTTP parameters in the HTTP request.
135:             *
136:             * @param theRequest the request object that serves to initialize the
137:             *                   HTTP connection to the server redirector.
138:             */
139:            public void beginSendParams(WebRequest theRequest) {
140:                theRequest.addParameter("param1", "value1");
141:                theRequest.addParameter("param2", "value2");
142:            }
143:
144:            /**
145:             * Verify that we can send several parameters in the HTTP request.
146:             */
147:            public void testSendParams() {
148:                SampleServlet servlet = new SampleServlet();
149:                Hashtable params = servlet.getRequestParameters(request);
150:
151:                assertNotNull(params.get("param1"));
152:                assertNotNull(params.get("param2"));
153:                assertEquals("value1", params.get("param1"));
154:                assertEquals("value2", params.get("param2"));
155:            }
156:
157:            //-------------------------------------------------------------------------
158:
159:            /**
160:             * Verify that we can simulate HTTP headers in the HTTP request.
161:             *
162:             * @param theRequest the request object that serves to initialize the
163:             *                   HTTP connection to the server redirector.
164:             */
165:            public void beginSendHeader(WebRequest theRequest) {
166:                theRequest.addHeader("testheader", "this is a header test");
167:            }
168:
169:            /**
170:             * Verify that we can simulate HTTP headers in the HTTP request.
171:             */
172:            public void testSendHeader() {
173:                SampleServlet servlet = new SampleServlet();
174:                String headerValue = servlet.getRequestHeader(request);
175:
176:                assertEquals("this is a header test", headerValue);
177:            }
178:
179:            //-------------------------------------------------------------------------
180:
181:            /**
182:             * Verify that we can simulate a single cookie sent in the HTTP request.
183:             *
184:             * @param theRequest the request object that serves to initialize the
185:             *                   HTTP connection to the server redirector.
186:             */
187:            public void beginSendCookie(WebRequest theRequest) {
188:                // Note: The cookie value that was chosen is a string without spaces
189:                // because there is a problem with Resin 1.2.1 which does not support
190:                // quoted cookies. It has been fixed since the 15/12/2000 release of
191:                // Resin.
192:                theRequest.addCookie("testcookie", "thisisacookie");
193:            }
194:
195:            /**
196:             * Verify that we can simulate a single cookie sent in the HTTP request.
197:             */
198:            public void testSendCookie() {
199:                SampleServlet servlet = new SampleServlet();
200:                Hashtable cookies = servlet.getRequestCookies(request);
201:
202:                assertNotNull("Cannot find [testcookie] cookie in request",
203:                        cookies.get("testcookie"));
204:                assertEquals("thisisacookie", cookies.get("testcookie"));
205:            }
206:
207:            /**
208:             * Verify that we can simulate multiple cookies sent in the HTTP request.
209:             *
210:             * @param theRequest the request object that serves to initialize the
211:             *                   HTTP connection to the server redirector.
212:             */
213:            public void beginSendMultipleCookies(WebRequest theRequest) {
214:                theRequest.addCookie("testcookie1", "cookie1");
215:                theRequest.addCookie("testcookie2", "cookie2");
216:            }
217:
218:            /**
219:             * Verify that we can simulate multiple cookies sent in the HTTP request.
220:             */
221:            public void testSendMultipleCookies() {
222:                SampleServlet servlet = new SampleServlet();
223:                Hashtable cookies = servlet.getRequestCookies(request);
224:
225:                assertNotNull(cookies.get("testcookie1"));
226:                assertEquals("cookie1", cookies.get("testcookie1"));
227:
228:                assertNotNull(cookies.get("testcookie2"));
229:                assertEquals("cookie2", cookies.get("testcookie2"));
230:            }
231:
232:            //-------------------------------------------------------------------------
233:
234:            /**
235:             * Verify that it is possible to send back a header and verify it on the
236:             * client side.
237:             */
238:            public void testReceiveHeader() {
239:                SampleServlet servlet = new SampleServlet();
240:
241:                servlet.setResponseHeader(response);
242:            }
243:
244:            /**
245:             * Verify that it is possible to send back a header and verify it on the
246:             * client side.
247:             *
248:             * @param theResponse the response from the server side.
249:             */
250:            public void endReceiveHeader(WebResponse theResponse) {
251:                assertEquals("this is a response header", theResponse
252:                        .getConnection().getHeaderField("responseheader"));
253:            }
254:
255:            //-------------------------------------------------------------------------
256:
257:            /**
258:             * Test that it is possible to send back a Cookie and verify it on the
259:             * client side.
260:             *
261:             * @param theRequest the request object that serves to initialize the
262:             *                   HTTP connection to the server redirector.
263:             */
264:            public void beginReceiveCookie(WebRequest theRequest) {
265:                // Why do we need to have a begin method here ? Good question !
266:                // The answer is that in this test, the SampleServlet's
267:                // setResponseCookie() method sets the domain name of the cookie
268:                // to return to jakarta.apache.org. It means that for this cookie
269:                // to be valid the domain of the request (i.e. the host) must be
270:                // jakarta.apache.org (this is according to the cookies RFCs). Thus
271:                // we need to simulate the URL and act as if we had sent a request
272:                // to jakarta.apache.org ! Logical, no ?
273:                theRequest.setURL("jakarta.apache.org", null, null, null, null);
274:            }
275:
276:            /**
277:             * Test that it is possible to send back a Cookie and verify it on the
278:             * client side.
279:             */
280:            public void testReceiveCookie() {
281:                SampleServlet servlet = new SampleServlet();
282:
283:                servlet.setResponseCookie(response);
284:            }
285:
286:            /**
287:             * Test that it is possible to send back a Cookie and verify it on the
288:             * client side.
289:             *
290:             * @param theResponse the response from the server side.
291:             *
292:             * @exception Exception for backward compatibility with JDK 1.2.2 (not
293:             *            needed for JDK 1.3+, but needed for URLDecoder.decode())
294:             */
295:            public void endReceiveCookie(WebResponse theResponse)
296:                    throws Exception {
297:                Cookie cookie = theResponse.getCookie("responsecookie");
298:
299:                assertNotNull("Cannot find [responsecookie]", cookie);
300:                assertEquals("responsecookie", cookie.getName());
301:
302:                // Some servers may encode the cookie value (ex: the latest
303:                // version of Tomcat 4.0). In order for this test to succeed on
304:                // all servlet engine, we URL decode the cookie value before
305:                // comparing it.
306:                assertEquals("this is a response cookie", URLDecoder
307:                        .decode(cookie.getValue()));
308:
309:                assertEquals("jakarta.apache.org", cookie.getDomain());
310:            }
311:
312:            //-------------------------------------------------------------------------
313:
314:            /**
315:             * Verify that we can use a <code>RequestDispatcher</code> in the class to
316:             * test to forward to another page and compare the result sent to the
317:             * output stream on the client side.
318:             * 
319:             * @exception Exception on test failure
320:             */
321:            public void testRequestDispatcherForward() throws Exception {
322:                SampleServlet servlet = new SampleServlet();
323:
324:                servlet.doForward(request, response, config);
325:            }
326:
327:            /**
328:             * Verify that we can use a <code>RequestDispatcher</code> in the class to
329:             * test to forward to another page and compare the result sent to the
330:             * output stream on the client side.
331:             *
332:             * @param theResponse the response from the server side.
333:             * 
334:             * @exception IOException on test failure
335:             */
336:            public void endRequestDispatcherForward(WebResponse theResponse)
337:                    throws IOException {
338:                // We cannot test what is exactly returned by the called JSP between
339:                // different Servlet engine return different text ! For example some
340:                // return the JSP comment, other do not, ...
341:                // Thus, we only test for a match of "Hello !"
342:                assertTrue("Text missing 'Hello !' : [" + theResponse.getText()
343:                        + "]", theResponse.getText().indexOf("Hello !") > 0);
344:            }
345:
346:            //-------------------------------------------------------------------------
347:
348:            /**
349:             * Verify that we can use a <code>RequestDispatcher</code> in the class to
350:             * test to include another page and compare the result sent to the
351:             * output stream on the client side.
352:             * 
353:             * @exception Exception on test failure
354:             */
355:            public void testRequestDispatcherInclude() throws Exception {
356:                SampleServlet servlet = new SampleServlet();
357:
358:                servlet.doInclude(request, response, config);
359:            }
360:
361:            /**
362:             * Verify that we can use a <code>RequestDispatcher</code> in the class to
363:             * test to include another page and compare the result sent to the
364:             * output stream on the client side.
365:             *
366:             * @param theResponse the response from the server side.
367:             * 
368:             * @exception IOException on test failure
369:             */
370:            public void endRequestDispatcherInclude(WebResponse theResponse)
371:                    throws IOException {
372:                // We cannot test what is exactly returned by the included JSP between
373:                // different Servlet engine return different text ! For example some
374:                // return the JSP comment, other do not, ...
375:                // Thus, we only test for a match of "Hello !"
376:                assertTrue("Text missing 'Hello !' : [" + theResponse.getText()
377:                        + "]", theResponse.getText().indexOf("Hello !") > 0);
378:            }
379:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.