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.AppServerFactory;
009: import com.tc.test.server.appserver.StandardAppServerParameters;
010: import com.tc.test.server.appserver.deployment.AbstractOneServerDeploymentTest;
011: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
012: import com.tc.test.server.util.TcConfigBuilder;
013: import com.tctest.webapp.listeners.AttributeListener;
014: import com.tctest.webapp.listeners.BindingListener;
015: import com.tctest.webapp.listeners.SessionListener;
016: import com.tctest.webapp.servlets.ListenerReportingServlet;
017:
018: import junit.framework.Test;
019:
020: public class SessionEventsWithTCPropsTest extends
021: AbstractOneServerDeploymentTest {
022: private static final String CONTEXT = "SessionEventsWithTCPropsTest";
023: private static final String MAPPING = "ListenerReportingServlet";
024:
025: public SessionEventsWithTCPropsTest() {
026: // DEV-1117
027: if (AppServerFactory.getCurrentAppServerId() == AppServerFactory.WEBLOGIC) {
028: disableAllUntil("2007-12-30");
029: }
030: }
031:
032: public static Test suite() {
033: return new SessionEventsWithTCPropsTestSetup();
034: }
035:
036: public void testListener() throws Exception {
037: WebConversation wc = new WebConversation();
038:
039: // first, sanity check
040: assertEquals("INVALID REQUEST", request("action=0", wc));
041:
042: // now, put a string into session...
043: assertEquals("OK", request("action=set&key=attr1", wc));
044: // ... and check if it made it there.
045: assertEquals("attr1=attr1", request("action=get&key=attr1", wc));
046:
047: // check if sessionCreated event got fired
048: checkCallCount("SessionListener.sessionCreated", 1, wc);
049:
050: // check if attributeAdded event got fired
051: checkCallCount("AttributeListener.attributeAdded", 1, wc);
052: checkCallCount("BindingListener.valueBound", 1, wc);
053:
054: // now, replace the same attribute...
055: assertEquals("OK", request("action=set&key=attr1", wc));
056: checkCallCount("AttributeListener.attributeReplaced", 1, wc);
057: checkCallCount("BindingListener.valueUnbound", 1, wc);
058: checkCallCount("BindingListener.valueBound", 2, wc);
059:
060: // now, remove the attribute
061: assertEquals("OK", request("action=remove&key=attr1", wc));
062:
063: checkCallCount("AttributeListener.attributeRemoved", 1, wc);
064: checkCallCount("BindingListener.valueUnbound", 2, wc);
065:
066: // now add an attribute...
067: assertEquals("OK", request("action=set&key=attr1", wc));
068: // ... and check if it made it there
069: assertEquals("attr1=attr1", request("action=get&key=attr1", wc));
070:
071: // ...check if right events got fired
072: checkCallCount("SessionListener.sessionCreated", 1, wc);
073: checkCallCount("AttributeListener.attributeAdded", 2, wc);
074: checkCallCount("BindingListener.valueBound", 3, wc);
075:
076: // ...now proactively invalidate the session
077: assertEquals("OK", request("action=invalidate", wc));
078:
079: // ...and check if the next request creates a new session
080: assertEquals("OK", request("action=isNew", wc));
081:
082: // ...now check events counts again
083: // ...check if right events got fired
084: checkCallCount("SessionListener.sessionCreated", 2, wc);
085: checkCallCount("SessionListener.sessionDestroyed", 1, wc);
086: checkCallCount("AttributeListener.attributeAdded", 2, wc);
087: checkCallCount("BindingListener.valueBound", 3, wc);
088: checkCallCount("AttributeListener.attributeRemoved", 2, wc);
089: checkCallCount("BindingListener.valueUnbound", 3, wc);
090: }
091:
092: private void checkCallCount(final String key, int expectedCount,
093: WebConversation wc) throws Exception {
094: String response = request("action=call_count&key=" + key, wc);
095: assertEquals(key + "=" + expectedCount, response);
096: }
097:
098: private String request(String params, WebConversation wc)
099: throws Exception {
100: return server0.ping(
101: "/" + CONTEXT + "/" + MAPPING + "?" + params, wc)
102: .getText().trim();
103: }
104:
105: private static class SessionEventsWithTCPropsTestSetup extends
106: OneServerTestSetup {
107:
108: public SessionEventsWithTCPropsTestSetup() {
109: super (SessionEventsWithTCPropsTest.class, CONTEXT);
110: }
111:
112: protected void configureWar(DeploymentBuilder builder) {
113: builder.addServlet("ListenerReportingServlet", "/"
114: + MAPPING + "/*", ListenerReportingServlet.class,
115: null, false);
116: builder.addListener(AttributeListener.class);
117: builder.addListener(SessionListener.class);
118: builder.addListener(BindingListener.class);
119: }
120:
121: protected void configureTcConfig(TcConfigBuilder tcConfigBuilder) {
122: tcConfigBuilder.addWebApplication(CONTEXT);
123: /**
124: * these 2 listeners are intentionally commented out and will be set through tc.properties instead making sure
125: * they could be picked up by app servers tcConfigBuilder.addInstrumentedClass(AttributeListener.class.getName());
126: * tcConfigBuilder.addInstrumentedClass(SessionListener.class.getName());
127: */
128: tcConfigBuilder.addInstrumentedClass(BindingListener.class
129: .getName());
130: }
131:
132: protected void configureServerParamers(
133: StandardAppServerParameters params) {
134: params.appendSysProp("com.tc.session.attribute.listeners",
135: AttributeListener.class.getName());
136: params.appendSysProp("com.tc.session.listeners",
137: SessionListener.class.getName());
138: }
139: }
140: }
|