directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r593967 - in /directory/shared/branches/bigbang/ldap/src: main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java test/java/org/apache/directory/shared/ldap/util/AttributeUtilsTest.java
Date Sun, 11 Nov 2007 21:58:01 GMT
Author: elecharny
Date: Sun Nov 11 13:58:01 2007
New Revision: 593967

URL: http://svn.apache.org/viewvc?rev=593967&view=rev
Log:
Fixed a NPE
Fixed a bug when we romve the last value of an attribute : the attribute must be deleted
Added some tests for the applyModification() method

Added:
    directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/util/AttributeUtilsTest.java
Modified:
    directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java

Modified: directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java?rev=593967&r1=593966&r2=593967&view=diff
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java
(original)
+++ directory/shared/branches/bigbang/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java
Sun Nov 11 13:58:01 2007
@@ -1229,6 +1229,11 @@
                     // We just have to remove the values from the original
                     // entry, if they exist.
                     modifiedAttr = entry.get( modificationId ) ;
+                    
+                    if ( modifiedAttr == null )
+                    {
+                        break;
+                    }
 
                     NamingEnumeration<?> values = modAttr.getAll();
                     
@@ -1238,6 +1243,12 @@
                         // Note that the attribute *must* have been
                         // normalized before.
                         modifiedAttr.remove( values.nextElement() );
+                    }
+                    
+                    if ( modifiedAttr.size() == 0 )
+                    {
+                        // If this was the last value, remove the attribute
+                        entry.remove( modifiedAttr.getID() );
                     }
                 }
 

Added: directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/util/AttributeUtilsTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/util/AttributeUtilsTest.java?rev=593967&view=auto
==============================================================================
--- directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/util/AttributeUtilsTest.java
(added)
+++ directory/shared/branches/bigbang/ldap/src/test/java/org/apache/directory/shared/ldap/util/AttributeUtilsTest.java
Sun Nov 11 13:58:01 2007
@@ -0,0 +1,288 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.ldap.util;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.ModificationItem;
+
+import org.apache.directory.shared.ldap.message.AttributeImpl;
+import org.apache.directory.shared.ldap.message.AttributesImpl;
+import org.apache.directory.shared.ldap.message.ModificationItemImpl;
+
+import org.junit.Test;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNull;
+
+/**
+ * A test case for the AttributeUtils methods 
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class AttributeUtilsTest
+{
+    /**
+     * Test a addModification applied to an empty entry
+     */
+    @Test
+    public void testApplyAddModificationToEmptyEntry() throws NamingException
+    {
+        Attributes entry = new AttributesImpl();
+        Attribute attr = new AttributeImpl( "cn", "test" );
+        ModificationItem modification = new ModificationItemImpl(DirContext.ADD_ATTRIBUTE,
attr );
+        AttributeUtils.applyModification( entry, modification );
+        assertNotNull( entry.get(  "cn" ) );
+        assertEquals( 1, entry.size() );
+        assertEquals( attr, entry.get( "cn" ) );
+    }
+
+
+    /**
+     * Test a addModification applied to an entry 
+     */
+    @Test
+    public void testApplyAddModificationToEntry() throws NamingException
+    {
+        Attributes entry = new AttributesImpl();
+        entry.put( "dc", "apache" );
+        assertEquals( 1, entry.size() );
+
+        Attribute attr = new AttributeImpl( "cn", "test" );
+        ModificationItem modification = new ModificationItemImpl(DirContext.ADD_ATTRIBUTE,
attr );
+        AttributeUtils.applyModification( entry, modification );
+        assertNotNull( entry.get(  "cn" ) );
+        assertEquals( 2, entry.size() );
+        assertEquals( attr, entry.get( "cn" ) );
+    }
+
+
+    /**
+     * Test a addModification applied to an entry with the same attribute
+     * but with another value 
+     */
+    @Test
+    public void testApplyAddModificationToEntryWithValues() throws NamingException
+    {
+        Attributes entry = new AttributesImpl();
+        entry.put( "cn", "apache" );
+        assertEquals( 1, entry.size() );
+
+        Attribute attr = new AttributeImpl( "cn", "test" );
+        ModificationItem modification = new ModificationItemImpl(DirContext.ADD_ATTRIBUTE,
attr );
+        AttributeUtils.applyModification( entry, modification );
+        assertNotNull( entry.get(  "cn" ) );
+        assertEquals( 1, entry.size() );
+        
+        NamingEnumeration<?> values = entry.get( "cn" ).getAll();
+        
+        assertTrue( values.hasMoreElements() );
+        
+        Set<String> expectedValues = new HashSet<String>();
+        expectedValues.add( "apache" );
+        expectedValues.add( "test" );
+        
+        while ( values.hasMoreElements() )
+        {
+            String value = (String)values.nextElement();
+            
+            assertTrue( expectedValues.contains( value ) );
+            
+            expectedValues.remove( value );
+        }
+        
+        assertEquals( 0, expectedValues.size() );
+    }
+
+
+    /**
+     * Test a addModification applied to an entry with the same attribute
+     * and the same value 
+     */
+    @Test
+    public void testApplyAddModificationToEntryWithSameValue() throws NamingException
+    {
+        Attributes entry = new AttributesImpl();
+        Attribute cn = new AttributeImpl( "cn" );
+        cn.add( "test" );
+        cn.add( "apache" );
+        entry.put( cn );
+        
+        assertEquals( 1, entry.size() );
+
+        Attribute attr = new AttributeImpl( "cn", "test" );
+        ModificationItem modification = new ModificationItemImpl(DirContext.ADD_ATTRIBUTE,
attr );
+        AttributeUtils.applyModification( entry, modification );
+        assertNotNull( entry.get(  "cn" ) );
+        assertEquals( 1, entry.size() );
+        
+        NamingEnumeration<?> values = entry.get( "cn" ).getAll();
+        
+        assertTrue( values.hasMoreElements() );
+        
+        Set<String> expectedValues = new HashSet<String>();
+        expectedValues.add( "apache" );
+        expectedValues.add( "test" );
+        
+        while ( values.hasMoreElements() )
+        {
+            String value = (String)values.nextElement();
+            
+            assertTrue( expectedValues.contains( value ) );
+            
+            expectedValues.remove( value );
+        }
+        
+        assertEquals( 0, expectedValues.size() );
+    }
+
+    
+    /**
+     * Test the deletion of an attribute into an empty entry
+     */
+    @Test
+    public void testApplyRemoveModificationFromEmptyEntry() throws NamingException
+    {
+        Attributes entry = new AttributesImpl();
+        Attribute attr = new AttributeImpl( "cn", "test" );
+        ModificationItem modification = new ModificationItemImpl(DirContext.REMOVE_ATTRIBUTE,
attr );
+        AttributeUtils.applyModification( entry, modification );
+        assertNull( entry.get( "cn" ) );
+        assertEquals( 0, entry.size() );
+    }
+
+
+    /**
+     * Test the deletion of an attribute into an entry which does not contain the attribute
+     */
+    @Test
+    public void testApplyRemoveModificationFromEntryAttributeNotPresent() throws NamingException
+    {
+        Attributes entry = new AttributesImpl();
+        Attribute dc = new AttributeImpl( "dc", "apache" );
+        entry.put( dc );
+        
+        Attribute attr = new AttributeImpl( "cn", "test" );
+        
+        ModificationItem modification = new ModificationItemImpl(DirContext.REMOVE_ATTRIBUTE,
attr );
+        
+        AttributeUtils.applyModification( entry, modification );
+        
+        assertNull( entry.get( "cn" ) );
+        assertNotNull( entry.get( "dc" ) );
+        assertEquals( 1, entry.size() );
+        assertEquals( dc, entry.get( "dc" ) );
+    }
+
+
+    /**
+     * Test the deletion of an attribute into an entry which contains the attribute
+     * but without the value to be deleted
+     */
+    @Test
+    public void testApplyRemoveModificationFromEntryAttributeNotSameValue() throws NamingException
+    {
+        Attributes entry = new AttributesImpl();
+        Attribute cn = new AttributeImpl( "cn", "apache" );
+        entry.put( cn );
+        
+        Attribute attr = new AttributeImpl( "cn", "test" );
+        
+        ModificationItem modification = new ModificationItemImpl(DirContext.REMOVE_ATTRIBUTE,
attr );
+        
+        AttributeUtils.applyModification( entry, modification );
+        
+        assertNotNull( entry.get( "cn" ) );
+        assertEquals( 1, entry.size() );
+        assertEquals( cn, entry.get( "cn" ) );
+    }
+
+
+    /**
+     * Test the deletion of an attribute into an entry which contains the attribute.
+     * 
+     * The entry should not contain the attribute after the operation
+     */
+    @Test
+    public void testApplyRemoveModificationFromEntrySameAttributeSameValue() throws NamingException
+    {
+        Attributes entry = new AttributesImpl();
+        Attribute cn = new AttributeImpl( "cn", "test" );
+        entry.put( cn );
+        
+        Attribute attr = new AttributeImpl( "cn", "test" );
+        
+        ModificationItem modification = new ModificationItemImpl(DirContext.REMOVE_ATTRIBUTE,
attr );
+        
+        AttributeUtils.applyModification( entry, modification );
+        
+        assertNull( entry.get( "cn" ) );
+        assertEquals( 0, entry.size() );
+    }
+
+
+    /**
+     * Test the deletion of an attribute into an entry which contains the attribute
+     * with more than one value
+     * 
+     * The entry should contain the attribute after the operation, but with one less value
+     */
+    @Test
+    public void testApplyRemoveModificationFromEntrySameAttributeValues() throws NamingException
+    {
+        Attributes entry = new AttributesImpl();
+        Attribute cn = new AttributeImpl( "cn", "test" );
+        cn.add( "apache" );
+        entry.put( cn );
+        
+        Attribute attr = new AttributeImpl( "cn", "test" );
+        
+        ModificationItem modification = new ModificationItemImpl(DirContext.REMOVE_ATTRIBUTE,
attr );
+        
+        AttributeUtils.applyModification( entry, modification );
+        
+        assertNotNull( entry.get( "cn" ) );
+        assertEquals( 1, entry.size() );
+        
+        Attribute modifiedAttr = entry.get( "cn" );
+        
+        NamingEnumeration<?> values = modifiedAttr.getAll();
+        
+        assertTrue( values.hasMoreElements() );
+        
+        boolean isFirst = true;
+        
+        while ( values.hasMoreElements() )
+        {
+            assertTrue( isFirst );
+            
+            isFirst = false;
+            assertEquals( "apache", values.nextElement() );
+        }
+    }
+}



Mime
View raw message