1 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 ; 13 import java.net.ConnectException ; 14 import java.net.URL ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 18 import javax.servlet.http.HttpServlet ; 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 import javax.servlet.http.HttpSession ; 22 import javax.servlet.http.HttpSessionAttributeListener ; 23 import javax.servlet.http.HttpSessionBindingEvent ; 24 import javax.servlet.http.HttpSessionBindingListener ; 25 import javax.servlet.http.HttpSessionEvent ; 26 import javax.servlet.http.HttpSessionListener ; 27 28 public class SessionEventsTest extends AbstractAppServerTestCase { 29 30 public static final class ListenerReportingServlet extends HttpServlet { 31 private static Map callCounts = new HashMap (); 32 33 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { 34 final String action = req.getParameter("action"); 36 final String key = req.getParameter("key"); 37 String 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 (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 key) { 58 Integer i = (Integer ) callCounts.get(key); 59 return i == null ? 0 : i.intValue(); 60 } 61 62 public synchronized static void incrementCallCount(String key) { 63 Integer i = (Integer ) callCounts.get(key); 64 if (i == null) { 65 i = new Integer (1); 66 } else { 67 i = new Integer (i.intValue() + 1); 68 } 69 callCounts.put(key, i); 70 } 71 } 72 73 public static final class AttributeListener implements HttpSessionAttributeListener { 74 75 public AttributeListener() { 76 System.err.println("### AttributeListener() is here!!!"); 77 } 78 79 public void attributeAdded(HttpSessionBindingEvent httpsessionbindingevent) { 80 ListenerReportingServlet.incrementCallCount("AttributeListener.attributeAdded"); 81 System.err.println("### AttributeListener.attributeAdded() is here!!!"); 82 } 83 84 public void attributeRemoved(HttpSessionBindingEvent httpsessionbindingevent) { 85 System.err.println("### AttributeListener.attributeRemoved() is here!!!"); 86 ListenerReportingServlet.incrementCallCount("AttributeListener.attributeRemoved"); 87 } 88 89 public void attributeReplaced(HttpSessionBindingEvent 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 { 96 public SessionListener() { 97 System.err.println("### SessionListener() is here!!!"); 98 } 99 100 public void sessionCreated(HttpSessionEvent httpsessionevent) { 101 System.err.println("### SessionListener.sessionCreated() is here!!!"); 102 ListenerReportingServlet.incrementCallCount("SessionListener.sessionCreated"); 103 } 104 105 public void sessionDestroyed(HttpSessionEvent 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 session) { 112 115 String [] attrs = session.getValueNames(); 116 if (attrs == null || attrs.length == 0) { 117 throw new AssertionError ("Attributes should be present during this phase"); 119 } 120 121 for (int i = 0; i < attrs.length; i++) { 122 String attr = attrs[i]; 123 session.getAttribute(attr); 124 } 125 } 126 } 127 128 public static final class BindingListener implements HttpSessionBindingListener { 129 130 private final String key; 131 132 public BindingListener(String key) { 133 System.err.println("### BindingListener is here!!! key = " + key); 134 this.key = key; 135 } 136 137 public void valueBound(HttpSessionBindingEvent e) { 138 System.err.println("### BindingListener.valueBound"); 139 ListenerReportingServlet.incrementCallCount("BindingListener.valueBound"); 141 } 142 143 public void valueUnbound(HttpSessionBindingEvent e) { 144 System.err.println("### BindingListener.valueUnbound"); 145 ListenerReportingServlet.incrementCallCount("BindingListener.valueUnbound"); 147 } 148 149 public String toString() { 150 return key; 151 } 152 } 153 154 private int port; 155 156 public void testListener() throws Exception { 157 collectVmStats(); 158 startDsoServer(); 159 160 port = startAppServer(true).serverPort(); 161 162 HttpClient client = HttpUtil.createHttpClient(); 163 164 URL url = new URL (createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=0"); 166 assertEquals("INVALID REQUEST", HttpUtil.getResponseBody(url, client)); 167 168 url = new URL (createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=set&key=attr1"); 170 assertEquals("OK", HttpUtil.getResponseBody(url, client)); 171 url = new URL (createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=get&key=attr1"); 173 assertEquals("attr1=attr1", HttpUtil.getResponseBody(url, client)); 174 175 checkCallCount("SessionListener.sessionCreated", 1, client); 177 178 checkCallCount("AttributeListener.attributeAdded", 1, client); 180 checkCallCount("BindingListener.valueBound", 1, client); 181 182 url = new URL (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 url = new URL (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 url = new URL (createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=set&key=attr1"); 198 assertEquals("OK", HttpUtil.getResponseBody(url, client)); 199 url = new URL (createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=get&key=attr1"); 201 assertEquals("attr1=attr1", HttpUtil.getResponseBody(url, client)); 202 203 checkCallCount("SessionListener.sessionCreated", 1, client); 205 checkCallCount("AttributeListener.attributeAdded", 2, client); 206 checkCallCount("BindingListener.valueBound", 3, client); 207 208 url = new URL (createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=invalidate"); 210 assertEquals("OK", HttpUtil.getResponseBody(url, client)); 211 212 url = new URL (createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=isNew"); 214 assertEquals("OK", HttpUtil.getResponseBody(url, client)); 215 216 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 key, int expectedCount, HttpClient client) throws ConnectException , 227 IOException { 228 URL url = new URL (createUrl(port, SessionEventsTest.ListenerReportingServlet.class) + "?action=call_count&key=" 229 + key); 230 assertEquals(key + "=" + expectedCount, HttpUtil.getResponseBody(url, client)); 231 } 232 } 233 | Popular Tags |