001: /* ------------------------------------------------------------------------
002: * $Id$
003: * Copyright 2006 Tim Vernum
004: * ------------------------------------------------------------------------
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: * ------------------------------------------------------------------------
017: */
018:
019: package org.mortbay.jetty.servlet;
020:
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpSession;
023:
024: import junit.framework.TestCase;
025:
026: import org.mortbay.component.AbstractLifeCycle;
027: import org.mortbay.jetty.Server;
028: import org.mortbay.jetty.SessionIdManager;
029: import org.mortbay.jetty.handler.ContextHandler;
030:
031: /**
032: * @version $Revision$
033: */
034: public class SessionManagerTest extends TestCase {
035: TestSessionIdManager idManager = new TestSessionIdManager();
036: HashSessionManager sessionManager = new HashSessionManager();
037: SessionHandler handler = new SessionHandler(sessionManager);
038: Server server = new Server();
039:
040: protected void setUp() throws Exception {
041: sessionManager.setIdManager(idManager);
042: ContextHandler context = new ContextHandler();
043: sessionManager.setSessionHandler(handler);
044: server.setHandler(context);
045: context.setHandler(handler);
046: server.start();
047: }
048:
049: protected void tearDown() throws Exception {
050: server.stop();
051: }
052:
053: public void testSetAttributeToNullIsTheSameAsRemoveAttribute()
054: throws Exception {
055: HttpSession session = sessionManager.newHttpSession(null);
056:
057: assertNull(session.getAttribute("foo"));
058: assertFalse(session.getAttributeNames().hasMoreElements());
059: session.setAttribute("foo", this );
060: assertNotNull(session.getAttribute("foo"));
061: assertTrue(session.getAttributeNames().hasMoreElements());
062: session.removeAttribute("foo");
063: assertNull(session.getAttribute("foo"));
064: assertFalse(session.getAttributeNames().hasMoreElements());
065: session.setAttribute("foo", this );
066: assertNotNull(session.getAttribute("foo"));
067: assertTrue(session.getAttributeNames().hasMoreElements());
068: session.setAttribute("foo", null);
069: assertNull(session.getAttribute("foo"));
070: assertFalse(session.getAttributeNames().hasMoreElements());
071: }
072:
073: public void testWorker() throws Exception {
074: try {
075: idManager._worker = "node0";
076: HttpSession session = sessionManager.newHttpSession(null);
077:
078: assertTrue(session.getId().endsWith("node0"));
079: String id0 = session.getId();
080: String clusterId = id0.substring(0, id0.lastIndexOf('.'));
081: String id1 = clusterId + ".node1";
082:
083: assertTrue(sessionManager.getSessionCookie(session,
084: "/context", false) != null);
085:
086: assertEquals(session, sessionManager.getHttpSession(session
087: .getId()));
088: assertTrue(sessionManager.access(session, false) == null);
089:
090: assertEquals(session, sessionManager.getHttpSession(id1));
091: assertTrue(sessionManager.access(session, false) != null);
092:
093: } finally {
094: idManager._worker = null;
095: }
096: }
097:
098: class TestSessionIdManager extends AbstractLifeCycle implements
099: SessionIdManager {
100: String _worker;
101:
102: public boolean idInUse(String id) {
103: return false;
104: }
105:
106: public void addSession(HttpSession session) {
107: // Ignore
108: }
109:
110: public void invalidateAll(String id) {
111: // Ignore
112: }
113:
114: public String newSessionId(HttpServletRequest request,
115: long created) {
116: return "xyzzy";
117: }
118:
119: public void removeSession(HttpSession session) {
120: // ignore
121: }
122:
123: public String getWorkerName() {
124: return _worker;
125: }
126:
127: }
128: }
|