Fix valuesAsNames on NameAwareAttribute.remote(int)

Issue gh-437
This commit is contained in:
Patryk Petrowski
2016-11-23 14:45:06 +01:00
committed by Rob Winch
parent 37f8833826
commit b1caf86375
2 changed files with 27 additions and 1 deletions

View File

@@ -251,8 +251,13 @@ public final class NameAwareAttribute implements Attribute, Iterable<Object> {
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");

View File

@@ -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());
}
}