diff --git a/spring-ldap/src/itest/java/org/springframework/ldap/LdapTemplateAttributesMapperITest.java b/spring-ldap/src/itest/java/org/springframework/ldap/LdapTemplateAttributesMapperITest.java index 184e1be9..0dc0713c 100644 --- a/spring-ldap/src/itest/java/org/springframework/ldap/LdapTemplateAttributesMapperITest.java +++ b/spring-ldap/src/itest/java/org/springframework/ldap/LdapTemplateAttributesMapperITest.java @@ -16,8 +16,12 @@ package org.springframework.ldap; +import java.util.LinkedList; import java.util.List; +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.Attributes; import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.LdapTemplate; @@ -48,6 +52,32 @@ public class LdapTemplateAttributesMapperITest extends assertEquals("Sweden, Company1, Some Person2", person.getDescription()); } + /** + * Demonstrates how to retrieve all values of a multi-value attribute. + * + * @see LdapTemplateContextMapperITest#testSearch_ContextMapper_MultiValue() + */ + public void testSearch_AttributesMapper_MultiValue() throws Exception { + AttributesMapper mapper = new AttributesMapper() { + public Object mapFromAttributes(Attributes attributes) throws NamingException { + LinkedList list = new LinkedList(); + NamingEnumeration enumeration = attributes.get("uniqueMember").getAll(); + while (enumeration.hasMoreElements()) { + String value = (String) enumeration.nextElement(); + list.add(value); + } + String[] members = (String[]) list.toArray(new String[0]); + return members; + } + }; + List result = tested.search("ou=groups", + "(objectclass=groupOfUniqueNames)", mapper); + + assertEquals(2, result.size()); + assertEquals(1, ((String[]) result.get(0)).length); + assertEquals(5, ((String[]) result.get(1)).length); + } + public void setTested(LdapTemplate tested) { this.tested = tested; } diff --git a/spring-ldap/src/itest/java/org/springframework/ldap/LdapTemplateContextMapperITest.java b/spring-ldap/src/itest/java/org/springframework/ldap/LdapTemplateContextMapperITest.java index d7c7ccc5..cd38df57 100644 --- a/spring-ldap/src/itest/java/org/springframework/ldap/LdapTemplateContextMapperITest.java +++ b/spring-ldap/src/itest/java/org/springframework/ldap/LdapTemplateContextMapperITest.java @@ -18,9 +18,9 @@ package org.springframework.ldap; import java.util.List; - import org.springframework.ldap.core.ContextMapper; import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.support.DirContextAdapter; import org.springframework.test.AbstractDependencyInjectionSpringContextTests; /** @@ -53,6 +53,27 @@ public class LdapTemplateContextMapperITest extends assertEquals("Sweden, Company1, Some Person2", person.getDescription()); } + /** + * Demonstrates how to retrieve all values of a multi-value attribute. + * + * @see LdapTemplateAttributesMapperITest#testSearch_AttributesMapper_MultiValue() + */ + public void testSearch_ContextMapper_MultiValue() throws Exception { + ContextMapper mapper = new ContextMapper() { + public Object mapFromContext(Object ctx) { + DirContextAdapter adapter = (DirContextAdapter) ctx; + String[] members = adapter.getStringAttributes("uniqueMember"); + return members; + } + }; + List result = tested.search("ou=groups", + "(objectclass=groupOfUniqueNames)", mapper); + + assertEquals(2, result.size()); + assertEquals(1, ((String[]) result.get(0)).length); + assertEquals(5, ((String[]) result.get(1)).length); + } + public void setTested(LdapTemplate tested) { this.tested = tested; }