T
- target typepublic abstract class TypeMatcher<T>
extends org.hamcrest.TypeSafeMatcher<java.lang.String>
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))));
Modifier and Type | Class and Description |
---|---|
private static class |
TypeMatcher.BigDecimalTypeMatcher |
private static class |
TypeMatcher.BooleanTypeMatcher |
private static class |
TypeMatcher.DoubleTypeMatcher |
private static class |
TypeMatcher.IntegerTypeMatcher |
Modifier and Type | Field and Description |
---|---|
private java.lang.Class<T> |
clazz |
private java.lang.Exception |
exception |
private T |
value |
private org.hamcrest.Matcher<? extends T> |
valueMatcher |
Constructor and Description |
---|
TypeMatcher(java.lang.Class<T> clazz,
org.hamcrest.Matcher<? extends T> valueMatcher) |
Modifier and Type | Method and 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) |
private T value
private java.lang.Exception exception
private final java.lang.Class<T> clazz
private final org.hamcrest.Matcher<? extends T> valueMatcher
protected boolean matchesSafely(java.lang.String item)
matchesSafely
in class org.hamcrest.TypeSafeMatcher<java.lang.String>
public void describeTo(org.hamcrest.Description description)
protected void describeMismatchSafely(java.lang.String item, org.hamcrest.Description mismatchDescription)
describeMismatchSafely
in class org.hamcrest.TypeSafeMatcher<java.lang.String>
private T nullSafeConvert(java.lang.String item)
protected abstract T convert(java.lang.String item)
public static TypeMatcher<java.math.BigDecimal> asBigDecimal(org.hamcrest.Matcher<? extends java.math.BigDecimal> valueMatcher)
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))));
valueMatcher
- valueMatcher for the converted valuepublic static TypeMatcher<java.lang.Double> asDouble(org.hamcrest.Matcher<? extends java.lang.Double> valueMatcher)
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))));
valueMatcher
- valueMatcher for the converted valuepublic static TypeMatcher<java.lang.Integer> asInt(org.hamcrest.Matcher<? extends java.lang.Integer> valueMatcher)
Integer
and converted value satisfies the specified valueMatcher
.
For example:
assertThat("3", asInt(lessThan(4))); assertThat(xml, hasXPath("count(//fruits/fruit)", asInt(lessThan(4))));
valueMatcher
- valueMatcher for the converted valuepublic static TypeMatcher<java.lang.Boolean> asBoolean(org.hamcrest.Matcher<? extends java.lang.Boolean> valueMatcher)
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))));
valueMatcher
- valueMatcher for the converted value