};
}
+ /**
+ * Creates a {@link Matcher} for {@link Identity}s that verifies
+ * their {@link Identity#getContexts() contexts} against the given
+ * {@link Matcher}.
+ *
+ * @param contextsMatcher The {@link Matcher} to verify the
+ * {@link Identity#getContexts() contexts} against
+ * @return A {@link Matcher} for {@link Identity}s
+ */
public static Matcher<Identity> hasContexts(Matcher<? super Set<? super String>> contextsMatcher) {
return new TypeSafeDiagnosingMatcher<Identity>() {
@Override
@Override
public void describeTo(Description description) {
- description.appendText("has contexts ").appendValue(contextsMatcher);
+ description.appendText("has contexts ").appendDescriptionOf(contextsMatcher);
}
};
}
* @param id The ID to match
* @return A {@link Matcher} for {@link Identity Identities}
*/
- public static Matcher<? extends Identity> hasId(String id) {
+ public static Matcher<Identity> hasId(String id) {
return hasId(equalTo(id));
}
* @param idMatcher The matcher to verify the ID against
* @return A {@link Matcher} for {@link Identity Identities}
*/
- public static Matcher<? extends Identity> hasId(Matcher<? super String> idMatcher) {
+ public static Matcher<Identity> hasId(Matcher<? super String> idMatcher) {
return new TypeSafeDiagnosingMatcher<Identity>() {
@Override
protected boolean matchesSafely(Identity identity, Description mismatchDescription) {
* @param nickname The nickname to match
* @return A {@link Matcher} for {@link Identity Identities}
*/
- public static Matcher<? extends Identity> hasNickname(String nickname) {
+ public static Matcher<Identity> hasNickname(String nickname) {
return hasNickname(equalTo(nickname));
}
* @param nicknameMatcher The matcher to verify the nickname against
* @return A {@link Matcher} for {@link Identity Identities}
*/
- public static Matcher<? extends Identity> hasNickname(Matcher<? super String> nicknameMatcher) {
+ public static Matcher<Identity> hasNickname(Matcher<? super String> nicknameMatcher) {
return new TypeSafeDiagnosingMatcher<Identity>() {
@Override
protected boolean matchesSafely(Identity identity, Description mismatchDescription) {
};
}
+ /**
+ * Creates a {@link Matcher} for {@link Identity identities} which
+ * verifies the {@link Identity#getRequestUri() request URI} against
+ * the given request URI.
+ *
+ * @param requestUri The {@link Identity#getRequestUri() request URI}
+ * to match
+ * @return A {@link Matcher} for an {@link Identity}
+ */
+ public static Matcher<Identity> hasRequestUri(String requestUri) {
+ return hasRequestUri(equalTo(requestUri));
+ }
+
+ /**
+ * Creates a {@link Matcher} for {@link Identity identities} which
+ * verifies the {@link Identity#getRequestUri() request URI} against
+ * the given {@link Matcher}.
+ *
+ * @param requestUriMatcher The {@link Matcher} for the
+ * {@link Identity#getRequestUri() request URI}
+ * @return A {@link Matcher} for an {@link Identity}
+ */
+ public static Matcher<Identity> hasRequestUri(Matcher<? super String> requestUriMatcher) {
+ return new TypeSafeDiagnosingMatcher<Identity>() {
+ @Override
+ protected boolean matchesSafely(Identity identity, Description mismatchDescription) {
+ if (!requestUriMatcher.matches(identity.getRequestUri())) {
+ mismatchDescription.appendText("was ").appendValue(identity.getRequestUri());
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("has request URI matching ").appendDescriptionOf(requestUriMatcher);
+ }
+ };
+ }
+
+ /**
+ * Creates a {@link Matcher} for {@link OwnIdentity}s which
+ * verifies the {@link OwnIdentity#getInsertUri() insert URI} against
+ * the given insert URI.
+ *
+ * @param insertUri The insert URI to verify the
+ * {@link OwnIdentity#getInsertUri() insert URI} against
+ * @return A {@link Matcher} for {@link OwnIdentity}s
+ */
+ public static Matcher<OwnIdentity> hasInsertUri(String insertUri) {
+ return hasInsertUri(equalTo(insertUri));
+ }
+
+ /**
+ * Creates a {@link Matcher} for {@link OwnIdentity}s which
+ * verifies the {@link OwnIdentity#getInsertUri() insert URI} against
+ * the given {@link Matcher}.
+ *
+ * @param insertUriMatcher The {@link Matcher} for the
+ * {@link OwnIdentity#getInsertUri() insert URI}
+ * @return A {@link Matcher} for {@link OwnIdentity}s
+ */
+ public static Matcher<OwnIdentity> hasInsertUri(Matcher<? super String> insertUriMatcher) {
+ return new TypeSafeDiagnosingMatcher<OwnIdentity>() {
+ @Override
+ protected boolean matchesSafely(OwnIdentity ownIdentity, Description mismatchDescription) {
+ if (!insertUriMatcher.matches(ownIdentity.getInsertUri())) {
+ mismatchDescription.appendText("has insert URI ").appendValue(ownIdentity.getInsertUri());
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("has insert URI ").appendDescriptionOf(insertUriMatcher);
+ }
+ };
+ }
+
+ /**
+ * Creates a {@link Matcher} for {@link Identity}s which verifies the
+ * {@link Identity#getContexts() contexts} contain the context with the
+ * given name.
+ *
+ * @param context The name of the context to verify
+ * @return A {@link Matcher} for {@link Identity}s
+ */
+ public static Matcher<Identity> hasContext(String context) {
+ return new TypeSafeDiagnosingMatcher<Identity>() {
+ @Override
+ protected boolean matchesSafely(Identity identity, Description mismatchDescription) {
+ if (!identity.getContexts().contains(context)) {
+ mismatchDescription.appendText("no context matches");
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("has context ").appendValue(context);
+ }
+ };
+ }
+
}
package net.pterodactylus.fcp.test;
+import java.util.stream.Stream;
import net.pterodactylus.fcp.plugin.Identity;
import net.pterodactylus.fcp.plugin.OwnIdentity;
import org.hamcrest.Description;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
+import static java.util.stream.Collectors.toSet;
+import static net.pterodactylus.fcp.test.IdentityMatchers.hasContext;
+import static net.pterodactylus.fcp.test.IdentityMatchers.hasContexts;
import static net.pterodactylus.fcp.test.IdentityMatchers.hasId;
+import static net.pterodactylus.fcp.test.IdentityMatchers.hasInsertUri;
import static net.pterodactylus.fcp.test.IdentityMatchers.hasNickname;
+import static net.pterodactylus.fcp.test.IdentityMatchers.hasRequestUri;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.any;
+import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasItem;
public class IdentityMatchersTest {
assertThat(hasNickname("nickname").matches(ownIdentity), equalTo(true));
}
+ @Test
+ public void hasRequestUriWithMatcherDoesNotMatchIfRequestUriIsDifferent() {
+ assertThat(hasRequestUri(equalTo("abc")).matches(identity), equalTo(false));
+ }
+
+ @Test
+ public void hasRequestUriWithMatcherDescribesMismatchCorrectly() {
+ hasRequestUri(equalTo("abc")).describeMismatch(identity, description);
+ assertThat(description.toString(), equalTo("was \"request-uri\""));
+ }
+
+ @Test
+ public void hasRequestUriWithMatcherMatchesIfRequestUriIsTheSame() {
+ assertThat(hasRequestUri(equalTo("request-uri")).matches(identity), equalTo(true));
+ }
+
+ @Test
+ public void hasRequestUriWithMatcherDescribesItselfCorrectly() {
+ hasRequestUri(equalTo("abc")).describeTo(description);
+ assertThat(description.toString(), equalTo("has request URI matching \"abc\""));
+ }
+
+ @Test
+ public void hasRequestUriWithMatcherMatchesOwnIdentityAsWell() {
+ assertThat(hasRequestUri(equalTo("request-uri")).matches(ownIdentity), equalTo(true));
+ }
+
+ @Test
+ public void hasRequestUriWithStringDoesNotMatchIfRequestUriIsDifferent() {
+ assertThat(hasRequestUri("abc").matches(identity), equalTo(false));
+ }
+
+ @Test
+ public void hasRequestUriWithStringDescribesMismatchCorrectly() {
+ hasRequestUri("abc").describeMismatch(identity, description);
+ assertThat(description.toString(), equalTo("was \"request-uri\""));
+ }
+
+ @Test
+ public void hasRequestUriWithStringMatchesIfRequestUriIsTheSame() {
+ assertThat(hasRequestUri("request-uri").matches(identity), equalTo(true));
+ }
+
+ @Test
+ public void hasRequestUriWithStringDescribesItselfCorrectly() {
+ hasRequestUri("abc").describeTo(description);
+ assertThat(description.toString(), equalTo("has request URI matching \"abc\""));
+ }
+
+ @Test
+ public void hasRequestUriWithStringMatchesOwnIdentityAsWell() {
+ assertThat(hasRequestUri("request-uri").matches(ownIdentity), equalTo(true));
+ }
+
+ @Test
+ public void hasInsertUriWithMatcherDoesNotMatchIfInsertUriIsDifferent() {
+ assertThat(hasInsertUri(equalTo("abc")).matches(ownIdentity), equalTo(false));
+ }
+
+ @Test
+ public void hasInsertUriWithMatcherDescribesMismatchCorrectly() {
+ hasInsertUri(equalTo("abc")).describeMismatch(ownIdentity, description);
+ assertThat(description.toString(), equalTo("has insert URI \"insert-uri\""));
+ }
+
+ @Test
+ public void hasInsertUriWithMatcherMatchesIfInsertUriIsTheSame() {
+ assertThat(hasInsertUri(equalTo("insert-uri")).matches(ownIdentity), equalTo(true));
+ }
+
+ @Test
+ public void hasInsertUriWithMatcherDescribesItselfCorrectly() {
+ hasInsertUri(equalTo("abc")).describeTo(description);
+ assertThat(description.toString(), equalTo("has insert URI \"abc\""));
+ }
+
+ @Test
+ public void hasInsertUriWithStringDoesNotMatchIfInsertUriIsDifferent() {
+ assertThat(hasInsertUri("abc").matches(ownIdentity), equalTo(false));
+ }
+
+ @Test
+ public void hasInsertUriWithStringDescribesMismatchCorrectly() {
+ hasInsertUri("abc").describeMismatch(ownIdentity, description);
+ assertThat(description.toString(), equalTo("has insert URI \"insert-uri\""));
+ }
+
+ @Test
+ public void hasInsertUriWithStringMatchesIfInsertUriIsTheSame() {
+ assertThat(hasInsertUri("insert-uri").matches(ownIdentity), equalTo(true));
+ }
+
+ @Test
+ public void hasInsertUriWithStringDescribesItselfCorrectly() {
+ hasInsertUri("abc").describeTo(description);
+ assertThat(description.toString(), equalTo("has insert URI \"abc\""));
+ }
+
+ @Test
+ public void hasContextWithStringDoesNotMatchIfContextDoesNotExist() {
+ assertThat(hasContext("abc").matches(identity), equalTo(false));
+ }
+
+ @Test
+ public void hasContextWithStringDescribesMismatchCorrectly() {
+ hasContext("abc").describeMismatch(identity, description);
+ assertThat(description.toString(), equalTo("no context matches"));
+ }
+
+ @Test
+ public void hasContextWithStringMatchesIfContextExists() {
+ Identity identity = new Identity("", "", "", Stream.of("test-context").collect(toSet()), emptyMap());
+ assertThat(hasContext("test-context").matches(identity), equalTo(true));
+ }
+
+ @Test
+ public void hasContextWithStringDescribesItselfCorrectly() {
+ hasContext("test-context").describeTo(description);
+ assertThat(description.toString(), equalTo("has context \"test-context\""));
+ }
+
+ @Test
+ public void hasContextWithStringCanBeUsedOnAnOwnIdentity() {
+ OwnIdentity ownIdentity = new OwnIdentity("", "", "", "", Stream.of("test-context").collect(toSet()), emptyMap());
+ assertThat(hasContext("test-context").matches(ownIdentity), equalTo(true));
+ }
+
+ @Test
+ public void hasContextsWithMatcherDoesNotMatchIfContextsAreDifferent() {
+ Identity identity = new Identity("", "", "", Stream.of("test-context").collect(toSet()), emptyMap());
+ assertThat(hasContexts(allOf(hasItem("abc"), hasItem("def"))).matches(identity), equalTo(false));
+ }
+
+ @Test
+ public void hasContextsWithMatcherDescribesMismatchCorrectly() {
+ Identity identity = new Identity("", "", "", Stream.of("test-context").collect(toSet()), emptyMap());
+ hasContexts(allOf(hasItem("abc"), hasItem("def"))).describeMismatch(identity, description);
+ assertThat(description.toString(), equalTo("a collection containing \"abc\" mismatches were: [was \"test-context\"]"));
+ }
+
+ @Test
+ public void hasContextsWithMatcherMatchesIfContextsAreTheSame() {
+ Identity identity = new Identity("", "", "", Stream.of("abc", "def").collect(toSet()), emptyMap());
+ assertThat(hasContexts(allOf(hasItem("abc"), hasItem("def"))).matches(identity), equalTo(true));
+ }
+
+ @Test
+ public void hasContextsWithMatcherDescribesItselfCorrectly() {
+ hasContexts(allOf(hasItem("abc"), hasItem("def"))).describeTo(description);
+ assertThat(description.toString(), equalTo("has contexts (a collection containing \"abc\" and a collection containing \"def\")"));
+ }
+
+ @Test
+ public void hasContextsWithMatcherMatchesOwnIdentity() {
+ assertThat(hasContexts(empty()).matches(ownIdentity), equalTo(true));
+ }
+
private final Identity identity = new Identity("id", "nickname", "request-uri", emptyList(), emptyMap());
private final OwnIdentity ownIdentity = new OwnIdentity("id", "nickname", "request-uri", "insert-uri", emptyList(), emptyMap());
private final Description description = new StringDescription();