KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > server > appserver > unit > SessionEventsTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tctest.server.appserver.unit;
6
7 import org.apache.commons.httpclient.HttpClient;
8
9 import com.tc.test.server.appserver.unit.AbstractAppServerTestCase;
10 import com.tc.test.server.util.HttpUtil;
11
12 import java.io.IOException JavaDoc;
13 import java.net.ConnectException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import javax.servlet.http.HttpServlet JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21 import javax.servlet.http.HttpSession JavaDoc;
22 import javax.servlet.http.HttpSessionAttributeListener JavaDoc;
23 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
24 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
25 import javax.servlet.http.HttpSessionEvent JavaDoc;
26 import javax.servlet.http.HttpSessionListener JavaDoc;
27
28 public class SessionEventsTest extends AbstractAppServerTestCase {
29
30   public static final class ListenerReportingServlet extends HttpServlet JavaDoc {
31     private static Map JavaDoc callCounts = new HashMap JavaDoc();
32
33     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws IOException JavaDoc {
34       // requests should have 2 standard params: action=get|set|remove|call_count and key
35
final String JavaDoc action = req.getParameter("action");
36       final String JavaDoc key = req.getParameter("key");
37       String JavaDoc reply = "OK";
38       if ("get".equals(action)) {
39         reply = key + "=" + req.getSession().getAttribute(key);
40       } else if ("set".equals(action)) {
41         req.getSession().setAttribute(key, new BindingListener JavaDoc(key));
42       } else if ("remove".equals(action)) {
43         req.getSession().removeAttribute(key);
44       } else if ("call_count".equals(action)) {
45         reply = key + "=" + getCallCount(key);
46       } else if ("invalidate".equals(action)) {
47         req.getSession().invalidate();
48       } else if ("isNew".equals(action)) {
49         if (!req.getSession().isNew()) reply = "ERROR: OLD SESSION!";
50       } else {
51         reply = "INVALID REQUEST";
52       }
53       resp.getWriter().print(reply);
54       resp.flushBuffer();
55     }
56
57     private synchronized static int getCallCount(String JavaDoc key) {
58       Integer JavaDoc i = (Integer JavaDoc) callCounts.get(key);
59       return i == null ? 0 : i.intValue();
60     }
61
62     public synchronized static void incrementCallCount(String JavaDoc key) {
63       Integer JavaDoc i = (Integer JavaDoc) callCounts.get(key);
64       if (i == null) {
65         i = new Integer JavaDoc(1);
66       } else {
67         i = new Integer JavaDoc(i.intValue() + 1);
68       }
69       callCounts.put(key, i);
70     }
71   }
72
73   public static final class AttributeListener implements HttpSessionAttributeListener JavaDoc {
74
75     public AttributeListener() {
76       System.err.println("### AttributeListener() is here!!!");
77     }
78
79     public void attributeAdded(HttpSessionBindingEvent JavaDoc httpsessionbindingevent) {
80       ListenerReportingServlet.incrementCallCount("AttributeListener.attributeAdded");
81       System.err.println("### AttributeListener.attributeAdded() is here!!!");
82     }
83
84     public void attributeRemoved(HttpSessionBindingEvent JavaDoc httpsessionbindingevent) {
85       System.err.println("### AttributeListener.attributeRemoved() is here!!!");
86       ListenerReportingServlet.incrementCallCount("AttributeListener.attributeRemoved");
87     }
88
89     public void attributeReplaced(HttpSessionBindingEvent JavaDoc httpsessionbindingevent) {
90       System.err.println("### AttributeListener.attributeReplaced() is here!!!");
91       ListenerReportingServlet.incrementCallCount("AttributeListener.attributeReplaced");
92     }
93   }
94
95   public static final class SessionListener implements HttpSessionListener JavaDoc {
96     public SessionListener() {
97       System.err.println("### SessionListener() is here!!!");
98     }
99
100     public void sessionCreated(HttpSessionEvent JavaDoc httpsessionevent) {
101       System.err.println("### SessionListener.sessionCreated() is here!!!");
102       ListenerReportingServlet.incrementCallCount("SessionListener.sessionCreated");
103     }
104
105     public void sessionDestroyed(HttpSessionEvent JavaDoc httpsessionevent) {
106       testAttributeAccess(httpsessionevent.getSession());
107       System.err.println("### SessionListener.sessionDestroyed() is here!!!");
108       ListenerReportingServlet.incrementCallCount("SessionListener.sessionDestroyed");
109     }
110
111     private void testAttributeAccess(HttpSession JavaDoc session) {
112       // While session destroyed event is being called, you should still be able to get
113
// attributes
114

115       String JavaDoc[] attrs = session.getValueNames();
116       if (attrs == null || attrs.length == 0) {
117         // please make at least one attribute is present
118
throw new AssertionError JavaDoc("Attributes should be present during this phase");
119       }
120
121       for (int i = 0; i < attrs.length; i++) {
122         String JavaDoc attr = attrs[i];
123         session.getAttribute(attr);
124       }
125     }
126   }
127
128   public static final class BindingListener implements HttpSessionBindingListener JavaDoc {
129
130     private final String JavaDoc key;
131
132     public BindingListener(String JavaDoc key) {
133       System.err.println("### BindingListener is here!!! key = " + key);
134       this.key = key;
135     }
136
137     public void valueBound(HttpSessionBindingEvent JavaDoc e) {
138       System.err.println("### BindingListener.valueBound");
139       // the value being bound must not be in session yet...
140
ListenerReportingServlet.incrementCallCount("BindingListener.valueBound");
141     }
142
143     public void valueUnbound(HttpSessionBindingEvent JavaDoc e) {
144       System.err.println("### BindingListener.valueUnbound");
145       // the value being unbound must not be in session already...
146
ListenerReportingServlet.incrementCallCount("BindingListener.valueUnbound");
147     }
148
149     public String JavaDoc toString() {
150       return key;
151     }
152   }
153
154   private int port;
155
156   public void testListener() throws Exception JavaDoc {
157     collectVmStats();
158     startDsoServer();
159
160     port = startAppServer(true).serverPort();
161
162     HttpClient client = HttpUtil.createHttpClient();
163
164     // first, sanity check
165
URL JavaDoc url = new URL JavaDoc(createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=0");
166     assertEquals("INVALID REQUEST", HttpUtil.getResponseBody(url, client));
167
168     // now, put a string into session...
169
url = new URL JavaDoc(createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=set&key=attr1");
170     assertEquals("OK", HttpUtil.getResponseBody(url, client));
171     // ... and check if it made it there.
172
url = new URL JavaDoc(createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=get&key=attr1");
173     assertEquals("attr1=attr1", HttpUtil.getResponseBody(url, client));
174
175     // check if sessionCreated event got fired
176
checkCallCount("SessionListener.sessionCreated", 1, client);
177
178     // check if attributeAdded event got fired
179
checkCallCount("AttributeListener.attributeAdded", 1, client);
180     checkCallCount("BindingListener.valueBound", 1, client);
181
182     // now, replace the same attribute...
183
url = new URL JavaDoc(createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=set&key=attr1");
184     assertEquals("OK", HttpUtil.getResponseBody(url, client));
185     checkCallCount("AttributeListener.attributeReplaced", 1, client);
186     checkCallCount("BindingListener.valueUnbound", 1, client);
187     checkCallCount("BindingListener.valueBound", 2, client);
188
189     // now, remove the attribute
190
url = new URL JavaDoc(createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=remove&key=attr1");
191     assertEquals("OK", HttpUtil.getResponseBody(url, client));
192
193     checkCallCount("AttributeListener.attributeRemoved", 1, client);
194     checkCallCount("BindingListener.valueUnbound", 2, client);
195
196     // now add an attribute...
197
url = new URL JavaDoc(createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=set&key=attr1");
198     assertEquals("OK", HttpUtil.getResponseBody(url, client));
199     // ... and check if it made it there
200
url = new URL JavaDoc(createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=get&key=attr1");
201     assertEquals("attr1=attr1", HttpUtil.getResponseBody(url, client));
202
203     // ...check if right events got fired
204
checkCallCount("SessionListener.sessionCreated", 1, client);
205     checkCallCount("AttributeListener.attributeAdded", 2, client);
206     checkCallCount("BindingListener.valueBound", 3, client);
207
208     // ...now proactively invalidate the session
209
url = new URL JavaDoc(createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=invalidate");
210     assertEquals("OK", HttpUtil.getResponseBody(url, client));
211
212     // ...and check if the next request creates a new session
213
url = new URL JavaDoc(createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=isNew");
214     assertEquals("OK", HttpUtil.getResponseBody(url, client));
215
216     // ...now check events counts again
217
// ...check if right events got fired
218
checkCallCount("SessionListener.sessionCreated", 2, client);
219     checkCallCount("SessionListener.sessionDestroyed", 1, client);
220     checkCallCount("AttributeListener.attributeAdded", 2, client);
221     checkCallCount("BindingListener.valueBound", 3, client);
222     checkCallCount("AttributeListener.attributeRemoved", 2, client);
223     checkCallCount("BindingListener.valueUnbound", 3, client);
224   }
225
226   private void checkCallCount(final String JavaDoc key, int expectedCount, HttpClient client) throws ConnectException JavaDoc,
227       IOException JavaDoc {
228     URL JavaDoc url = new URL JavaDoc(createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=call_count&key="
229                       + key);
230     assertEquals(key + "=" + expectedCount, HttpUtil.getResponseBody(url, client));
231   }
232 }
233
Popular Tags