Added tests for AuthenticationDetailsSourceImpl (and AuthenticationDetails).
This commit is contained in:
parent
8283074097
commit
a09b15ce5f
|
@ -21,14 +21,10 @@ public class AuthenticationDetails implements Serializable {
|
||||||
* @param context that the authentication request is initiated from
|
* @param context that the authentication request is initiated from
|
||||||
*/
|
*/
|
||||||
public AuthenticationDetails(Object context) {
|
public AuthenticationDetails(Object context) {
|
||||||
this.context = context==null?"":context.toString();
|
this.context = context == null ? "" : context.toString();
|
||||||
doPopulateAdditionalInformation(context);
|
doPopulateAdditionalInformation(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected AuthenticationDetails() {
|
|
||||||
throw new IllegalArgumentException("Cannot use default constructor");
|
|
||||||
}
|
|
||||||
|
|
||||||
//~ Methods ========================================================================================================
|
//~ Methods ========================================================================================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,19 +38,10 @@ public class AuthenticationDetails implements Serializable {
|
||||||
if (obj instanceof AuthenticationDetails) {
|
if (obj instanceof AuthenticationDetails) {
|
||||||
AuthenticationDetails rhs = (AuthenticationDetails) obj;
|
AuthenticationDetails rhs = (AuthenticationDetails) obj;
|
||||||
|
|
||||||
if ((context == null) && (rhs.getContext() != null)) {
|
// this.context cannot be null
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((context != null) && (rhs.getContext() == null)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (context != null) {
|
|
||||||
if (!context.equals(rhs.getContext())) {
|
if (!context.equals(rhs.getContext())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -65,7 +52,7 @@ public class AuthenticationDetails implements Serializable {
|
||||||
/**
|
/**
|
||||||
* Indicates the context.
|
* Indicates the context.
|
||||||
*
|
*
|
||||||
* @return the address
|
* @return the context
|
||||||
*/
|
*/
|
||||||
public String getContext() {
|
public String getContext() {
|
||||||
return context;
|
return context;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package org.springframework.security.ui;
|
package org.springframework.security.ui;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
|
|
||||||
import org.springframework.security.ui.AuthenticationDetailsSource;
|
import org.springframework.security.ui.AuthenticationDetailsSource;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
@ -26,20 +25,15 @@ public class AuthenticationDetailsSourceImpl implements AuthenticationDetailsSou
|
||||||
//~ Methods ========================================================================================================
|
//~ Methods ========================================================================================================
|
||||||
|
|
||||||
public Object buildDetails(Object context) {
|
public Object buildDetails(Object context) {
|
||||||
|
Object result = null;
|
||||||
try {
|
try {
|
||||||
Constructor<?> constructor = getFirstMatchingConstructor(context);
|
Constructor<?> constructor = getFirstMatchingConstructor(context);
|
||||||
return constructor.newInstance(new Object[] { context });
|
result = constructor.newInstance(new Object[] { context });
|
||||||
} catch (NoSuchMethodException ex) {
|
} catch (Exception ex) {
|
||||||
ReflectionUtils.handleReflectionException(ex);
|
|
||||||
} catch (InvocationTargetException ex) {
|
|
||||||
ReflectionUtils.handleReflectionException(ex);
|
|
||||||
} catch (InstantiationException ex) {
|
|
||||||
ReflectionUtils.handleReflectionException(ex);
|
|
||||||
} catch (IllegalAccessException ex) {
|
|
||||||
ReflectionUtils.handleReflectionException(ex);
|
ReflectionUtils.handleReflectionException(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -9,8 +9,6 @@ import java.util.Set;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of AuthenticationDetailsSource which converts the user's J2EE roles (as obtained by calling
|
* Implementation of AuthenticationDetailsSource which converts the user's J2EE roles (as obtained by calling
|
||||||
* {@link HttpServletRequest#isUserInRole(String)}) into GrantedAuthoritys and stores these in the authentication
|
* {@link HttpServletRequest#isUserInRole(String)}) into GrantedAuthoritys and stores these in the authentication
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
package org.springframework.security.ui;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.security.providers.TestingAuthenticationToken;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Luke Taylor
|
||||||
|
* @version $Id$
|
||||||
|
*/
|
||||||
|
public class AuthenticationDetailsSourceImplTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void buildDetailsReturnsExpectedAuthenticationDetails() {
|
||||||
|
AuthenticationDetailsSourceImpl ads = new AuthenticationDetailsSourceImpl();
|
||||||
|
AuthenticationDetails details = (AuthenticationDetails) ads.buildDetails("the context");
|
||||||
|
assertEquals("the context", details.getContext());
|
||||||
|
assertEquals(new AuthenticationDetails("the context"), details);
|
||||||
|
ads.setClazz(AuthenticationDetails.class);
|
||||||
|
details = (AuthenticationDetails) ads.buildDetails("another context");
|
||||||
|
assertEquals("another context", details.getContext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalStateException.class)
|
||||||
|
public void nonMatchingConstructorIsRejected() {
|
||||||
|
AuthenticationDetailsSourceImpl ads = new AuthenticationDetailsSourceImpl();
|
||||||
|
ads.setClazz(String.class);
|
||||||
|
ads.buildDetails(new Object());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalStateException.class)
|
||||||
|
public void constructorTakingMultipleArgumentsIsRejected() {
|
||||||
|
AuthenticationDetailsSourceImpl ads = new AuthenticationDetailsSourceImpl();
|
||||||
|
ads.setClazz(TestingAuthenticationToken.class);
|
||||||
|
ads.buildDetails(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void authenticationDetailsEqualsBehavesAsExpected() {
|
||||||
|
AuthenticationDetails details = new AuthenticationDetails("the context");
|
||||||
|
assertFalse((new AuthenticationDetails("different context")).equals(details));
|
||||||
|
assertFalse((new AuthenticationDetails(null)).equals(details));
|
||||||
|
assertFalse(details.equals(new AuthenticationDetails(null)));
|
||||||
|
assertFalse(details.equals("a string"));
|
||||||
|
// Just check toString() functions OK
|
||||||
|
details.toString();
|
||||||
|
(new AuthenticationDetails(null)).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue