Added integration tests that demonstrates how to retrieve multi-valued attributes.

This commit is contained in:
Ulrik Sandberg
2007-01-07 10:24:33 +00:00
parent 4521360748
commit 0de9b35459
2 changed files with 52 additions and 1 deletions

View File

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

View File

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