Matchers for use with JMock expectations

This commit is contained in:
Luke Taylor 2008-11-11 08:43:17 +00:00
parent 7731a3df57
commit be34724207
1 changed files with 51 additions and 0 deletions

View File

@ -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<Authentication> {
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<Authentication> anAuthenticationWithUsername(String name) {
AuthenticationMatcher matcher = new AuthenticationMatcher();
matcher.username = name;
return matcher;
}
@Factory
public static Matcher<Authentication> anAuthenticationWithUsernameAndPassword(String name, String password) {
AuthenticationMatcher matcher = new AuthenticationMatcher();
matcher.username = name;
matcher.password = password;
return matcher;
}
}