Selaa lähdekoodia

SEC-169: Add SessionRegistry.getAllPrincipals() method.

Ben Alex 19 vuotta sitten
vanhempi
commit
8c0ce12332

+ 11 - 2
core/src/main/java/org/acegisecurity/concurrent/SessionRegistry.java

@@ -1,4 +1,4 @@
-/* Copyright 2004, 2005 Acegi Technology Pty Limited
+/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,6 +24,14 @@ package org.acegisecurity.concurrent;
 public interface SessionRegistry {
     //~ Methods ================================================================
 
+    /**
+     * Obtains all the known principals in the <code>SessionRegistry</code>.
+     *
+     * @return each of the unique principals, which can then be presented to
+     *         {@link #getAllSessions(Object)}.
+     */
+    public Object[] getAllPrincipals();
+
     /**
      * Obtains all the known sessions for the specified principal. Sessions
      * that have expired or destroyed are not returned.
@@ -50,7 +58,8 @@ public interface SessionRegistry {
     /**
      * Updates the given <code>sessionId</code> so its last request time is
      * equal to the present date and time. Silently returns if the given
-     * <code>sessionId</code> cannot be found or the session is marked to expire.
+     * <code>sessionId</code> cannot be found or the session is marked to
+     * expire.
      *
      * @param sessionId for which to update the date and time of the last
      *        request (should never be <code>null</code>)

+ 4 - 0
core/src/main/java/org/acegisecurity/concurrent/SessionRegistryImpl.java

@@ -144,4 +144,8 @@ public class SessionRegistryImpl implements SessionRegistry,
             }
         }
     }
+
+	public Object[] getAllPrincipals() {
+		return principals.keySet().toArray();
+	}
 }

+ 36 - 20
core/src/test/java/org/acegisecurity/concurrent/SessionRegistryImplTests.java

@@ -1,4 +1,4 @@
-/* Copyright 2004, 2005 Acegi Technology Pty Limited
+/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -52,6 +52,23 @@ public class SessionRegistryImplTests extends TestCase {
         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";
@@ -95,7 +112,7 @@ public class SessionRegistryImplTests extends TestCase {
         assertNull(sessionRegistry.getAllSessions(principal));
     }
 
-    public void testTwoSessionsOnePrincipalHandling() throws Exception {
+    public void testTwoSessionsOnePrincipalExpiring() throws Exception {
         Object principal = "Some principal object";
         String sessionId1 = "1234567890";
         String sessionId2 = "9876543210";
@@ -113,19 +130,16 @@ public class SessionRegistryImplTests extends TestCase {
         assertEquals(sessionId2,
             sessionRegistry.getAllSessions(principal)[1].getSessionId());
 
-        // Clear session information
-        sessionRegistry.removeSessionInformation(sessionId1);
-        assertEquals(1, sessionRegistry.getAllSessions(principal).length);
-        assertEquals(sessionId2,
-            sessionRegistry.getAllSessions(principal)[0].getSessionId());
+        // Expire one session
+        SessionInformation session = sessionRegistry.getSessionInformation(sessionId2);
+        session.expireNow();
 
-        // Clear final session
-        sessionRegistry.removeSessionInformation(sessionId2);
-        assertNull(sessionRegistry.getSessionInformation(sessionId2));
-        assertNull(sessionRegistry.getAllSessions(principal));
+        // Check retrieval still correct
+        assertTrue(sessionRegistry.getSessionInformation(sessionId2).isExpired());
+        assertFalse(sessionRegistry.getSessionInformation(sessionId1).isExpired());
     }
 
-    public void testTwoSessionsOnePrincipalExpiring() throws Exception {
+    public void testTwoSessionsOnePrincipalHandling() throws Exception {
         Object principal = "Some principal object";
         String sessionId1 = "1234567890";
         String sessionId2 = "9876543210";
@@ -143,13 +157,15 @@ public class SessionRegistryImplTests extends TestCase {
         assertEquals(sessionId2,
             sessionRegistry.getAllSessions(principal)[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());
-    }
+        // Clear session information
+        sessionRegistry.removeSessionInformation(sessionId1);
+        assertEquals(1, sessionRegistry.getAllSessions(principal).length);
+        assertEquals(sessionId2,
+            sessionRegistry.getAllSessions(principal)[0].getSessionId());
 
+        // Clear final session
+        sessionRegistry.removeSessionInformation(sessionId2);
+        assertNull(sessionRegistry.getSessionInformation(sessionId2));
+        assertNull(sessionRegistry.getAllSessions(principal));
+    }
 }