KickJava   Java API By Example, From Geeks To Geeks.

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


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 1 of Session Tests. Ensures that there is no current session, then
30  * creates a new session and sets a session attribute. Also, ensure that
31  * calling setAttribute("name", null) acts like removeAttribute().
32  *
33  * @author Craig R. McClanahan
34  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
35  */

36
37 public class Session01 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 no current session
46
boolean ok = true;
47         HttpSession session = request.getSession(false);
48         if (session != null) {
49             writer.println("Session01 FAILED - Requested existing session " +
50                            session.getId());
51             ok = false;
52         }
53
54         // Create a new session
55
if (ok) {
56             session = request.getSession(true);
57             if (session == null) {
58                 writer.println("Session01 FAILED - No session created");
59                 ok = false;
60             }
61         }
62
63         // Store an activation event listener in the session
64
if (ok) {
65             session.setAttribute("activationListener",
66                                  new SessionListener03());
67         }
68
69         // Ensure that there is no existing attribute
70
if (ok) {
71             if (session.getAttribute("sessionBean") != null) {
72                 writer.println("Session01 FAILED - Attribute already exists");
73                 ok = false;
74             }
75         }
76
77         // Create and stash a session attribute
78
if (ok) {
79             SessionBean bean = new SessionBean();
80             bean.setStringProperty("Session01");
81             session.setAttribute("sessionBean", bean);
82         }
83
84         // Ensure that we can retrieve the attribute successfully
85
if (ok) {
86             Object JavaDoc bean = session.getAttribute("sessionBean");
87             if (bean == null) {
88                 writer.println("Session01 FAILED - Cannot retrieve attribute");
89                 ok = false;
90             } else if (!(bean instanceof SessionBean)) {
91                 writer.println("Session01 FAILED - Attribute instance of " +
92                                bean.getClass().getName());
93                 ok = false;
94             } else {
95                 String JavaDoc value = ((SessionBean) bean).getStringProperty();
96                 if (!"Session01".equals(value)) {
97                     writer.println("Session01 FAILED - Property = " + value);
98                     ok = false;
99                 }
100             }
101         }
102
103         // Ensure that setAttribute("name", null) works correctly
104
if (ok) {
105             session.setAttribute("FOO", "BAR");
106             session.setAttribute("FOO", null);
107             if (session.getAttribute("FOO") != null) {
108                 writer.println("Session01 FAILED - setAttribute(name,null)");
109                 ok = false;
110             }
111         }
112
113         // Create more beans that will be used to test application restart
114
if (ok) {
115             SharedSessionBean ssb = new SharedSessionBean();
116             ssb.setStringProperty("Session01");
117             session.setAttribute("sharedSessionBean", ssb);
118             UnpSharedSessionBean ussb = new UnpSharedSessionBean();
119             ussb.setStringProperty("Session01");
120             session.setAttribute("unpSharedSessionBean", ussb);
121             UnsharedSessionBean usb = new UnsharedSessionBean();
122             usb.setStringProperty("Session01");
123             session.setAttribute("unsharedSessionBean", usb);
124         }
125
126         // Report success if everything is still ok
127
if (ok)
128             writer.println("Session01 PASSED");
129         while (true) {
130             String JavaDoc message = StaticLogger.read();
131             if (message == null)
132                 break;
133             writer.println(message);
134         }
135         StaticLogger.reset();
136
137     }
138
139 }
140
Popular Tags