• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.hamcrest.collection;
2 
3 import org.hamcrest.AbstractMatcherTest;
4 import org.hamcrest.Matcher;
5 
6 import java.util.Map;
7 import java.util.TreeMap;
8 
9 import static org.hamcrest.collection.IsMapContaining.hasEntry;
10 import static org.hamcrest.core.IsAnything.anything;
11 import static org.hamcrest.core.IsEqual.equalTo;
12 
13 public class IsMapContainingTest extends AbstractMatcherTest {
14 
15     @Override
createMatcher()16     protected Matcher<?> createMatcher() {
17         return IsMapContaining.hasEntry("irrelevant", "irrelevant");
18     }
19 
testMatchesMapContainingMatchingKeyAndValue()20     public void testMatchesMapContainingMatchingKeyAndValue() {
21         Map<String,Integer> map = new TreeMap<String,Integer>();
22         map.put("a", 1);
23         map.put("b", 2);
24 
25         assertMatches("matcherA", hasEntry(equalTo("a"), equalTo(1)), map);
26         assertMatches("matcherB", hasEntry(equalTo("b"), equalTo(2)), map);
27         assertMismatchDescription("map was [<a=1>, <b=2>]", hasEntry(equalTo("c"), equalTo(3)), map);
28     }
29 
30 //    no longer compiles. SF
31 //    public void testMatchesMapContainingMatchingKeyAndValueWithoutGenerics() {
32 //        Map map = new HashMap();
33 //        map.put("a", 1);
34 //        map.put("b", 2);
35 //
36 //        assertMatches("matcherA", hasEntry(equalTo("a"), equalTo(1)), map);
37 //        assertMatches("matcherB", hasEntry(equalTo("b"), equalTo(2)), map);
38 //        assertDoesNotMatch("matcherC", hasEntry(equalTo("c"), equalTo(3)), map);
39 //    }
40 //
testDoesNotMatchNull()41     public void testDoesNotMatchNull() {
42         assertMismatchDescription("was null", hasEntry(anything(), anything()), null);
43     }
44 
testHasReadableDescription()45     public void testHasReadableDescription() {
46         assertDescription("map containing [\"a\"-><2>]", hasEntry(equalTo("a"), (equalTo(2))));
47     }
48 
49     // Remaining code no longer compiles, thanks to generics. I think that's a good thing, but
50     // I still need to investigate how this behaves with code that doesn't use generics.
51     // I expect ClassCastExceptions will be thrown.
52     // -Joe.
53 
54 //    public void testDoesNotMatchAnObjectThatIsNotAMap() {
55 //        assertDoesNotMatch("should not matches a string",
56 //                mapContaining(ANYTHING, ANYTHING), "not a map");
57 //    }
58 }
59