Renamed 'execute' method in LdapCallback in line with Spring equivalents. Added some extra tests.
This commit is contained in:
parent
9623eb3d04
commit
0d6b3ab9f3
|
@ -23,5 +23,5 @@ import javax.naming.directory.DirContext;
|
|||
* @author Ben Alex
|
||||
*/
|
||||
public interface LdapCallback {
|
||||
public Object execute(DirContext dirContext) throws NamingException;
|
||||
public Object doInDirContext(DirContext dirContext) throws NamingException;
|
||||
}
|
||||
|
|
|
@ -95,7 +95,7 @@ public class LdapTemplate {
|
|||
dirContextFactory.newInitialDirContext() :
|
||||
dirContextFactory.newInitialDirContext(principalDn, password);
|
||||
|
||||
return callback.execute(ctx);
|
||||
return callback.doInDirContext(ctx);
|
||||
|
||||
} catch (NamingException exception) {
|
||||
throw exceptionTranslator.translate("LdapCallback", exception);
|
||||
|
@ -118,7 +118,7 @@ public class LdapTemplate {
|
|||
|
||||
class LdapCompareCallback implements LdapCallback {
|
||||
|
||||
public Object execute(DirContext ctx) throws NamingException {
|
||||
public Object doInDirContext(DirContext ctx) throws NamingException {
|
||||
SearchControls ctls = new SearchControls();
|
||||
ctls.setReturningAttributes(NO_ATTRS);
|
||||
ctls.setSearchScope(SearchControls.OBJECT_SCOPE);
|
||||
|
@ -154,7 +154,7 @@ public class LdapTemplate {
|
|||
|
||||
class SingleAttributeSearchCallback implements LdapCallback {
|
||||
|
||||
public Object execute(DirContext ctx) throws NamingException {
|
||||
public Object doInDirContext(DirContext ctx) throws NamingException {
|
||||
Set unionOfValues = new HashSet();
|
||||
|
||||
// We're only interested in a single attribute for this method, so we make a copy of
|
||||
|
@ -200,7 +200,7 @@ public class LdapTemplate {
|
|||
|
||||
Boolean exists = (Boolean) execute( new LdapCallback() {
|
||||
|
||||
public Object execute(DirContext ctx) throws NamingException {
|
||||
public Object doInDirContext(DirContext ctx) throws NamingException {
|
||||
try {
|
||||
ctx.lookup( LdapUtils.getRelativeName(dn, ctx) );
|
||||
} catch(NameNotFoundException nnfe) {
|
||||
|
@ -226,7 +226,7 @@ public class LdapTemplate {
|
|||
public Object retrieveEntry(final String dn, final LdapEntryMapper mapper, final String[] attributesToRetrieve) {
|
||||
return execute ( new LdapCallback() {
|
||||
|
||||
public Object execute(DirContext ctx) throws NamingException {
|
||||
public Object doInDirContext(DirContext ctx) throws NamingException {
|
||||
return mapper.mapAttributes(dn, ctx.getAttributes(LdapUtils.getRelativeName(dn, ctx), attributesToRetrieve) );
|
||||
|
||||
}
|
||||
|
@ -248,7 +248,7 @@ public class LdapTemplate {
|
|||
public Object searchForSingleEntry(final String base, final String filter, final Object[] params, final LdapEntryMapper mapper) {
|
||||
return execute ( new LdapCallback() {
|
||||
|
||||
public Object execute(DirContext ctx) throws NamingException {
|
||||
public Object doInDirContext(DirContext ctx) throws NamingException {
|
||||
NamingEnumeration results = ctx.search(base, filter, params, searchControls);
|
||||
|
||||
if (!results.hasMore()) {
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
|
||||
package org.acegisecurity.ldap;
|
||||
|
||||
import javax.naming.directory.DirContext;
|
||||
import javax.naming.NamingException;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
|
@ -69,4 +71,18 @@ public class LdapTemplateTests extends AbstractLdapServerTestCase {
|
|||
public void testNameExistsForInValidNameFails() {
|
||||
assertFalse(template.nameExists("ou=doesntexist,dc=acegisecurity,dc=org"));
|
||||
}
|
||||
|
||||
public void testNamingExceptionIsTranslatedCorrectly() {
|
||||
try {
|
||||
template.execute(new LdapCallback() {
|
||||
|
||||
public Object doInDirContext(DirContext dirContext) throws NamingException {
|
||||
throw new NamingException();
|
||||
}
|
||||
});
|
||||
fail("Expected LdapDataAccessException on NamingException");
|
||||
}
|
||||
catch(LdapDataAccessException expected) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ import org.jmock.Mock;
|
|||
|
||||
import javax.naming.directory.DirContext;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.NamingException;
|
||||
|
||||
/**
|
||||
* Tests {@link LdapUtils}
|
||||
|
@ -28,6 +29,8 @@ import javax.naming.Context;
|
|||
*/
|
||||
public class LdapUtilsTests extends MockObjectTestCase {
|
||||
|
||||
private final LdapDataAccessException tempCoverageBoost = new LdapDataAccessException("");
|
||||
|
||||
public void testRootDnsAreParsedFromUrlsCorrectly() {
|
||||
assertEquals("", LdapUtils.parseRootDnFromUrl("ldap://monkeymachine"));
|
||||
assertEquals("", LdapUtils.parseRootDnFromUrl("ldap://monkeymachine/"));
|
||||
|
@ -54,4 +57,12 @@ public class LdapUtilsTests extends MockObjectTestCase {
|
|||
|
||||
assertEquals("", LdapUtils.getRelativeName("dc=acegisecurity,dc=org", (Context) mockCtx.proxy()));
|
||||
}
|
||||
|
||||
public void testCloseContextSwallowsNamingException() {
|
||||
Mock mockCtx = mock(DirContext.class);
|
||||
|
||||
mockCtx.expects(once()).method("close").will(throwException(new NamingException()));
|
||||
|
||||
LdapUtils.closeContext((Context) mockCtx.proxy());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.acegisecurity.providers.ldap.authenticator;
|
|||
import org.acegisecurity.ldap.AbstractLdapServerTestCase;
|
||||
import org.acegisecurity.BadCredentialsException;
|
||||
import org.acegisecurity.GrantedAuthorityImpl;
|
||||
import org.acegisecurity.AcegiMessageSource;
|
||||
import org.acegisecurity.userdetails.ldap.LdapUserDetailsImpl;
|
||||
import org.acegisecurity.userdetails.ldap.LdapUserDetails;
|
||||
import org.acegisecurity.userdetails.ldap.LdapUserDetailsMapper;
|
||||
|
@ -19,15 +20,16 @@ public class BindAuthenticatorTests extends AbstractLdapServerTestCase {
|
|||
|
||||
public void onSetUp() {
|
||||
authenticator = new BindAuthenticator(getInitialCtxFactory());
|
||||
authenticator.setMessageSource(new AcegiMessageSource());
|
||||
}
|
||||
|
||||
public void testUserDnPatternReturnsCorrectDn() throws Exception {
|
||||
public void testUserDnPatternReturnsCorrectDn() {
|
||||
authenticator.setUserDnPatterns(new String[] {"cn={0},ou=people"});
|
||||
assertEquals("cn=Joe,ou=people,"+ getInitialCtxFactory().getRootDn(),
|
||||
authenticator.getUserDns("Joe").get(0));
|
||||
}
|
||||
|
||||
public void testAuthenticationWithCorrectPasswordSucceeds() throws Exception {
|
||||
public void testAuthenticationWithCorrectPasswordSucceeds() {
|
||||
authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
|
||||
LdapUserDetails user = authenticator.authenticate("bob","bobspassword");
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.acegisecurity.providers.ldap.authenticator;
|
|||
|
||||
import org.acegisecurity.ldap.AbstractLdapServerTestCase;
|
||||
import org.acegisecurity.BadCredentialsException;
|
||||
import org.acegisecurity.providers.encoding.PlaintextPasswordEncoder;
|
||||
import org.acegisecurity.userdetails.UsernameNotFoundException;
|
||||
import org.acegisecurity.userdetails.ldap.LdapUserDetailsImpl;
|
||||
import org.acegisecurity.userdetails.ldap.LdapUserDetailsMapper;
|
||||
|
@ -72,9 +73,8 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapServerTest
|
|||
authenticator.authenticate("Bob", "bobspassword");
|
||||
}
|
||||
|
||||
public void testLocalCompareSucceedsWithShaEncodedPassword() {
|
||||
authenticator = new PasswordComparisonAuthenticator(getInitialCtxFactory());
|
||||
authenticator.setUserDnPatterns(new String[] {"uid={0},ou=people"});
|
||||
public void testLocalComparisonSucceedsWithShaEncodedPassword() {
|
||||
// Ben's password is SHA encoded
|
||||
authenticator.authenticate("ben", "benspassword");
|
||||
}
|
||||
|
||||
|
@ -92,16 +92,16 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapServerTest
|
|||
assertEquals("User should have 5 attributes", 5, user.getAttributes().size());
|
||||
|
||||
}
|
||||
/*
|
||||
|
||||
public void testOnlySpecifiedAttributesAreRetrieved() throws Exception {
|
||||
authenticator.setUserAttributes(new String[] {"cn", "uid"});
|
||||
authenticator.setUserAttributes(new String[] {"userPassword"});
|
||||
authenticator.setPasswordEncoder(new PlaintextPasswordEncoder());
|
||||
LdapUserInfo user = authenticator.authenticate("Bob", "bobspassword");
|
||||
assertEquals("Should have retrieved 2 attributes (cn, uid)",2, user.getAttributes().size());
|
||||
assertEquals("Bob Hamilton", user.getAttributes().get("cn").get());
|
||||
assertEquals("bob", user.getAttributes().get("uid").get());
|
||||
LdapUserDetails user = authenticator.authenticate("Bob", "bobspassword");
|
||||
assertEquals("Should have retrieved 1 attribute (userPassword)",1, user.getAttributes().size());
|
||||
// assertEquals("Bob Hamilton", user.getAttributes().get("cn").get());
|
||||
// assertEquals("bob", user.getAttributes().get("uid").get());
|
||||
}
|
||||
*/
|
||||
|
||||
public void testUseOfDifferentPasswordAttribute() {
|
||||
LdapUserDetailsMapper mapper = new LdapUserDetailsMapper();
|
||||
mapper.setPasswordAttributeName("uid");
|
||||
|
@ -110,7 +110,7 @@ public class PasswordComparisonAuthenticatorTests extends AbstractLdapServerTest
|
|||
authenticator.authenticate("bob", "bob");
|
||||
}
|
||||
/*
|
||||
public void testLdapCompareWithDifferentPasswordAttribute() {
|
||||
public void testLdapCompareWithDifferentPasswordAttributeSucceeds() {
|
||||
authenticator.setUserAttributes(new String[] {"cn"});
|
||||
authenticator.setPasswordEncoder(new PlaintextPasswordEncoder());
|
||||
authenticator.setPasswordAttributeName("uid");
|
||||
|
|
Loading…
Reference in New Issue