Skip to content

Commit eba1cc0

Browse files
committed
finishing up ignored properties
added description added to Matchers refactoring
1 parent d2eb80d commit eba1cc0

3 files changed

Lines changed: 62 additions & 27 deletions

File tree

‎hamcrest-library/src/main/java/org/hamcrest/Matchers.java‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import java.util.regex.Pattern;
99

10-
@SuppressWarnings("unused")
10+
@SuppressWarnings({"unused", "WeakerAccess"})
1111
public class Matchers {
1212

1313
/**
@@ -167,7 +167,7 @@ public static <LHS> org.hamcrest.core.CombinableMatcher.CombinableEitherMatcher<
167167
* @param matcher
168168
* the matcher to wrap
169169
* @param values
170-
* optional values to insert into the tokenised description
170+
* optional values to insert into the tokenized description
171171
*/
172172
public static <T> org.hamcrest.Matcher<T> describedAs(java.lang.String description, org.hamcrest.Matcher<T> matcher, java.lang.Object... values) {
173173
return org.hamcrest.core.DescribedAs.describedAs(description, matcher, values);
@@ -1605,15 +1605,20 @@ public static <T> org.hamcrest.Matcher<T> hasProperty(java.lang.String propertyN
16051605
/**
16061606
* Creates a matcher that matches when the examined object has values for all of
16071607
* its JavaBean properties that are equal to the corresponding values of the
1608-
* specified bean.
1608+
* specified bean. If any properties are marked as ignored, they will be dropped from
1609+
* both the expected and actual bean. Note that the ignored properties use JavaBean
1610+
* display names, for example <pre>age</pre> rather than method names such as <pre>getAge</pre>.
16091611
* For example:
16101612
* <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean))</pre>
1611-
*
1613+
* <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean), "age", "height")</pre>
1614+
*
16121615
* @param expectedBean
16131616
* the bean against which examined beans are compared
1617+
* @param ignoredProperties
1618+
* do not check any of these named properties.
16141619
*/
1615-
public static <T> org.hamcrest.Matcher<T> samePropertyValuesAs(T expectedBean) {
1616-
return org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs(expectedBean);
1620+
public static <B> Matcher<B> samePropertyValuesAs(B expectedBean, String... ignoredProperties) {
1621+
return org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs(expectedBean, ignoredProperties);
16171622
}
16181623

16191624
/**

‎hamcrest-library/src/main/java/org/hamcrest/beans/SamePropertyValuesAs.java‎

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public class SamePropertyValuesAs<T> extends DiagnosingMatcher<T> {
2020
private final List<String> ignoredFields;
2121

2222
@SuppressWarnings("WeakerAccess")
23-
public SamePropertyValuesAs(T expectedBean, String... ignoredFields) {
23+
public SamePropertyValuesAs(T expectedBean, List<String> ignoredProperties) {
2424
PropertyDescriptor[] descriptors = propertyDescriptorsFor(expectedBean, Object.class);
2525
this.expectedBean = expectedBean;
26-
this.ignoredFields = asList(ignoredFields);
27-
this.propertyNames = propertyNamesFrom(descriptors, this.ignoredFields);
28-
this.propertyMatchers = propertyMatchersFor(expectedBean, descriptors, this.ignoredFields);
26+
this.ignoredFields = ignoredProperties;
27+
this.propertyNames = propertyNamesFrom(descriptors, ignoredProperties);
28+
this.propertyMatchers = propertyMatchersFor(expectedBean, descriptors, ignoredProperties);
2929
}
3030

3131
@Override
@@ -40,6 +40,10 @@ && hasNoExtraProperties(actual, mismatch)
4040
public void describeTo(Description description) {
4141
description.appendText("same property values as " + expectedBean.getClass().getSimpleName())
4242
.appendList(" [", ", ", "]", propertyMatchers);
43+
if (! ignoredFields.isEmpty()) {
44+
description.appendText(" ignoring ")
45+
.appendValueList("[", ", ", "]", ignoredFields);
46+
}
4347
}
4448

4549

@@ -75,7 +79,7 @@ private boolean hasMatchingValues(Object actual, Description mismatchDescription
7579
private static <T> List<PropertyMatcher> propertyMatchersFor(T bean, PropertyDescriptor[] descriptors, List<String> ignoredFields) {
7680
List<PropertyMatcher> result = new ArrayList<>(descriptors.length);
7781
for (PropertyDescriptor propertyDescriptor : descriptors) {
78-
if (! ignoredFields.contains(propertyDescriptor.getDisplayName())) {
82+
if (isIgnored(ignoredFields, propertyDescriptor)) {
7983
result.add(new PropertyMatcher(propertyDescriptor, bean));
8084
}
8185
}
@@ -85,14 +89,18 @@ private static <T> List<PropertyMatcher> propertyMatchersFor(T bean, PropertyDes
8589
private static Set<String> propertyNamesFrom(PropertyDescriptor[] descriptors, List<String> ignoredFields) {
8690
HashSet<String> result = new HashSet<>();
8791
for (PropertyDescriptor propertyDescriptor : descriptors) {
88-
final String displayName = propertyDescriptor.getDisplayName();
89-
if (! ignoredFields.contains(displayName)) {
90-
result.add(displayName);
92+
if (isIgnored(ignoredFields, propertyDescriptor)) {
93+
result.add(propertyDescriptor.getDisplayName());
9194
}
9295
}
9396
return result;
9497
}
9598

99+
private static boolean isIgnored(List<String> ignoredFields, PropertyDescriptor propertyDescriptor) {
100+
return ! ignoredFields.contains(propertyDescriptor.getDisplayName());
101+
}
102+
103+
@SuppressWarnings("WeakerAccess")
96104
private static class PropertyMatcher extends DiagnosingMatcher<Object> {
97105
private final Method readMethod;
98106
private final Matcher<Object> matcher;
@@ -132,18 +140,20 @@ private static Object readProperty(Method method, Object target) {
132140
/**
133141
* Creates a matcher that matches when the examined object has values for all of
134142
* its JavaBean properties that are equal to the corresponding values of the
135-
* specified bean. If any fields are marked as ignored, they will be dropped from
136-
* both the expected and actual bean.
143+
* specified bean. If any properties are marked as ignored, they will be dropped from
144+
* both the expected and actual bean. Note that the ignored properties use JavaBean
145+
* display names, for example <pre>age</pre> rather than method names such as <pre>getAge</pre>.
137146
* For example:
138147
* <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean))</pre>
139-
*
148+
* <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean), "age", "height")</pre>
149+
*
140150
* @param expectedBean
141151
* the bean against which examined beans are compared
142-
* @param ignoredFields
143-
* do not check any of these named fields.
152+
* @param ignoredProperties
153+
* do not check any of these named properties.
144154
*/
145-
public static <B> Matcher<B> samePropertyValuesAs(B expectedBean, String... ignoredFields) {
146-
return new SamePropertyValuesAs<>(expectedBean, ignoredFields);
155+
public static <B> Matcher<B> samePropertyValuesAs(B expectedBean, String... ignoredProperties) {
156+
return new SamePropertyValuesAs<>(expectedBean, asList(ignoredProperties));
147157
}
148158

149159
}

‎hamcrest-library/src/test/java/org/hamcrest/beans/SamePropertyValuesAsTest.java‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,37 @@ public void test_rejects_subtype_that_has_extra_properties() {
4545
samePropertyValuesAs(expectedBean), new SubBeanWithExtraProperty("same", 1, aValue));
4646
}
4747

48-
public void test_ignores_extra_fields() {
48+
public void test_ignores_extra_subtype_properties() {
4949
final SubBeanWithExtraProperty withExtraProperty = new SubBeanWithExtraProperty("same", 1, aValue);
50-
assertMatches("matched properties", samePropertyValuesAs(expectedBean, "extraProperty"), withExtraProperty);
50+
assertMatches("extra property", samePropertyValuesAs(expectedBean, "extraProperty"), withExtraProperty);
5151
}
52-
53-
public void test_ignores_different_fields_in_both_directions() {
52+
53+
public void test_ignores_different_properties() {
5454
final ExampleBean differentBean = new ExampleBean("different", 1, aValue);
55-
assertMatches("matched properties", samePropertyValuesAs(expectedBean, "stringProperty"), differentBean);
55+
assertMatches("different property", samePropertyValuesAs(expectedBean, "stringProperty"), differentBean);
56+
}
57+
58+
public void test_accepts_missing_properties_to_ignore() {
59+
assertMatches("ignored property", samePropertyValuesAs(expectedBean, "notAProperty"), actualBean);
5660
}
5761

62+
public void test_can_ignore_all_properties() {
63+
final ExampleBean differentBean = new ExampleBean("different", 2, new Value("not expected"));
64+
assertMatches(
65+
"different property",
66+
samePropertyValuesAs(expectedBean, "stringProperty", "intProperty", "valueProperty"),
67+
differentBean);
68+
}
69+
70+
5871
public void testDescribesItself() {
59-
assertDescription("same property values as ExampleBean [intProperty: <1>, stringProperty: \"same\", valueProperty: <Value expected>]", samePropertyValuesAs(expectedBean));
72+
assertDescription(
73+
"same property values as ExampleBean [intProperty: <1>, stringProperty: \"same\", valueProperty: <Value expected>]",
74+
samePropertyValuesAs(expectedBean));
75+
76+
assertDescription(
77+
"same property values as ExampleBean [intProperty: <1>, stringProperty: \"same\", valueProperty: <Value expected>] ignoring [\"ignored1\", \"ignored2\"]",
78+
samePropertyValuesAs(expectedBean, "ignored1", "ignored2"));
6079
}
6180

6281
public static class Value {
@@ -71,6 +90,7 @@ public String toString() {
7190
}
7291
}
7392

93+
@SuppressWarnings("unused")
7494
public static class ExampleBean {
7595
private String stringProperty;
7696
private int intProperty;

0 commit comments

Comments
 (0)