From b1caf86375da88ca11d2c0c78853885dee511e8c Mon Sep 17 00:00:00 2001 From: Patryk Petrowski Date: Wed, 23 Nov 2016 14:45:06 +0100 Subject: [PATCH] Fix valuesAsNames on NameAwareAttribute.remote(int) Issue gh-437 --- .../ldap/core/NameAwareAttribute.java | 7 ++++++- .../ldap/core/NameAwareAttributeTest.java | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/springframework/ldap/core/NameAwareAttribute.java b/core/src/main/java/org/springframework/ldap/core/NameAwareAttribute.java index 6f9c0d76..6b623ede 100644 --- a/core/src/main/java/org/springframework/ldap/core/NameAwareAttribute.java +++ b/core/src/main/java/org/springframework/ldap/core/NameAwareAttribute.java @@ -251,8 +251,13 @@ public final class NameAwareAttribute implements Attribute, Iterable { for(int i = 0; i < ix; i++) { value = iterator.next(); } - iterator.remove(); + if (value instanceof String) { + try { + valuesAsNames.remove(new LdapName((String) value)); + } catch (javax.naming.InvalidNameException ignored) { + } + } return value; } catch (NoSuchElementException e) { throw new IndexOutOfBoundsException("No value at index i"); diff --git a/core/src/test/java/org/springframework/ldap/core/NameAwareAttributeTest.java b/core/src/test/java/org/springframework/ldap/core/NameAwareAttributeTest.java index 566daa27..e9b2c172 100644 --- a/core/src/test/java/org/springframework/ldap/core/NameAwareAttributeTest.java +++ b/core/src/test/java/org/springframework/ldap/core/NameAwareAttributeTest.java @@ -19,9 +19,13 @@ package org.springframework.ldap.core; import org.junit.Test; import org.springframework.ldap.support.LdapUtils; +import javax.naming.InvalidNameException; +import javax.naming.Name; import javax.naming.NamingException; +import javax.naming.ldap.LdapName; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertTrue; /** * @author Mattias Hellborg Arthursson @@ -258,4 +262,21 @@ public class NameAwareAttributeTest { assertThat(attr1.get()).isEqualTo(expectedName1); assertThat(attr2.get()).isEqualTo(expectedValue2); } + + @Test + public void testRemoveByIndexUpdatesHashcodeAndEquals() throws InvalidNameException { + // given + Name a = new LdapName("cn=user1"); + Name b = new LdapName("cn=user2"); + final NameAwareAttribute attribute = new NameAwareAttribute("test attribute"); + attribute.add(a); + attribute.add(b); + // when + attribute.remove(0); + // then + final NameAwareAttribute expectedAttribute = new NameAwareAttribute("test attribute"); + expectedAttribute.add(b); + assertTrue(attribute.equals(expectedAttribute)); + assertTrue(attribute.hashCode() == expectedAttribute.hashCode()); + } }