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
70
|
package org.hamcrest.collection;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsAnything;
import static org.hamcrest.core.IsEqual.equalTo;
import java.util.Map;
import java.util.Map.Entry;
public class IsMapContaining<K,V> extends TypeSafeMatcher<Map<K, V>> {
private final Matcher<K> keyMatcher;
private final Matcher<V> valueMatcher;
public IsMapContaining(Matcher<K> keyMatcher, Matcher<V> valueMatcher) {
this.keyMatcher = keyMatcher;
this.valueMatcher = valueMatcher;
}
public boolean matchesSafely(Map<K, V> map) {
for (Entry<K, V> entry : map.entrySet()) {
if (keyMatcher.matches(entry.getKey()) && valueMatcher.matches(entry.getValue())) {
return true;
}
}
return false;
}
public void describeTo(Description description) {
description.appendText("map containing [")
.appendDescriptionOf(keyMatcher)
.appendText("->")
.appendDescriptionOf(valueMatcher)
.appendText("]");
}
@Factory
public static <K,V> Matcher<Map<K,V>> hasEntry(Matcher<K> keyMatcher, Matcher<V> valueMatcher) {
return new IsMapContaining<K,V>(keyMatcher, valueMatcher);
}
@Factory
public static <K,V> Matcher<Map<K,V>> hasEntry(K key, V value) {
return hasEntry(equalTo(key), equalTo(value));
}
@Factory
public static <K,V> Matcher<Map<K,V>> hasKey(Matcher<K> keyMatcher) {
return hasEntry(keyMatcher, IsAnything.<V>anything());
}
@Factory
public static <K,V> Matcher<Map<K,V>> hasKey(K key) {
return hasKey(equalTo(key));
}
@Factory
public static <K,V> Matcher<Map<K,V>> hasValue(Matcher<V> valueMatcher) {
return hasEntry(IsAnything.<K>anything(), valueMatcher);
}
@Factory
public static <K,V> Matcher<Map<K,V>> hasValue(V value) {
return hasValue(equalTo(value));
}
}
|