Class MatchersExtensions


  • @ThreadSafe
    public class MatchersExtensions
    extends org.hamcrest.Matchers
    Drop-in replacement for Hamcrest's Matchers.
    Author:
    aherscu
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T,​R>
      org.hamcrest.Matcher<Iterable<T>>
      adapted​(Function<T,​R> converter, org.hamcrest.Matcher<Iterable<R>> matcher)
      Adapts a matcher of collection of objects by specified converter function.
      static <T,​R>
      org.hamcrest.Matcher<Iterable<T>>
      adaptedByJXPath​(String jxpath, org.hamcrest.Matcher<Iterable<R>> matcher)
      Adapts a matcher of collection of objects by specified JXPath expression; see Commons Apache JXPath User Manual
      static org.hamcrest.Matcher<Stream<String>> adaptedByRegex​(String regex, org.hamcrest.Matcher<Stream<String>> matcher)
      Adapts a matcher of collection of strings by specified regular expression.
      static <T> org.hamcrest.Matcher<Collection<T>> adaptedCollectionToIterableMatcher​(org.hamcrest.Matcher<Iterable<T>> matcher)
      Adapts a Collection matcher to an Iterable matcher.
      static <T> org.hamcrest.Matcher<Iterable<T>> adaptedIterableToCollectionMatcher​(org.hamcrest.Matcher<Collection<T>> matcher)
      Adapts a Collection matcher to an Iterable matcher.
      static <T,​R>
      org.hamcrest.Matcher<T>
      adaptedObject​(Function<T,​R> converter, org.hamcrest.Matcher<R> matcher)
      Adapts a matcher of object by specified converter function.
      static org.hamcrest.Matcher<String> adaptedStringToBooleanMatcher​(org.hamcrest.Matcher<Boolean> booleanMatcher)
      Adapts an expected string to a boolean matcher.
      static org.hamcrest.Matcher<String> adaptedStringToNumericMatcher​(org.hamcrest.Matcher<? extends Number> numericMatcher, Class<? extends Number>... typeOfExpectedNumber)
      Adapts an expected string to a numeric matcher.
      static org.hamcrest.Matcher<Number> adaptedTypeOfNumericMatcher​(org.hamcrest.Matcher<? extends Number> numericMatcher, Class<? extends Number> targetType)
      Adapts an expected number to a numeric matcher.
      static <T> org.hamcrest.Matcher<Collection<T>> collection​(org.hamcrest.Matcher<Collection<? extends T>> matcher)
      Workaround for https://github.com/hamcrest/JavaHamcrest/issues/289
      static <C,​MR extends org.hamcrest.Matcher<?>,​MT extends org.hamcrest.Matcher<?>>
      jcurry.util.function.CurryingBiFunction<C,​MR,​MT>
      curriedAdapter​(BiFunction<C,​MR,​MT> adapted)
      Experimental.
      static <T> org.hamcrest.Matcher<Iterable<T>> iterable​(org.hamcrest.Matcher<Iterable<? extends T>> matcher)
      Workaround for https://github.com/hamcrest/JavaHamcrest/issues/289
      static <T> org.hamcrest.Matcher<Iterable<T>> iterableSuper​(org.hamcrest.Matcher<Iterable<? super T>> matcher)
      Workaround for https://github.com/hamcrest/JavaHamcrest/issues/289
      static <T> org.hamcrest.Matcher<Iterable<T>> ordered​(com.google.common.collect.Ordering<T> ordering)
      Matches an ordered Iterable by specified ordering.
      • Methods inherited from class org.hamcrest.Matchers

        allOf, allOf, allOf, allOf, allOf, allOf, allOf, aMapWithSize, aMapWithSize, anEmptyMap, any, anyOf, anyOf, anyOf, anyOf, anyOf, anyOf, anyOf, anything, anything, array, arrayContaining, arrayContaining, arrayContaining, arrayContainingInAnyOrder, arrayContainingInAnyOrder, arrayContainingInAnyOrder, arrayWithSize, arrayWithSize, blankOrNullString, blankString, both, closeTo, closeTo, comparesEqualTo, contains, contains, contains, contains, containsInAnyOrder, containsInAnyOrder, containsInAnyOrder, containsInRelativeOrder, containsInRelativeOrder, containsInRelativeOrder, containsString, containsStringIgnoringCase, describedAs, either, empty, emptyArray, emptyCollectionOf, emptyIterable, emptyIterableOf, emptyOrNullString, emptyString, endsWith, endsWithIgnoringCase, equalTo, equalToCompressingWhiteSpace, equalToIgnoringCase, equalToIgnoringWhiteSpace, equalToObject, eventFrom, eventFrom, everyItem, greaterThan, greaterThanOrEqualTo, hasEntry, hasEntry, hasItem, hasItem, hasItemInArray, hasItemInArray, hasItems, hasItems, hasKey, hasKey, hasLength, hasProperty, hasProperty, hasSize, hasSize, hasToString, hasToString, hasValue, hasValue, hasXPath, hasXPath, hasXPath, hasXPath, in, in, instanceOf, is, is, isA, isEmptyOrNullString, isEmptyString, isIn, isIn, isOneOf, iterableWithSize, iterableWithSize, lessThan, lessThanOrEqualTo, matchesPattern, matchesPattern, matchesRegex, matchesRegex, not, not, notANumber, notNullValue, notNullValue, nullValue, nullValue, oneOf, sameInstance, samePropertyValuesAs, startsWith, startsWithIgnoringCase, stringContainsInOrder, stringContainsInOrder, theInstance, typeCompatibleWith
    • Constructor Detail

      • MatchersExtensions

        public MatchersExtensions()
    • Method Detail

      • adapted

        public static <T,​R> org.hamcrest.Matcher<Iterable<T>> adapted​(Function<T,​R> converter,
                                                                            org.hamcrest.Matcher<Iterable<R>> matcher)
        Adapts a matcher of collection of objects by specified converter function.
        Type Parameters:
        T - type to be adapted
        R - type after adaption
        Parameters:
        converter - the converter function
        matcher - the matcher
        Returns:
        adapted matcher
      • adaptedByJXPath

        public static <T,​R> org.hamcrest.Matcher<Iterable<T>> adaptedByJXPath​(String jxpath,
                                                                                    org.hamcrest.Matcher<Iterable<R>> matcher)
        Adapts a matcher of collection of objects by specified JXPath expression; see Commons Apache JXPath User Manual
        Parameters:
        jxpath - the JXPath expression
        matcher - the matcher
        Returns:
        adapted matcher
      • adaptedByRegex

        public static org.hamcrest.Matcher<Stream<String>> adaptedByRegex​(String regex,
                                                                          org.hamcrest.Matcher<Stream<String>> matcher)
        Adapts a matcher of collection of strings by specified regular expression.

        For example:

         MatcherAssert.assertThat(
             asList("oiojf[[test]]-1fqwe", "fds[[best]]-4fas",
                 "fw[[test]]-3vxc", "sf[[test]]-3fae"),
             adaptedByRegex("\\[\\[test\\]\\]\\-\\d+",
                 adaptedCollectionToIterableMatcher(hasDistinctElements())));
         

        should fail because there are two items containing [[test]]-3

        Parameters:
        regex - the regular expression
        matcher - the matcher
        Returns:
        adapted matcher
      • adaptedCollectionToIterableMatcher

        public static <T> org.hamcrest.Matcher<Collection<T>> adaptedCollectionToIterableMatcher​(org.hamcrest.Matcher<Iterable<T>> matcher)
        Adapts a Collection matcher to an Iterable matcher.
        Parameters:
        matcher - the matcher to adapted
        Returns:
        the adapted matcher
      • adaptedIterableToCollectionMatcher

        public static <T> org.hamcrest.Matcher<Iterable<T>> adaptedIterableToCollectionMatcher​(org.hamcrest.Matcher<Collection<T>> matcher)
        Adapts a Collection matcher to an Iterable matcher.
        Parameters:
        matcher - the matcher to adapted
        Returns:
        the adapted matcher
      • adaptedObject

        public static <T,​R> org.hamcrest.Matcher<T> adaptedObject​(Function<T,​R> converter,
                                                                        org.hamcrest.Matcher<R> matcher)
        Adapts a matcher of object by specified converter function.
        Type Parameters:
        T - type to be adapted
        R - type after adaption
        Parameters:
        converter - the converter function
        matcher - the matcher
        Returns:
        adapted matcher
      • adaptedStringToBooleanMatcher

        public static org.hamcrest.Matcher<String> adaptedStringToBooleanMatcher​(org.hamcrest.Matcher<Boolean> booleanMatcher)
        Adapts an expected string to a boolean matcher.
        Parameters:
        booleanMatcher - the boolean matcher
        Returns:
        a string matcher
      • adaptedStringToNumericMatcher

        @SafeVarargs
        public static org.hamcrest.Matcher<String> adaptedStringToNumericMatcher​(org.hamcrest.Matcher<? extends Number> numericMatcher,
                                                                                 Class<? extends Number>... typeOfExpectedNumber)
        Adapts an expected string to a numeric matcher.
        Parameters:
        numericMatcher - the numeric matcher
        typeOfExpectedNumber - optional, the desired type to be adapted; if not supplied Double is assumed
        Returns:
        a string matcher
      • adaptedTypeOfNumericMatcher

        public static org.hamcrest.Matcher<Number> adaptedTypeOfNumericMatcher​(org.hamcrest.Matcher<? extends Number> numericMatcher,
                                                                               Class<? extends Number> targetType)
        Adapts an expected number to a numeric matcher. Use when a receiving matcher expects a different numeric type than provided.

        Example:

         hasSize(
             adaptedTypeOfNumericMatcher(
                 greaterThanOrEqualTo(
                     configuration.getNonDuplicatedLogsQuantity() * 0.9)))
         

        In this case, #hasSize(Matcher) expects an Integer, while Matchers.greaterThanOrEqualTo(Comparable) provides a double because of the 0.9 multiplication.

        NOT READY YET: fails to convert Integer to Double

        Parameters:
        numericMatcher - the numeric matcher
        targetType - the target type
        Returns:
        a numeric matcher
      • collection

        public static <T> org.hamcrest.Matcher<Collection<T>> collection​(org.hamcrest.Matcher<Collection<? extends T>> matcher)
        Workaround for https://github.com/hamcrest/JavaHamcrest/issues/289
        Type Parameters:
        T - matched type
        Parameters:
        matcher - matcher to fix
        Returns:
        fixed matcher
      • curriedAdapter

        public static <C,​MR extends org.hamcrest.Matcher<?>,​MT extends org.hamcrest.Matcher<?>> jcurry.util.function.CurryingBiFunction<C,​MR,​MT> curriedAdapter​(BiFunction<C,​MR,​MT> adapted)
        Experimental. Can be used to bind an adapter to a fixed converter.
        Type Parameters:
        C - the type of converter
        MR - the type of matcher to adapt into
        MT - the type of matcher
        Parameters:
        adapted - the adapter to be bound
        Returns:
        the currying bi-function
      • iterable

        public static <T> org.hamcrest.Matcher<Iterable<T>> iterable​(org.hamcrest.Matcher<Iterable<? extends T>> matcher)
        Workaround for https://github.com/hamcrest/JavaHamcrest/issues/289
        Type Parameters:
        T - matched type
        Parameters:
        matcher - matcher to fix
        Returns:
        fixed matcher
      • iterableSuper

        public static <T> org.hamcrest.Matcher<Iterable<T>> iterableSuper​(org.hamcrest.Matcher<Iterable<? super T>> matcher)
        Workaround for https://github.com/hamcrest/JavaHamcrest/issues/289
        Type Parameters:
        T - matched type
        Parameters:
        matcher - matcher to fix
        Returns:
        fixed matcher
      • ordered

        public static <T> org.hamcrest.Matcher<Iterable<T>> ordered​(com.google.common.collect.Ordering<T> ordering)
        Matches an ordered Iterable by specified ordering.
        Parameters:
        ordering - the expected ordering
        Returns:
        the ordering matcher