下面列出了org.mockito.hamcrest.MockitoHamcrest#org.hamcrest.TypeSafeMatcher 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Matcher<Intent> hasExtras(final Matcher<Bundle> bundleMatcher) {
checkNotNull(bundleMatcher);
return new TypeSafeMatcher<Intent>() {
@Override
public void describeTo(Description description) {
description.appendText("has extras: ");
description.appendDescriptionOf(bundleMatcher);
}
@Override
public boolean matchesSafely(Intent intent) {
return bundleMatcher.matches(intent.getExtras());
}
};
}
public static Matcher<View> withOnlyMatchingDataItems(final Matcher<View> dataMatcher) {
return new TypeSafeMatcher<View>() {
@Override
protected boolean matchesSafely(View view) {
if (!(view instanceof RecyclerView))
return false;
RecyclerView recyclerView = (RecyclerView) view;
for (int i = 0; i < recyclerView.getChildCount(); i++) {
RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i);
if (! dataMatcher.matches(viewHolder.itemView)) {
return false;
}
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendText("withOnlyMatchingDataItems: ");
dataMatcher.describeTo(description);
}
};
}
private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
public static Matcher<Response> aHeader(final String header, final Matcher<?> matcher) {
return new TypeSafeMatcher<Response>() {
@Override
protected boolean matchesSafely(Response response) {
String actualHeaderValue = response.getHeader(header);
return matcher.matches(actualHeaderValue);
}
@Override
public void describeTo(Description description) {
// description.appendText("has statusCode").appendDescriptionOf(statusCodeMatches);
}
@Override
protected void describeMismatchSafely(Response item, Description mismatchDescription) {
// mismatchDescription.appendText("statusCode<").appendValue(item.statusCode()+"").appendText(">");
}
};
}
static Matcher<OLogHeader> equalToHeader(OLogHeader header) {
return new TypeSafeMatcher<OLogHeader>() {
@Override
protected boolean matchesSafely(OLogHeader item) {
return item.getBaseSeqNum() == header.getBaseSeqNum()
&& item.getBaseTerm() == header.getBaseTerm()
&& QuorumConfiguration.fromProtostuff(item.getBaseConfiguration())
.equals(QuorumConfiguration.fromProtostuff(header.getBaseConfiguration()));
}
@Override
public void describeTo(Description description) {
description.appendText(" equal to ").appendValue(header);
}
};
}
public static Matcher<QueryTrace> hasIdentifier(final Matcher<QueryTrace.Identifier> inner) {
return new TypeSafeMatcher<QueryTrace>() {
@Override
protected boolean matchesSafely(final QueryTrace queryTrace) {
return inner.matches(queryTrace.what());
}
@Override
public void describeTo(final Description description) {
description.appendText("has identifier that is ").appendDescriptionOf(inner);
}
@Override
public void describeMismatchSafely(QueryTrace item, Description mismatchDescription) {
inner.describeMismatch(item, mismatchDescription);
}
};
}
/** Returns a matcher that matches TextInputLayouts with non-displayed end icons. */
public static Matcher<View> doesNotShowEndIcon() {
return new TypeSafeMatcher<View>(TextInputLayout.class) {
@Override
public void describeTo(Description description) {
description.appendText("TextInputLayout doesn't show end icon.");
}
@Override
protected boolean matchesSafely(View item) {
// Reach in and find the end icon since we don't have a public API
// to get a reference to it
View endIcon = item.findViewById(R.id.text_input_end_icon);
return endIcon.getVisibility() != View.VISIBLE;
}
};
}
private static TypeSafeMatcher<Bucket<String, String>> hasNullInProgressFile(final boolean isNull) {
return new TypeSafeMatcher<Bucket<String, String>>() {
@Override
protected boolean matchesSafely(Bucket<String, String> bucket) {
final PartFileWriter<String, String> inProgressPart = bucket.getInProgressPart();
return isNull == (inProgressPart == null);
}
@Override
public void describeTo(Description description) {
description.appendText("a Bucket with its inProgressPart being ")
.appendText(isNull ? " null." : " not null.");
}
};
}
private static Matcher<JsonDeserializer> anInlineAssociationDeserializerMatching(
Matcher<RestOperations> restOperations, Matcher<ClientProxyFactory> proxyFactory) {
return new TypeSafeMatcher<JsonDeserializer>() {
@Override
public boolean matchesSafely(JsonDeserializer item) {
if (!(item instanceof InlineAssociationDeserializer)) {
return false;
}
InlineAssociationDeserializer other = (InlineAssociationDeserializer) item;
return restOperations.matches(other.getRestOperations())
&& proxyFactory.matches(other.getProxyFactory());
}
@Override
public void describeTo(Description description) {
description.appendText("instanceof ").appendValue(InlineAssociationDeserializer.class)
.appendText(", restOperations ").appendValue(restOperations)
.appendText(", proxyFactory ").appendValue(proxyFactory);
}
};
}
public static Matcher<Uri> hasScheme(final Matcher<String> schemeMatcher) {
checkNotNull(schemeMatcher);
return new TypeSafeMatcher<Uri>() {
@Override
public boolean matchesSafely(Uri uri) {
return schemeMatcher.matches(uri.getScheme());
}
@Override
public void describeTo(Description description) {
description.appendText("has scheme: ");
description.appendDescriptionOf(schemeMatcher);
}
};
}
public static Matcher<? super String> isValidDateTime() {
return new TypeSafeMatcher<String>() {
@Override
protected boolean matchesSafely(String item) {
try {
ZonedDateTime.parse(item);
return true;
} catch (DateTimeException e) {
return false;
}
}
@Override
public void describeTo(Description description) {
description.appendText("Valid ISO zoned date time");
}
};
}
@Test
public void testWithBody_NormalDocument() {
Matcher<Element> hasAttributes =
new TypeSafeMatcher<Element>() {
@Override
public void describeTo(Description description) {
description.appendText("has attributes");
}
@Override
public boolean matchesSafely(Element element) {
return element.hasAttributes();
}
};
assertTrue(withBody(not(hasAttributes)).matches(document));
}
/**
* Returns a matcher that matches {@link MyTextView}s resourceId
*/
public static Matcher<View> hasResId(int resId) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("has resId");
}
@Override
public boolean matchesSafely(View view) {
try {
return ((MyTextView) view).getBackgroundResource() == resId;
} catch (Exception exception) {
return false;
}
}
};
}
public static <T> TypeSafeMatcher<? extends T> meetsAssertions(final Assertable<? super T> assertions) {
return new TypeSafeMatcher<T>() {
private final Logger log = getLogger(getClass());
@Override
protected boolean matchesSafely(final T item) {
try {
assertions.doAssertions(item);
return true;
}
catch (final AssertionError e) {
log.error("Assertions failed", e);
return false;
}
}
@Override
public void describeTo(final Description description) {
description.appendText("object that meets all assertions");
}
};
}
private static TypeSafeMatcher<Bucket<String, String>> hasNullInProgressFile(final boolean isNull) {
return new TypeSafeMatcher<Bucket<String, String>>() {
@Override
protected boolean matchesSafely(Bucket<String, String> bucket) {
final PartFileWriter<String, String> inProgressPart = bucket.getInProgressPart();
return isNull == (inProgressPart == null);
}
@Override
public void describeTo(Description description) {
description.appendText("a Bucket with its inProgressPart being ")
.appendText(isNull ? " null." : " not null.");
}
};
}
public static Matcher<JApiClass> hasJApiMethodWithName(final String methodName) {
return new TypeSafeMatcher<JApiClass>() {
@Override
public void describeTo(Description description) {
description.appendText("JApiClass should have a method with name '").appendValue(methodName)
.appendText("'.");
}
@Override
protected boolean matchesSafely(JApiClass jApiClass) {
List<JApiMethod> jApiMethods = jApiClass.getMethods();
boolean found = false;
for (JApiMethod jApiMethod : jApiMethods) {
if (methodName.equals(jApiMethod.getName())) {
found = true;
break;
}
}
return found;
}
};
}
public static Matcher<PersistentObject> hasSameId(final PersistentObject expected) {
return new TypeSafeMatcher<PersistentObject>() {
public String message;
@Override
public boolean matchesSafely(PersistentObject item) {
message = "should have same database id. Expected " + expected.getId() + " but was " + item.getId();
return expected.getId() == item.getId();
}
@Override
public void describeTo(Description description) {
description.appendText(message);
}
};
}
@Override
protected boolean matchesSafely(PRMSQuery query) {
PRMSDisjunctionMaxQuery prmsDmq = ((PRMSDisjunctionMaxQuery) query);
for (TypeSafeMatcher<PRMSQuery> disjunct : disjuncts) {
boolean found = false;
for (PRMSQuery q : prmsDmq.getDisjuncts()) {
found = disjunct.matches(q);
if (found) {
break;
}
}
if (!found) {
return false;
}
}
return true;
}
public static Matcher<String> isExpectedPrefixFollowedByUuid(final String prefix) {
return new TypeSafeMatcher<String>() {
@Override
protected boolean matchesSafely(String item) {
int expectedLength = prefix.length() + 1 + org.keycloak.models.utils.KeycloakModelUtils.generateId().length();
return item.startsWith(prefix) && expectedLength == item.length();
}
@Override
public void describeTo(Description description) {
description.appendText("resourcePath in the format like \"" + prefix + "/<UUID>\"");
}
};
}
/**
* Matches TextView elements having ellipsized text. If text is too long to fit into a TextView,
* it can be either ellipsized ('Too long' shown as 'Too l…' or '… long') or cut off ('Too long'
* shown as 'Too l'). Though acceptable in some cases, usually indicates bad user experience.
*/
public static Matcher<View> hasEllipsizedText() {
return new TypeSafeMatcher<View>(TextView.class) {
@Override
public void describeTo(Description description) {
description.appendText("has ellipsized text");
}
@Override
public boolean matchesSafely(View tv) {
Layout layout = ((TextView) tv).getLayout();
if (layout != null) {
int lines = layout.getLineCount();
return lines > 0 && layout.getEllipsisCount(lines - 1) > 0;
}
return false;
}
};
}
private static Matcher<View> orderMatcher() {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("with correct position order");
}
@Override
public boolean matchesSafely(View v) {
RecyclerView view = (RecyclerView) v;
if (view.getLayoutManager() == null) return false;
ChildViewsIterable childViews = new ChildViewsIterable(view.getLayoutManager());
int pos = view.getChildAdapterPosition(childViews.iterator().next());
for (View child : childViews) {
if (pos != view.getChildAdapterPosition(child)) {
return false;
}
pos ++;
}
return true;
}
};
}
private static Matcher<QueryParamEntry> queryParamEntry(
final Matcher<String> paramName, final Matcher<String> paramVal) {
final Matcher<Iterable<? super String>> valMatcher = hasItem(paramVal);
return new TypeSafeMatcher<QueryParamEntry>(QueryParamEntry.class) {
@Override
public boolean matchesSafely(QueryParamEntry qpe) {
return paramName.matches(qpe.paramName) && valMatcher.matches(qpe.paramVals);
}
@Override
public void describeTo(Description description) {
description.appendDescriptionOf(paramName);
description.appendDescriptionOf(paramVal);
}
};
}
private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
public static Matcher<View> withError(final String expected) {
return new TypeSafeMatcher<View>() {
@Override
public boolean matchesSafely(View view) {
if (!(view instanceof EditText)) {
return false;
}
EditText editText = (EditText) view;
return editText.getError().toString().equals(expected);
}
@Override
public void describeTo(Description description) {
}
};
}
private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + 0 + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(0));
}
};
}
static Matcher<EventCollector> failureIs(final Matcher<? super Throwable> exceptionMatcher) {
return new TypeSafeMatcher<EventCollector>() {
@Override
public boolean matchesSafely(EventCollector item) {
for (Failure f : item.fFailures) {
return exceptionMatcher.matches(f.getException());
}
return false;
}
public void describeTo(org.hamcrest.Description description) {
description.appendText("failure is ");
exceptionMatcher.describeTo(description);
}
};
}
protected boolean matchDisjunctionMaxQuery(DisjunctionMaxQuery dmq) {
if (tieBreaker != dmq.getTieBreakerMultiplier()) {
return false;
}
List<Query> dmqDisjuncts = dmq.getDisjuncts();
if (dmqDisjuncts == null || dmqDisjuncts.size() != disjuncts.length) {
return false;
}
for (TypeSafeMatcher<? extends Query> disjunct : disjuncts) {
boolean found = false;
for (Query q : dmqDisjuncts) {
found = disjunct.matches(q);
if (found) {
break;
}
}
if (!found) {
return false;
}
}
return true;
}
static Matcher<EventCollector> failureIs(final Matcher<? super Throwable> exceptionMatcher) {
return new TypeSafeMatcher<EventCollector>() {
@Override
public boolean matchesSafely(EventCollector item) {
for (Failure f : item.fFailures) {
return exceptionMatcher.matches(f.getException());
}
return false;
}
public void describeTo(org.hamcrest.Description description) {
description.appendText("failure is ");
exceptionMatcher.describeTo(description);
}
};
}
public static Matcher<TypeName> typeName(final Matcher<? super ClassName> matcher) {
final Matcher<? super ClassName> subMatcher = matcher;
return new TypeSafeMatcher<TypeName>() {
@Override
protected boolean matchesSafely(TypeName item) {
return subMatcher.matches(item);
}
@Override
public void describeTo(Description description) {
description.appendText("typename ").appendDescriptionOf(subMatcher);
}
};
}
/**
* This helps "find" the lists in the activity, since there is more than 1.
*
* @param tag
* @return
*/
private static Matcher<View> withTag(final Object tag) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(final Description description) {
description.appendText("has tag equals to: " + tag);
}
@Override
protected boolean matchesSafely(final View view) {
Object viewTag = view.getTag();
if (viewTag == null) {
return tag == null;
}
return viewTag.equals(tag);
}
};
}