Class TypeMatcher<T>

  • Type Parameters:
    T - target type
    All Implemented Interfaces:
    org.hamcrest.Matcher<java.lang.String>, org.hamcrest.SelfDescribing
    Direct Known Subclasses:
    TypeMatcher.BigDecimalTypeMatcher, TypeMatcher.BooleanTypeMatcher, TypeMatcher.DoubleTypeMatcher, TypeMatcher.IntegerTypeMatcher

    public abstract class TypeMatcher<T>
    extends org.hamcrest.TypeSafeMatcher<java.lang.String>
    This Hamcrest Matcher is base Matcher to verify whether examined string value is convertible to the specified type and whether converted value corresponds to the given value valueMatcher. Examined string value can be evaluation of an XPath expression.

    Currently BigDecimal, Double, Integer and Boolean types are supported.

    Simple examples

         assertThat("3.0", asDouble(greaterThanOrEqualTo(2.0)));
         assertThat("1.0e1", asBigDecimal(equalTo(BigDecimal.TEN)));
         assertThat("3", asInt(lessThan(4)));
         assertThat("false", asBoolean(equalTo(false)));
         assertThat("True", asBoolean(equalTo(true)));
     

    Examples with XPath evaluation

         String xml = "<fruits>" +
                 "<fruit name=\"apple\"/>" +
                 "<fruit name=\"orange\"/>" +
                 "<fruit name=\"banana\"/>" +
                 "<fruit name=\"pear\" fresh=\"false\"/>" +
                 "</fruits>";
    
         assertThat(xml, hasXPath("count(//fruits/fruit)", asDouble(equalTo(4.0))));
         assertThat(xml, hasXPath("count(//fruits/fruit)", asBigDecimal(greaterThan(BigDecimal.ONE))));
         assertThat(xml, hasXPath("count(//fruits/fruit)", asInt(lessThan(5))));
         assertThat(xml, hasXPath("//fruits/fruit[@name=\"pear\"]/@fresh", asBoolean(equalTo(false))));
     
    Since:
    XMLUnit 2.6.2
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Class<T> clazz  
      private java.lang.Exception exception  
      private T value  
      private org.hamcrest.Matcher<? extends T> valueMatcher  
    • Constructor Summary

      Constructors 
      Constructor Description
      TypeMatcher​(java.lang.Class<T> clazz, org.hamcrest.Matcher<? extends T> valueMatcher)  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      static TypeMatcher<java.math.BigDecimal> asBigDecimal​(org.hamcrest.Matcher<? extends java.math.BigDecimal> valueMatcher)
      Creates a matcher that matches when the examined string is convertible to BigDecimal and converted value satisfies the specified valueMatcher.
      static TypeMatcher<java.lang.Boolean> asBoolean​(org.hamcrest.Matcher<? extends java.lang.Boolean> valueMatcher)
      Creates a matcher that matches when the examined string is convertible to Boolean and converted value satisfies the specified valueMatcher.
      static TypeMatcher<java.lang.Double> asDouble​(org.hamcrest.Matcher<? extends java.lang.Double> valueMatcher)
      Creates a matcher that matches when the examined string is convertible to Double and converted value satisfies the specified valueMatcher.
      static TypeMatcher<java.lang.Integer> asInt​(org.hamcrest.Matcher<? extends java.lang.Integer> valueMatcher)
      Creates a matcher that matches when the examined string is convertible to Integer and converted value satisfies the specified valueMatcher.
      protected abstract T convert​(java.lang.String item)  
      protected void describeMismatchSafely​(java.lang.String item, org.hamcrest.Description mismatchDescription)  
      void describeTo​(org.hamcrest.Description description)  
      protected boolean matchesSafely​(java.lang.String item)  
      private T nullSafeConvert​(java.lang.String item)  
      • Methods inherited from class org.hamcrest.TypeSafeMatcher

        describeMismatch, matches
      • Methods inherited from class org.hamcrest.BaseMatcher

        _dont_implement_Matcher___instead_extend_BaseMatcher_, isNotNull, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • value

        private T value
      • exception

        private java.lang.Exception exception
      • clazz

        private final java.lang.Class<T> clazz
      • valueMatcher

        private final org.hamcrest.Matcher<? extends T> valueMatcher
    • Constructor Detail

      • TypeMatcher

        public TypeMatcher​(java.lang.Class<T> clazz,
                           org.hamcrest.Matcher<? extends T> valueMatcher)
    • Method Detail

      • matchesSafely

        protected boolean matchesSafely​(java.lang.String item)
        Specified by:
        matchesSafely in class org.hamcrest.TypeSafeMatcher<java.lang.String>
      • describeTo

        public void describeTo​(org.hamcrest.Description description)
      • describeMismatchSafely

        protected void describeMismatchSafely​(java.lang.String item,
                                              org.hamcrest.Description mismatchDescription)
        Overrides:
        describeMismatchSafely in class org.hamcrest.TypeSafeMatcher<java.lang.String>
      • nullSafeConvert

        private T nullSafeConvert​(java.lang.String item)
      • convert

        protected abstract T convert​(java.lang.String item)
      • asBigDecimal

        public static TypeMatcher<java.math.BigDecimal> asBigDecimal​(org.hamcrest.Matcher<? extends java.math.BigDecimal> valueMatcher)
        Creates a matcher that matches when the examined string is convertible to BigDecimal and converted value satisfies the specified valueMatcher.

        For example:

             assertThat("1.0e1", asBigDecimal(equalTo(BigDecimal.TEN)));
             assertThat(xml, hasXPath("count(//fruits/fruit)", asBigDecimal(greaterThan(BigDecimal.ONE))));
         
        Parameters:
        valueMatcher - valueMatcher for the converted value
        Returns:
        the BigDecimal matcher
      • asDouble

        public static TypeMatcher<java.lang.Double> asDouble​(org.hamcrest.Matcher<? extends java.lang.Double> valueMatcher)
        Creates a matcher that matches when the examined string is convertible to Double and converted value satisfies the specified valueMatcher.

        For example:

             assertThat("3.0", asDouble(greaterThanOrEqualTo(2.0)));
             assertThat(xml, hasXPath("count(//fruits/fruit)", asDouble(equalTo(3.0))));
         
        Parameters:
        valueMatcher - valueMatcher for the converted value
        Returns:
        the Double matcher
      • asInt

        public static TypeMatcher<java.lang.Integer> asInt​(org.hamcrest.Matcher<? extends java.lang.Integer> valueMatcher)
        Creates a matcher that matches when the examined string is convertible to Integer and converted value satisfies the specified valueMatcher.

        For example:

             assertThat("3", asInt(lessThan(4)));
             assertThat(xml, hasXPath("count(//fruits/fruit)", asInt(lessThan(4))));
         
        Parameters:
        valueMatcher - valueMatcher for the converted value
        Returns:
        the Integer matcher
      • asBoolean

        public static TypeMatcher<java.lang.Boolean> asBoolean​(org.hamcrest.Matcher<? extends java.lang.Boolean> valueMatcher)
        Creates a matcher that matches when the examined string is convertible to Boolean and converted value satisfies the specified valueMatcher.

        For example:

             assertThat("false", asBoolean(equalTo(false)));
             assertThat(xml, hasXPath("//fruits/fruit[@name=\"apple\"]/@fresh", asBoolean(equalTo(true))));
         
        Parameters:
        valueMatcher - valueMatcher for the converted value
        Returns:
        the Boolean matcher