KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.tester.shared.SharedSessionBean;
24 import org.apache.tester.unshared.UnsharedSessionBean;
25
26
27 /**
28  * Part 1 of Context Tests. Exercise various methods for dealing with
29  * servlet context attributes. Leave an attribute named "context01"
30  * present, which should be erased after a web application restart.
31  *
32  * @author Craig R. McClanahan
33  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:56 $
34  */

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