1 package com.lexicalscope.fluentreflection.bean.endtoend;
2
3 import static org.hamcrest.Matchers.*;
4
5 import java.util.Map.Entry;
6
7 import org.hamcrest.Description;
8 import org.hamcrest.Matcher;
9 import org.hamcrest.TypeSafeMatcher;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 class MapEntryMatcher<K, V> extends TypeSafeMatcher<Entry<K, V>> {
28 private final Matcher<? super K> key;
29 private final Matcher<? super V> value;
30
31 private MapEntryMatcher(final Matcher<? super K> key, final Matcher<? super V> value) {
32 this.key = key;
33 this.value = value;
34 }
35
36 @Override protected boolean matchesSafely(final Entry<K, V> item) {
37 return key.matches(item.getKey()) && value.matches(item.getValue());
38 }
39
40 @Override public void describeTo(final Description description) {
41 description.
42 appendText("Map.Entry ").
43 appendDescriptionOf(key).
44 appendText(" -> ").
45 appendDescriptionOf(value);
46 }
47
48 public static <K, V> MapEntryMatcher<K, V> mapEntry(final K key, final V value) {
49 return new MapEntryMatcher<K, V>(equalTo(key), equalTo(value));
50 }
51
52 public static <K, V> MapEntryMatcher<K, V> mapEntry(final K key) {
53 return new MapEntryMatcher<K, V>(equalTo(key), anything());
54 }
55 }