LDAP-139: Added mirrored method in SimpleLdapTemplate that take Name parameters.

LDAP-140: Added bindmethod in LdapTemplate and SimpleLdapTemplate that take a DirContextOperations instance and
uses the information from that instance (DN and Attributes) to perform the bind.
This commit is contained in:
Mattias Arthursson
2008-10-23 09:14:06 +00:00
parent e22fa053a3
commit c416a4576f
8 changed files with 583 additions and 106 deletions

View File

@@ -93,6 +93,19 @@ public class LdapTemplateBindUnbindITest extends AbstractLdapTemplateIntegration
verifyCleanup();
}
@Test
public void testBindAndUnbindPlainDirContextAdapter() {
DirContextAdapter adapter = new DirContextAdapter(new DistinguishedName(DN));
adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
adapter.setAttributeValue("cn", "Some Person4");
adapter.setAttributeValue("sn", "Person4");
tested.bind(adapter);
verifyBoundCorrectData();
tested.unbind(DN);
verifyCleanup();
}
private Attributes setupAttributes() {
Attributes attributes = new BasicAttributes();
BasicAttribute ocattr = new BasicAttribute("objectclass");

View File

@@ -16,18 +16,30 @@
package org.springframework.ldap.core.simple;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import java.util.List;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.AbstractLdapTemplateIntegrationTest;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.DirContextProcessor;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(locations = { "/conf/simpleLdapTemplateTestContext.xml" })
public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest {
private static String DN_STRING = "cn=Some Person4,ou=company1,c=Sweden";
private static DistinguishedName DN = new DistinguishedName("cn=Some Person4,ou=company1,c=Sweden");
@Autowired
private SimpleLdapTemplate ldapTemplate;
@@ -38,6 +50,13 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest
assertEquals("Some Person", result);
}
@Test
public void testLookupName() {
String result = ldapTemplate.lookup(new DistinguishedName("cn=Some Person,ou=company1,c=Sweden"),
new CnContextMapper());
assertEquals("Some Person", result);
}
@Test
public void testSearch() {
List<String> cns = ldapTemplate.search("", "(&(objectclass=person)(sn=Person3))", new CnContextMapper());
@@ -46,6 +65,45 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest
assertEquals("Some Person3", cns.get(0));
}
@Test
public void testSearchProcessor() {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
DummyDirContextProcessor processor = new DummyDirContextProcessor();
List<String> cns = ldapTemplate.search("", "(&(objectclass=person)(sn=Person3))", searchControls,
new CnContextMapper(), processor);
assertEquals(1, cns.size());
assertEquals("Some Person3", cns.get(0));
assertTrue(processor.isPreProcessCalled());
assertTrue(processor.isPostProcessCalled());
}
@Test
public void testSearchProcessorName() {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
DummyDirContextProcessor processor = new DummyDirContextProcessor();
List<String> cns = ldapTemplate.search(DistinguishedName.EMPTY_PATH, "(&(objectclass=person)(sn=Person3))",
searchControls, new CnContextMapper(), processor);
assertEquals(1, cns.size());
assertEquals("Some Person3", cns.get(0));
assertTrue(processor.isPreProcessCalled());
assertTrue(processor.isPostProcessCalled());
}
@Test
public void testSearchName() {
List<String> cns = ldapTemplate.search(DistinguishedName.EMPTY_PATH, "(&(objectclass=person)(sn=Person3))",
new CnContextMapper());
assertEquals(1, cns.size());
assertEquals("Some Person3", cns.get(0));
}
@Test
public void testModifyAttributes() {
DirContextOperations ctx = ldapTemplate.lookupContext("cn=Some Person,ou=company1,c=Sweden");
@@ -66,7 +124,83 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest
});
}
private static class CnContextMapper implements ParameterizedContextMapper<String> {
@Test
public void testModifyAttributesName() {
DirContextOperations ctx = ldapTemplate.lookupContext(new DistinguishedName(
"cn=Some Person,ou=company1,c=Sweden"));
ctx.setAttributeValue("description", "updated description");
ctx.setAttributeValue("telephoneNumber", "0000001");
ldapTemplate.modifyAttributes(ctx);
// verify that the data was properly updated.
ldapTemplate.lookup("cn=Some Person,ou=company1,c=Sweden", new ParameterizedContextMapper<Object>() {
public Object mapFromContext(Object ctx) {
DirContextAdapter adapter = (DirContextAdapter) ctx;
assertEquals("updated description", adapter.getStringAttribute("description"));
assertEquals("0000001", adapter.getStringAttribute("telephoneNumber"));
return null;
}
});
}
@Test
public void testBindAndUnbind() {
DirContextAdapter adapter = new DirContextAdapter();
adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
adapter.setAttributeValue("cn", "Some Person4");
adapter.setAttributeValue("sn", "Person4");
ldapTemplate.bind(DN_STRING, adapter, null);
verifyBoundCorrectData();
ldapTemplate.unbind(DN_STRING);
verifyCleanup();
}
@Test
public void testBindAndUnbindName() {
DirContextAdapter adapter = new DirContextAdapter();
adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
adapter.setAttributeValue("cn", "Some Person4");
adapter.setAttributeValue("sn", "Person4");
ldapTemplate.bind(DN, adapter, null);
verifyBoundCorrectData();
ldapTemplate.unbind(DN);
verifyCleanup();
}
@Test
public void testBindAndUnbindWithDirContextAdapter() {
DirContextAdapter adapter = new DirContextAdapter(DN);
adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
adapter.setAttributeValue("cn", "Some Person4");
adapter.setAttributeValue("sn", "Person4");
ldapTemplate.bind(adapter);
verifyBoundCorrectData();
ldapTemplate.unbind(DN);
verifyCleanup();
}
private void verifyBoundCorrectData() {
DirContextOperations result = ldapTemplate.lookupContext(DN_STRING);
assertEquals("Some Person4", result.getStringAttribute("cn"));
assertEquals("Person4", result.getStringAttribute("sn"));
}
private void verifyCleanup() {
try {
ldapTemplate.lookupContext(DN_STRING);
fail("NameNotFoundException expected");
}
catch (NameNotFoundException expected) {
assertTrue(true);
}
}
private static final class CnContextMapper implements ParameterizedContextMapper<String> {
public String mapFromContext(Object ctx) {
DirContextAdapter adapter = (DirContextAdapter) ctx;
@@ -75,4 +209,27 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest
}
}
private static final class DummyDirContextProcessor implements DirContextProcessor {
private boolean preProcessCalled;
private boolean postProcessCalled;
public boolean isPreProcessCalled() {
return preProcessCalled;
}
public boolean isPostProcessCalled() {
return postProcessCalled;
}
public void postProcess(DirContext ctx) throws NamingException {
preProcessCalled = true;
}
public void preProcess(DirContext ctx) throws NamingException {
postProcessCalled = true;
}
}
}