BAEL-1579: Hamcrest custom matchers. (#3905)
* BAEL-1579: Hamcrest custom matchers. * BAEL-1589: Removing hamcrest dependency test scope.
This commit is contained in:
+28
@@ -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);
|
||||
}
|
||||
}
|
||||
+27
@@ -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();
|
||||
}
|
||||
}
|
||||
+22
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user