Skip to content

Commit 8e00b60

Browse files
committed
Added use of Condition
Upgraded JUnit to 4.10
1 parent aa3ddb6 commit 8e00b60

21 files changed

Lines changed: 314 additions & 239 deletions

File tree

‎.classpath‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<classpathentry kind="lib" path="lib/integration/easymock-2.2.jar"/>
1212
<classpathentry kind="lib" path="lib/integration/jmock-1.10RC1.jar"/>
1313
<classpathentry kind="lib" path="lib/integration/testng-4.6-jdk15.jar"/>
14-
<classpathentry kind="lib" path="lib/integration/junit-dep-4.5.jar"/>
1514
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
15+
<classpathentry kind="lib" path="lib/integration/junit-dep-4.10.jar"/>
1616
<classpathentry kind="output" path="classes"/>
1717
</classpath>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.hamcrest;
2+
3+
/**
4+
* A Condition implements part of a multi-step match. We sometimes need to write matchers
5+
* that have a sequence of steps, where each step depends on the result of the previous
6+
* step and we can stop processing as soon as a step fails. These classes provide
7+
* infrastructure for writing such a sequence.
8+
*
9+
* Based on https://github.com/npryce/maybe-java
10+
* @author Steve Freeman 2012 http://www.hamcrest.com
11+
*/
12+
13+
public abstract class Condition<T> {
14+
public static final NotMatched<Object> NOT_MATCHED = new NotMatched<Object>();
15+
16+
public interface Step<I, O> {
17+
Condition<O> apply(I value, Description mismatch);
18+
}
19+
20+
public boolean matching(Matcher<T> match) { return matching(match, ""); }
21+
public abstract boolean matching(Matcher<T> match, String message);
22+
public abstract <U> Condition<U> and(Step<? super T, U> mapping);
23+
public final <U> Condition<U> then(Step<? super T, U> mapping) { return and(mapping); }
24+
25+
@SuppressWarnings("unchecked")
26+
public static <T> Condition<T> notMatched() {
27+
return (Condition<T>) NOT_MATCHED;
28+
}
29+
30+
public static <T> Condition<T> matched(final T theValue, final Description mismatch) {
31+
return new Condition<T>() {
32+
@Override
33+
public boolean matching(Matcher<T> matcher, String message) {
34+
if (matcher.matches(theValue)) {
35+
return true;
36+
}
37+
mismatch.appendText(message);
38+
matcher.describeMismatch(theValue, mismatch);
39+
return false;
40+
}
41+
42+
@Override
43+
public <U> Condition<U> and(Step<? super T, U> next) {
44+
return next.apply(theValue, mismatch);
45+
}
46+
};
47+
}
48+
49+
private static class NotMatched<T> extends Condition<T> {
50+
@Override public boolean matching(Matcher<T> match, String message) { return false; }
51+
52+
@Override public <U> Condition<U> and(Step<? super T, U> mapping) {
53+
return notMatched();
54+
}
55+
}
56+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ public FeatureMatcher(Matcher<? super U> subMatcher, String featureDescription,
3636
protected abstract U featureValueOf(T actual);
3737

3838
@Override
39-
protected boolean matchesSafely(T actual, Description mismatchDescription) {
39+
protected boolean matchesSafely(T actual, Description mismatch) {
4040
final U featureValue = featureValueOf(actual);
4141
if (!subMatcher.matches(featureValue)) {
42-
mismatchDescription.appendText(featureName).appendText(" ");
43-
subMatcher.describeMismatch(featureValue, mismatchDescription);
42+
mismatch.appendText(featureName).appendText(" ");
43+
subMatcher.describeMismatch(featureValue, mismatch);
4444
return false;
4545
}
4646
return true;
@@ -51,4 +51,4 @@ public final void describeTo(Description description) {
5151
description.appendText(featureDescription).appendText(" ")
5252
.appendDescriptionOf(subMatcher);
5353
}
54-
}
54+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public AllOf(Iterable<Matcher<? super T>> matchers) {
2222
}
2323

2424
@Override
25-
public boolean matches(Object o, Description mismatchDescription) {
25+
public boolean matches(Object o, Description mismatch) {
2626
for (Matcher<? super T> matcher : matchers) {
2727
if (!matcher.matches(o)) {
28-
mismatchDescription.appendDescriptionOf(matcher).appendText(" ");
29-
matcher.describeMismatch(o, mismatchDescription);
28+
mismatch.appendDescriptionOf(matcher).appendText(" ");
29+
matcher.describeMismatch(o, mismatch);
3030
return false;
3131
}
3232
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import java.util.regex.Pattern;
1111

12+
import static java.lang.Integer.parseInt;
13+
1214
/**
1315
* Provides a custom description to another matcher.
1416
*/
@@ -37,8 +39,7 @@ public void describeTo(Description description) {
3739
int textStart = 0;
3840
while (arg.find()) {
3941
description.appendText(descriptionTemplate.substring(textStart, arg.start()));
40-
int argIndex = Integer.parseInt(arg.group(1));
41-
description.appendValue(values[argIndex]);
42+
description.appendValue(values[parseInt(arg.group(1))]);
4243
textStart = arg.end();
4344
}
4445

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public void describeTo(Description description) {
3434

3535
@Override
3636
public void describeMismatch(Object item, Description mismatchDescription) {
37-
// TODO(ngd): unit tests....
3837
matcher.describeMismatch(item, mismatchDescription);
3938
}
4039

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ private static Class<?> matchableClass(Class<?> expectedClass) {
4040
}
4141

4242
@Override
43-
protected boolean matches(Object item, Description mismatchDescription) {
43+
protected boolean matches(Object item, Description mismatch) {
4444
if (null == item) {
45-
mismatchDescription.appendText("null");
45+
mismatch.appendText("null");
4646
return false;
4747
}
4848

4949
if (!matchableClass.isInstance(item)) {
50-
mismatchDescription.appendValue(item).appendText(" is a " + item.getClass().getName());
50+
mismatch.appendValue(item).appendText(" is a " + item.getClass().getName());
5151
return false;
5252
}
5353

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ public HasProperty(String propertyName) {
2626

2727
@Override
2828
public boolean matchesSafely(T obj) {
29-
// TODO(ngd): this is not type safe.
3029
try {
3130
return PropertyUtil.getPropertyDescriptor(propertyName, obj) != null;
3231
} catch (IllegalArgumentException e) {
33-
// introspection failure is treated as a matcher failure
3432
return false;
3533
}
3634
}

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

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
*/
33
package org.hamcrest.beans;
44

5-
import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS;
5+
import org.hamcrest.*;
66

77
import java.beans.PropertyDescriptor;
8-
import java.lang.reflect.InvocationTargetException;
98
import java.lang.reflect.Method;
109

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;
1513

1614
/**
1715
* Matcher that asserts that a JavaBean property on an argument passed to the
@@ -67,57 +65,70 @@
6765
* @author Steve Freeman
6866
*/
6967
public class HasPropertyWithValue<T> extends TypeSafeDiagnosingMatcher<T> {
68+
private static final Condition.Step<PropertyDescriptor,Method> WITH_READ_METHOD = withReadMethod();
7069
private final String propertyName;
71-
private final Matcher<?> valueMatcher;
70+
private final Matcher<Object> valueMatcher;
7271

7372
public HasPropertyWithValue(String propertyName, Matcher<?> valueMatcher) {
7473
this.propertyName = propertyName;
75-
this.valueMatcher = valueMatcher;
74+
this.valueMatcher = nastyGenericsWorkaround(valueMatcher);
7675
}
7776

7877
@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 + "' ");
9983
}
10084

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();
11096
}
111-
return readMethod;
97+
98+
return matched(property, mismatch);
11299
}
113100

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+
};
121132
}
122133

123134
@Factory

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static PropertyDescriptor getPropertyDescriptor(String propertyName, Obje
3838
* Returns all the property descriptors for the class associated with the given object
3939
*
4040
* @param fromObj Use the class of this object
41-
* @param stopClass TODO
41+
* @param stopClass Don't include any properties from this ancestor class upwards.
4242
* @return Property descriptors
4343
* @throws IllegalArgumentException if there's a introspection failure
4444
*/

0 commit comments

Comments
 (0)