Skip to content

Commit a2de299

Browse files
author
smgfreeman
committed
Reverted exposing of classes in CoreMatchers (shouldn't have done it).
Added HasSamePropertyValuesAs matcher
1 parent 8c92825 commit a2de299

7 files changed

Lines changed: 264 additions & 30 deletions

File tree

‎hamcrest-core/src/main/java/org/hamcrest/core/AllOf.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,23 @@ public void describeTo(Description description) {
4141
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
4242
*/
4343
@Factory
44-
public static <T> AllOf<T> allOf(Iterable<Matcher<? super T>> matchers) {
44+
public static <T> Matcher<T> allOf(Iterable<Matcher<? super T>> matchers) {
4545
return new AllOf<T>(matchers);
4646
}
4747

4848
/**
4949
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
5050
*/
5151
@Factory
52-
public static <T> AllOf<T> allOf(Matcher<? super T>... matchers) {
52+
public static <T> Matcher<T> allOf(Matcher<? super T>... matchers) {
5353
return allOf(Arrays.asList(matchers));
5454
}
5555

5656
/**
5757
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
5858
*/
5959
@Factory
60-
public static <T> AllOf<T> allOf(Matcher<T> first, Matcher<? super T> second) {
60+
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second) {
6161
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(2);
6262
matchers.add(first);
6363
matchers.add(second);
@@ -68,7 +68,7 @@ public static <T> AllOf<T> allOf(Matcher<T> first, Matcher<? super T> second) {
6868
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
6969
*/
7070
@Factory
71-
public static <T> AllOf<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third) {
71+
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third) {
7272
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(3);
7373
matchers.add(first);
7474
matchers.add(second);
@@ -80,7 +80,7 @@ public static <T> AllOf<T> allOf(Matcher<T> first, Matcher<? super T> second, Ma
8080
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
8181
*/
8282
@Factory
83-
public static <T> AllOf<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) {
83+
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) {
8484
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(4);
8585
matchers.add(first);
8686
matchers.add(second);
@@ -93,7 +93,7 @@ public static <T> AllOf<T> allOf(Matcher<T> first, Matcher<? super T> second, Ma
9393
* Evaluates to true only if ALL of the passed in matchers evaluate to true.
9494
*/
9595
@Factory
96-
public static <T> AllOf<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth) {
96+
public static <T> Matcher<T> allOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth) {
9797
List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>(5);
9898
matchers.add(first);
9999
matchers.add(second);

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
*/
33
package org.hamcrest.beans;
44

5-
import java.beans.IntrospectionException;
6-
75
import org.hamcrest.Description;
86
import org.hamcrest.Factory;
7+
import org.hamcrest.Matcher;
98
import org.hamcrest.TypeSafeMatcher;
109

1110
/**
@@ -30,7 +29,7 @@ public boolean matchesSafely(T obj) {
3029
// TODO(ngd): this is not type safe.
3130
try {
3231
return PropertyUtil.getPropertyDescriptor(propertyName, obj) != null;
33-
} catch (IntrospectionException e) {
32+
} catch (IllegalArgumentException e) {
3433
// introspection failure is treated as a matcher failure
3534
return false;
3635
}
@@ -41,7 +40,7 @@ public void describeTo(Description description) {
4140
}
4241

4342
@Factory
44-
public static <T> HasProperty<T> hasProperty(String propertyName) {
43+
public static <T> Matcher<T> hasProperty(String propertyName) {
4544
return new HasProperty<T>(propertyName);
4645
}
4746

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
*/
33
package org.hamcrest.beans;
44

5-
import java.beans.IntrospectionException;
5+
import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS;
6+
67
import java.beans.PropertyDescriptor;
78
import java.lang.reflect.InvocationTargetException;
89
import java.lang.reflect.Method;
@@ -66,9 +67,6 @@
6667
* @author Steve Freeman
6768
*/
6869
public class HasPropertyWithValue<T> extends TypeSafeDiagnosingMatcher<T> {
69-
70-
private static final Object[] NO_ARGUMENTS = new Object[0];
71-
7270
private final String propertyName;
7371
private final Matcher<?> value;
7472

@@ -92,8 +90,6 @@ public boolean matchesSafely(T argument, Description mismatchDescription) {
9290
mismatchDescription.appendText(".");
9391
}
9492
return valueMatches;
95-
} catch (IntrospectionException e) {
96-
return false;
9793
} catch (IllegalArgumentException e) {
9894
return false;
9995
} catch (IllegalAccessException e) {
@@ -103,7 +99,7 @@ public boolean matchesSafely(T argument, Description mismatchDescription) {
10399
}
104100
}
105101

106-
private Method getReadMethod(Object argument) throws IntrospectionException {
102+
private Method getReadMethod(Object argument) throws IllegalArgumentException {
107103
PropertyDescriptor property = PropertyUtil.getPropertyDescriptor(propertyName, argument);
108104
return property == null ? null : property.getReadMethod();
109105
}
@@ -117,7 +113,7 @@ public void describeTo(Description description) {
117113
}
118114

119115
@Factory
120-
public static <T> HasPropertyWithValue<T> hasProperty(String propertyName, Matcher<?> value) {
116+
public static <T> Matcher<T> hasProperty(String propertyName, Matcher<?> value) {
121117
return new HasPropertyWithValue<T>(propertyName, value);
122118
}
123119
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package org.hamcrest.beans;
2+
3+
import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS;
4+
import static org.hamcrest.beans.PropertyUtil.propertyDescriptorsFor;
5+
6+
import java.beans.PropertyDescriptor;
7+
import java.lang.reflect.Method;
8+
import java.util.ArrayList;
9+
import java.util.HashSet;
10+
import java.util.List;
11+
import java.util.Set;
12+
13+
import org.hamcrest.Description;
14+
import org.hamcrest.DiagnosingMatcher;
15+
import org.hamcrest.Factory;
16+
import org.hamcrest.Matcher;
17+
import org.hamcrest.TypeSafeDiagnosingMatcher;
18+
import org.hamcrest.core.IsEqual;
19+
20+
public class HasSamePropertyValuesAs<T> extends TypeSafeDiagnosingMatcher<T> {
21+
private final T expectedBean;
22+
private final Set<String> propertyNames;
23+
private final List<PropertyMatcher> propertyMatchers;
24+
25+
26+
public HasSamePropertyValuesAs(T expectedBean) {
27+
PropertyDescriptor[] descriptors = propertyDescriptorsFor(expectedBean, Object.class);
28+
this.expectedBean = expectedBean;
29+
this.propertyNames = propertyNamesFrom(descriptors);
30+
this.propertyMatchers = propertyMatchersFor(expectedBean, descriptors);
31+
}
32+
33+
@Override
34+
public boolean matchesSafely(T item, Description mismatchDescription) {
35+
return isCompatibleType(item, mismatchDescription)
36+
&& hasNoExtraProperties(item, mismatchDescription)
37+
&& hasMatchingValues(item, mismatchDescription);
38+
}
39+
40+
private boolean isCompatibleType(T item, Description mismatchDescription) {
41+
if (! expectedBean.getClass().isAssignableFrom(item.getClass())) {
42+
mismatchDescription.appendText("is incompatible type: " + item.getClass().getSimpleName());
43+
return false;
44+
}
45+
return true;
46+
}
47+
48+
private boolean hasNoExtraProperties(T item, Description mismatchDescription) {
49+
Set<String> actualPropertyNames = propertyNamesFrom(propertyDescriptorsFor(item, Object.class));
50+
actualPropertyNames.removeAll(propertyNames);
51+
if (! actualPropertyNames.isEmpty()) {
52+
mismatchDescription.appendText("has extra properties called " + actualPropertyNames);
53+
return false;
54+
}
55+
return true;
56+
}
57+
58+
private boolean hasMatchingValues(T item, Description mismatchDescription) {
59+
for (PropertyMatcher propertyMatcher : propertyMatchers) {
60+
if (! propertyMatcher.matches(item)) {
61+
propertyMatcher.describeMismatch(item, mismatchDescription);
62+
return false;
63+
}
64+
}
65+
return true;
66+
}
67+
68+
public void describeTo(Description description) {
69+
description.appendList("has same property values: [", ", ", "]", propertyMatchers);
70+
}
71+
72+
private static <T> List<PropertyMatcher> propertyMatchersFor(T bean, PropertyDescriptor[] descriptors) {
73+
List<PropertyMatcher> result = new ArrayList<PropertyMatcher>(descriptors.length);
74+
for (PropertyDescriptor propertyDescriptor : descriptors) {
75+
result.add(new PropertyMatcher(propertyDescriptor, bean));
76+
}
77+
return result;
78+
}
79+
80+
private static Set<String> propertyNamesFrom(PropertyDescriptor[] descriptors) {
81+
HashSet<String> result = new HashSet<String>();
82+
for (PropertyDescriptor propertyDescriptor : descriptors) {
83+
result.add(propertyDescriptor.getDisplayName());
84+
}
85+
return result;
86+
}
87+
88+
public static class PropertyMatcher extends DiagnosingMatcher<Object> {
89+
private final Method readMethod;
90+
private final Matcher<Object> matcher;
91+
private final String propertyName;
92+
93+
public PropertyMatcher(PropertyDescriptor descriptor, Object expectedObject) {
94+
this.propertyName = descriptor.getDisplayName();
95+
this.readMethod = descriptor.getReadMethod();
96+
this.matcher = new IsEqual<Object>(readProperty(readMethod, expectedObject));
97+
}
98+
@Override
99+
public boolean matches(Object actual, Description mismatchDescription) {
100+
Object actualValue = readProperty(readMethod, actual);
101+
if (! matcher.matches(actualValue)) {
102+
mismatchDescription.appendText(propertyName + " ");
103+
matcher.describeMismatch(actualValue, mismatchDescription);
104+
return false;
105+
}
106+
return true;
107+
}
108+
109+
public void describeTo(Description description) {
110+
description.appendText(propertyName + ": ").appendDescriptionOf(matcher);
111+
}
112+
}
113+
114+
private static Object readProperty(Method method, Object target) {
115+
try {
116+
return method.invoke(target, NO_ARGUMENTS);
117+
} catch (Exception e) {
118+
throw new IllegalArgumentException("Could not invoke " + method + " on " + target, e);
119+
}
120+
}
121+
122+
@Factory
123+
public static <T> Matcher<T> hasSamePropertyValuesAs(T expectedBean) {
124+
return new HasSamePropertyValuesAs<T>(expectedBean);
125+
}
126+
127+
}
128+

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
*/
33
package org.hamcrest.beans;
44

5-
import java.beans.BeanInfo;
65
import java.beans.IntrospectionException;
76
import java.beans.Introspector;
87
import java.beans.PropertyDescriptor;
@@ -14,29 +13,42 @@
1413
* more information on JavaBeans.
1514
*
1615
* @author Iain McGinniss
16+
* @author Steve Freeman
1717
* @since 1.1.0
1818
*/
1919
public class PropertyUtil {
2020
/**
2121
* Returns the description of the property with the provided
2222
* name on the provided object's interface.
2323
*
24-
* @return
25-
* the description of the property, or null if the property
26-
* does not exist.
27-
* @throws IntrospectionException
28-
* if an error occured using the JavaBean Introspector class
29-
* to query the properties of the provided class.
24+
* @return the descriptor of the property, or null if the property does not exist.
25+
* @throws IllegalArgumentException if there's a introspection failure
3026
*/
31-
public static PropertyDescriptor getPropertyDescriptor(String propertyName, Object fromObj) throws IntrospectionException {
32-
BeanInfo beanInfo = Introspector.getBeanInfo(fromObj.getClass());
33-
34-
for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
27+
public static PropertyDescriptor getPropertyDescriptor(String propertyName, Object fromObj) throws IllegalArgumentException {
28+
for (PropertyDescriptor property : propertyDescriptorsFor(fromObj, null)) {
3529
if (property.getName().equals(propertyName)) {
3630
return property;
3731
}
3832
}
3933

4034
return null;
4135
}
36+
37+
/**
38+
* Returns all the property descriptors for the class associated with the given object
39+
*
40+
* @param fromObj Use the class of this object
41+
* @param stopClass TODO
42+
* @return Property descriptors
43+
* @throws IllegalArgumentException if there's a introspection failure
44+
*/
45+
public static PropertyDescriptor[] propertyDescriptorsFor(Object fromObj, Class<Object> stopClass) throws IllegalArgumentException {
46+
try {
47+
return Introspector.getBeanInfo(fromObj.getClass(), stopClass).getPropertyDescriptors();
48+
} catch (IntrospectionException e) {
49+
throw new IllegalArgumentException("Could not get property descriptors for " + fromObj.getClass(), e);
50+
}
51+
}
52+
53+
public static final Object[] NO_ARGUMENTS = new Object[0];
4254
}

‎hamcrest-unit-test/src/main/java/org/hamcrest/beans/HasPropertyWithValueTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void testDescribeTo() {
6363
}
6464

6565
public void testDoesNotWriteMismatchIfPropertyMatches() {
66-
HasPropertyWithValue<Object> matcher = hasProperty( "property", anything());
66+
Matcher<Object> matcher = hasProperty( "property", anything());
6767
assertTrue("Precondtion: Matcher should match item.", matcher.matches(beanWithInfo));
6868
Description description = new StringDescription();
6969
matcher.describeMismatch(beanWithInfo, description);

0 commit comments

Comments
 (0)