Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 99363 invoked from network); 11 Nov 2007 21:58:26 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 11 Nov 2007 21:58:26 -0000 Received: (qmail 17039 invoked by uid 500); 11 Nov 2007 21:58:14 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 17006 invoked by uid 500); 11 Nov 2007 21:58:14 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 16995 invoked by uid 99); 11 Nov 2007 21:58:14 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 11 Nov 2007 13:58:14 -0800 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 11 Nov 2007 21:59:08 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 445321A9832; Sun, 11 Nov 2007 13:58:02 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@directory.apache.org From: elecharny@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20071111215802.445321A9832@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 Apache Directory Project + * @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 expectedValues = new HashSet(); + 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 expectedValues = new HashSet(); + 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() ); + } + } +}