001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.server.appserver.unit;
006:
007: import com.meterware.httpunit.WebConversation;
008: import com.tc.test.server.appserver.deployment.AbstractOneServerDeploymentTest;
009: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
010: import com.tc.test.server.util.TcConfigBuilder;
011: import com.tctest.webapp.listeners.AttributeListener;
012: import com.tctest.webapp.listeners.BindingListener;
013: import com.tctest.webapp.listeners.SessionListener;
014: import com.tctest.webapp.servlets.ListenerReportingServlet;
015:
016: import junit.framework.Test;
017:
018: public class SessionEventsTest extends AbstractOneServerDeploymentTest {
019: private static final String CONTEXT = "SessionEventsTest";
020: private static final String MAPPING = "ListenerReportingServlet";
021:
022: public static Test suite() {
023: return new SessionEventsTestSetup();
024: }
025:
026: public void testListener() throws Exception {
027: WebConversation wc = new WebConversation();
028:
029: // first, sanity check
030: assertEquals("INVALID REQUEST", request("action=0", wc));
031:
032: // now, put a string into session...
033: assertEquals("OK", request("action=set&key=attr1", wc));
034: // ... and check if it made it there.
035: assertEquals("attr1=attr1", request("action=get&key=attr1", wc));
036:
037: // check if sessionCreated event got fired
038: checkCallCount("SessionListener.sessionCreated", 1, wc);
039:
040: // check if attributeAdded event got fired
041: checkCallCount("AttributeListener.attributeAdded", 1, wc);
042: checkCallCount("BindingListener.valueBound", 1, wc);
043:
044: // now, replace the same attribute...
045: assertEquals("OK", request("action=set&key=attr1", wc));
046: checkCallCount("AttributeListener.attributeReplaced", 1, wc);
047: checkCallCount("BindingListener.valueUnbound", 1, wc);
048: checkCallCount("BindingListener.valueBound", 2, wc);
049:
050: // now, remove the attribute
051: assertEquals("OK", request("action=remove&key=attr1", wc));
052:
053: checkCallCount("AttributeListener.attributeRemoved", 1, wc);
054: checkCallCount("BindingListener.valueUnbound", 2, wc);
055:
056: // now add an attribute...
057: assertEquals("OK", request("action=set&key=attr1", wc));
058: // ... and check if it made it there
059: assertEquals("attr1=attr1", request("action=get&key=attr1", wc));
060:
061: // ...check if right events got fired
062: checkCallCount("SessionListener.sessionCreated", 1, wc);
063: checkCallCount("AttributeListener.attributeAdded", 2, wc);
064: checkCallCount("BindingListener.valueBound", 3, wc);
065:
066: // ...now proactively invalidate the session
067: assertEquals("OK", request("action=invalidate", wc));
068:
069: // ...and check if the next request creates a new session
070: assertEquals("OK", request("action=isNew", wc));
071:
072: // ...now check events counts again
073: // ...check if right events got fired
074: checkCallCount("SessionListener.sessionCreated", 2, wc);
075: checkCallCount("SessionListener.sessionDestroyed", 1, wc);
076: checkCallCount("AttributeListener.attributeAdded", 2, wc);
077: checkCallCount("BindingListener.valueBound", 3, wc);
078: checkCallCount("AttributeListener.attributeRemoved", 2, wc);
079: checkCallCount("BindingListener.valueUnbound", 3, wc);
080: }
081:
082: private void checkCallCount(final String key, int expectedCount,
083: WebConversation wc) throws Exception {
084: String response = request("action=call_count&key=" + key, wc);
085: assertEquals(key + "=" + expectedCount, response);
086: }
087:
088: private String request(String params, WebConversation wc)
089: throws Exception {
090: return server0.ping(
091: "/" + CONTEXT + "/" + MAPPING + "?" + params, wc)
092: .getText().trim();
093: }
094:
095: private static class SessionEventsTestSetup extends
096: OneServerTestSetup {
097:
098: public SessionEventsTestSetup() {
099: super (SessionEventsTest.class, CONTEXT);
100: }
101:
102: protected void configureWar(DeploymentBuilder builder) {
103: builder.addServlet("ListenerReportingServlet", "/"
104: + MAPPING + "/*", ListenerReportingServlet.class,
105: null, false);
106: builder.addListener(AttributeListener.class);
107: builder.addListener(SessionListener.class);
108: builder.addListener(BindingListener.class);
109: }
110:
111: protected void configureTcConfig(TcConfigBuilder tcConfigBuilder) {
112: tcConfigBuilder.addWebApplication(CONTEXT);
113: tcConfigBuilder
114: .addInstrumentedClass(AttributeListener.class
115: .getName());
116: tcConfigBuilder.addInstrumentedClass(SessionListener.class
117: .getName());
118: tcConfigBuilder.addInstrumentedClass(BindingListener.class
119: .getName());
120: }
121:
122: }
123: }
|