/
BaseMatcherCollector.java
69 lines (57 loc) · 2.18 KB
/
BaseMatcherCollector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package jdk8;
import java.util.ArrayList;
import java.util.List;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import custombasematchers.SpeedCameraMatchers;
import jdk8.speedcameratests.SoftAssertionsJUnitTest;
import jdk8.speedcameratests.SoftAssertionsTestNgTest;
/**
*
* @author David Baak, Copyright March 2016 <br />
* This BaseMatcherCollector enables you to chain custom {@link BaseMatcher} of type <T> with {@code chain} and {@code and}.<br />
* An example of a custom {@link BaseMatcher} is: {@link SpeedCameraMatchers}<br />
* An example of chaining in Junit is: {@link SoftAssertionsJUnitTest}<br />
* An example of chaining in TestNg is: {@link SoftAssertionsTestNgTest}<br />
* @param <T>
*/
public class BaseMatcherCollector<T> extends BaseMatcher<T> {
private final List<BaseMatcher<? super T>> matchers = new ArrayList<BaseMatcher<? super T>>();
private final List<BaseMatcher<? super T>> mismatches = new ArrayList<BaseMatcher<? super T>>();
private BaseMatcherCollector(final BaseMatcher<? super T> matcher) {
matchers.add(matcher);
}
public boolean matches(final Object itemToMatch) {
matchers
.stream()
.filter(matcher->!matcher.matches(itemToMatch))
.forEach(matcher->mismatches.add(matcher));
return mismatches.isEmpty();
}
public void describeTo(final Description description) {
description.appendList("\n", "\n" + "AND" + " ", "", matchers);
}
@Override
public void describeMismatch(final Object item, final Description description) {
mismatches.forEach(mismatch->mismatch
.describeMismatch(item, description.appendText("\n")
.appendDescriptionOf(mismatch).appendText(" BUT ")));
}
/**
* Adds your nth assertion to the chain.
* @param matcher
* @return the existing instance of BaseMatcherCollector
*/
public BaseMatcherCollector<T> and(final BaseMatcher<? super T> matcher) {
matchers.add(matcher);
return this;
}
/**
* Initiates your assertion chain.
* @param matcher
* @return an instance of BaseMatcherCollector Type {@code<T>} <br />
*/
public static <T> BaseMatcherCollector<T> collect(final BaseMatcher<? super T> matcher) {
return new BaseMatcherCollector<T>(matcher);
}
}