001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.geronimo.tomcat.cluster;
021:
022: import java.io.IOException;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import javax.servlet.http.HttpSession;
027:
028: import org.apache.catalina.Context;
029: import org.apache.catalina.Session;
030: import org.apache.geronimo.clustering.SessionListener;
031: import org.apache.geronimo.clustering.SessionManager;
032:
033: import com.agical.rmock.core.describe.ExpressionDescriber;
034: import com.agical.rmock.core.match.operator.AbstractExpression;
035: import com.agical.rmock.extension.junit.RMockTestCase;
036:
037: /**
038: *
039: * @version $Rev:$ $Date:$
040: */
041: public class ClusteredManagerTest extends RMockTestCase {
042: private SessionManager sessionManager;
043: private SessionListener sessionListener;
044: private String sessionId;
045: private Context context;
046:
047: @Override
048: protected void setUp() throws Exception {
049: sessionId = "sessionId";
050:
051: sessionManager = (SessionManager) mock(SessionManager.class);
052: sessionManager.registerListener(null);
053: modify().args(new AbstractExpression() {
054: public void describeWith(ExpressionDescriber arg0)
055: throws IOException {
056: }
057:
058: public boolean passes(Object arg0) {
059: sessionListener = (SessionListener) arg0;
060: return true;
061: }
062: });
063:
064: context = (Context) mock(Context.class);
065:
066: context.getSessionTimeout();
067: modify().returnValue(10);
068:
069: context.addPropertyChangeListener(null);
070: modify().args(is.NOT_NULL);
071:
072: context.getApplicationLifecycleListeners();
073: modify().multiplicity(expect.from(0));
074:
075: context.getApplicationEventListeners();
076: modify().multiplicity(expect.from(0));
077: }
078:
079: public void testCreatedSession() throws Exception {
080: recordCreateUnderlyingSession();
081:
082: startVerification();
083:
084: ClusteredManager manager = newManager();
085: Session createdSession = manager.createSession(null);
086: assertTrue(createdSession.isValid());
087:
088: HttpSession httpSession = createdSession.getSession();
089: assertEquals("value", httpSession.getAttribute("key"));
090: assertTrue(httpSession.isNew());
091:
092: assertSame(createdSession, manager.findSession(sessionId));
093: }
094:
095: public void testSessionDestructionRemovesSession() throws Exception {
096: org.apache.geronimo.clustering.Session underlyingSession = recordCreateUnderlyingSession();
097:
098: startVerification();
099:
100: ClusteredManager manager = newManager();
101: manager.createSession(null);
102:
103: sessionListener.notifySessionDestruction(underlyingSession);
104:
105: assertNull(manager.findSession(sessionId));
106: }
107:
108: public void testOutboundSessionDestructionRemovesSession()
109: throws Exception {
110: org.apache.geronimo.clustering.Session underlyingSession = recordCreateUnderlyingSession();
111:
112: startVerification();
113:
114: ClusteredManager manager = newManager();
115: manager.createSession(null);
116:
117: sessionListener
118: .notifyOutboundSessionMigration(underlyingSession);
119:
120: assertNull(manager.findSession(sessionId));
121: }
122:
123: public void testInboundSessionMigrationAddsSession()
124: throws Exception {
125: org.apache.geronimo.clustering.Session underlyingSession = (org.apache.geronimo.clustering.Session) mock(org.apache.geronimo.clustering.Session.class);
126: recordUnderlyingSessionState(underlyingSession);
127:
128: startVerification();
129:
130: ClusteredManager manager = newManager();
131:
132: sessionListener
133: .notifyInboundSessionMigration(underlyingSession);
134:
135: Session foundSession = manager.findSession(sessionId);
136: assertNotNull(foundSession);
137:
138: assertTrue(foundSession.isValid());
139:
140: HttpSession httpSession = foundSession.getSession();
141: assertEquals("value", httpSession.getAttribute("key"));
142: assertFalse(httpSession.isNew());
143: }
144:
145: public void testInvalidateSessionReleasesUnderlyingSession()
146: throws Exception {
147: org.apache.geronimo.clustering.Session underlyingSession = recordCreateUnderlyingSession();
148: underlyingSession.release();
149:
150: startVerification();
151:
152: ClusteredManager manager = newManager();
153: Session session = manager.createSession(null);
154: HttpSession httpSession = session.getSession();
155: httpSession.invalidate();
156: }
157:
158: public void testSessionEndAccessTriggersOnEndAccess()
159: throws Exception {
160: org.apache.geronimo.clustering.Session underlyingSession = recordCreateUnderlyingSession();
161: underlyingSession.onEndAccess();
162:
163: startVerification();
164:
165: ClusteredManager manager = newManager();
166: Session session = manager.createSession(null);
167: session.endAccess();
168: }
169:
170: private org.apache.geronimo.clustering.Session recordCreateUnderlyingSession()
171: throws Exception {
172: org.apache.geronimo.clustering.Session underlyingSession = sessionManager
173: .createSession(sessionId);
174: recordUnderlyingSessionState(underlyingSession);
175:
176: return underlyingSession;
177: }
178:
179: private void recordUnderlyingSessionState(
180: org.apache.geronimo.clustering.Session underlyingSession) {
181: underlyingSession.getSessionId();
182: modify().multiplicity(expect.from(0)).returnValue(sessionId);
183:
184: underlyingSession.getState();
185: Map attributes = new HashMap();
186: attributes.put("key", "value");
187: modify().returnValue(attributes);
188: }
189:
190: private ClusteredManager newManager() {
191: ClusteredManager manager = new ClusteredManager(sessionManager) {
192: @Override
193: protected synchronized String generateSessionId() {
194: return sessionId;
195: }
196: };
197: manager.setContainer(context);
198: return manager;
199: }
200:
201: }
|