Browse Source

Fixed tests which were making assumptions about ordering within sets.

Luke Taylor 17 năm trước cách đây
mục cha
commit
f121b6ac90

+ 36 - 24
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;
+    }
 }