directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vte...@apache.org
Subject svn commit: rev 57543 - in incubator/directory/janus/trunk/sandbox/src: java/org/apache/janus/authentication/attribute test/org/apache/janus/authentication/attribute
Date Fri, 12 Nov 2004 21:53:27 GMT
Author: vtence
Date: Fri Nov 12 13:53:26 2004
New Revision: 57543

Modified:
   incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/AttributeProvider.java
   incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/attribute/AttributeProviderTest.java
Log:
Refactored following addition of mutable functionality

Modified: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/AttributeProvider.java
==============================================================================
--- incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/AttributeProvider.java
(original)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/authentication/attribute/AttributeProvider.java
Fri Nov 12 13:53:26 2004
@@ -20,9 +20,9 @@
 
 import javax.security.auth.Subject;
 import java.security.Principal;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 
 public class AttributeProvider implements InformationProvider
@@ -45,24 +45,29 @@
         for ( int i = 0; i < principals.length; i++ )
         {
             Principal p = principals[i];
-            Collection attributes = ( Collection ) m_attributes.get( p );
-            if ( attributes != null ) s.getPrincipals().addAll( attributes );
+            s.getPrincipals().addAll( attributes( p ) );
         }
     }
 
-    public void addAttribute( Principal principal, Principal attribute )
+    public boolean addAllAttributes( Principal principal, Collection attributes )
     {
-        if ( !m_attributes.containsKey( principal ) ) m_attributes.put( principal, new ArrayList()
);
-
-        Collection principalAttributes = ( Collection ) m_attributes.get( principal );
-        principalAttributes.add( attribute );
+        return attributes( principal ).addAll( attributes );
     }
 
-    public void addAllAttributes( Principal principal, Collection attributes )
+    public boolean addAttribute( Principal principal, Principal attribute )
     {
-        if ( !m_attributes.containsKey( principal ) ) m_attributes.put( principal, new ArrayList()
);
+        return attributes( principal ).add( attribute );
+    }
 
-        Collection principalAttributes = ( Collection ) m_attributes.get( principal );
-        principalAttributes.addAll( attributes );
+    private Collection attributes( Principal principal )
+    {
+        Collection attributes = ( Collection ) m_attributes.get( principal );
+        if ( attributes == null)
+        {
+            attributes = new HashSet();
+            m_attributes.put( principal, attributes );
+        }
+        return attributes;
     }
 }
+

Modified: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/attribute/AttributeProviderTest.java
==============================================================================
--- incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/attribute/AttributeProviderTest.java
(original)
+++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/authentication/attribute/AttributeProviderTest.java
Fri Nov 12 13:53:26 2004
@@ -27,6 +27,7 @@
 import java.security.Principal;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 
 public class AttributeProviderTest extends TestCase
 {
@@ -38,8 +39,6 @@
     }
 
     /**
-     * o ignore duplicates
-     *
      * o population traverses entire graph of associations
      */
 
@@ -69,9 +68,25 @@
     public void testPrincipalHasNoAttributeByDefault()
     {
         m_provider = new AttributeProvider();
-        Subject s = new Subject();
-        m_provider.populate( s );
+        Subject joe = Subjects.joe();
+        m_provider.populate( joe );
+
+        Collection expectedPrincipals = Collections.singleton( Usernames.joe() );
+        Collection actualPrincipals = joe.getPrincipals();
+        assertTrue( CollectionUtils.isEqualCollection( expectedPrincipals, actualPrincipals
) );
+    }
+
+    public void testDuplicateAttributesAreIgnored()
+    {
+        m_provider = new AttributeProvider();
+        assertTrue( m_provider.addAttribute( Usernames.joe(), Groups.geeks() ) );
+        assertFalse( m_provider.addAttribute( Usernames.joe(), Groups.geeks() ) );
+
+        Subject joe = Subjects.joe();
+        m_provider.populate( joe );
 
-        assertTrue( s.getPrincipals( GroupPrincipal.class ).isEmpty() );
+        Collection expectedPrincipals = Collections.singleton( Groups.geeks() );
+        Collection actualPrincipals = joe.getPrincipals( GroupPrincipal.class );
+        assertTrue( CollectionUtils.isEqualCollection( expectedPrincipals, actualPrincipals
) );
     }
 }

Mime
View raw message