/** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one entry whose key equals the specified <code>key</code> <b>and</b> whose value equals the * specified <code>value</code>. * <p/> * For example: * <pre>assertThat(myMap, hasEntry("bar", "foo"))</pre> * * @param key * the key that, in combination with the value, must be describe at least one entry * @param value * the value that, in combination with the key, must be describe at least one entry */ public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K, ? extends V>> hasEntry(K key, V value) { return org.hamcrest.collection.IsMapContaining.<K,V>hasEntry(key, value); }
/** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one entry whose key satisfies the specified <code>keyMatcher</code> <b>and</b> whose * value satisfies the specified <code>valueMatcher</code>. * <p/> * For example: * <pre>assertThat(myMap, hasEntry(equalTo("bar"), equalTo("foo")))</pre> * * @param keyMatcher * the key matcher that, in combination with the valueMatcher, must be satisfied by at least one entry * @param valueMatcher * the value matcher that, in combination with the keyMatcher, must be satisfied by at least one entry */ public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K, ? extends V>> hasEntry(org.hamcrest.Matcher<? super K> keyMatcher, org.hamcrest.Matcher<? super V> valueMatcher) { return org.hamcrest.collection.IsMapContaining.<K,V>hasEntry(keyMatcher, valueMatcher); }
/** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one entry whose key satisfies the specified <code>keyMatcher</code> <b>and</b> whose * value satisfies the specified <code>valueMatcher</code>. * For example: * <pre>assertThat(myMap, hasEntry(equalTo("bar"), equalTo("foo")))</pre> * * @param keyMatcher * the key matcher that, in combination with the valueMatcher, must be satisfied by at least one entry * @param valueMatcher * the value matcher that, in combination with the keyMatcher, must be satisfied by at least one entry */ public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> hasEntry(org.hamcrest.Matcher<? super K> keyMatcher, org.hamcrest.Matcher<? super V> valueMatcher) { return org.hamcrest.collection.IsMapContaining.hasEntry(keyMatcher, valueMatcher); }
/** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one entry whose key equals the specified <code>key</code> <b>and</b> whose value equals the * specified <code>value</code>. * For example: * <pre>assertThat(myMap, hasEntry("bar", "foo"))</pre> * * @param key * the key that, in combination with the value, must be describe at least one entry * @param value * the value that, in combination with the key, must be describe at least one entry */ public static <K, V> org.hamcrest.Matcher<java.util.Map<? extends K,? extends V>> hasEntry(K key, V value) { return org.hamcrest.collection.IsMapContaining.hasEntry(key, value); }
@Override protected Matcher<?> createMatcher() { return IsMapContaining.hasEntry("irrelevant", "irrelevant"); }
public void testMatchesMapContainingMatchingKeyAndValue() { Map<String,Integer> map = new TreeMap<>(); map.put("a", 1); map.put("b", 2); assertMatches("matcherA", hasEntry(equalTo("a"), equalTo(1)), map); assertMatches("matcherB", hasEntry(equalTo("b"), equalTo(2)), map); assertMismatchDescription("map was [<a=1>, <b=2>]", hasEntry(equalTo("c"), equalTo(3)), map); }
@SuppressWarnings("unchecked") public void testMatchesMapContainingMatchingKeyAndValueWithoutGenerics() { Map map = new HashMap(); map.put("a", 1); map.put("b", 2); assertMatches("matcherA", hasEntry(equalTo("a"), equalTo(1)), map); assertMatches("matcherB", hasEntry(equalTo("b"), equalTo(2)), map); assertFalse("matcherC", hasEntry(equalTo("c"), equalTo(3)).matches(map)); // working around generics problem }
public void testHasReadableDescription() { assertDescription("map containing [\"a\"-><2>]", hasEntry(equalTo("a"), (equalTo(2)))); } }
@Test public void shouldCreateMapFromTaskList() throws Exception { Task t1 = new Task("Write blog on Java 8 Map improvements", TaskType.BLOGGING); Task t2 = new Task("Write factorial program in Java 8", TaskType.CODING); List<Task> tasks = Arrays.asList(t1, t2); Map<String, Task> taskIdToTaskMap = tasks.stream().collect(toMap(Task::getId, identity())); assertThat(taskIdToTaskMap, hasEntry(notNullValue(), equalTo(t1))); assertThat(taskIdToTaskMap, hasEntry(notNullValue(), equalTo(t2))); }
@Test public void shouldCreateLinkedMapFromTaskList() throws Exception { Task t1 = new Task("Write blog on Java 8 Map improvements", TaskType.BLOGGING); Task t2 = new Task("Write factorial program in Java 8", TaskType.CODING); List<Task> tasks = Arrays.asList(t1, t2); Map<String, Task> taskIdToTaskMap = tasks.stream().collect(toMap(Task::getId, identity(), (k1, k2) -> k1, LinkedHashMap::new)); assertThat(taskIdToTaskMap, instanceOf(LinkedHashMap.class)); assertThat(taskIdToTaskMap, hasEntry(notNullValue(), equalTo(t1))); assertThat(taskIdToTaskMap, hasEntry(notNullValue(), equalTo(t2))); }
@Test public void shouldHaveTree() throws Exception { HashMap<String, JsonNode> map = new HashMap<>(); JsonNode node = NullNode.getInstance(); map.put("key", node); BasicHeader header = new BasicHeader(null, null, null, null, map, objectReader); assertThat(header.getTree(), is(notNullValue())); assertThat(header.getTree(), is(IsMapContaining.hasEntry("key", node))); }
@Test public void shouldHandleTaskListWithDuplicates() throws Exception { Task t1 = new Task("1", "Write blog on Java 8 Map improvements", TaskType.BLOGGING); Task t2 = new Task("1", "Write factorial program in Java 8", TaskType.CODING); List<Task> tasks = Arrays.asList(t1, t2); Map<String, Task> taskIdToTaskMap = tasks.stream().collect(toMap(Task::getId, identity(), (k1, k2) -> k2)); assertThat(taskIdToTaskMap, hasEntry(notNullValue(), equalTo(t2))); } }
@Test public void multipleConfigurations() { //tag::multipleManagers[] XmlMultiConfiguration multipleConfiguration = XmlMultiConfiguration .from(getClass().getResource("/configs/docs/multi/multiple-managers.xml")) // <1> .build(); // <2> Configuration fooConfiguration = multipleConfiguration.configuration("foo-manager"); // <3> //end::multipleManagers[] Assert.assertThat(resourceMap(multipleConfiguration.identities().stream().collect( Collectors.toMap(Function.identity(), multipleConfiguration::configuration) )), AllOf.allOf( IsMapContaining.hasEntry(Is.is("foo-manager"), IsMapContaining.hasEntry(Is.is("foo"), IsIterableContainingInAnyOrder.containsInAnyOrder(ResourceType.Core.HEAP, ResourceType.Core.OFFHEAP))), IsMapContaining.hasEntry(Is.is("bar-manager"), IsMapContaining.hasEntry(Is.is("bar"), IsIterableContainingInAnyOrder.containsInAnyOrder(ResourceType.Core.HEAP, ResourceType.Core.OFFHEAP))) )); }
@SuppressWarnings("RedundantCast") @Test public void shouldGetClaims() throws Exception { HashMap<String, Object> claims = new HashMap<>(); claims.put("iss", "auth0"); ClaimsHolder holder = new ClaimsHolder(claims); assertThat(holder, is(notNullValue())); assertThat(holder.getClaims(), is(notNullValue())); assertThat(holder.getClaims(), is(instanceOf(Map.class))); assertThat(holder.getClaims(), is(IsMapContaining.hasEntry("iss", (Object) "auth0"))); }
@Test public void multipleVariants() { //tag::multipleVariants[] XmlMultiConfiguration variantConfiguration = XmlMultiConfiguration .from(getClass().getResource("/configs/docs/multi/multiple-variants.xml")) .build(); Configuration fooConfiguration = variantConfiguration.configuration("foo-manager", "offheap"); // <1> //end::multipleVariants[] Assert.assertThat(resourceMap(variantConfiguration.identities().stream().collect( Collectors.toMap(Function.identity(), i -> variantConfiguration.configuration(i, "offheap")) )), AllOf.allOf( IsMapContaining.hasEntry(Is.is("foo-manager"), IsMapContaining.hasEntry(Is.is("foo"), IsIterableContainingInAnyOrder.containsInAnyOrder(ResourceType.Core.HEAP, ResourceType.Core.OFFHEAP))), IsMapContaining.hasEntry(Is.is("bar-manager"), IsMapContaining.hasEntry(Is.is("bar"), IsIterableContainingInAnyOrder.containsInAnyOrder(ResourceType.Core.HEAP))) )); Assert.assertThat(resourceMap(variantConfiguration.identities().stream().collect( Collectors.toMap(Function.identity(), i -> variantConfiguration.configuration(i, "heap")) )), AllOf.allOf( IsMapContaining.hasEntry(Is.is("foo-manager"), IsMapContaining.hasEntry(Is.is("foo"), IsIterableContainingInAnyOrder.containsInAnyOrder(ResourceType.Core.HEAP))), IsMapContaining.hasEntry(Is.is("bar-manager"), IsMapContaining.hasEntry(Is.is("bar"), IsIterableContainingInAnyOrder.containsInAnyOrder(ResourceType.Core.HEAP))) )); }
@Test public void multipleRetrieval() { XmlMultiConfiguration multipleConfiguration = XmlMultiConfiguration .from(getClass().getResource("/configs/docs/multi/multiple-managers.xml")) .build(); XmlMultiConfiguration variantConfiguration = XmlMultiConfiguration .from(getClass().getResource("/configs/docs/multi/multiple-variants.xml")) .build(); //tag::multipleRetrieval[] Map<String, Configuration> allConfigurations = multipleConfiguration.identities().stream() // <1> .collect(Collectors.toMap(i -> i, i -> multipleConfiguration.configuration(i))); // <2> Map<String, Configuration> offheapConfigurations = variantConfiguration.identities().stream() .collect(Collectors.toMap(i -> i, i -> variantConfiguration.configuration(i, "offheap"))); // <3> //end::multipleRetrieval[] Assert.assertThat(resourceMap(allConfigurations), AllOf.allOf( IsMapContaining.hasEntry(Is.is("foo-manager"), IsMapContaining.hasEntry(Is.is("foo"), IsIterableContainingInAnyOrder.containsInAnyOrder(ResourceType.Core.HEAP, ResourceType.Core.OFFHEAP))), IsMapContaining.hasEntry(Is.is("bar-manager"), IsMapContaining.hasEntry(Is.is("bar"), IsIterableContainingInAnyOrder.containsInAnyOrder(ResourceType.Core.HEAP, ResourceType.Core.OFFHEAP))) )); Assert.assertThat(resourceMap(offheapConfigurations), AllOf.allOf( IsMapContaining.hasEntry(Is.is("foo-manager"), IsMapContaining.hasEntry(Is.is("foo"), IsIterableContainingInAnyOrder.containsInAnyOrder(ResourceType.Core.HEAP, ResourceType.Core.OFFHEAP))), IsMapContaining.hasEntry(Is.is("bar-manager"), IsMapContaining.hasEntry(Is.is("bar"), IsIterableContainingInAnyOrder.containsInAnyOrder(ResourceType.Core.HEAP))) )); }
public void testDoesNotMatchNull() { assertMismatchDescription("was null", hasEntry(anything(), anything()), null); }
@Test public void shouldGetMapValue() throws Exception { Map<String, Object> map = new HashMap<>(); map.put("text", "extraValue"); map.put("number", 12); map.put("boolean", true); map.put("object", Collections.singletonMap("something", "else")); JsonNode value = mapper.valueToTree(map); Claim claim = claimFromNode(value); assertThat(claim, is(notNullValue())); Map<String, Object> backMap = claim.asMap(); assertThat(backMap, is(notNullValue())); assertThat(backMap, hasEntry("text", (Object) "extraValue")); assertThat(backMap, hasEntry("number", (Object) 12)); assertThat(backMap, hasEntry("boolean", (Object) true)); assertThat(backMap, hasKey("object")); assertThat((Map<String, Object>) backMap.get("object"), IsMapContaining.hasEntry("something", (Object) "else")); }