|
2 | 2 | */ |
3 | 3 | package org.hamcrest.beans; |
4 | 4 |
|
5 | | -import org.hamcrest.*; |
6 | | - |
7 | 5 | import java.beans.PropertyDescriptor; |
8 | 6 | import java.lang.reflect.Method; |
9 | 7 |
|
| 8 | +import org.hamcrest.Condition; |
| 9 | +import org.hamcrest.Description; |
| 10 | +import org.hamcrest.Factory; |
| 11 | +import org.hamcrest.Matcher; |
| 12 | +import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 13 | + |
10 | 14 | import static org.hamcrest.Condition.matched; |
11 | 15 | import static org.hamcrest.Condition.notMatched; |
12 | 16 | import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS; |
|
20 | 24 | * <h2>Example Usage</h2> |
21 | 25 | * Consider the situation where we have a class representing a person, which |
22 | 26 | * follows the basic JavaBean convention of having get() and possibly set() |
23 | | - * methods for it's properties: <code> |
| 27 | + * methods for it's properties: |
| 28 | + * <pre> |
24 | 29 | * public class Person { |
25 | | - * private String name; |
26 | | - * <p/> |
27 | | - * public Person(String person) { |
28 | | - * this.person = person; |
29 | | - * } |
30 | | - * <p/> |
31 | | - * public String getName() { |
32 | | - * return name; |
33 | | - * } |
34 | | - * } |
35 | | - * </code> And that these person |
36 | | - * objects are generated within a piece of code under test (a class named |
37 | | - * PersonGenerator). This object is sent to one of our mock objects which |
38 | | - * overrides the PersonGenerationListener interface: <code> |
| 30 | + * private String name; |
| 31 | + * public Person(String person) { |
| 32 | + * this.person = person; |
| 33 | + * } |
| 34 | + * public String getName() { |
| 35 | + * return name; |
| 36 | + * } |
| 37 | + * }</pre> |
| 38 | + * |
| 39 | + * And that these person objects are generated within a piece of code under test |
| 40 | + * (a class named PersonGenerator). This object is sent to one of our mock objects |
| 41 | + * which overrides the PersonGenerationListener interface: |
| 42 | + * <pre> |
39 | 43 | * public interface PersonGenerationListener { |
40 | | - * public void personGenerated(Person person); |
41 | | - * } |
42 | | - * </code> |
| 44 | + * public void personGenerated(Person person); |
| 45 | + * }</pre> |
| 46 | + * |
43 | 47 | * In order to check that the code under test generates a person with name |
44 | 48 | * "Iain" we would do the following: |
45 | | - * <p/> |
46 | | - * <code> |
| 49 | + * <pre> |
47 | 50 | * Mock personGenListenerMock = mock(PersonGenerationListener.class); |
48 | 51 | * personGenListenerMock.expects(once()).method("personGenerated").with(and(isA(Person.class), hasProperty("Name", eq("Iain"))); |
49 | | - * PersonGenerationListener listener = (PersonGenerationListener)personGenListenerMock.proxy(); |
50 | | - * </code> |
51 | | - * <p/> |
| 52 | + * PersonGenerationListener listener = (PersonGenerationListener)personGenListenerMock.proxy();</pre> |
| 53 | + * |
52 | 54 | * If an exception is thrown by the getter method for a property, the property |
53 | 55 | * does not exist, is not readable, or a reflection related exception is thrown |
54 | 56 | * when trying to invoke it then this is treated as an evaluation failure and |
@@ -131,8 +133,20 @@ public Condition<Method> apply(PropertyDescriptor property, Description mismatch |
131 | 133 | }; |
132 | 134 | } |
133 | 135 |
|
| 136 | + /** |
| 137 | + * Creates a matcher that matches when the examined object has a JavaBean property |
| 138 | + * with the specified name whose value satisfies the specified matcher. |
| 139 | + * <p/> |
| 140 | + * For example: |
| 141 | + * <pre>assertThat(myBean, hasProperty("foo", equalTo("bar"))</pre> |
| 142 | + * |
| 143 | + * @param propertyName |
| 144 | + * the name of the JavaBean property that examined beans should possess |
| 145 | + * @param valueMatcher |
| 146 | + * a matcher for the value of the specified property of the examined bean |
| 147 | + */ |
134 | 148 | @Factory |
135 | | - public static <T> Matcher<T> hasProperty(String propertyName, Matcher<?> value) { |
136 | | - return new HasPropertyWithValue<T>(propertyName, value); |
| 149 | + public static <T> Matcher<T> hasProperty(String propertyName, Matcher<?> valueMatcher) { |
| 150 | + return new HasPropertyWithValue<T>(propertyName, valueMatcher); |
137 | 151 | } |
138 | 152 | } |
0 commit comments