All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hamcrest.collection.IsMapContaining Maven / Gradle / Ivy

The newest version!
package org.hamcrest.collection;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import java.util.Map;
import java.util.Map.Entry;

import static org.hamcrest.core.IsAnything.anything;
import static org.hamcrest.core.IsEqual.equalTo;

public class IsMapContaining extends TypeSafeMatcher> {

    private final Matcher keyMatcher;
    private final Matcher valueMatcher;

    public IsMapContaining(Matcher keyMatcher, Matcher valueMatcher) {
        this.keyMatcher = keyMatcher;
        this.valueMatcher = valueMatcher;
    }

    @Override
    public boolean matchesSafely(Map map) {
        for (Entry entry : map.entrySet()) {
            if (keyMatcher.matches(entry.getKey()) && valueMatcher.matches(entry.getValue())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void describeMismatchSafely(Map map, Description mismatchDescription) {
      mismatchDescription.appendText("map was ").appendValueList("[", ", ", "]", map.entrySet());
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("map containing [")
                   .appendDescriptionOf(keyMatcher)
                   .appendText("->")
                   .appendDescriptionOf(valueMatcher)
                   .appendText("]");
    }

    /**
     * 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 keyMatcher and whose
     * value satisfies the specified valueMatcher.
     * For example:
     * 
assertThat(myMap, hasEntry(equalTo("bar"), equalTo("foo")))
* * @param * the map key type. * @param * the map value type. * @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 * @return The matcher. */ public static Matcher> hasEntry(Matcher keyMatcher, Matcher valueMatcher) { return new IsMapContaining<>(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 key and whose value equals the * specified value. * For example: *
assertThat(myMap, hasEntry("bar", "foo"))
* * @param * the map key type. * @param * the map value type. * @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 * @return The matcher. */ public static Matcher> hasEntry(K key, V value) { return new IsMapContaining<>(equalTo(key), equalTo(value)); } /** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one key that satisfies the specified matcher. * For example: *
assertThat(myMap, hasKey(equalTo("bar")))
* * @param * the map key type. * @param keyMatcher * the matcher that must be satisfied by at least one key * @return The matcher. */ public static Matcher> hasKey(Matcher keyMatcher) { return new IsMapContaining<>(keyMatcher, anything()); } /** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one key that is equal to the specified key. * For example: *
assertThat(myMap, hasKey("bar"))
* * @param * the map key type. * @param key * the key that satisfying maps must contain * @return The matcher. */ public static Matcher> hasKey(K key) { return new IsMapContaining<>(equalTo(key), anything()); } /** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one value that satisfies the specified valueMatcher. * For example: *
assertThat(myMap, hasValue(equalTo("foo")))
* * @param * the value type. * @param valueMatcher * the matcher that must be satisfied by at least one value * @return The matcher. */ public static Matcher> hasValue(Matcher valueMatcher) { return new IsMapContaining<>(anything(), valueMatcher); } /** * Creates a matcher for {@link java.util.Map}s matching when the examined {@link java.util.Map} contains * at least one value that is equal to the specified value. * For example: *
assertThat(myMap, hasValue("foo"))
* * @param * the value type. * @param value * the value that satisfying maps must contain * @return The matcher. */ public static Matcher> hasValue(V value) { return new IsMapContaining<>(anything(), equalTo(value)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy