KickJava   Java API By Example, From Geeks To Geeks.

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


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 2 of Context Tests. The context attribute from Context00 should
29  * still be here after a restart (because Context00 is a load-on-startup
30  * servlet, so the <code>init()</code> method should have been triggered
31  * during the restart). However, the context attribute from Context01
32  * should <strong>not</strong> be here, because context attributes should
33  * be cleaned up during a restart.
34  *
35  * @author Craig R. McClanahan
36  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:56 $
37  */

38
39 public class Context02 extends HttpServlet {
40
41     public void doGet(HttpServletRequest request, HttpServletResponse response)
42         throws IOException, ServletException {
43
44         response.setContentType("text/plain");
45         boolean ok = true;
46         PrintWriter writer = response.getWriter();
47         ServletContext context = getServletContext();
48
49         // Check for the attribute from Context01
50
if (ok) {
51             Object JavaDoc bean = context.getAttribute("context01");
52             if (bean != null) {
53                 writer.println("Context02 FAILED - context01 value " +
54                                bean);
55                 ok = false;
56                 context.removeAttribute("context01");
57             }
58         }
59
60         // Check for the attribute from Context00
61
if (ok) {
62             Object JavaDoc bean = context.getAttribute("context00");
63             if (bean == null) {
64                 writer.println("Context02 FAILED - context00 missing");
65                 ok = false;
66             } else if (!(bean instanceof ContextBean)) {
67                 writer.println("Context02 FAILED - context00 class " +
68                                bean.getClass().getName());
69                 ok = false;
70             } else {
71                 String JavaDoc value = ((ContextBean) bean).getStringProperty();
72                 if (!"Context00".equals(value)) {
73                     writer.println("Context02 FAILED - context00 value " +
74                                    value);
75                     ok = false;
76                 } else {
77                     String JavaDoc lifecycle = ((ContextBean) bean).getLifecycle();
78                     if (!"/add".equals(lifecycle)) {
79                         writer.println("Context02 FAILED -" +
80                                        " context00 lifecycle " +
81                                        lifecycle);
82                         ok = false;
83                     }
84                 }
85             }
86         }
87
88         // Check for the attribute from ContextListener01
89
if (ok) {
90             Object JavaDoc bean = context.getAttribute("contextListener01");
91             if (bean == null) {
92                 writer.println("Context02 FAILED - contextListener01 missing");
93                 ok = false;
94             } else if (!(bean instanceof ContextBean)) {
95                 writer.println("Context02 FAILED - contextListener01 class " +
96                                bean.getClass().getName());
97                 ok = false;
98             } else {
99                 String JavaDoc value = ((ContextBean) bean).getStringProperty();
100                 if (!"ContextListener01".equals(value)) {
101                     writer.println("Context02 FAILED - contextListener01 " +
102                                    "value " + value);
103                     ok = false;
104                 } else {
105                     String JavaDoc lifecycle = ((ContextBean) bean).getLifecycle();
106                     if (!"/add".equals(lifecycle)) {
107                         writer.println("Context02 FAILED -" +
108                                        " contextListener01 lifecycle " +
109                                        lifecycle);
110                         ok = false;
111                     }
112                 }
113             }
114         }
115
116         // Report success if everything is still ok
117
if (ok)
118             writer.println("Context02 PASSED");
119         while (true) {
120             String JavaDoc message = StaticLogger.read();
121             if (message == null)
122                 break;
123             writer.println(message);
124         }
125         StaticLogger.reset();
126
127     }
128
129 }
130
Popular Tags