|
@@ -23,137 +23,153 @@ import org.springframework.mock.web.MockHttpSession;
|
|
|
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Tests {@link SessionRegistryImpl}.
|
|
* Tests {@link SessionRegistryImpl}.
|
|
- *
|
|
|
|
|
|
+ *
|
|
* @author Ben Alex
|
|
* @author Ben Alex
|
|
* @version $Id$
|
|
* @version $Id$
|
|
*/
|
|
*/
|
|
public class SessionRegistryImplTests extends TestCase {
|
|
public class SessionRegistryImplTests extends TestCase {
|
|
- //~ Methods ========================================================================================================
|
|
|
|
-
|
|
|
|
- public void testEventPublishing() {
|
|
|
|
- MockHttpSession httpSession = new MockHttpSession();
|
|
|
|
- Object principal = "Some principal object";
|
|
|
|
- String sessionId = httpSession.getId();
|
|
|
|
- assertNotNull(sessionId);
|
|
|
|
-
|
|
|
|
- SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
|
|
|
|
|
|
+ // ~ Methods
|
|
|
|
+ // ========================================================================================================
|
|
|
|
+
|
|
|
|
+ public void testEventPublishing() {
|
|
|
|
+ MockHttpSession httpSession = new MockHttpSession();
|
|
|
|
+ Object principal = "Some principal object";
|
|
|
|
+ String sessionId = httpSession.getId();
|
|
|
|
+ assertNotNull(sessionId);
|
|
|
|
+
|
|
|
|
+ SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
|
|
|
|
+
|
|
|
|
+ // Register new Session
|
|
|
|
+ sessionRegistry.registerNewSession(sessionId, principal);
|
|
|
|
+
|
|
|
|
+ // Deregister session via an ApplicationEvent
|
|
|
|
+ sessionRegistry.onApplicationEvent(new HttpSessionDestroyedEvent(
|
|
|
|
+ httpSession));
|
|
|
|
+
|
|
|
|
+ // Check attempts to retrieve cleared session return null
|
|
|
|
+ assertNull(sessionRegistry.getSessionInformation(sessionId));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void testMultiplePrincipals() throws Exception {
|
|
|
|
+ Object principal1 = "principal_1";
|
|
|
|
+ Object principal2 = "principal_2";
|
|
|
|
+ String sessionId1 = "1234567890";
|
|
|
|
+ String sessionId2 = "9876543210";
|
|
|
|
+ String sessionId3 = "5432109876";
|
|
|
|
+
|
|
|
|
+ SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
|
|
|
|
+
|
|
|
|
+ sessionRegistry.registerNewSession(sessionId1, principal1);
|
|
|
|
+ sessionRegistry.registerNewSession(sessionId2, principal1);
|
|
|
|
+ sessionRegistry.registerNewSession(sessionId3, principal2);
|
|
|
|
+
|
|
|
|
+ assertEquals(principal1, sessionRegistry.getAllPrincipals()[0]);
|
|
|
|
+ assertEquals(principal2, sessionRegistry.getAllPrincipals()[1]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void testSessionInformationLifecycle() throws Exception {
|
|
|
|
+ Object principal = "Some principal object";
|
|
|
|
+ String sessionId = "1234567890";
|
|
|
|
+ SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
|
|
|
|
+
|
|
|
|
+ // Register new Session
|
|
|
|
+ sessionRegistry.registerNewSession(sessionId, principal);
|
|
|
|
+
|
|
|
|
+ // Retrieve existing session by session ID
|
|
|
|
+ Date currentDateTime = sessionRegistry.getSessionInformation(sessionId)
|
|
|
|
+ .getLastRequest();
|
|
|
|
+ assertEquals(principal, sessionRegistry
|
|
|
|
+ .getSessionInformation(sessionId).getPrincipal());
|
|
|
|
+ assertEquals(sessionId, sessionRegistry
|
|
|
|
+ .getSessionInformation(sessionId).getSessionId());
|
|
|
|
+ assertNotNull(sessionRegistry.getSessionInformation(sessionId)
|
|
|
|
+ .getLastRequest());
|
|
|
|
+
|
|
|
|
+ // Retrieve existing session by principal
|
|
|
|
+ assertEquals(1, sessionRegistry.getAllSessions(principal, false).length);
|
|
|
|
+
|
|
|
|
+ // Sleep to ensure SessionRegistryImpl will update time
|
|
|
|
+ Thread.sleep(1000);
|
|
|
|
+
|
|
|
|
+ // Update request date/time
|
|
|
|
+ sessionRegistry.refreshLastRequest(sessionId);
|
|
|
|
+
|
|
|
|
+ Date retrieved = sessionRegistry.getSessionInformation(sessionId)
|
|
|
|
+ .getLastRequest();
|
|
|
|
+ assertTrue(retrieved.after(currentDateTime));
|
|
|
|
+
|
|
|
|
+ // Check it retrieves correctly when looked up via principal
|
|
|
|
+ assertEquals(retrieved, sessionRegistry
|
|
|
|
+ .getAllSessions(principal, false)[0].getLastRequest());
|
|
|
|
+
|
|
|
|
+ // Clear session information
|
|
|
|
+ sessionRegistry.removeSessionInformation(sessionId);
|
|
|
|
+
|
|
|
|
+ // Check attempts to retrieve cleared session return null
|
|
|
|
+ assertNull(sessionRegistry.getSessionInformation(sessionId));
|
|
|
|
+ assertNull(sessionRegistry.getAllSessions(principal, false));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void testTwoSessionsOnePrincipalExpiring() throws Exception {
|
|
|
|
+ Object principal = "Some principal object";
|
|
|
|
+ String sessionId1 = "1234567890";
|
|
|
|
+ String sessionId2 = "9876543210";
|
|
|
|
+ SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
|
|
|
|
+
|
|
|
|
+ // Register new Session
|
|
|
|
+ sessionRegistry.registerNewSession(sessionId1, principal);
|
|
|
|
+ assertEquals(1, sessionRegistry.getAllSessions(principal, false).length);
|
|
|
|
+ assertEquals(sessionId1, sessionRegistry.getAllSessions(principal,
|
|
|
|
+ false)[0].getSessionId());
|
|
|
|
+
|
|
|
|
+ // Register new Session
|
|
|
|
+ sessionRegistry.registerNewSession(sessionId2, principal);
|
|
|
|
+ assertEquals(2, sessionRegistry.getAllSessions(principal, false).length);
|
|
|
|
+ assertEquals(sessionId2, sessionRegistry.getAllSessions(principal,
|
|
|
|
+ false)[1].getSessionId());
|
|
|
|
+
|
|
|
|
+ // Expire one session
|
|
|
|
+ SessionInformation session = sessionRegistry
|
|
|
|
+ .getSessionInformation(sessionId2);
|
|
|
|
+ session.expireNow();
|
|
|
|
+
|
|
|
|
+ // Check retrieval still correct
|
|
|
|
+ assertTrue(sessionRegistry.getSessionInformation(sessionId2)
|
|
|
|
+ .isExpired());
|
|
|
|
+ assertFalse(sessionRegistry.getSessionInformation(sessionId1)
|
|
|
|
+ .isExpired());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void testTwoSessionsOnePrincipalHandling() throws Exception {
|
|
|
|
+ Object principal = "Some principal object";
|
|
|
|
+ String sessionId1 = "1234567890";
|
|
|
|
+ String sessionId2 = "9876543210";
|
|
|
|
+ SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
|
|
|
|
+
|
|
|
|
+ // Register new Session
|
|
|
|
+ sessionRegistry.registerNewSession(sessionId1, principal);
|
|
|
|
+ assertEquals(1, sessionRegistry.getAllSessions(principal, false).length);
|
|
|
|
+ assertEquals(sessionId1, sessionRegistry.getAllSessions(principal,
|
|
|
|
+ false)[0].getSessionId());
|
|
|
|
+
|
|
|
|
+ // Register new Session
|
|
|
|
+ sessionRegistry.registerNewSession(sessionId2, principal);
|
|
|
|
+ assertEquals(2, sessionRegistry.getAllSessions(principal, false).length);
|
|
|
|
+ assertEquals(sessionId2, sessionRegistry.getAllSessions(principal,
|
|
|
|
+ false)[1].getSessionId());
|
|
|
|
+
|
|
|
|
+ // Clear session information
|
|
|
|
+ sessionRegistry.removeSessionInformation(sessionId1);
|
|
|
|
+ assertEquals(1, sessionRegistry.getAllSessions(principal, false).length);
|
|
|
|
+ assertEquals(sessionId2, sessionRegistry.getAllSessions(principal,
|
|
|
|
+ false)[0].getSessionId());
|
|
|
|
+
|
|
|
|
+ // Clear final session
|
|
|
|
+ sessionRegistry.removeSessionInformation(sessionId2);
|
|
|
|
+ assertNull(sessionRegistry.getSessionInformation(sessionId2));
|
|
|
|
+ assertNull(sessionRegistry.getAllSessions(principal, false));
|
|
|
|
+ }
|
|
|
|
|
|
- // Register new Session
|
|
|
|
- sessionRegistry.registerNewSession(sessionId, principal);
|
|
|
|
-
|
|
|
|
- // Deregister session via an ApplicationEvent
|
|
|
|
- sessionRegistry.onApplicationEvent(new HttpSessionDestroyedEvent(httpSession));
|
|
|
|
-
|
|
|
|
- // Check attempts to retrieve cleared session return null
|
|
|
|
- assertNull(sessionRegistry.getSessionInformation(sessionId));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public void testMultiplePrincipals() throws Exception {
|
|
|
|
- Object principal1 = "principal_1";
|
|
|
|
- Object principal2 = "principal_2";
|
|
|
|
- String sessionId1 = "1234567890";
|
|
|
|
- String sessionId2 = "9876543210";
|
|
|
|
- String sessionId3 = "5432109876";
|
|
|
|
-
|
|
|
|
- SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
|
|
|
|
-
|
|
|
|
- sessionRegistry.registerNewSession(sessionId1, principal1);
|
|
|
|
- sessionRegistry.registerNewSession(sessionId2, principal1);
|
|
|
|
- sessionRegistry.registerNewSession(sessionId3, principal2);
|
|
|
|
-
|
|
|
|
- assertEquals(principal1, sessionRegistry.getAllPrincipals()[0]);
|
|
|
|
- assertEquals(principal2, sessionRegistry.getAllPrincipals()[1]);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public void testSessionInformationLifecycle() throws Exception {
|
|
|
|
- Object principal = "Some principal object";
|
|
|
|
- String sessionId = "1234567890";
|
|
|
|
- SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
|
|
|
|
-
|
|
|
|
- // Register new Session
|
|
|
|
- sessionRegistry.registerNewSession(sessionId, principal);
|
|
|
|
-
|
|
|
|
- // Retrieve existing session by session ID
|
|
|
|
- Date currentDateTime = sessionRegistry.getSessionInformation(sessionId).getLastRequest();
|
|
|
|
- assertEquals(principal, sessionRegistry.getSessionInformation(sessionId).getPrincipal());
|
|
|
|
- assertEquals(sessionId, sessionRegistry.getSessionInformation(sessionId).getSessionId());
|
|
|
|
- assertNotNull(sessionRegistry.getSessionInformation(sessionId).getLastRequest());
|
|
|
|
-
|
|
|
|
- // Retrieve existing session by principal
|
|
|
|
- assertEquals(1, sessionRegistry.getAllSessions(principal, false).length);
|
|
|
|
-
|
|
|
|
- // Sleep to ensure SessionRegistryImpl will update time
|
|
|
|
- Thread.sleep(1000);
|
|
|
|
-
|
|
|
|
- // Update request date/time
|
|
|
|
- sessionRegistry.refreshLastRequest(sessionId);
|
|
|
|
-
|
|
|
|
- Date retrieved = sessionRegistry.getSessionInformation(sessionId).getLastRequest();
|
|
|
|
- assertTrue(retrieved.after(currentDateTime));
|
|
|
|
-
|
|
|
|
- // Check it retrieves correctly when looked up via principal
|
|
|
|
- assertEquals(retrieved, sessionRegistry.getAllSessions(principal, false)[0].getLastRequest());
|
|
|
|
-
|
|
|
|
- // Clear session information
|
|
|
|
- sessionRegistry.removeSessionInformation(sessionId);
|
|
|
|
-
|
|
|
|
- // Check attempts to retrieve cleared session return null
|
|
|
|
- assertNull(sessionRegistry.getSessionInformation(sessionId));
|
|
|
|
- assertNull(sessionRegistry.getAllSessions(principal, false));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public void testTwoSessionsOnePrincipalExpiring() throws Exception {
|
|
|
|
- Object principal = "Some principal object";
|
|
|
|
- String sessionId1 = "1234567890";
|
|
|
|
- String sessionId2 = "9876543210";
|
|
|
|
- SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
|
|
|
|
-
|
|
|
|
- // Register new Session
|
|
|
|
- sessionRegistry.registerNewSession(sessionId1, principal);
|
|
|
|
- assertEquals(1, sessionRegistry.getAllSessions(principal, false).length);
|
|
|
|
- assertEquals(sessionId1, sessionRegistry.getAllSessions(principal, false)[0].getSessionId());
|
|
|
|
-
|
|
|
|
- // Register new Session
|
|
|
|
- sessionRegistry.registerNewSession(sessionId2, principal);
|
|
|
|
- assertEquals(2, sessionRegistry.getAllSessions(principal, false).length);
|
|
|
|
- assertEquals(sessionId2, sessionRegistry.getAllSessions(principal, false)[1].getSessionId());
|
|
|
|
-
|
|
|
|
- // Expire one session
|
|
|
|
- SessionInformation session = sessionRegistry.getSessionInformation(sessionId2);
|
|
|
|
- session.expireNow();
|
|
|
|
-
|
|
|
|
- // Check retrieval still correct
|
|
|
|
- assertTrue(sessionRegistry.getSessionInformation(sessionId2).isExpired());
|
|
|
|
- assertFalse(sessionRegistry.getSessionInformation(sessionId1).isExpired());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public void testTwoSessionsOnePrincipalHandling() throws Exception {
|
|
|
|
- Object principal = "Some principal object";
|
|
|
|
- String sessionId1 = "1234567890";
|
|
|
|
- String sessionId2 = "9876543210";
|
|
|
|
- SessionRegistryImpl sessionRegistry = new SessionRegistryImpl();
|
|
|
|
-
|
|
|
|
- // Register new Session
|
|
|
|
- sessionRegistry.registerNewSession(sessionId1, principal);
|
|
|
|
- assertEquals(1, sessionRegistry.getAllSessions(principal, false).length);
|
|
|
|
- assertEquals(sessionId1, sessionRegistry.getAllSessions(principal, false)[0].getSessionId());
|
|
|
|
-
|
|
|
|
- // Register new Session
|
|
|
|
- sessionRegistry.registerNewSession(sessionId2, principal);
|
|
|
|
- assertEquals(2, sessionRegistry.getAllSessions(principal, false).length);
|
|
|
|
- assertEquals(sessionId2, sessionRegistry.getAllSessions(principal, false)[1].getSessionId());
|
|
|
|
-
|
|
|
|
- // Clear session information
|
|
|
|
- sessionRegistry.removeSessionInformation(sessionId1);
|
|
|
|
- assertEquals(1, sessionRegistry.getAllSessions(principal, false).length);
|
|
|
|
- assertEquals(sessionId2, sessionRegistry.getAllSessions(principal, false)[0].getSessionId());
|
|
|
|
-
|
|
|
|
- // Clear final session
|
|
|
|
- sessionRegistry.removeSessionInformation(sessionId2);
|
|
|
|
- assertNull(sessionRegistry.getSessionInformation(sessionId2));
|
|
|
|
- assertNull(sessionRegistry.getAllSessions(principal, false));
|
|
|
|
- }
|
|
|
|
}
|
|
}
|