org.hamcrest.beans.SamePropertyValuesAs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hamcrest-library Show documentation
Show all versions of hamcrest-library Show documentation
A library of Hamcrest matchers - deprecated, please use "hamcrest" instead
package org.hamcrest.beans;
import org.hamcrest.*;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS;
import static org.hamcrest.beans.PropertyUtil.propertyDescriptorsFor;
import static org.hamcrest.core.IsEqual.equalTo;
public class SamePropertyValuesAs extends TypeSafeDiagnosingMatcher {
private final T expectedBean;
private final Set propertyNames;
private final List propertyMatchers;
public SamePropertyValuesAs(T expectedBean) {
PropertyDescriptor[] descriptors = propertyDescriptorsFor(expectedBean, Object.class);
this.expectedBean = expectedBean;
this.propertyNames = propertyNamesFrom(descriptors);
this.propertyMatchers = propertyMatchersFor(expectedBean, descriptors);
}
@Override
public boolean matchesSafely(T bean, Description mismatch) {
return isCompatibleType(bean, mismatch)
&& hasNoExtraProperties(bean, mismatch)
&& hasMatchingValues(bean, mismatch);
}
@Override
public void describeTo(Description description) {
description.appendText("same property values as " + expectedBean.getClass().getSimpleName())
.appendList(" [", ", ", "]", propertyMatchers);
}
private boolean isCompatibleType(T item, Description mismatchDescription) {
if (!expectedBean.getClass().isAssignableFrom(item.getClass())) {
mismatchDescription.appendText("is incompatible type: " + item.getClass().getSimpleName());
return false;
}
return true;
}
private boolean hasNoExtraProperties(T item, Description mismatchDescription) {
Set actualPropertyNames = propertyNamesFrom(propertyDescriptorsFor(item, Object.class));
actualPropertyNames.removeAll(propertyNames);
if (!actualPropertyNames.isEmpty()) {
mismatchDescription.appendText("has extra properties called " + actualPropertyNames);
return false;
}
return true;
}
private boolean hasMatchingValues(T item, Description mismatchDescription) {
for (PropertyMatcher propertyMatcher : propertyMatchers) {
if (!propertyMatcher.matches(item)) {
propertyMatcher.describeMismatch(item, mismatchDescription);
return false;
}
}
return true;
}
private static List propertyMatchersFor(T bean, PropertyDescriptor[] descriptors) {
List result = new ArrayList(descriptors.length);
for (PropertyDescriptor propertyDescriptor : descriptors) {
result.add(new PropertyMatcher(propertyDescriptor, bean));
}
return result;
}
private static Set propertyNamesFrom(PropertyDescriptor[] descriptors) {
HashSet result = new HashSet();
for (PropertyDescriptor propertyDescriptor : descriptors) {
result.add(propertyDescriptor.getDisplayName());
}
return result;
}
public static class PropertyMatcher extends DiagnosingMatcher
© 2015 - 2024 Weber Informatics LLC | Privacy Policy