directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r366701 - in /directory/trunk: apacheds-server-unit/src/test/java/org/apache/ldap/server/ModifyAddTest.java apacheds/src/main/java/org/apache/ldap/server/schema/SchemaService.java
Date Sat, 07 Jan 2006 10:27:09 GMT
Author: akarasulu
Date: Sat Jan  7 02:27:02 2006
New Revision: 366701

URL: http://svn.apache.org/viewcvs?rev=366701&view=rev
Log:
fixed DIREVE-268 where duplicates in mods are not allowed - added tests and they pass now

Modified:
    directory/trunk/apacheds-server-unit/src/test/java/org/apache/ldap/server/ModifyAddTest.java
    directory/trunk/apacheds/src/main/java/org/apache/ldap/server/schema/SchemaService.java

Modified: directory/trunk/apacheds-server-unit/src/test/java/org/apache/ldap/server/ModifyAddTest.java
URL: http://svn.apache.org/viewcvs/directory/trunk/apacheds-server-unit/src/test/java/org/apache/ldap/server/ModifyAddTest.java?rev=366701&r1=366700&r2=366701&view=diff
==============================================================================
--- directory/trunk/apacheds-server-unit/src/test/java/org/apache/ldap/server/ModifyAddTest.java
(original)
+++ directory/trunk/apacheds-server-unit/src/test/java/org/apache/ldap/server/ModifyAddTest.java
Sat Jan  7 02:27:02 2006
@@ -24,6 +24,7 @@
 import javax.naming.directory.BasicAttribute;
 import javax.naming.directory.BasicAttributes;
 import javax.naming.directory.DirContext;
+import javax.naming.directory.ModificationItem;
 import javax.naming.ldap.InitialLdapContext;
 import javax.naming.ldap.LdapContext;
 
@@ -200,5 +201,57 @@
         assertNotNull(attr);
         assertTrue(attr.contains(PERSON_DESCRIPTION));
         assertEquals(1, attr.size());
+    }
+
+    /**
+     * Try to add a duplicate attribute value to an entry, where this attribute
+     * is already present (objectclass in this case). Expected behaviour is that
+     * the modify operation causes an error (error code 20, "Attribute or value
+     * exists").
+     * 
+     * @throws NamingException
+     */
+    public void testAddDuplicateValueToExistingAttribute() throws NamingException
+    {
+        // modify object classes, add a new value twice
+        Attribute ocls = new BasicAttribute("objectClass", "organizationalPerson");
+        ModificationItem[] modItems = new ModificationItem[2];
+        modItems[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, ocls);
+        modItems[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, ocls);
+        try {
+            ctx.modifyAttributes(RDN, modItems);
+            fail("Adding a duplicate attribute value should cause an error.");
+        } catch (AttributeInUseException ex) {}
+
+        // Check, whether attribute objectClass is unchanged
+        Attributes attrs = ctx.getAttributes(RDN);
+        ocls = attrs.get("objectClass");
+        assertEquals(ocls.size(), 2);
+        assertTrue(ocls.contains("top"));
+        assertTrue(ocls.contains("person"));
+    }
+
+    /**
+     * Try to add a duplicate attribute value to an entry, where this attribute
+     * is not present. Expected behaviour is that the modify operation causes an
+     * error (error code 20, "Attribute or value exists").
+     * 
+     * @throws NamingException
+     */
+    public void testAddDuplicateValueToNewAttribute() throws NamingException
+    {
+        // add the same description value twice
+        Attribute desc = new BasicAttribute("description", "another description value besides
songwriter");
+        ModificationItem[] modItems = new ModificationItem[2];
+        modItems[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, desc);
+        modItems[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, desc);
+        try {
+            ctx.modifyAttributes(RDN, modItems);
+            fail("Adding a duplicate attribute value should cause an error.");
+        } catch (AttributeInUseException ex) {}
+
+        // Check, whether attribute description is still not present
+        Attributes attrs = ctx.getAttributes(RDN);
+        assertEquals( 1, attrs.get("description").size() );
     }
 }

Modified: directory/trunk/apacheds/src/main/java/org/apache/ldap/server/schema/SchemaService.java
URL: http://svn.apache.org/viewcvs/directory/trunk/apacheds/src/main/java/org/apache/ldap/server/schema/SchemaService.java?rev=366701&r1=366700&r2=366701&view=diff
==============================================================================
--- directory/trunk/apacheds/src/main/java/org/apache/ldap/server/schema/SchemaService.java
(original)
+++ directory/trunk/apacheds/src/main/java/org/apache/ldap/server/schema/SchemaService.java
Sat Jan  7 02:27:02 2006
@@ -47,6 +47,7 @@
 import org.apache.ldap.common.util.DateUtils;
 import org.apache.ldap.common.util.AttributeUtils;
 import org.apache.ldap.common.util.StringTools;
+import org.apache.ldap.common.exception.LdapAttributeInUseException;
 import org.apache.ldap.common.exception.LdapSchemaViolationException;
 import org.apache.ldap.common.exception.LdapInvalidAttributeIdentifierException;
 import org.apache.ldap.common.exception.LdapNoSuchAttributeException;
@@ -559,13 +560,27 @@
     public void modify( NextInterceptor next, Name name, ModificationItem[] mods ) throws
NamingException
     {
         Attributes entry = nexus.lookup( name );
-
+        Set modset = new HashSet();
         ModificationItem objectClassMod = null;
+                    
         for ( int ii = 0; ii < mods.length; ii++ )
         {
             if ( mods[ii].getAttribute().getID().equalsIgnoreCase( "objectclass" ) )
             {
                 objectClassMod = mods[ii];
+            }
+            
+            StringBuffer keybuf = new StringBuffer();
+            keybuf.append( mods[ii].getModificationOp() );
+            keybuf.append( mods[ii].getAttribute().getID() );
+            for ( int jj = 0; jj < mods[ii].getAttribute().size(); jj++ )
+            {
+                keybuf.append( mods[ii].getAttribute().get( jj ) );
+            }
+            if ( ! modset.add( keybuf.toString() ) && mods[ii].getModificationOp()
== DirContext.ADD_ATTRIBUTE )
+            {
+                throw new LdapAttributeInUseException( "found two copies of the following
modification item: " 
+                    + mods[ii] );
             }
         }
         Attribute objectClass;



Mime
View raw message