directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vte...@apache.org
Subject svn commit: rev 9343 - in incubator/directory/janus/trunk/sandbox/src: java/org/apache/janus/script/xml test/org/apache/janus/script/xml
Date Wed, 10 Mar 2004 23:17:31 GMT
Author: vtence
Date: Wed Mar 10 15:17:29 2004
New Revision: 9343

Modified:
   incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JRoleManagerBuilder.java
   incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/Dom4JRoleManagerBuilderTest.java
Log:
o Resolved DIR-45

Modified: incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JRoleManagerBuilder.java
==============================================================================
--- incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JRoleManagerBuilder.java
(original)
+++ incubator/directory/janus/trunk/sandbox/src/java/org/apache/janus/script/xml/Dom4JRoleManagerBuilder.java
Wed Mar 10 15:17:29 2004
@@ -16,8 +16,8 @@
  */
 package org.apache.janus.script.xml;
 
-import org.apache.janus.authorization.role.MutableRoleManager;
 import org.apache.janus.authentication.realm.UsernamePrincipal;
+import org.apache.janus.authorization.role.MutableRoleManager;
 import org.dom4j.Document;
 import org.dom4j.DocumentException;
 import org.dom4j.Element;
@@ -25,9 +25,9 @@
 
 import java.io.IOException;
 import java.io.Reader;
-import java.util.List;
-import java.util.Iterator;
 import java.security.Principal;
+import java.util.Iterator;
+import java.util.List;
 
 /**
  * <strong>Warning:</strong> Document is assumed to be valid.
@@ -47,6 +47,15 @@
     {
         Element root = m_doc.getRootElement();
         addRoles( roleManager, root );
+        populateRoles( roleManager, root );
+    }
+
+    private Document readDocument( Reader reader ) throws DocumentException
+    {
+        SAXReader xmlReader = new SAXReader();
+        Document doc = xmlReader.read( reader );
+
+        return doc;
     }
 
     private void addRoles( MutableRoleManager roleManager, Element roles )
@@ -58,8 +67,20 @@
             final Element element = (Element) it.next();
             String roleName = element.attributeValue( "name" );
             roleManager.addRole( roleName );
+        }
+    }
 
-            addUsersToRole( roleManager, roleName, element);
+    private void populateRoles( MutableRoleManager roleManager, Element roles )
+    {
+        List roleList = roles.elements( "role" );
+
+        for ( Iterator it = roleList.iterator(); it.hasNext(); )
+        {
+            final Element element = (Element) it.next();
+            String roleName = element.attributeValue( "name" );
+
+            addUsersToRole( roleManager, roleName, element );
+            addSubRolesToRole( roleManager, roleName, element );
         }
     }
 
@@ -74,15 +95,20 @@
             final Element element = (Element) it.next();
             String username = element.attributeValue( "username" );
             Principal user = new UsernamePrincipal( username );
-            roleManager.addPrincipalToRole( roleName,  user );
+            roleManager.addPrincipalToRole( roleName, user );
         }
     }
 
-    private Document readDocument( Reader reader ) throws DocumentException
+    private void addSubRolesToRole( MutableRoleManager roleManager, String roleName, Element
role )
     {
-        SAXReader xmlReader = new SAXReader();
-        Document doc = xmlReader.read( reader );
+        List roleList = role.elements( "role-ref" );
 
-        return doc;
+        for ( Iterator it = roleList.iterator(); it.hasNext(); )
+        {
+            Element subRole = (Element) it.next();
+            String subRoleName = subRole.attributeValue( "name" );
+
+            roleManager.addSubRole( roleName, subRoleName );
+        }
     }
 }

Modified: incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/Dom4JRoleManagerBuilderTest.java
==============================================================================
--- incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/Dom4JRoleManagerBuilderTest.java
(original)
+++ incubator/directory/janus/trunk/sandbox/src/test/org/apache/janus/script/xml/Dom4JRoleManagerBuilderTest.java
Wed Mar 10 15:17:29 2004
@@ -24,37 +24,26 @@
 import java.io.StringReader;
 
 /**
+ * test: duplicate role
+ * test: duplicate principal in role
+ * test: unknown role
+ * test: unkwnow sub-role
+ * test: role circular dependency
+ *
  * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
  */
 public class Dom4JRoleManagerBuilderTest extends junit.framework.TestCase
 {
+    private Mock m_mockRoleManager;
+
     public static void main( String[] args )
     {
         junit.textui.TestRunner.run( Dom4JRoleManagerBuilderTest.class );
     }
 
-    public void testEmptyRolesBuild() throws Exception
-    {
-        Dom4JRoleManagerBuilder builder = new Dom4JRoleManagerBuilder(
-                new StringReader( emptyRolesDefinition() ) );
-
-        Mock mockRoleManager = new Mock( MutableRoleManager.class );
-        mockRoleManager.expectAndReturn( "addRole", "member", true );
-        mockRoleManager.expectAndReturn( "addRole", "vip", true );
-
-        builder.buildRoleManager( (MutableRoleManager) mockRoleManager.proxy() );
-
-        mockRoleManager.verify();
-    }
-
-    private String emptyRolesDefinition()
+    protected void setUp() throws Exception
     {
-        String content = "<?xml version=\"1.0\"?>\n"
-                         + "<roles>\n"
-                         + "    <role name=\"member\"/>\n"
-                         + "    <role name=\"vip\"/>\n"
-                         + "</roles>";
-        return content;
+        m_mockRoleManager = new Mock( MutableRoleManager.class );
     }
 
     public void testSimpleRolesBuild() throws Exception
@@ -62,17 +51,17 @@
         Dom4JRoleManagerBuilder builder = new Dom4JRoleManagerBuilder(
                 new StringReader( simpleRolesDefinition() ) );
 
-        Mock mockRoleManager = new Mock( MutableRoleManager.class );
-        mockRoleManager.matchAndReturn( "addRole", C.ANY_ARGS, true );
+        m_mockRoleManager.expectAndReturn( "addRole", "member", true );
+        m_mockRoleManager.expectAndReturn( "addRole", "vip", true );
 
-        mockRoleManager.expectAndReturn( "addPrincipalToRole",
+        m_mockRoleManager.expectAndReturn( "addPrincipalToRole",
                 C.args( C.eq( "member" ), C.eq( john() ) ), true );
-        mockRoleManager.expectAndReturn( "addPrincipalToRole",
+        m_mockRoleManager.expectAndReturn( "addPrincipalToRole",
                 C.args( C.eq( "member" ), C.eq( jane() ) ), true );
 
-        builder.buildRoleManager( (MutableRoleManager) mockRoleManager.proxy() );
+        builder.buildRoleManager( (MutableRoleManager) m_mockRoleManager.proxy() );
 
-        mockRoleManager.verify();
+        m_mockRoleManager.verify();
     }
 
     private String simpleRolesDefinition()
@@ -83,6 +72,7 @@
                          + "        <user username=\"john\"/>"
                          + "        <user username=\"jane\"/>"
                          + "    </role>\n"
+                         + "    <role name=\"vip\"/>\n"
                          + "</roles>";
         return content;
     }
@@ -95,5 +85,37 @@
     private UsernamePrincipal jane()
     {
         return new UsernamePrincipal( "jane" );
+    }
+
+    public void testSubRolesBuild() throws Exception
+    {
+        Dom4JRoleManagerBuilder builder = new Dom4JRoleManagerBuilder(
+                new StringReader( subRolesDefinition() ) );
+
+        m_mockRoleManager = new Mock( MutableRoleManager.class );
+        m_mockRoleManager.matchAndReturn( "addRole", C.ANY_ARGS, true);
+
+        m_mockRoleManager.expectAndReturn( "addSubRole",
+                C.args( C.eq( "member" ), C.eq( "vip" ) ), true );
+        m_mockRoleManager.expectAndReturn( "addSubRole",
+                C.args( C.eq( "member" ), C.eq( "guest" ) ), true );
+
+        builder.buildRoleManager( (MutableRoleManager) m_mockRoleManager.proxy() );
+
+        m_mockRoleManager.verify();
+    }
+
+    private String subRolesDefinition()
+    {
+        String content = "<?xml version=\"1.0\"?>\n"
+                         + "<roles>\n"
+                         + "    <role name=\"member\">\n"
+                         + "        <role-ref name=\"vip\"/>"
+                         + "        <role-ref name=\"guest\"/>"
+                         + "    </role>\n"
+                         + "    <role name=\"vip\"/>\n"
+                         + "    <role name=\"guest\"/>\n"
+                         + "</roles>";
+        return content;
     }
 }

Mime
View raw message