KickJava   Java API By Example, From Geeks To Geeks.

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


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.unpshared.UnpSharedSessionBean;
25 import org.apache.tester.unshared.UnsharedSessionBean;
26
27
28 /**
29  * Part 3 of Session Tests. Ensures that there is an existing session, and
30  * that the session bean stashed in Part 1 can be retrieved successfully.
31  * Then, it removes that attribute and verifies successful removal.
32  *
33  * @author Craig R. McClanahan
34  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
35  */

36
37 public class Session03 extends HttpServlet {
38
39     public void doGet(HttpServletRequest request, HttpServletResponse response)
40         throws IOException, ServletException {
41
42         response.setContentType("text/plain");
43         PrintWriter writer = response.getWriter();
44
45         // Ensure that there is a current session
46
boolean ok = true;
47         HttpSession session = request.getSession(false);
48         if (session == null) {
49             writer.println("Session03 FAILED - No existing session " +
50                            request.getRequestedSessionId());
51             ok = false;
52         }
53
54         // Ensure that we can retrieve the attribute successfully
55
SessionBean bean = null;
56         if (ok) {
57             Object JavaDoc object = session.getAttribute("sessionBean");
58             if (object == null) {
59                 writer.println("Session03 FAILED - Cannot retrieve attribute");
60                 ok = false;
61             } else if (!(object instanceof SessionBean)) {
62                 writer.println("Session03 FAILED - Attribute instance of " +
63                                object.getClass().getName());
64                 ok = false;
65             } else {
66                 bean = (SessionBean) object;
67                 String JavaDoc value = bean.getStringProperty();
68                 if (!"Session01".equals(value)) {
69                     writer.println("Session03 FAILED - Property = " + value);
70                     ok = false;
71                 }
72             }
73         }
74
75         // Remove the attribute and guarantee that this was successful
76
if (ok) {
77             session.removeAttribute("sessionBean");
78             if (session.getAttribute("sessionBean") != null) {
79                 writer.println("Session03 FAILED - Removal failed");
80                 ok = false;
81             }
82         }
83
84     // Validate the bean lifecycle of this bean
85
if (ok) {
86         String JavaDoc lifecycle = bean.getLifecycle();
87         if (!"/vb/swp/sda/vu".equals(lifecycle)) {
88             writer.println("Session03 FAILED - Invalid bean lifecycle '" +
89                    lifecycle + "'");
90         ok = false;
91         }
92     }
93
94         // Retrieve and validate the shared session bean
95
SharedSessionBean ssb = null;
96         if (ok) {
97             Object JavaDoc object = session.getAttribute("sharedSessionBean");
98             if (object == null) {
99                 writer.println("Session03 FAILED - Cannot retrieve ssb");
100                 ok = false;
101             } else if (!(object instanceof SharedSessionBean)) {
102                 writer.println("Session03 FAILED - Shared attribute class "
103                                + object.getClass().getName());
104                 ok = false;
105             } else {
106                 ssb = (SharedSessionBean) object;
107                 String JavaDoc value = ssb.getStringProperty();
108                 if (!"Session01".equals(value)) {
109                     writer.println("Session03 FAILED - Shared property ="
110                                    + value);
111                     ok = false;
112                 } else {
113                     session.removeAttribute("sharedSessionBean");
114                     String JavaDoc lifecycle = ssb.getLifecycle();
115                     if (!"/vb/swp/sda/vu".equals(lifecycle)) {
116                         writer.println("Session03 FAILED - Shared lifecycle ="
117                                        + lifecycle);
118                         ok = false;
119                     }
120                 }
121             }
122         }
123
124         // Retrieve and validate the unpacked shared session bean
125
UnpSharedSessionBean ussb = null;
126         if (ok) {
127             Object JavaDoc object = session.getAttribute("unpSharedSessionBean");
128             if (object == null) {
129                 writer.println("Session03 FAILED - Cannot retrieve ussb");
130                 ok = false;
131             } else if (!(object instanceof UnpSharedSessionBean)) {
132                 writer.println("Session03 FAILED - unpShared attribute class "
133                                + object.getClass().getName());
134                 ok = false;
135             } else {
136                 ussb = (UnpSharedSessionBean) object;
137                 String JavaDoc value = ussb.getStringProperty();
138                 if (!"Session01".equals(value)) {
139                     writer.println("Session03 FAILED - unpShared property ="
140                                    + value);
141                     ok = false;
142                 } else {
143                     session.removeAttribute("unpSharedSessionBean");
144                     String JavaDoc lifecycle = ssb.getLifecycle();
145                     if (!"/vb/swp/sda/vu".equals(lifecycle)) {
146                         writer.println("Session03 FAILED - unpShared lifecycle ="
147                                        + lifecycle);
148                         ok = false;
149                     }
150                 }
151             }
152         }
153
154         // Retrieve and validate the unshared session bean
155
UnsharedSessionBean usb = null;
156         if (ok) {
157             Object JavaDoc object = session.getAttribute("unsharedSessionBean");
158             if (object == null) {
159                 writer.println("Session03 FAILED - Cannot retrieve usb");
160                 ok = false;
161             } else if (!(object instanceof UnsharedSessionBean)) {
162                 writer.println("Session03 FAILED - Unshared attribute class "
163                                + object.getClass().getName());
164                 ok = false;
165             } else {
166                 usb = (UnsharedSessionBean) object;
167                 String JavaDoc value = usb.getStringProperty();
168                 if (!"Session01".equals(value)) {
169                     writer.println("Session03 FAILED - Unshared property = "
170                                    + value);
171                     ok = false;
172                 } else {
173                     session.removeAttribute("unsharedSessionBean");
174                     String JavaDoc lifecycle = usb.getLifecycle();
175                     if (!"/vb/swp/sda/vu".equals(lifecycle)) {
176                         writer.println("Session03 FAILED - Unshared lifecycle"
177                                        + " = " + lifecycle);
178                         ok = false;
179                     }
180                 }
181             }
182         }
183
184
185         // Report success if everything is still ok
186
if (ok)
187             writer.println("Session03 PASSED");
188         while (true) {
189             String JavaDoc message = StaticLogger.read();
190             if (message == null)
191                 break;
192             writer.println(message);
193         }
194         StaticLogger.reset();
195
196     }
197
198 }
199
Popular Tags