Quellcode durchsuchen

Added tests for AuthenticationDetailsSourceImpl (and AuthenticationDetails).

Luke Taylor vor 17 Jahren
Ursprung
Commit
a09b15ce5f

+ 5 - 18
core/src/main/java/org/springframework/security/ui/AuthenticationDetails.java

@@ -21,14 +21,10 @@ public class AuthenticationDetails implements Serializable {
     * @param context that the authentication request is initiated from
     */
    public AuthenticationDetails(Object context) {
-       this.context = context==null?"":context.toString();
+       this.context = context == null ? "" : context.toString();
        doPopulateAdditionalInformation(context);
    }
 
-   protected AuthenticationDetails() {
-       throw new IllegalArgumentException("Cannot use default constructor");
-   }
-
    //~ Methods ========================================================================================================
 
    /**
@@ -42,20 +38,11 @@ public class AuthenticationDetails implements Serializable {
        if (obj instanceof AuthenticationDetails) {
            AuthenticationDetails rhs = (AuthenticationDetails) obj;
 
-           if ((context == null) && (rhs.getContext() != null)) {
-               return false;
-           }
-
-           if ((context != null) && (rhs.getContext() == null)) {
+           // this.context cannot be null
+           if (!context.equals(rhs.getContext())) {
                return false;
            }
 
-           if (context != null) {
-               if (!context.equals(rhs.getContext())) {
-                   return false;
-               }
-           }
-
            return true;
        }
 
@@ -65,7 +52,7 @@ public class AuthenticationDetails implements Serializable {
    /**
     * Indicates the context.
     *
-    * @return the address
+    * @return the context
     */
    public String getContext() {
        return context;
@@ -75,7 +62,7 @@ public class AuthenticationDetails implements Serializable {
        StringBuffer sb = new StringBuffer();
        sb.append(super.toString() + ": ");
        sb.append("Context: " + this.getContext());
-       
+
        return sb.toString();
    }
 }

+ 4 - 10
core/src/main/java/org/springframework/security/ui/AuthenticationDetailsSourceImpl.java

@@ -1,7 +1,6 @@
 package org.springframework.security.ui;
 
 import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
 
 import org.springframework.security.ui.AuthenticationDetailsSource;
 import org.springframework.util.Assert;
@@ -26,20 +25,15 @@ public class AuthenticationDetailsSourceImpl implements AuthenticationDetailsSou
     //~ Methods ========================================================================================================
 
     public Object buildDetails(Object context) {
+        Object result = null;
         try {
             Constructor<?> constructor = getFirstMatchingConstructor(context);
-            return constructor.newInstance(new Object[] { context });
-        } catch (NoSuchMethodException ex) {
-            ReflectionUtils.handleReflectionException(ex);
-        } catch (InvocationTargetException ex) {
-            ReflectionUtils.handleReflectionException(ex);
-        } catch (InstantiationException ex) {
-            ReflectionUtils.handleReflectionException(ex);
-        } catch (IllegalAccessException ex) {
+            result = constructor.newInstance(new Object[] { context });
+        } catch (Exception ex) {
             ReflectionUtils.handleReflectionException(ex);
         }
 
-        return null;
+        return result;
     }
 
     /**

+ 0 - 2
core/src/main/java/org/springframework/security/ui/preauth/j2ee/J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource.java

@@ -9,8 +9,6 @@ import java.util.Set;
 
 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
  * {@link HttpServletRequest#isUserInRole(String)}) into GrantedAuthoritys and stores these in the authentication

+ 52 - 0
core/src/test/java/org/springframework/security/ui/AuthenticationDetailsSourceImplTests.java

@@ -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();
+    }
+
+}