/** * Creates a matcher that matches when the examined object has values for all of * its JavaBean properties that are equal to the corresponding values of the * specified bean. * <p/> * For example: * <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean))</pre> * * @param expectedBean * the bean against which examined beans are compared */ public static <T> org.hamcrest.Matcher<T> samePropertyValuesAs(T expectedBean) { return org.hamcrest.beans.SamePropertyValuesAs.<T>samePropertyValuesAs(expectedBean); }
/** * Creates a matcher that matches when the examined object has values for all of * its JavaBean properties that are equal to the corresponding values of the * specified bean. * <p/> * For example: * <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean))</pre> * * @param expectedBean * the bean against which examined beans are compared */ @Factory public static <T> Matcher<T> samePropertyValuesAs(T expectedBean) { return new SamePropertyValuesAs<T>(expectedBean); }
@Override protected boolean matches(Object actual, Description mismatch) { return isNotNull(actual, mismatch) && isCompatibleType(actual, mismatch) && hasNoExtraProperties(actual, mismatch) && hasMatchingValues(actual, mismatch); }
@Override public boolean matchesSafely(T bean, Description mismatch) { return isCompatibleType(bean, mismatch) && hasNoExtraProperties(bean, mismatch) && hasMatchingValues(bean, mismatch); }
public SamePropertyValuesAs(T expectedBean) { PropertyDescriptor[] descriptors = propertyDescriptorsFor(expectedBean, Object.class); this.expectedBean = expectedBean; this.propertyNames = propertyNamesFrom(descriptors); this.propertyMatchers = propertyMatchersFor(expectedBean, descriptors); }
private boolean hasNoExtraProperties(T item, Description mismatchDescription) { Set<String> actualPropertyNames = propertyNamesFrom(propertyDescriptorsFor(item, Object.class)); actualPropertyNames.removeAll(propertyNames); if (!actualPropertyNames.isEmpty()) { mismatchDescription.appendText("has extra properties called " + actualPropertyNames); return false; } return true; }
private static <T> List<PropertyMatcher> propertyMatchersFor(T bean, PropertyDescriptor[] descriptors, List<String> ignoredFields) { List<PropertyMatcher> result = new ArrayList<>(descriptors.length); for (PropertyDescriptor propertyDescriptor : descriptors) { if (isIgnored(ignoredFields, propertyDescriptor)) { result.add(new PropertyMatcher(propertyDescriptor, bean)); } } return result; }
@Override public boolean matchesSafely(T bean, Description mismatch) { return isCompatibleType(bean, mismatch) && hasNoExtraProperties(bean, mismatch) && hasMatchingValues(bean, mismatch); }
@SuppressWarnings("WeakerAccess") public SamePropertyValuesAs(T expectedBean, List<String> ignoredProperties) { PropertyDescriptor[] descriptors = propertyDescriptorsFor(expectedBean, Object.class); this.expectedBean = expectedBean; this.ignoredFields = ignoredProperties; this.propertyNames = propertyNamesFrom(descriptors, ignoredProperties); this.propertyMatchers = propertyMatchersFor(expectedBean, descriptors, ignoredProperties); }
private boolean hasNoExtraProperties(Object actual, Description mismatchDescription) { Set<String> actualPropertyNames = propertyNamesFrom(propertyDescriptorsFor(actual, Object.class), ignoredFields); actualPropertyNames.removeAll(propertyNames); if (!actualPropertyNames.isEmpty()) { mismatchDescription.appendText("has extra properties called " + actualPropertyNames); return false; } return true; }
private static Set<String> propertyNamesFrom(PropertyDescriptor[] descriptors, List<String> ignoredFields) { HashSet<String> result = new HashSet<>(); for (PropertyDescriptor propertyDescriptor : descriptors) { if (isIgnored(ignoredFields, propertyDescriptor)) { result.add(propertyDescriptor.getDisplayName()); } } return result; }
/** * Creates a matcher that matches when the examined object has values for all of * its JavaBean properties that are equal to the corresponding values of the * specified bean. If any properties are marked as ignored, they will be dropped from * both the expected and actual bean. Note that the ignored properties use JavaBean * display names, for example <pre>age</pre> rather than method names such as <pre>getAge</pre>. * For example: * <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean))</pre> * <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean), "age", "height")</pre> * * @param expectedBean * the bean against which examined beans are compared * @param ignoredProperties * do not check any of these named properties. */ public static <B> Matcher<B> samePropertyValuesAs(B expectedBean, String... ignoredProperties) { return org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs(expectedBean, ignoredProperties); }
@Override protected boolean matches(Object actual, Description mismatch) { return isNotNull(actual, mismatch) && isCompatibleType(actual, mismatch) && hasNoExtraProperties(actual, mismatch) && hasMatchingValues(actual, mismatch); }
public SamePropertyValuesAs(T expectedBean) { PropertyDescriptor[] descriptors = propertyDescriptorsFor(expectedBean, Object.class); this.expectedBean = expectedBean; this.propertyNames = propertyNamesFrom(descriptors); this.propertyMatchers = propertyMatchersFor(expectedBean, descriptors); }
private boolean hasNoExtraProperties(T item, Description mismatchDescription) { Set<String> actualPropertyNames = propertyNamesFrom(propertyDescriptorsFor(item, Object.class)); actualPropertyNames.removeAll(propertyNames); if (!actualPropertyNames.isEmpty()) { mismatchDescription.appendText("has extra properties called " + actualPropertyNames); return false; } return true; }
/** * Creates a matcher that matches when the examined object has values for all of * its JavaBean properties that are equal to the corresponding values of the * specified bean. If any properties are marked as ignored, they will be dropped from * both the expected and actual bean. Note that the ignored properties use JavaBean * display names, for example <pre>age</pre> rather than method names such as <pre>getAge</pre>. * For example: * <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean))</pre> * <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean), "age", "height")</pre> * * @param expectedBean * the bean against which examined beans are compared * @param ignoredProperties * do not check any of these named properties. */ public static <B> Matcher<B> samePropertyValuesAs(B expectedBean, String... ignoredProperties) { return new SamePropertyValuesAs<>(expectedBean, asList(ignoredProperties)); }
private static Set<String> propertyNamesFrom(PropertyDescriptor[] descriptors, List<String> ignoredFields) { HashSet<String> result = new HashSet<>(); for (PropertyDescriptor propertyDescriptor : descriptors) { if (isIgnored(ignoredFields, propertyDescriptor)) { result.add(propertyDescriptor.getDisplayName()); } } return result; }
@Override protected Matcher<?> createMatcher() { return samePropertyValuesAs(expectedBean); }
@SuppressWarnings("WeakerAccess") public SamePropertyValuesAs(T expectedBean, List<String> ignoredProperties) { PropertyDescriptor[] descriptors = propertyDescriptorsFor(expectedBean, Object.class); this.expectedBean = expectedBean; this.ignoredFields = ignoredProperties; this.propertyNames = propertyNamesFrom(descriptors, ignoredProperties); this.propertyMatchers = propertyMatchersFor(expectedBean, descriptors, ignoredProperties); }
private boolean hasNoExtraProperties(Object actual, Description mismatchDescription) { Set<String> actualPropertyNames = propertyNamesFrom(propertyDescriptorsFor(actual, Object.class), ignoredFields); actualPropertyNames.removeAll(propertyNames); if (!actualPropertyNames.isEmpty()) { mismatchDescription.appendText("has extra properties called " + actualPropertyNames); return false; } return true; }