001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.concurrent;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.ui.session.HttpSessionDestroyedEvent;
021:
022: import org.springframework.mock.web.MockHttpSession;
023:
024: import java.util.Date;
025:
026: /**
027: * Tests {@link SessionRegistryImpl}.
028: *
029: * @author Ben Alex
030: * @version $Id: SessionRegistryImplTests.java 1496 2006-05-23 13:38:33Z benalex $
031: */
032: public class SessionRegistryImplTests extends TestCase {
033: //~ Methods ========================================================================================================
034:
035: public void testEventPublishing() {
036: MockHttpSession httpSession = new MockHttpSession();
037: Object principal = "Some principal object";
038: String sessionId = httpSession.getId();
039: assertNotNull(sessionId);
040:
041: SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
042:
043: // Register new Session
044: sessionRegistry.registerNewSession(sessionId, principal);
045:
046: // Deregister session via an ApplicationEvent
047: sessionRegistry
048: .onApplicationEvent(new HttpSessionDestroyedEvent(
049: httpSession));
050:
051: // Check attempts to retrieve cleared session return null
052: assertNull(sessionRegistry.getSessionInformation(sessionId));
053: }
054:
055: public void testMultiplePrincipals() throws Exception {
056: Object principal1 = "principal_1";
057: Object principal2 = "principal_2";
058: String sessionId1 = "1234567890";
059: String sessionId2 = "9876543210";
060: String sessionId3 = "5432109876";
061:
062: SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
063:
064: sessionRegistry.registerNewSession(sessionId1, principal1);
065: sessionRegistry.registerNewSession(sessionId2, principal1);
066: sessionRegistry.registerNewSession(sessionId3, principal2);
067:
068: assertEquals(principal1, sessionRegistry.getAllPrincipals()[0]);
069: assertEquals(principal2, sessionRegistry.getAllPrincipals()[1]);
070: }
071:
072: public void testSessionInformationLifecycle() throws Exception {
073: Object principal = "Some principal object";
074: String sessionId = "1234567890";
075: SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
076:
077: // Register new Session
078: sessionRegistry.registerNewSession(sessionId, principal);
079:
080: // Retrieve existing session by session ID
081: Date currentDateTime = sessionRegistry.getSessionInformation(
082: sessionId).getLastRequest();
083: assertEquals(principal, sessionRegistry.getSessionInformation(
084: sessionId).getPrincipal());
085: assertEquals(sessionId, sessionRegistry.getSessionInformation(
086: sessionId).getSessionId());
087: assertNotNull(sessionRegistry.getSessionInformation(sessionId)
088: .getLastRequest());
089:
090: // Retrieve existing session by principal
091: assertEquals(1, sessionRegistry
092: .getAllSessions(principal, false).length);
093:
094: // Sleep to ensure SessionRegistryImpl will update time
095: Thread.sleep(1000);
096:
097: // Update request date/time
098: sessionRegistry.refreshLastRequest(sessionId);
099:
100: Date retrieved = sessionRegistry.getSessionInformation(
101: sessionId).getLastRequest();
102: assertTrue(retrieved.after(currentDateTime));
103:
104: // Check it retrieves correctly when looked up via principal
105: assertEquals(retrieved, sessionRegistry.getAllSessions(
106: principal, false)[0].getLastRequest());
107:
108: // Clear session information
109: sessionRegistry.removeSessionInformation(sessionId);
110:
111: // Check attempts to retrieve cleared session return null
112: assertNull(sessionRegistry.getSessionInformation(sessionId));
113: assertNull(sessionRegistry.getAllSessions(principal, false));
114: }
115:
116: public void testTwoSessionsOnePrincipalExpiring() throws Exception {
117: Object principal = "Some principal object";
118: String sessionId1 = "1234567890";
119: String sessionId2 = "9876543210";
120: SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
121:
122: // Register new Session
123: sessionRegistry.registerNewSession(sessionId1, principal);
124: assertEquals(1, sessionRegistry
125: .getAllSessions(principal, false).length);
126: assertEquals(sessionId1, sessionRegistry.getAllSessions(
127: principal, false)[0].getSessionId());
128:
129: // Register new Session
130: sessionRegistry.registerNewSession(sessionId2, principal);
131: assertEquals(2, sessionRegistry
132: .getAllSessions(principal, false).length);
133: assertEquals(sessionId2, sessionRegistry.getAllSessions(
134: principal, false)[1].getSessionId());
135:
136: // Expire one session
137: SessionInformation session = sessionRegistry
138: .getSessionInformation(sessionId2);
139: session.expireNow();
140:
141: // Check retrieval still correct
142: assertTrue(sessionRegistry.getSessionInformation(sessionId2)
143: .isExpired());
144: assertFalse(sessionRegistry.getSessionInformation(sessionId1)
145: .isExpired());
146: }
147:
148: public void testTwoSessionsOnePrincipalHandling() throws Exception {
149: Object principal = "Some principal object";
150: String sessionId1 = "1234567890";
151: String sessionId2 = "9876543210";
152: SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
153:
154: // Register new Session
155: sessionRegistry.registerNewSession(sessionId1, principal);
156: assertEquals(1, sessionRegistry
157: .getAllSessions(principal, false).length);
158: assertEquals(sessionId1, sessionRegistry.getAllSessions(
159: principal, false)[0].getSessionId());
160:
161: // Register new Session
162: sessionRegistry.registerNewSession(sessionId2, principal);
163: assertEquals(2, sessionRegistry
164: .getAllSessions(principal, false).length);
165: assertEquals(sessionId2, sessionRegistry.getAllSessions(
166: principal, false)[1].getSessionId());
167:
168: // Clear session information
169: sessionRegistry.removeSessionInformation(sessionId1);
170: assertEquals(1, sessionRegistry
171: .getAllSessions(principal, false).length);
172: assertEquals(sessionId2, sessionRegistry.getAllSessions(
173: principal, false)[0].getSessionId());
174:
175: // Clear final session
176: sessionRegistry.removeSessionInformation(sessionId2);
177: assertNull(sessionRegistry.getSessionInformation(sessionId2));
178: assertNull(sessionRegistry.getAllSessions(principal, false));
179: }
180: }
|