Home | History | Annotate | Download | only in collection
      1 package org.hamcrest.collection;
      2 
      3 import org.hamcrest.AbstractMatcherTest;
      4 import org.hamcrest.Matcher;
      5 
      6 import java.util.HashMap;
      7 import java.util.Map;
      8 import java.util.TreeMap;
      9 
     10 import static org.hamcrest.MatcherAssert.assertThat;
     11 import static org.hamcrest.collection.IsMapContaining.hasKey;
     12 
     13 public class IsMapContainingKeyTest extends AbstractMatcherTest {
     14 
     15     @Override
     16     protected Matcher<?> createMatcher() {
     17         return hasKey("foo");
     18     }
     19 
     20     public void testMatchesSingletonMapContainingKey() {
     21         Map<String,Integer> map = new HashMap<String, Integer>();
     22         map.put("a", 1);
     23 
     24         assertMatches("Matches single key", hasKey("a"), map);
     25     }
     26 
     27     public void testMatchesMapContainingKey() {
     28         Map<String,Integer> map = new HashMap<String, Integer>();
     29         map.put("a", 1);
     30         map.put("b", 2);
     31         map.put("c", 3);
     32 
     33         assertMatches("Matches a", hasKey("a"), map);
     34         assertMatches("Matches c", hasKey("c"), map);
     35     }
     36 
     37 
     38 //    No longer compiles
     39 //    public void testMatchesMapContainingKeyWithNoGenerics() {
     40 //        Map map = new HashMap();
     41 //        map.put("a", 1);
     42 //        map.put("b", 2);
     43 //        map.put("c", 3);
     44 //
     45 //        assertMatches("Matches a", hasKey("a"), map);
     46 //        assertMatches("Matches c", hasKey("c"), map);
     47 //    }
     48 
     49     public void testMatchesMapContainingKeyWithIntegerKeys() throws Exception {
     50         Map<Integer, String> map = new HashMap<Integer, String>();
     51         map.put(1, "A");
     52         map.put(2, "B");
     53 
     54         assertThat(map, hasKey(1));
     55     }
     56 
     57     public void testMatchesMapContainingKeyWithNumberKeys() throws Exception {
     58         Map<Number, String> map = new HashMap<Number, String>();
     59         map.put(1, "A");
     60         map.put(2, "B");
     61 
     62         assertThat(map, hasKey((Number)1));
     63 
     64         // TODO: work out the correct sprinkling of wildcards to get this to work!
     65 //        assertThat(map, hasKey(1));
     66     }
     67 
     68     public void testHasReadableDescription() {
     69         assertDescription("map containing [\"a\"->ANYTHING]", hasKey("a"));
     70     }
     71 
     72     public void testDoesNotMatchEmptyMap() {
     73         assertMismatchDescription("map was []", hasKey("Foo"), new HashMap<String,Integer>());
     74     }
     75 
     76     public void testDoesNotMatchMapMissingKey() {
     77         Map<String,Integer> map = new TreeMap<String, Integer>();
     78         map.put("a", 1);
     79         map.put("b", 2);
     80         map.put("c", 3);
     81 
     82         assertMismatchDescription("map was [<a=1>, <b=2>, <c=3>]", hasKey("d"), map);
     83     }
     84 }
     85