com.github.opennano.reflectionassert.comparers.MapComparer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reflection-assert Show documentation
Show all versions of reflection-assert Show documentation
performs deep comparisons of objects via field reflection
package com.github.opennano.reflectionassert.comparers;
import static com.github.opennano.reflectionassert.diffs.NullDiff.NULL_TOKEN;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.github.opennano.reflectionassert.diffs.Diff;
import com.github.opennano.reflectionassert.diffs.MissingValueDiff;
import com.github.opennano.reflectionassert.diffs.PartialDiff;
import com.github.opennano.reflectionassert.diffs.UnexpectedValueDiff;
import com.github.opennano.reflectionassert.worker.ComparerManager;
import com.github.opennano.reflectionassert.worker.ValueComparer;
/**
* A comparer for maps. This will compare all values with corresponding keys, where the equality of
* keys is the usual map sematics (i.e. equals and hashcode).
*/
public class MapComparer extends ValueComparer {
private static final String PROPERTY_PATH_TEMPLATE = "%s{%s}";
/** returns true when both objects are maps. */
@Override
public boolean canCompare(Object expected, Object actual) {
return areBothOneOfTheseTypes(expected, actual, Map.class);
}
/**
* Compares the given maps by looping over the keys and comparing their values. The keys are
* compared using standard map semantics (equals and hashcode), while values are compared with
* reflection.
*
* @param path the path so far (from root down to the objects being compared)
* @param expected the expected object
* @param actual the actual object
* @param comparer used when recursion is necessary on child objects
* @param fullDiff when false comparison should end at the first found difference, in which case a
* {@link PartialDiff#PARTIAL_DIFF_TOKEN} should be returned.
*/
@Override
public Diff compare(
String path, Object expected, Object actual, ComparerManager comparer, boolean fullDiff) {
// create copies so we can remove elements.
Map