net.amygdalum.extensions.hamcrest.objects.ReflectiveEqualsMatcher Maven / Gradle / Ivy
package net.amygdalum.extensions.hamcrest.objects;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
public class ReflectiveEqualsMatcher extends TypeSafeMatcher {
private T object;
private Set excluded;
private Set> customBaseTypes;
public ReflectiveEqualsMatcher(T object) {
this.object = object;
this.excluded = new HashSet<>();
this.customBaseTypes = new HashSet<>();
}
public ReflectiveEqualsMatcher excluding(String... excludedFields) {
excluded.addAll(asList(excludedFields));
return this;
}
public ReflectiveEqualsMatcher withBaseTypes(Class>... classes) {
customBaseTypes.addAll(asList(classes));
return this;
}
@Override
public void describeTo(Description description) {
description.appendText("should reflectively equal the given object:\n" + describe(object, new HashSet<>(), 0));
}
@Override
protected void describeMismatchSafely(T item, Description mismatchDescription) {
mismatchDescription.appendText(", but was:\n" + describe(item, new HashSet<>(), 0));
}
private String describe(Object item, Set