Skip to content

Commit a29b657

Browse files
author
joeretro
committed
Java5ified HasProperty matchers.
1 parent a461680 commit a29b657

6 files changed

Lines changed: 62 additions & 63 deletions

File tree

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ public static IsEqual eq(double operand) {
7575
return eq(new Double(operand));
7676
}
7777

78-
public static IsSame same(Object operand) {
79-
return new IsSame(operand);
80-
}
81-
8278
public static IsInstanceOf isA(Class operandClass) {
8379
return new IsInstanceOf(operandClass);
8480
}
@@ -95,14 +91,6 @@ public static <T> Matcher<T> or(Matcher<T> left, Matcher<T> right) {
9591
return new Or<T>(left, right);
9692
}
9793

98-
public static HasPropertyWithValue hasProperty(String propertyName, Matcher expectation) {
99-
return new HasPropertyWithValue(propertyName, expectation);
100-
}
101-
102-
public static HasProperty hasProperty(String propertyName) {
103-
return new HasProperty(propertyName);
104-
}
105-
10694
public static HasToString toString(Matcher toStringMatcher) {
10795
return new HasToString(toStringMatcher);
10896
}

‎src/library/org/hamcrest/core/IsSame.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89

910
/**
@@ -27,8 +28,12 @@ public boolean match(Object arg) {
2728
}
2829

2930
public void describeTo(Description description) {
30-
description.appendText("same(")
31-
.appendValue(object)
32-
.appendText(")");
31+
description.appendText("same(") .appendValue(object) .appendText(")");
3332
}
33+
34+
@Factory
35+
public static <T> Matcher<T> same(T operand) {
36+
return new IsSame<T>(operand);
37+
}
38+
3439
}

‎src/library/org/hamcrest/object/HasProperty.java‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89
import java.beans.IntrospectionException;
910

@@ -17,7 +18,7 @@
1718
* @author Steve Freeman
1819
* @since 1.1.0
1920
*/
20-
public class HasProperty implements Matcher {
21+
public class HasProperty<T> implements Matcher<T> {
2122

2223
private final String propertyName;
2324

@@ -35,9 +36,12 @@ public boolean match(Object obj) {
3536
}
3637

3738
public void describeTo(Description description) {
38-
description.appendText("hasProperty(\"");
39-
description.appendText(propertyName);
40-
description.appendText("\")");
39+
description.appendText("hasProperty(").appendValue(propertyName).appendText(")");
40+
}
41+
42+
@Factory
43+
public static <T> Matcher<T> hasProperty(String propertyName) {
44+
return new HasProperty<T>(propertyName);
4145
}
4246

4347
}

‎src/library/org/hamcrest/object/HasPropertyWithValue.java‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.hamcrest.Description;
66
import org.hamcrest.Matcher;
7+
import org.hamcrest.Factory;
78

89
import java.beans.IntrospectionException;
910
import java.beans.PropertyDescriptor;
@@ -64,24 +65,24 @@
6465
* @author Steve Freeman
6566
* @since 1.1.0
6667
*/
67-
public class HasPropertyWithValue implements Matcher {
68+
public class HasPropertyWithValue<T> implements Matcher<T> {
6869

6970
private static final Object[] NO_ARGUMENTS = new Object[0];
7071

7172
private final String propertyName;
72-
private final Matcher expectation;
73+
private final Matcher value;
7374

74-
public HasPropertyWithValue(String propertyName, Matcher expectation) {
75+
public HasPropertyWithValue(String propertyName, Matcher value) {
7576
this.propertyName = propertyName;
76-
this.expectation = expectation;
77+
this.value = value;
7778
}
7879

80+
@SuppressWarnings({"unchecked"})
7981
public boolean match(Object argument) {
8082
try {
8183
Method readMethod = getReadMethod(argument);
8284
return readMethod != null
83-
&& expectation.match(readMethod.invoke(argument, NO_ARGUMENTS));
84-
85+
&& value.match(readMethod.invoke(argument, NO_ARGUMENTS));
8586
} catch (IntrospectionException e) {
8687
return false;
8788
} catch (IllegalArgumentException e) {
@@ -99,10 +100,16 @@ private Method getReadMethod(Object argument) throws IntrospectionException {
99100
}
100101

101102
public void describeTo(Description description) {
102-
description.appendText("hasProperty(\"");
103-
description.appendText(propertyName);
104-
description.appendText("\", ");
105-
expectation.describeTo(description);
103+
description.appendText("hasProperty(");
104+
description.appendValue(propertyName);
105+
description.appendText(", ");
106+
value.describeTo(description);
106107
description.appendText(")");
107108
}
109+
110+
@Factory
111+
public static <T> Matcher<T> hasProperty(String propertyName, Matcher value) {
112+
return new HasPropertyWithValue<T>(propertyName, value);
113+
}
114+
108115
}

‎src/unit-test/org/hamcrest/object/HasPropertyTest.java‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,31 @@
22
*/
33
package org.hamcrest.object;
44

5-
import junit.framework.TestCase;
6-
import org.hamcrest.Description;
7-
import org.hamcrest.internal.StringDescription;
5+
import org.hamcrest.AbstractMatcherTest;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.core.IsNot.not;
8+
import static org.hamcrest.object.HasProperty.hasProperty;
89

910
/**
1011
* @author Iain McGinniss
1112
* @author Nat Pryce
1213
* @author Steve Freeman
1314
* @since 1.1.0
1415
*/
15-
public class HasPropertyTest extends TestCase {
16+
public class HasPropertyTest extends AbstractMatcherTest {
1617

17-
private final HasPropertyWithValueTest.BeanWithoutInfo bean = new HasPropertyWithValueTest.BeanWithoutInfo("a bean");
18+
private final HasPropertyWithValueTest.BeanWithoutInfo bean
19+
= new HasPropertyWithValueTest.BeanWithoutInfo("a bean");
1820

1921
public void testReturnsTrueIfPropertyExists() {
20-
HasProperty hasProperty = new HasProperty("writeOnlyProperty");
21-
22-
assertTrue(hasProperty.match(bean));
22+
assertThat(bean, hasProperty("writeOnlyProperty"));
2323
}
2424

2525
public void testReturnsFalseIfPropertyDoesNotExist() {
26-
HasProperty hasProperty = new HasProperty("aNonExistentProp");
27-
28-
assertFalse(hasProperty.match(bean));
26+
assertThat(bean, not(hasProperty("aNonExistentProp")));
2927
}
3028

3129
public void testDescribeTo() {
32-
HasProperty hasProperty = new HasProperty("property");
33-
Description description = new StringDescription();
34-
hasProperty.describeTo(description);
35-
36-
assertEquals("hasProperty(\"property\")", description.toString());
30+
assertDescription("hasProperty(\"property\")", hasProperty("property"));
3731
}
3832
}

‎src/unit-test/org/hamcrest/object/HasPropertyWithValueTest.java‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
*/
33
package org.hamcrest.object;
44

5+
import static org.hamcrest.core.IsAnything.anything;
6+
import static org.hamcrest.core.IsEqual.eq;
7+
import static org.hamcrest.core.IsNot.not;
8+
import static org.hamcrest.MatcherAssert.assertThat;
9+
import static org.hamcrest.object.HasPropertyWithValue.hasProperty;
510
import org.hamcrest.AbstractMatcherTest;
611
import org.hamcrest.Description;
12+
import org.hamcrest.Matcher;
713
import org.hamcrest.core.IsAnything;
814
import org.hamcrest.core.IsEqual;
9-
import org.hamcrest.core.IsSame;
1015
import org.hamcrest.internal.StringDescription;
1116

1217
import java.beans.IntrospectionException;
@@ -20,39 +25,35 @@
2025
* @since 1.1.0
2126
*/
2227
public class HasPropertyWithValueTest extends AbstractMatcherTest {
23-
final private BeanWithoutInfo shouldMatch = new BeanWithoutInfo("is expected");
24-
final private BeanWithoutInfo shouldNotMatch = new BeanWithoutInfo("not expected");
25-
final private BeanWithInfo beanWithInfo = new BeanWithInfo("with info");
2628

27-
public void testMatchesInfolessBeanWithMatchedNamedProperty() {
28-
HasPropertyWithValue hasProperty = new HasPropertyWithValue("property", new IsSame("is expected"));
29+
private final BeanWithoutInfo shouldMatch = new BeanWithoutInfo("is expected");
30+
private final BeanWithoutInfo shouldNotMatch = new BeanWithoutInfo("not expected");
31+
private final BeanWithInfo beanWithInfo = new BeanWithInfo("with info");
2932

30-
assertTrue(hasProperty.match(shouldMatch));
31-
assertFalse(hasProperty.match(shouldNotMatch));
33+
public void testMatchesInfolessBeanWithMatchedNamedProperty() {
34+
assertThat(shouldMatch, hasProperty("property", eq("is expected")));
35+
assertThat(shouldNotMatch, not(hasProperty("property", eq("is expected"))));
3236
}
3337

3438
public void testMatchesBeanWithInfoWithMatchedNamedProperty() {
35-
HasPropertyWithValue hasProperty = new HasPropertyWithValue("property", new IsSame("with info"));
36-
assertTrue(hasProperty.match(beanWithInfo));
39+
assertThat(beanWithInfo, hasProperty("property", eq("with info")));
3740
}
3841

3942
public void testDoesNotMatchInfolessBeanWithoutMatchedNamedProperty() {
40-
HasPropertyWithValue hasProperty = new HasPropertyWithValue("nonExistentProperty", new IsAnything());
41-
assertFalse(hasProperty.match(shouldNotMatch));
43+
assertThat(shouldNotMatch, not(hasProperty("nonExistentProperty", anything())));
4244
}
4345

4446
public void testDoesNotMatchWriteOnlyProperty() {
45-
HasPropertyWithValue hasProperty = new HasPropertyWithValue("writeOnlyProperty", new IsAnything());
46-
assertFalse(hasProperty.match(shouldNotMatch));
47+
assertThat(shouldNotMatch, not(hasProperty("writeOnlyProperty", anything())));
4748
}
4849

4950
public void testDescribeTo() {
50-
IsEqual isEqual = new IsEqual(Boolean.TRUE);
51+
Matcher matcher = eq(true);
5152
Description isEqualDescription = new StringDescription();
52-
isEqual.describeTo(isEqualDescription);
53-
HasPropertyWithValue hasProperty = new HasPropertyWithValue("property", new IsEqual(Boolean.TRUE));
53+
matcher.describeTo(isEqualDescription);
5454

55-
assertDescription("hasProperty(\"property\", " + isEqualDescription + ")", hasProperty);
55+
assertDescription("hasProperty(\"property\", " + isEqualDescription + ")",
56+
hasProperty("property", matcher));
5657
}
5758

5859
public static class BeanWithoutInfo {

0 commit comments

Comments
 (0)