Matchers for use with JMock expectations
This commit is contained in:
parent
7731a3df57
commit
be34724207
|
@ -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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue