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

it.ozimov.cirneco.hamcrest.map.IsMapWithSameKeySet Maven / Gradle / Ivy

package it.ozimov.cirneco.hamcrest.map;

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

import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * Does the map has the same key set of another? {@linkplain com.google.common.base.Equivalence} can be used in the
 * key set comparison.
 *
 * @since version 0.1 for JDK7
 */
public class IsMapWithSameKeySet extends TypeSafeMatcher> {

    private final Map comparisonMap;

    /**
     * Creates an instance of the class using the default equality comparison for the given keys.
     */
    public IsMapWithSameKeySet(final Map comparisonMap) {
        this.comparisonMap = comparisonMap;
    }

    /**
     * Creates a matcher for {@link Map}s matching when the examined {@link Map} has exactly
     * the same key set of the given map.
     * For example:
     * 
assertThat(myMap, hasSameKeySet(anotherMap))
*/ public static Matcher> hasSameKeySet(final Map comparisonMap) { return new IsMapWithSameKeySet<>(comparisonMap); } @Override public boolean matchesSafely(final Map map) { final Set mapKeySet = map.keySet(); final Set comparisonMapKeySet = new TreeSet<>(comparisonMap.keySet()); return mapKeySet.equals(comparisonMapKeySet); } @Override public void describeMismatchSafely(final Map map, final Description mismatchDescription) { mismatchDescription.appendText("map key set was ") .appendValueList("[", ", ", "]", map.entrySet()) .appendText(" while expected key set was ") .appendValueList("[", ", ", "]", comparisonMap.entrySet()); } @Override public void describeTo(final Description description) { description.appendText("map containing same key set as ") .appendValueList("[", ", ", "]", comparisonMap.entrySet()); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy