BAEL-1579: Hamcrest custom matchers. (#3905)

* BAEL-1579: Hamcrest custom matchers.

* BAEL-1589: Removing hamcrest dependency test scope.
This commit is contained in:
Magdalena Krause
2018-03-29 17:58:19 -03:00
committed by maibin
parent 48b8d2c8a5
commit 6549e41afa
5 changed files with 127 additions and 1 deletions
@@ -0,0 +1,28 @@
package org.baeldung.hamcrest.custommatchers;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
public class IsDivisibleBy extends TypeSafeMatcher<Integer> {
private Integer divider;
private IsDivisibleBy(Integer divider) {
this.divider = divider;
}
@Override
protected boolean matchesSafely(Integer dividend) {
return ((dividend % divider) == 0);
}
@Override
public void describeTo(Description description) {
description.appendText("divisible by " + divider);
}
public static Matcher<Integer> divisibleBy(Integer divider) {
return new IsDivisibleBy(divider);
}
}
@@ -0,0 +1,27 @@
package org.baeldung.hamcrest.custommatchers;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
public class IsOnlyNumbers extends TypeSafeMatcher<String> {
@Override
protected boolean matchesSafely(String s) {
try {
Integer.parseInt(s);
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
@Override
public void describeTo(Description description) {
description.appendText("only numbers");
}
public static Matcher<String> onlyNumbers() {
return new IsOnlyNumbers();
}
}
@@ -0,0 +1,22 @@
package org.baeldung.hamcrest.custommatchers;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
public class IsUppercase extends TypeSafeMatcher<String> {
@Override
protected boolean matchesSafely(String s) {
return s.equals(s.toUpperCase());
}
@Override
public void describeTo(Description description) {
description.appendText("all uppercase");
}
public static Matcher<String> uppercase() {
return new IsUppercase();
}
}