private boolean contains(String sessionId, Object principal) { List<SessionInformation> info = sessionRegistry.getAllSessions(principal, false); for (int i = 0; i < info.size(); i++) { if (sessionId.equals(info.get(i).getSessionId())) { return true; } } return false; } }
@Test public void testTwoSessionsOnePrincipalHandling() throws Exception { Object principal = "Some principal object"; String sessionId1 = "1234567890"; String sessionId2 = "9876543210"; sessionRegistry.registerNewSession(sessionId1, principal); List<SessionInformation> sessions = sessionRegistry.getAllSessions(principal, false); assertThat(sessions).hasSize(1); assertThat(contains(sessionId1, principal)).isTrue(); sessionRegistry.registerNewSession(sessionId2, principal); sessions = sessionRegistry.getAllSessions(principal, false); assertThat(sessions).hasSize(2); assertThat(contains(sessionId2, principal)).isTrue(); sessionRegistry.removeSessionInformation(sessionId1); sessions = sessionRegistry.getAllSessions(principal, false); assertThat(sessions).hasSize(1); assertThat(contains(sessionId2, principal)).isTrue(); sessionRegistry.removeSessionInformation(sessionId2); assertThat(sessionRegistry.getSessionInformation(sessionId2)).isNull(); assertThat(sessionRegistry.getAllSessions(principal, false)).isEmpty(); }
@Test public void testTwoSessionsOnePrincipalExpiring() throws Exception { Object principal = "Some principal object"; String sessionId1 = "1234567890"; String sessionId2 = "9876543210"; sessionRegistry.registerNewSession(sessionId1, principal); List<SessionInformation> sessions = sessionRegistry.getAllSessions(principal, false); assertThat(sessions).hasSize(1); assertThat(contains(sessionId1, principal)).isTrue(); sessionRegistry.registerNewSession(sessionId2, principal); sessions = sessionRegistry.getAllSessions(principal, false); assertThat(sessions).hasSize(2); assertThat(contains(sessionId2, principal)).isTrue(); // Expire one session SessionInformation session = sessionRegistry.getSessionInformation(sessionId2); session.expireNow(); // Check retrieval still correct assertThat(sessionRegistry.getSessionInformation(sessionId2).isExpired()).isTrue(); assertThat(sessionRegistry.getSessionInformation(sessionId1).isExpired()).isFalse(); }
@Test public void testSessionInformationLifecycle() throws Exception { Object principal = "Some principal object"; String sessionId = "1234567890"; // Register new Session sessionRegistry.registerNewSession(sessionId, principal); // Retrieve existing session by session ID Date currentDateTime = sessionRegistry.getSessionInformation(sessionId) .getLastRequest(); assertThat(sessionRegistry.getSessionInformation(sessionId).getPrincipal()).isEqualTo(principal); assertThat(sessionRegistry.getSessionInformation(sessionId).getSessionId()).isEqualTo(sessionId); assertThat(sessionRegistry.getSessionInformation(sessionId).getLastRequest()).isNotNull(); // Retrieve existing session by principal assertThat(sessionRegistry.getAllSessions(principal, false)).hasSize(1); // Sleep to ensure SessionRegistryImpl will update time Thread.sleep(1000); // Update request date/time sessionRegistry.refreshLastRequest(sessionId); Date retrieved = sessionRegistry.getSessionInformation(sessionId) .getLastRequest(); assertThat(retrieved.after(currentDateTime)).isTrue(); // Check it retrieves correctly when looked up via principal assertThat(sessionRegistry.getAllSessions(principal, false).get(0).getLastRequest()).isCloseTo(retrieved, 2000L); // Clear session information sessionRegistry.removeSessionInformation(sessionId); // Check attempts to retrieve cleared session return null assertThat(sessionRegistry.getSessionInformation(sessionId)).isNull(); assertThat(sessionRegistry.getAllSessions(principal, false)).isEmpty(); }