diff --git a/core/src/test/java/org/springframework/security/matcher/AuthenticationMatcher.java b/core/src/test/java/org/springframework/security/matcher/AuthenticationMatcher.java new file mode 100644 index 0000000000..826aee6cac --- /dev/null +++ b/core/src/test/java/org/springframework/security/matcher/AuthenticationMatcher.java @@ -0,0 +1,51 @@ +package org.springframework.security.matcher; + +import org.hamcrest.Description; +import org.hamcrest.Factory; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; +import org.springframework.security.Authentication; + +public class AuthenticationMatcher extends TypeSafeMatcher { + private String username; + private String password; + private String[] authorities; + + @Override + public boolean matchesSafely(Authentication auth) { + if (!username.equals(auth.getName())) { + return false; + } + + if (password != null && !password.equals(auth.getCredentials())) { + return false; + } + + return true; + } + + public void describeTo(Description d) { + d.appendText("an authentication object with username = '" + username + "'"); + if (password != null) { + d.appendText(", password = '" + password + "'"); + } + } + + @Factory + public static Matcher anAuthenticationWithUsername(String name) { + AuthenticationMatcher matcher = new AuthenticationMatcher(); + matcher.username = name; + return matcher; + } + + @Factory + public static Matcher anAuthenticationWithUsernameAndPassword(String name, String password) { + AuthenticationMatcher matcher = new AuthenticationMatcher(); + matcher.username = name; + matcher.password = password; + return matcher; + + } + + +}