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 java-hamcrest Show documentation
Show all versions of java-hamcrest Show documentation
Hamcrest matcher library for Java
The newest version!
package org.hamcrest.beans;
import org.hamcrest.Description;
import org.hamcrest.DiagnosingMatcher;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
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