Package org.xmlunit.matchers
Class TypeMatcher<T>
java.lang.Object
org.hamcrest.BaseMatcher<T>
org.hamcrest.TypeSafeMatcher<String>
org.xmlunit.matchers.TypeMatcher<T>
- Type Parameters:
T
- target type
- All Implemented Interfaces:
org.hamcrest.Matcher<String>
,org.hamcrest.SelfDescribing
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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic TypeMatcher<BigDecimal>
asBigDecimal
(org.hamcrest.Matcher<? extends BigDecimal> valueMatcher) Creates a matcher that matches when the examined string is convertible toBigDecimal
and converted value satisfies the specifiedvalueMatcher
.static TypeMatcher<Boolean>
Creates a matcher that matches when the examined string is convertible toBoolean
and converted value satisfies the specifiedvalueMatcher
.static TypeMatcher<Double>
Creates a matcher that matches when the examined string is convertible toDouble
and converted value satisfies the specifiedvalueMatcher
.static TypeMatcher<Integer>
Creates a matcher that matches when the examined string is convertible toInteger
and converted value satisfies the specifiedvalueMatcher
.protected abstract T
Converts the item.protected void
describeMismatchSafely
(String item, org.hamcrest.Description mismatchDescription) void
describeTo
(org.hamcrest.Description description) protected boolean
matchesSafely
(String item) Methods inherited from class org.hamcrest.TypeSafeMatcher
describeMismatch, matches
Methods inherited from class org.hamcrest.BaseMatcher
_dont_implement_Matcher___instead_extend_BaseMatcher_, toString
-
Constructor Details
-
TypeMatcher
- Parameters:
clazz
- expected type to convert value tovalueMatcher
- matcher to apply to converted value
-
-
Method Details
-
matchesSafely
- Specified by:
matchesSafely
in classorg.hamcrest.TypeSafeMatcher<String>
-
describeTo
public void describeTo(org.hamcrest.Description description) -
describeMismatchSafely
- Overrides:
describeMismatchSafely
in classorg.hamcrest.TypeSafeMatcher<String>
-
convert
Converts the item.- Parameters:
item
- value as String- Returns:
- converted value
-
asBigDecimal
public static TypeMatcher<BigDecimal> asBigDecimal(org.hamcrest.Matcher<? extends BigDecimal> valueMatcher) Creates a matcher that matches when the examined string is convertible toBigDecimal
and converted value satisfies the specifiedvalueMatcher
.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
Creates a matcher that matches when the examined string is convertible toDouble
and converted value satisfies the specifiedvalueMatcher
.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
Creates a matcher that matches when the examined string is convertible toInteger
and converted value satisfies the specifiedvalueMatcher
.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
Creates a matcher that matches when the examined string is convertible toBoolean
and converted value satisfies the specifiedvalueMatcher
.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
-