|
15 | 15 | import org.hamcrest.AbstractMatcherTest; |
16 | 16 | import org.hamcrest.Matcher; |
17 | 17 | import org.hamcrest.StringDescription; |
| 18 | +import org.hamcrest.core.IsEqual; |
18 | 19 |
|
19 | 20 | /** |
20 | 21 | * @author Iain McGinniss |
|
23 | 24 | * @since 1.1.0 |
24 | 25 | */ |
25 | 26 | 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 | + } |
33 | 82 | } |
34 | 83 |
|
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 | + } |
39 | 86 |
|
40 | | - public void testMatchesBeanWithInfoWithMatchedNamedProperty() { |
41 | | - assertThat(beanWithInfo, hasProperty("property", equalTo("with info"))); |
42 | | - } |
| 87 | + interface IX { |
| 88 | + int getTest(); |
| 89 | + } |
43 | 90 |
|
44 | | - public void testDoesNotMatchInfolessBeanWithoutMatchedNamedProperty() { |
45 | | - assertThat(shouldNotMatch, not(hasProperty("nonExistentProperty", anything()))); |
46 | | - } |
| 91 | + public static class BeanWithoutInfo { |
| 92 | + private String property; |
47 | 93 |
|
48 | | - public void testDoesNotMatchWriteOnlyProperty() { |
49 | | - assertThat(shouldNotMatch, not(hasProperty("writeOnlyProperty", anything()))); |
| 94 | + public BeanWithoutInfo(String property) { |
| 95 | + this.property = property; |
50 | 96 | } |
51 | 97 |
|
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; |
57 | 100 | } |
58 | 101 |
|
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 | + } |
81 | 105 |
|
82 | | - public void setWriteOnlyProperty(float property) { |
83 | | - } |
| 106 | + public void setWriteOnlyProperty(float property) { |
| 107 | + } |
84 | 108 |
|
85 | | - @Override |
86 | | - public String toString() { |
87 | | - return "[Person: " + property + "]"; |
88 | | - } |
| 109 | + @Override |
| 110 | + public String toString() { |
| 111 | + return "[Person: " + property + "]"; |
89 | 112 | } |
| 113 | + } |
90 | 114 |
|
91 | | - public static class BeanWithInfo { |
92 | | - private final String propertyValue; |
| 115 | + public static class BeanWithInfo { |
| 116 | + private final String propertyValue; |
93 | 117 |
|
94 | | - public BeanWithInfo(String propertyValue) { |
95 | | - this.propertyValue = propertyValue; |
96 | | - } |
| 118 | + public BeanWithInfo(String propertyValue) { |
| 119 | + this.propertyValue = propertyValue; |
| 120 | + } |
97 | 121 |
|
98 | | - public String property() { |
99 | | - return propertyValue; |
100 | | - } |
| 122 | + public String property() { |
| 123 | + return propertyValue; |
101 | 124 | } |
| 125 | + } |
102 | 126 |
|
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 | + } |
114 | 136 | } |
| 137 | + } |
115 | 138 | } |
0 commit comments