KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > Request01


1 /*
2  * Copyright 1999, 2000 ,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.tester;
18
19
20 import java.io.*;
21 import javax.servlet.*;
22 import javax.servlet.http.*;
23
24
25 /**
26  * Request listener test. Exercise various methods for dealing with
27  * servlet request attributes. Leave an attribute named "request01"
28  * present, which should be erased after a web application restart.
29  *
30  * @author Justyna Horwat
31  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
32  */

33
34 public class Request01 extends HttpServlet {
35
36     public void doGet(HttpServletRequest request, HttpServletResponse response)
37         throws IOException, ServletException {
38
39         response.setContentType("text/plain");
40         boolean ok = true;
41         PrintWriter writer = response.getWriter();
42         ServletContext context = getServletContext();
43
44         // Ensure that there is no existing attribute
45
if (ok) {
46             if (request.getAttribute("request01") != null) {
47                 writer.println("Request01 FAILED - Attribute already exists");
48                 ok = false;
49             }
50         }
51
52         // Create and stash a request attribute
53
if (ok) {
54             ContextBean bean = new ContextBean();
55             bean.setStringProperty("Request01");
56             request.setAttribute("request01", bean);
57         }
58
59         // Ensure that we can retrieve the attribute successfully
60
if (ok) {
61             Object JavaDoc bean = request.getAttribute("request01");
62             if (bean == null) {
63                 writer.println("Request01 FAILED - Cannot retrieve attribute");
64                 ok = false;
65             }
66             if (ok) {
67                 if (!(bean instanceof ContextBean)) {
68                     writer.println("Request01 FAILED - Bean instance of " +
69                                    bean.getClass().getName());
70                     ok = false;
71                 }
72             }
73             if (ok) {
74                 String JavaDoc value = ((ContextBean) bean).getStringProperty();
75                 if (!"Request01".equals(value)) {
76                     writer.println("Request01 FAILED - Value = " + value);
77                     ok = false;
78                 }
79             }
80             if (ok) {
81                 String JavaDoc lifecycle = ((ContextBean) bean).getLifecycle();
82                 if (!"/add".equals(lifecycle)) {
83                     writer.println("Request01 FAILED - Bean lifecycle is " +
84                                    lifecycle);
85                     ok = false;
86                 }
87             }
88         }
89
90         // Ensure that we can update this attribute and check its lifecycle
91
if (ok) {
92             ContextBean bean = (ContextBean) request.getAttribute("request01");
93             request.setAttribute("request01", bean);
94             String JavaDoc lifecycle = bean.getLifecycle();
95             if (!"/add/rep".equals(lifecycle)) {
96                 writer.println("Request01 FAILED - Bean lifecycle is " +
97                                lifecycle);
98                 ok = false;
99             }
100         }
101
102         // Ensure that we can remove this attribute and check its lifecycle
103
if (ok) {
104             ContextBean bean = (ContextBean) request.getAttribute("request01");
105             request.removeAttribute("request01");
106             String JavaDoc lifecycle = bean.getLifecycle();
107             if (!"/add/rep/rem".equals(lifecycle)) {
108                 writer.println("Request01 FAILED - Bean lifecycle is " +
109                                lifecycle);
110                 ok = false;
111             }
112         }
113
114         // Add a bean back for the restart application test
115
request.setAttribute("request01", new ContextBean());
116
117         // Ensure that setAttribute("name", null) works correctly
118
if (ok) {
119             request.setAttribute("FOO", "BAR");
120             request.setAttribute("FOO", null);
121             if (request.getAttribute("FOO") != null) {
122                 writer.println("Request01 FAILED - setAttribute(name,null)");
123                 ok = false;
124             }
125         }
126
127         // Report success if everything is still ok
128
if (ok)
129             writer.println("Request01 PASSED");
130         while (true) {
131             String JavaDoc message = StaticLogger.read();
132             if (message == null)
133                 break;
134             writer.println(message);
135         }
136         StaticLogger.reset();
137
138     }
139
140 }
141
Popular Tags