|
2 | 2 | */ |
3 | 3 | package org.hamcrest.beans; |
4 | 4 |
|
5 | | -import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS; |
| 5 | +import org.hamcrest.*; |
6 | 6 |
|
7 | 7 | import java.beans.PropertyDescriptor; |
8 | | -import java.lang.reflect.InvocationTargetException; |
9 | 8 | import java.lang.reflect.Method; |
10 | 9 |
|
11 | | -import org.hamcrest.Description; |
12 | | -import org.hamcrest.Factory; |
13 | | -import org.hamcrest.Matcher; |
14 | | -import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 10 | +import static org.hamcrest.Condition.matched; |
| 11 | +import static org.hamcrest.Condition.notMatched; |
| 12 | +import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS; |
15 | 13 |
|
16 | 14 | /** |
17 | 15 | * Matcher that asserts that a JavaBean property on an argument passed to the |
|
67 | 65 | * @author Steve Freeman |
68 | 66 | */ |
69 | 67 | public class HasPropertyWithValue<T> extends TypeSafeDiagnosingMatcher<T> { |
| 68 | + private static final Condition.Step<PropertyDescriptor,Method> WITH_READ_METHOD = withReadMethod(); |
70 | 69 | private final String propertyName; |
71 | | - private final Matcher<?> valueMatcher; |
| 70 | + private final Matcher<Object> valueMatcher; |
72 | 71 |
|
73 | 72 | public HasPropertyWithValue(String propertyName, Matcher<?> valueMatcher) { |
74 | 73 | this.propertyName = propertyName; |
75 | | - this.valueMatcher = valueMatcher; |
| 74 | + this.valueMatcher = nastyGenericsWorkaround(valueMatcher); |
76 | 75 | } |
77 | 76 |
|
78 | 77 | @Override |
79 | | - public boolean matchesSafely(T bean, Description mismatchDescription) { |
80 | | - try { |
81 | | - Method readMethod = findReadMethod(bean, mismatchDescription); |
82 | | - if (readMethod == null) { |
83 | | - return false; |
84 | | - } |
85 | | - Object propertyValue = readMethod.invoke(bean, NO_ARGUMENTS); |
86 | | - boolean valueMatches = valueMatcher.matches(propertyValue); |
87 | | - if (!valueMatches) { |
88 | | - mismatchDescription.appendText("property \"" + propertyName + "\" "); |
89 | | - valueMatcher.describeMismatch(propertyValue, mismatchDescription); |
90 | | - } |
91 | | - return valueMatches; |
92 | | - } catch (IllegalArgumentException e) { |
93 | | - return false; |
94 | | - } catch (IllegalAccessException e) { |
95 | | - return false; |
96 | | - } catch (InvocationTargetException e) { |
97 | | - return false; |
98 | | - } |
| 78 | + public boolean matchesSafely(T bean, Description mismatch) { |
| 79 | + return propertyOn(bean, mismatch) |
| 80 | + .and(WITH_READ_METHOD) |
| 81 | + .and(withPropertyValue(bean)) |
| 82 | + .matching(valueMatcher, "property '" + propertyName + "' "); |
99 | 83 | } |
100 | 84 |
|
101 | | - private Method findReadMethod(Object argument, Description mismatchDescription) throws IllegalArgumentException { |
102 | | - PropertyDescriptor property = PropertyUtil.getPropertyDescriptor(propertyName, argument); |
103 | | - if (null == property) { |
104 | | - mismatchDescription.appendText("No property \"" + propertyName + "\""); |
105 | | - return null; |
106 | | - } |
107 | | - Method readMethod = property.getReadMethod(); |
108 | | - if (null == readMethod) { |
109 | | - mismatchDescription.appendText("property \"" + propertyName + "\" is not readable"); |
| 85 | + @Override |
| 86 | + public void describeTo(Description description) { |
| 87 | + description.appendText("hasProperty(").appendValue(propertyName).appendText(", ") |
| 88 | + .appendDescriptionOf(valueMatcher).appendText(")"); |
| 89 | + } |
| 90 | + |
| 91 | + private Condition<PropertyDescriptor> propertyOn(T bean, Description mismatch) { |
| 92 | + PropertyDescriptor property = PropertyUtil.getPropertyDescriptor(propertyName, bean); |
| 93 | + if (property == null) { |
| 94 | + mismatch.appendText("No property \"" + propertyName + "\""); |
| 95 | + return notMatched(); |
110 | 96 | } |
111 | | - return readMethod; |
| 97 | + |
| 98 | + return matched(property, mismatch); |
112 | 99 | } |
113 | 100 |
|
114 | | - @Override |
115 | | - public void describeTo(Description description) { |
116 | | - description.appendText("hasProperty("); |
117 | | - description.appendValue(propertyName); |
118 | | - description.appendText(", "); |
119 | | - description.appendDescriptionOf(valueMatcher); |
120 | | - description.appendText(")"); |
| 101 | + private Condition.Step<Method, Object> withPropertyValue(final T bean) { |
| 102 | + return new Condition.Step<Method, Object>() { |
| 103 | + @Override |
| 104 | + public Condition<Object> apply(Method readMethod, Description mismatch) { |
| 105 | + try { |
| 106 | + return matched(readMethod.invoke(bean, NO_ARGUMENTS), mismatch); |
| 107 | + } catch (Exception e) { |
| 108 | + mismatch.appendText(e.getMessage()); |
| 109 | + return notMatched(); |
| 110 | + } |
| 111 | + } |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + @SuppressWarnings("unchecked") |
| 116 | + private static Matcher<Object> nastyGenericsWorkaround(Matcher<?> valueMatcher) { |
| 117 | + return (Matcher<Object>) valueMatcher; |
| 118 | + } |
| 119 | + |
| 120 | + private static Condition.Step<PropertyDescriptor,Method> withReadMethod() { |
| 121 | + return new Condition.Step<PropertyDescriptor, java.lang.reflect.Method>() { |
| 122 | + @Override |
| 123 | + public Condition<Method> apply(PropertyDescriptor property, Description mismatch) { |
| 124 | + final Method readMethod = property.getReadMethod(); |
| 125 | + if (null == readMethod) { |
| 126 | + mismatch.appendText("property \"" + property.getName() + "\" is not readable"); |
| 127 | + return notMatched(); |
| 128 | + } |
| 129 | + return matched(readMethod, mismatch); |
| 130 | + } |
| 131 | + }; |
121 | 132 | } |
122 | 133 |
|
123 | 134 | @Factory |
|
0 commit comments