org.hamcrest.TypeSafeMatcher Java Examples

The following examples show how to use org.hamcrest.TypeSafeMatcher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ControlYourDeviceActivityTest.java    From AdminControl with GNU General Public License v3.0 6 votes vote down vote up
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));
        }
    };
}
 
Example #2
Source File: LayoutMatchers.java    From android-test with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }
  };
}
 
Example #3
Source File: IntentMatchers.java    From android-test with Apache License 2.0 6 votes vote down vote up
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());
    }
  };
}
 
Example #4
Source File: ViewRecordingTest.java    From go-bees with GNU General Public License v3.0 6 votes vote down vote up
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));
        }
    };
}
 
Example #5
Source File: ResponseMatcher.java    From cukes with Apache License 2.0 6 votes vote down vote up
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(">");
            }
        };
    }
 
Example #6
Source File: Helper.java    From japicmp with Apache License 2.0 6 votes vote down vote up
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;
		}
	};
}
 
Example #7
Source File: Matchers.java    From Kore with Apache License 2.0 6 votes vote down vote up
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);
        }
    };
}
 
Example #8
Source File: PersistentObjectMatchers.java    From gocd with Apache License 2.0 6 votes vote down vote up
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);
        }
    };

}
 
Example #9
Source File: AssertAdminEvents.java    From keycloak with Apache License 2.0 6 votes vote down vote up
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>\"");
        }

    };
}
 
Example #10
Source File: TypeNameMatcher.java    From raml-java-tools with Apache License 2.0 6 votes vote down vote up
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);
            }
        };
    }
 
Example #11
Source File: OnlineLayersPreferenceActivityTest.java    From mage-android with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
        }
    };
}
 
Example #12
Source File: TermSubQueryBuilderTest.java    From querqy with Apache License 2.0 6 votes vote down vote up
@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;
    
    
}
 
Example #13
Source File: EspTextViewMatcher.java    From espresso-macchiato with MIT License 6 votes vote down vote up
public static Matcher<View> withTextColor(@ColorInt final int color) {
    return new TypeSafeMatcher<View>() {

        @Override
        public void describeTo(Description description) {
            description.appendText("text color ");
            description.appendValue(color);
        }

        @Override
        protected boolean matchesSafely(View item) {
            //noinspection SimplifiableIfStatement - for more readable code
            if(!(item instanceof TextView)) {
                return false;
            }

            return ((TextView)item).getCurrentTextColor() == color;
        }
    };
}
 
Example #14
Source File: EventCollector.java    From hamcrest-junit with Eclipse Public License 1.0 6 votes vote down vote up
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);
        }
    };
}
 
Example #15
Source File: EventCollector.java    From hamcrest-junit with Eclipse Public License 1.0 6 votes vote down vote up
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);
        }
    };
}
 
Example #16
Source File: LogMatchers.java    From c5-replicator with Apache License 2.0 6 votes vote down vote up
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);
    }
  };
}
 
Example #17
Source File: Matchers.java    From heroic with Apache License 2.0 6 votes vote down vote up
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);
        }
    };
}
 
Example #18
Source File: TextInputLayoutMatchers.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** 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;
    }
  };
}
 
Example #19
Source File: BucketTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
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.");
			}
		};
	}
 
Example #20
Source File: RestOperationsFactoryTest.java    From bowman with Apache License 2.0 6 votes vote down vote up
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);
		}
	};
}
 
Example #21
Source File: CustomMatchers.java    From android-test-demo with MIT License 6 votes vote down vote up
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) {

        }
    };
}
 
Example #22
Source File: ScreengrabTest.java    From ETHWallet with GNU General Public License v3.0 6 votes vote down vote up
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));
        }
    };
}
 
Example #23
Source File: UriMatchers.java    From android-test with Apache License 2.0 6 votes vote down vote up
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);
    }
  };
}
 
Example #24
Source File: BucketTest.java    From flink with Apache License 2.0 6 votes vote down vote up
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.");
			}
		};
	}
 
Example #25
Source File: SystemTestMatchers.java    From elasticsearch with Apache License 2.0 6 votes vote down vote up
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");
        }
    };
}
 
Example #26
Source File: RecyclerViewEspressoFactory.java    From ChipsLayoutManager with Apache License 2.0 6 votes vote down vote up
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;
        }
    };
}
 
Example #27
Source File: DomMatchersTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #28
Source File: MatcherEx.java    From android-mvvm-with-tests with MIT License 6 votes vote down vote up
/**
 * 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;
            }
        }
    };
}
 
Example #29
Source File: UriMatchers.java    From android-test with Apache License 2.0 6 votes vote down vote up
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);
    }
  };
}
 
Example #30
Source File: MatcherHelper.java    From fullstop with Apache License 2.0 6 votes vote down vote up
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");
        }
    };
}