From f121b6ac9040ecfe26683eb3a82848792556de5b Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Tue, 29 Jan 2008 18:35:56 +0000 Subject: [PATCH] Fixed tests which were making assumptions about ordering within sets. --- .../concurrent/SessionRegistryImplTests.java | 60 +++++++++++-------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/core/src/test/java/org/springframework/security/concurrent/SessionRegistryImplTests.java b/core/src/test/java/org/springframework/security/concurrent/SessionRegistryImplTests.java index 6766b2b8e5..c0a6d191e4 100644 --- a/core/src/test/java/org/springframework/security/concurrent/SessionRegistryImplTests.java +++ b/core/src/test/java/org/springframework/security/concurrent/SessionRegistryImplTests.java @@ -22,6 +22,9 @@ import org.springframework.security.ui.session.HttpSessionDestroyedEvent; import org.springframework.mock.web.MockHttpSession; import java.util.Date; +import java.util.Set; +import java.util.HashSet; +import java.util.Arrays; /** @@ -31,16 +34,20 @@ import java.util.Date; * @version $Id$ */ public class SessionRegistryImplTests extends TestCase { + private SessionRegistryImpl sessionRegistry; + //~ Methods ======================================================================================================== + protected void setUp() throws Exception { + sessionRegistry = new SessionRegistryImpl(); + } + 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); @@ -58,8 +65,6 @@ public class SessionRegistryImplTests extends TestCase { String sessionId2 = "9876543210"; String sessionId3 = "5432109876"; - SessionRegistryImpl sessionRegistry = new SessionRegistryImpl(); - sessionRegistry.registerNewSession(sessionId1, principal1); sessionRegistry.registerNewSession(sessionId2, principal1); sessionRegistry.registerNewSession(sessionId3, principal2); @@ -71,8 +76,6 @@ public class SessionRegistryImplTests extends TestCase { public void testSessionInformationLifecycle() throws Exception { Object principal = "Some principal object"; String sessionId = "1234567890"; - SessionRegistryImpl sessionRegistry = new SessionRegistryImpl(); - // Register new Session sessionRegistry.registerNewSession(sessionId, principal); @@ -109,17 +112,16 @@ public class SessionRegistryImplTests extends TestCase { 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()); + SessionInformation[] sessions = sessionRegistry.getAllSessions(principal, false); + assertEquals(1, sessions.length); + assertTrue(contains(sessionId1, principal)); - // Register new Session sessionRegistry.registerNewSession(sessionId2, principal); - assertEquals(2, sessionRegistry.getAllSessions(principal, false).length); - assertEquals(sessionId2, sessionRegistry.getAllSessions(principal, false)[1].getSessionId()); + sessions = sessionRegistry.getAllSessions(principal, false); + assertEquals(2, sessions.length); + assertTrue(contains(sessionId2, principal)); // Expire one session SessionInformation session = sessionRegistry.getSessionInformation(sessionId2); @@ -134,26 +136,36 @@ public class SessionRegistryImplTests extends TestCase { 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()); + SessionInformation[] sessions = sessionRegistry.getAllSessions(principal, false); + assertEquals(1, sessions.length); + assertTrue(contains(sessionId1, principal)); - // Register new Session sessionRegistry.registerNewSession(sessionId2, principal); - assertEquals(2, sessionRegistry.getAllSessions(principal, false).length); - assertEquals(sessionId2, sessionRegistry.getAllSessions(principal, false)[1].getSessionId()); + sessions = sessionRegistry.getAllSessions(principal, false); + assertEquals(2, sessions.length); + assertTrue(contains(sessionId2, principal)); - // Clear session information sessionRegistry.removeSessionInformation(sessionId1); - assertEquals(1, sessionRegistry.getAllSessions(principal, false).length); - assertEquals(sessionId2, sessionRegistry.getAllSessions(principal, false)[0].getSessionId()); + sessions = sessionRegistry.getAllSessions(principal, false); + assertEquals(1, sessions.length); + assertTrue(contains(sessionId2, principal)); - // Clear final session sessionRegistry.removeSessionInformation(sessionId2); assertNull(sessionRegistry.getSessionInformation(sessionId2)); assertNull(sessionRegistry.getAllSessions(principal, false)); } + + boolean contains(String sessionId, Object principal) { + SessionInformation[] info = sessionRegistry.getAllSessions(principal, false); + + for (int i = 0; i < info.length; i++) { + if (sessionId.equals(info[i].getSessionId())) { + return true; + } + } + + return false; + } }