Skip to content

Commit a433c74

Browse files
author
smgfreeman
committed
Added a test for getting properties from anonymouse inner classes.
1 parent 4d9aae1 commit a433c74

1 file changed

Lines changed: 94 additions & 71 deletions

File tree

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

Lines changed: 94 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.hamcrest.AbstractMatcherTest;
1616
import org.hamcrest.Matcher;
1717
import org.hamcrest.StringDescription;
18+
import org.hamcrest.core.IsEqual;
1819

1920
/**
2021
* @author Iain McGinniss
@@ -23,93 +24,115 @@
2324
* @since 1.1.0
2425
*/
2526
public class HasPropertyWithValueTest extends AbstractMatcherTest {
26-
private final BeanWithoutInfo shouldMatch = new BeanWithoutInfo("is expected");
27-
private final BeanWithoutInfo shouldNotMatch = new BeanWithoutInfo("not expected");
28-
private final BeanWithInfo beanWithInfo = new BeanWithInfo("with info");
29-
30-
@Override
31-
protected Matcher<?> createMatcher() {
32-
return hasProperty("irrelevant", anything());
27+
private final BeanWithoutInfo shouldMatch = new BeanWithoutInfo("is expected");
28+
29+
private final BeanWithoutInfo shouldNotMatch = new BeanWithoutInfo(
30+
"not expected");
31+
32+
private final BeanWithInfo beanWithInfo = new BeanWithInfo("with info");
33+
34+
@Override
35+
protected Matcher<?> createMatcher() {
36+
return hasProperty("irrelevant", anything());
37+
}
38+
39+
public void testMatchesInfolessBeanWithMatchedNamedProperty() {
40+
assertThat(shouldMatch, hasProperty("property", equalTo("is expected")));
41+
assertThat(shouldNotMatch, not(hasProperty("property",
42+
equalTo("is expected"))));
43+
}
44+
45+
public void testMatchesBeanWithInfoWithMatchedNamedProperty() {
46+
assertThat(beanWithInfo, hasProperty("property", equalTo("with info")));
47+
}
48+
49+
public void testDoesNotMatchInfolessBeanWithoutMatchedNamedProperty() {
50+
assertThat(shouldNotMatch, not(hasProperty("nonExistentProperty",
51+
anything())));
52+
}
53+
54+
public void testDoesNotMatchWriteOnlyProperty() {
55+
assertThat(shouldNotMatch,
56+
not(hasProperty("writeOnlyProperty", anything())));
57+
}
58+
59+
public void testDescribeTo() {
60+
Matcher<?> matcher = equalTo(true);
61+
62+
assertDescription("hasProperty(\"property\", "
63+
+ StringDescription.asString(matcher) + ")", hasProperty("property",
64+
matcher));
65+
}
66+
67+
public void testDescribesMissingPropertyMismatch() {
68+
assertMismatchDescription("property \"honk\" was missing.", hasProperty(
69+
"honk", anything()), shouldNotMatch);
70+
}
71+
72+
public void testDescribesMismatchingPropertyValueMismatch() {
73+
assertMismatchDescription("property \"property\" was \"not expected\".",
74+
hasProperty("property", equalTo("foo")), shouldNotMatch);
75+
}
76+
77+
public void testCanAccessAnAnonymousInnerClass() {
78+
class X implements IX {
79+
public int getTest() {
80+
return 1;
81+
}
3382
}
3483

35-
public void testMatchesInfolessBeanWithMatchedNamedProperty() {
36-
assertThat(shouldMatch, hasProperty("property", equalTo("is expected")));
37-
assertThat(shouldNotMatch, not(hasProperty("property", equalTo("is expected"))));
38-
}
84+
assertThat(new X(), HasPropertyWithValue.hasProperty("test", IsEqual.equalTo(1)));
85+
}
3986

40-
public void testMatchesBeanWithInfoWithMatchedNamedProperty() {
41-
assertThat(beanWithInfo, hasProperty("property", equalTo("with info")));
42-
}
87+
interface IX {
88+
int getTest();
89+
}
4390

44-
public void testDoesNotMatchInfolessBeanWithoutMatchedNamedProperty() {
45-
assertThat(shouldNotMatch, not(hasProperty("nonExistentProperty", anything())));
46-
}
91+
public static class BeanWithoutInfo {
92+
private String property;
4793

48-
public void testDoesNotMatchWriteOnlyProperty() {
49-
assertThat(shouldNotMatch, not(hasProperty("writeOnlyProperty", anything())));
94+
public BeanWithoutInfo(String property) {
95+
this.property = property;
5096
}
5197

52-
public void testDescribeTo() {
53-
Matcher<?> matcher = equalTo(true);
54-
55-
assertDescription("hasProperty(\"property\", " + StringDescription.asString(matcher) + ")",
56-
hasProperty("property", matcher));
98+
public String getProperty() {
99+
return property;
57100
}
58101

59-
public void testDescribesMissingPropertyMismatch() {
60-
assertMismatchDescription("property \"honk\" was missing.", hasProperty("honk", anything()), shouldNotMatch);
61-
}
62-
63-
public void testDescribesMismatchingPropertyValueMismatch() {
64-
assertMismatchDescription("property \"property\" was \"not expected\".", hasProperty("property", equalTo("foo")), shouldNotMatch);
65-
}
66-
67-
public static class BeanWithoutInfo {
68-
private String property;
69-
70-
public BeanWithoutInfo(String property) {
71-
this.property = property;
72-
}
73-
74-
public String getProperty() {
75-
return property;
76-
}
77-
78-
public void setProperty(String property) {
79-
this.property = property;
80-
}
102+
public void setProperty(String property) {
103+
this.property = property;
104+
}
81105

82-
public void setWriteOnlyProperty(float property) {
83-
}
106+
public void setWriteOnlyProperty(float property) {
107+
}
84108

85-
@Override
86-
public String toString() {
87-
return "[Person: " + property + "]";
88-
}
109+
@Override
110+
public String toString() {
111+
return "[Person: " + property + "]";
89112
}
113+
}
90114

91-
public static class BeanWithInfo {
92-
private final String propertyValue;
115+
public static class BeanWithInfo {
116+
private final String propertyValue;
93117

94-
public BeanWithInfo(String propertyValue) {
95-
this.propertyValue = propertyValue;
96-
}
118+
public BeanWithInfo(String propertyValue) {
119+
this.propertyValue = propertyValue;
120+
}
97121

98-
public String property() {
99-
return propertyValue;
100-
}
122+
public String property() {
123+
return propertyValue;
101124
}
125+
}
102126

103-
public static class BeanWithInfoBeanInfo extends SimpleBeanInfo {
104-
@Override
105-
public PropertyDescriptor[] getPropertyDescriptors() {
106-
try {
107-
return new PropertyDescriptor[]{
108-
new PropertyDescriptor("property", BeanWithInfo.class, "property", null)
109-
};
110-
} catch (IntrospectionException e) {
111-
throw new RuntimeException("Introspection exception: " + e.getMessage());
112-
}
113-
}
127+
public static class BeanWithInfoBeanInfo extends SimpleBeanInfo {
128+
@Override
129+
public PropertyDescriptor[] getPropertyDescriptors() {
130+
try {
131+
return new PropertyDescriptor[] { new PropertyDescriptor("property",
132+
BeanWithInfo.class, "property", null) };
133+
} catch (IntrospectionException e) {
134+
throw new RuntimeException("Introspection exception: " + e.getMessage());
135+
}
114136
}
137+
}
115138
}

0 commit comments

Comments
 (0)