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 boolean matchesSafely(T obj) { try { return PropertyUtil.getPropertyDescriptor(propertyName, obj) != null; } catch (IllegalArgumentException e) { return false; } }
@Override protected boolean matches(Object actual, Description mismatch) { return isNotNull(actual, mismatch) && isCompatibleType(actual, mismatch) && hasNoExtraProperties(actual, mismatch) && hasMatchingValues(actual, mismatch); }
public void testExceptionsInBeanMethodsShouldBeReportedCorrectly() { assertMismatchDescription( "Calling 'public java.lang.String org.hamcrest.beans.HasPropertyWithValueTest$BeanWithBug.getBroken()': \"bean failed\"", hasProperty("broken", anything()), new BeanWithBug()); }
public void test_can_ignore_all_properties() { final ExampleBean differentBean = new ExampleBean("different", 2, new Value("not expected")); assertMatches( "different property", samePropertyValuesAs(expectedBean, "stringProperty", "intProperty", "valueProperty"), differentBean); }
public void test_reports_mismatch_on_first_property_difference() { assertMismatchDescription("stringProperty was \"different\"", samePropertyValuesAs(expectedBean), new ExampleBean("different", 1, aValue)); assertMismatchDescription("intProperty was <2>", samePropertyValuesAs(expectedBean), new ExampleBean("same", 2, aValue)); assertMismatchDescription("valueProperty was <Value other>", samePropertyValuesAs(expectedBean), new ExampleBean("same", 1, new Value("other"))); }
private boolean hasMatchingValues(T item, Description mismatchDescription) { for (PropertyMatcher propertyMatcher : propertyMatchers) { if (!propertyMatcher.matches(item)) { propertyMatcher.describeMismatch(item, mismatchDescription); return false; } } return true; }
public void test_ignores_extra_subtype_properties() { final SubBeanWithExtraProperty withExtraProperty = new SubBeanWithExtraProperty("same", 1, aValue); assertMatches("extra property", samePropertyValuesAs(expectedBean, "extraProperty"), withExtraProperty); }
public void test_ignores_different_properties() { final ExampleBean differentBean = new ExampleBean("different", 1, aValue); assertMatches("different property", samePropertyValuesAs(expectedBean, "stringProperty"), differentBean); }
public void test_matches_beans_with_inheritance_but_no_extra_properties() { assertMatches("sub type with same properties", samePropertyValuesAs(expectedBean), new SubBeanWithNoExtraProperties("same", 1, aValue)); }
public void test_rejects_subtype_that_has_extra_properties() { assertMismatchDescription("has extra properties called [extraProperty]", samePropertyValuesAs(expectedBean), new SubBeanWithExtraProperty("same", 1, aValue)); }
public void testDescribesItself() { assertDescription( "same property values as ExampleBean [intProperty: <1>, stringProperty: \"same\", valueProperty: <Value expected>]", samePropertyValuesAs(expectedBean)); assertDescription( "same property values as ExampleBean [intProperty: <1>, stringProperty: \"same\", valueProperty: <Value expected>] ignoring [\"ignored1\", \"ignored2\"]", samePropertyValuesAs(expectedBean, "ignored1", "ignored2")); }
/** * 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); }
public void test_reports_mismatch_when_actual_type_is_not_assignable_to_expected_type() { assertMismatchDescription("is incompatible type: ExampleBean", samePropertyValuesAs((Object)aValue), actualBean); }
public void test_accepts_missing_properties_to_ignore() { assertMatches("ignored property", samePropertyValuesAs(expectedBean, "notAProperty"), actualBean); }
/** * Creates a matcher that matches when the examined object has a JavaBean property * with the specified name. * <p/> * For example: * <pre>assertThat(myBean, hasProperty("foo"))</pre> * * @param propertyName * the name of the JavaBean property that examined beans should possess */ public static <T> org.hamcrest.Matcher<T> hasProperty(java.lang.String propertyName) { return org.hamcrest.beans.HasProperty.<T>hasProperty(propertyName); }
public HasPropertyWithValue(String propertyName, Matcher<?> valueMatcher) { this.propertyName = propertyName; this.valueMatcher = nastyGenericsWorkaround(valueMatcher); }
@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 hasMatchingValues(Object actual, Description mismatchDescription) { for (PropertyMatcher propertyMatcher : propertyMatchers) { if (!propertyMatcher.matches(actual)) { propertyMatcher.describeMismatch(actual, mismatchDescription); return false; } } return true; }