You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by er...@apache.org on 2005/10/19 16:12:12 UTC

svn commit: r326569 - in /directory/shared/ldap/trunk/common/src: antlr/ACIItem.g java/org/apache/ldap/common/util/NoDuplicateKeysMap.java test/org/apache/ldap/common/aci/ACIItemParserTest.java

Author: ersiner
Date: Wed Oct 19 07:11:58 2005
New Revision: 326569

URL: http://svn.apache.org/viewcvs?rev=326569&view=rev
Log:
changes:
 - Added duplication check to the grammar for ProtectedItems and UserClasses
 - Added a NoDuplicateKeysMap utility class that does not permit adding duplicate keys
 - Added test cases for new flexible grammar

Added:
    directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/util/NoDuplicateKeysMap.java
Modified:
    directory/shared/ldap/trunk/common/src/antlr/ACIItem.g
    directory/shared/ldap/trunk/common/src/test/org/apache/ldap/common/aci/ACIItemParserTest.java

Modified: directory/shared/ldap/trunk/common/src/antlr/ACIItem.g
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/antlr/ACIItem.g?rev=326569&r1=326568&r2=326569&view=diff
==============================================================================
--- directory/shared/ldap/trunk/common/src/antlr/ACIItem.g (original)
+++ directory/shared/ldap/trunk/common/src/antlr/ACIItem.g Wed Oct 19 07:11:58 2005
@@ -22,7 +22,9 @@
 
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 import java.util.Enumeration;
 
@@ -42,6 +44,7 @@
 import org.apache.ldap.common.subtree.SubtreeSpecification;
 import org.apache.ldap.common.subtree.SubtreeSpecificationModifier;
 import org.apache.ldap.common.util.NamespaceTools;
+import org.apache.ldap.common.util.NoDuplicateKeysMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -105,7 +108,9 @@
     
     // shared global data needed to avoid extensive pass/return stuff
     private Set m_protectedItems;
+    private Map m_protectedItemsMap;
     private Set m_userClasses;
+    private Map m_userClassesMap;
     private Set m_itemPermissions;
     private int m_precedence;
     private Set m_grantsAndDenials;
@@ -333,18 +338,25 @@
 protectedItems
 {
     log.debug( "entered protectedItems()" );
-    m_protectedItems = new HashSet();
+    m_protectedItemsMap = new NoDuplicateKeysMap();
 }
     :
     ID_protectedItems ( SP )*
         OPEN_CURLY ( SP )*
             (
-                //TODO: Add duplication checks
                 protectedItem ( SP )*
                     ( SEP ( SP )* protectedItem ( SP )* )*
             )?
         CLOSE_CURLY
+    {
+        m_protectedItems = new HashSet( m_protectedItemsMap.values() );
+    }
     ;
+    exception
+    catch [IllegalArgumentException e]
+    {
+        throw new RecognitionException( "Protected Items cannot be duplicated. " + e.getMessage() );
+    }
 
 protectedItem
 {
@@ -372,7 +384,7 @@
     :
     ID_entry
     {
-        m_protectedItems.add( ProtectedItem.ENTRY );
+        m_protectedItemsMap.put( "entry", ProtectedItem.ENTRY );
     }
     ;
 
@@ -383,7 +395,7 @@
     :
     ID_allUserAttributeTypes
     {
-        m_protectedItems.add( ProtectedItem.ALL_USER_ATTRIBUTE_TYPES );
+        m_protectedItemsMap.put( "allUserAttributeTypes", ProtectedItem.ALL_USER_ATTRIBUTE_TYPES );
     }
     ;
 
@@ -395,7 +407,7 @@
     :
     ID_attributeType ( SP )+ l_attributeTypeSet=attributeTypeSet
     {
-        m_protectedItems.add( new ProtectedItem.AttributeType( l_attributeTypeSet ) );
+        m_protectedItemsMap.put( "attributeType", new ProtectedItem.AttributeType( l_attributeTypeSet ) );
     }
     ;
 
@@ -407,7 +419,7 @@
     :
     ID_allAttributeValues ( SP )+ l_attributeTypeSet=attributeTypeSet
     {
-        m_protectedItems.add( new ProtectedItem.AllAttributeValues( l_attributeTypeSet ) );
+        m_protectedItemsMap.put( "allAttributeValues", new ProtectedItem.AllAttributeValues( l_attributeTypeSet ) );
     }
     ;
 
@@ -418,7 +430,7 @@
     :
     ID_allUserAttributeTypesAndValues
     {
-        m_protectedItems.add( ProtectedItem.ALL_USER_ATTRIBUTE_TYPES_AND_VALUES );
+        m_protectedItemsMap.put( "allUserAttributeTypesAndValues", ProtectedItem.ALL_USER_ATTRIBUTE_TYPES_AND_VALUES );
     }
     ;
 
@@ -445,7 +457,7 @@
             attributeSet.add( new BasicAttribute( attributeType, attributeValue ) );
             log.debug( "An attributeTypeAndValue from the set: " + attributeType + "=" +  attributeValue);
         }
-        m_protectedItems.add( new ProtectedItem.AttributeValue( attributeSet ) );
+        m_protectedItemsMap.put( "attributeValue", new ProtectedItem.AttributeValue( attributeSet ) );
     }
     ;
     exception
@@ -462,7 +474,7 @@
     :
     ID_selfValue ( SP )+ l_attributeTypeSet=attributeTypeSet
     {
-        m_protectedItems.add( new ProtectedItem.SelfValue( l_attributeTypeSet ) );
+        m_protectedItemsMap.put( "sefValue", new ProtectedItem.SelfValue( l_attributeTypeSet ) );
     }
     ;
 
@@ -473,7 +485,7 @@
     :
     token:RANGE_OF_VALUES_CANDIDATE
     {
-        m_protectedItems.add(
+        m_protectedItemsMap.put( "rangeOfValues",
                 new ProtectedItem.RangeOfValues(
                         filterParser.parse( token.getText() ) ) );
         log.debug( "filterParser parsed " + token.getText() );
@@ -505,7 +517,7 @@
             )*
     CLOSE_CURLY
     {
-        m_protectedItems.add( new ProtectedItem.MaxValueCount( maxValueCountSet ) );
+        m_protectedItemsMap.put( "maxValueCount", new ProtectedItem.MaxValueCount( maxValueCountSet ) );
     }
     ;
 
@@ -533,7 +545,7 @@
     ID_maxImmSub ( SP )+ token:INTEGER
     {
         
-        m_protectedItems.add(
+        m_protectedItemsMap.put( "maxImmSub",
                 new ProtectedItem.MaxImmSub(
                         token2Integer( token ) ) );
         
@@ -560,7 +572,7 @@
                     )*
         CLOSE_CURLY
     {
-        m_protectedItems.add( new ProtectedItem.RestrictedBy( l_restrictedBy ) );
+        m_protectedItemsMap.put( "restrictedBy", new ProtectedItem.RestrictedBy( l_restrictedBy ) );
     }
     ;
 
@@ -609,7 +621,7 @@
     :
     ID_classes ( SP )+ l_classes=refinement
     {
-        m_protectedItems.add( new ProtectedItem.Classes( l_classes ) );
+        m_protectedItemsMap.put( "classes", new ProtectedItem.Classes( l_classes ) );
     }
     ;
 
@@ -722,18 +734,25 @@
 userClasses
 {
     log.debug( "entered userClasses()" );
-    m_userClasses = new HashSet();
+    m_userClassesMap = new NoDuplicateKeysMap();
 }
     :
     ID_userClasses ( SP )+
     OPEN_CURLY ( SP )*
         (
-            //TODO: Add duplication checks
             userClass ( SP )*
                 ( SEP ( SP )* userClass ( SP )* )*
         )?
     CLOSE_CURLY
+    {
+        m_userClasses  = new HashSet( m_userClassesMap.values() );
+    }
     ;
+    exception
+    catch [IllegalArgumentException e]
+    {
+        throw new RecognitionException( "User Classes cannot be duplicated. " + e.getMessage() );
+    }
 
 userClass
 {
@@ -754,7 +773,7 @@
     :
     ID_allUsers
     {
-        m_userClasses.add( UserClass.ALL_USERS );
+        m_userClassesMap.put( "allUsers", UserClass.ALL_USERS );
     }
     ;
 
@@ -765,7 +784,7 @@
     :
     ID_thisEntry
     {
-        m_userClasses.add( UserClass.THIS_ENTRY );
+        m_userClassesMap.put( "thisEntry", UserClass.THIS_ENTRY );
     }
     ;
 
@@ -788,7 +807,7 @@
                 } )*
         CLOSE_CURLY
     {
-        m_userClasses.add( new UserClass.Name( l_name ) );
+        m_userClassesMap.put( "name", new UserClass.Name( l_name ) );
     }
     ;
 
@@ -811,7 +830,7 @@
                 } )*
         CLOSE_CURLY
     {
-        m_userClasses.add( new UserClass.UserGroup( l_userGroup ) );
+        m_userClassesMap.put( "userGroup", new UserClass.UserGroup( l_userGroup ) );
     }
     ;
 
@@ -834,7 +853,7 @@
                 } )*
         CLOSE_CURLY
     {
-        m_userClasses.add( new UserClass.Subtree( l_subtree ) );
+        m_userClassesMap.put( "subtree", new UserClass.Subtree( l_subtree ) );
     }
     ;
 

Added: directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/util/NoDuplicateKeysMap.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/util/NoDuplicateKeysMap.java?rev=326569&view=auto
==============================================================================
--- directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/util/NoDuplicateKeysMap.java (added)
+++ directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/util/NoDuplicateKeysMap.java Wed Oct 19 07:11:58 2005
@@ -0,0 +1,29 @@
+package org.apache.ldap.common.util;
+
+
+import java.util.HashMap;
+
+
+public class NoDuplicateKeysMap extends HashMap
+{
+    /**
+     * Overrides java.util.Map.put(java.lang.Object, java.lang.Object) to prevent duplicate keys.
+     *  
+     * @see java.util.Map#put(java.lang.Object, java.lang.Object)
+     */
+    public Object put( Object key, Object value ) throws IllegalArgumentException
+    {
+        if ( containsKey( key ) )
+        {
+            throw new IllegalArgumentException( "Adding duplicate keys is not permitted." );
+        }
+        else
+        {
+            return super.put( key, value );
+        }
+    }
+    
+    // add a serial version uid, so that if we change things in the future
+    // without changing the format, we can still deserialize properly.
+    private static final long serialVersionUID = 5107433500719957457L;
+}

Modified: directory/shared/ldap/trunk/common/src/test/org/apache/ldap/common/aci/ACIItemParserTest.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/test/org/apache/ldap/common/aci/ACIItemParserTest.java?rev=326569&r1=326568&r2=326569&view=diff
==============================================================================
--- directory/shared/ldap/trunk/common/src/test/org/apache/ldap/common/aci/ACIItemParserTest.java (original)
+++ directory/shared/ldap/trunk/common/src/test/org/apache/ldap/common/aci/ACIItemParserTest.java Wed Oct 19 07:11:58 2005
@@ -19,8 +19,7 @@
 package org.apache.ldap.common.aci;
 
 
-import org.apache.ldap.common.aci.ACIItem;
-import org.apache.ldap.common.aci.ACIItemParser;
+import java.text.ParseException;
 
 import junit.framework.TestCase;
 
@@ -66,16 +65,16 @@
      */    
     public void testItemFirst() throws Exception
     {
-        String spec1 = " {  identificationTag  \"id1\" , precedence 114  , authenticationLevel simple  , " +
-        "itemOrUserFirst itemFirst  :{ protectedItems  { entry  , attributeType { 1.2.3    , ou }  , " +
-        " attributeValue { ou=people  , cn=Ersin  }  , rangeOfValues (cn=ErsinEr) , " +
-        "classes and : { item: xyz , or:{item:X,item:Y}   }}  , " +
-        "itemPermissions { { userClasses {allUsers  , userGroup { \"1.2=y,z=t\"  , \"a=b,c=d\" } " +
-        " , subtree { { base \"ou=people\" } } }   , grantsAndDenials  {  denyCompare  , grantModify } }," +
-        "{ precedence 10, userClasses {allUsers  , userGroup { \"1.2=y,z=t\"  , \"a=b,c=d\" } " +
-        " , subtree { { base \"ou=people\" } } }   , grantsAndDenials  {  denyCompare  , grantModify } } } }}";
+        String spec = " {  identificationTag  \"id1\" , precedence 114  , authenticationLevel simple  , " +
+                "itemOrUserFirst itemFirst  :{ protectedItems  { entry  , attributeType { 1.2.3    , ou }  , " +
+                " attributeValue { ou=people  , cn=Ersin  }  , rangeOfValues (cn=ErsinEr) , " +
+                "classes and : { item: xyz , or:{item:X,item:Y}   }}  , " +
+                "itemPermissions { { userClasses {allUsers  , userGroup { \"1.2=y,z=t\"  , \"a=b,c=d\" } " +
+                " , subtree { { base \"ou=people\" } } }   , grantsAndDenials  {  denyCompare  , grantModify } }," +
+                "{ precedence 10, userClasses {allUsers  , userGroup { \"1.2=y,z=t\"  , \"a=b,c=d\" } " +
+                " , subtree { { base \"ou=people\" } } }   , grantsAndDenials  {  denyCompare  , grantModify } } } }}";
         
-        parser.parse(spec1);
+        parser.parse( spec );
     }
 
 
@@ -84,14 +83,14 @@
      */    
     public void testUserFirst() throws Exception
     {
-        String spec2 = "{ identificationTag \"id2\"   , precedence 14, authenticationLevel none  , " +
-        "itemOrUserFirst userFirst:  { userClasses {  allUsers  , name { \"ou=people,cn=ersin\" }, " +
-        "subtree {{ base \"ou=system\", specificationFilter and:{ } }, { base \"ou=ORGANIZATIONUNIT\"," +
-        "minimum  1, maximum   2 } } }  , " +
-        "userPermissions { { protectedItems{ entry  , attributeType { cn  , ou }  , attributeValue {x=y,m=n,k=l} , " +
-        "rangeOfValues (cn=ErsinEr) }  , grantsAndDenials { grantBrowse } } } }  }   ";
+        String spec = "{ identificationTag \"id2\"   , precedence 14, authenticationLevel none  , " +
+                "itemOrUserFirst userFirst:  { userClasses {  allUsers  , name { \"ou=people,cn=ersin\" }, " +
+                "subtree {{ base \"ou=system\", specificationFilter and:{ } }, { base \"ou=ORGANIZATIONUNIT\"," +
+                "minimum  1, maximum   2 } } }  , " +
+                "userPermissions { { protectedItems{ entry  , attributeType { cn  , ou }  , attributeValue {x=y,m=n,k=l} , " +
+                "rangeOfValues (cn=ErsinEr) }  , grantsAndDenials { grantBrowse } } } }  }   ";
         
-        parser.parse(spec2);
+        parser.parse( spec );
     }
 
 
@@ -104,6 +103,7 @@
                 "userClasses { allUsers }, " +
                 "userPermissions { { protectedItems {entry}, " +
                 "grantsAndDenials { grantAdd } } } } }";
+        
         parser.parse( spec );
     }
 
@@ -117,6 +117,7 @@
                 "userClasses { allUsers, name { \"ou=blah\" } }, " +
                 "userPermissions { { protectedItems {entry}, " +
                 "grantsAndDenials { grantAdd } } } } }";
+        
         parser.parse( spec );
     }
     
@@ -124,30 +125,68 @@
     public void testItemFirstOrderOfProtectedItemsDoesNotMatter() throws Exception
     {
         String spec = " {  identificationTag  \"id1\" , precedence 114  , authenticationLevel simple  , " +
-        "itemOrUserFirst itemFirst  :{ protectedItems  { attributeType { 1.2.3    , ou }, entry , " +
-        " rangeOfValues (cn=ErsinEr) , attributeValue { ou=people  , cn=Ersin  }," +
-        "classes and : { item: xyz , or:{item:X,item:Y}   }}  , " +
-        "itemPermissions { { userClasses {allUsers  , userGroup { \"1.2=y,z=t\"  , \"a=b,c=d\" } " +
-        " , subtree { { base \"ou=people\" } } }   , grantsAndDenials  {  denyCompare  , grantModify } }," +
-        "{ precedence 10, userClasses {allUsers  , userGroup { \"1.2=y,z=t\"  , \"a=b,c=d\" } " +
-        " , subtree { { base \"ou=people\" } } }   , grantsAndDenials  {  denyCompare  , grantModify } } } }}";
+                "itemOrUserFirst itemFirst  :{ protectedItems  { attributeType { 1.2.3    , ou }, entry , " +
+                " rangeOfValues (cn=ErsinEr) , attributeValue { ou=people  , cn=Ersin  }," +
+                "classes and : { item: xyz , or:{item:X,item:Y}   }}  , " +
+                "itemPermissions { { userClasses {allUsers  , userGroup { \"1.2=y,z=t\"  , \"a=b,c=d\" } " +
+                " , subtree { { base \"ou=people\" } } }   , grantsAndDenials  {  denyCompare  , grantModify } }," +
+                "{ precedence 10, userClasses {allUsers  , userGroup { \"1.2=y,z=t\"  , \"a=b,c=d\" } " +
+                " , subtree { { base \"ou=people\" } } }   , grantsAndDenials  {  denyCompare  , grantModify } } } }}";
         
-        /*ACIItem l_ACIItem = */parser.parse(spec);
-        
-        // System.out.println( l_ACIItem );
+        parser.parse(spec);
     }
     
     public void testUserFirstOrderOfUserClassesDoesNotMatter() throws Exception
     {
         String spec = "{ identificationTag \"id2\"   , precedence 14, authenticationLevel none  , " +
-        "itemOrUserFirst userFirst:  { userClasses {  name { \"ou=people,cn=ersin\" }, allUsers, " +
-        "subtree {{ base \"ou=system\", specificationFilter and:{ } }, { base \"ou=ORGANIZATIONUNIT\"," +
-        "minimum  1, maximum   2 } } }  , " +
-        "userPermissions { { protectedItems{ entry  , attributeType { cn  , ou }  , attributeValue {x=y,m=n,k=l} , " +
-        "rangeOfValues (cn=ErsinEr) }  , grantsAndDenials { grantBrowse } } } }  }   ";
-        
-        /*ACIItem l_ACIItem = */parser.parse(spec);
+                "itemOrUserFirst userFirst:  { userClasses {  name { \"ou=people,cn=ersin\" }, allUsers, " +
+                "subtree {{ base \"ou=system\", specificationFilter and:{ } }, { base \"ou=ORGANIZATIONUNIT\"," +
+                "minimum  1, maximum   2 } } }  , " +
+                "userPermissions { { protectedItems{ entry  , attributeType { cn  , ou }  , attributeValue {x=y,m=n,k=l} , " +
+                "rangeOfValues (cn=ErsinEr) }  , grantsAndDenials { grantBrowse } } } }  }   ";
         
-        // System.out.println( l_ACIItem );
+        parser.parse(spec);
+    }
+    
+    public void testItemFirstOrderOfProtectedItemsDoesNotMatterButDuplicatesMatter() throws Exception
+    {
+        String spec = " {  identificationTag  \"id1\" , precedence 114  , authenticationLevel simple  , " +
+                "itemOrUserFirst itemFirst  :{ protectedItems  { attributeType { 1.2.3    , ou }, entry, entry , " +
+                " rangeOfValues (cn=ErsinEr) , attributeValue { ou=people  , cn=Ersin  }," +
+                "classes and : { item: xyz , or:{item:X,item:Y}   }}  , " +
+                "itemPermissions { { userClasses {allUsers  , userGroup { \"1.2=y,z=t\"  , \"a=b,c=d\" } " +
+                " , subtree { { base \"ou=people\" } } }   , grantsAndDenials  {  denyCompare  , grantModify } }," +
+                "{ precedence 10, userClasses {allUsers  , userGroup { \"1.2=y,z=t\"  , \"a=b,c=d\" } " +
+                " , subtree { { base \"ou=people\" } } }   , grantsAndDenials  {  denyCompare  , grantModify } } } }}";
+
+        try
+        {
+            parser.parse(spec);
+            fail( "testItemFirstOrderOfProtectedItemsDoesNotMatterButDuplicatesMatter() should not have run this line." );
+        }
+        catch ( ParseException e )
+        {
+            assertNotNull( e );
+        }
+    }
+    
+    public void testUserFirstOrderOfUserClassesDoesNotMatterButDuplicatesMatter() throws Exception
+    {
+        String spec = "{ identificationTag \"id2\"   , precedence 14, authenticationLevel none  , " +
+                "itemOrUserFirst userFirst:  { userClasses {  name { \"ou=people,cn=ersin\" }, allUsers, allUsers, " +
+                "subtree {{ base \"ou=system\", specificationFilter and:{ } }, { base \"ou=ORGANIZATIONUNIT\"," +
+                "minimum  1, maximum   2 } } }  , " +
+                "userPermissions { { protectedItems{ entry  , attributeType { cn  , ou }  , attributeValue {x=y,m=n,k=l} , " +
+                "rangeOfValues (cn=ErsinEr) }  , grantsAndDenials { grantBrowse } } } }  }   ";
+        
+        try
+        {
+            parser.parse(spec);
+            fail( "testUserFirstOrderOfUserClassesDoesNotMatterButDuplicatesMatter() should not have run this line." );
+        }
+        catch ( ParseException e )
+        {
+            assertNotNull( e );
+        }
     }
 }