Skip to content

Commit af548a4

Browse files
committed
add formal JavaDoc for factory methods in hamcrest-library beans and most of hamcrest-library collection. This goes partway to addressing issue 19 (on the google code issue tracker) - Add documentation to Matcher static factories
1 parent 9ecdf0b commit af548a4

14 files changed

+380
-65
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public void describeTo(Description description) {
4343
description.appendText("hasProperty(").appendValue(propertyName).appendText(")");
4444
}
4545

46+
/**
47+
* Creates a matcher that matches when the examined object has a JavaBean property
48+
* with the specified name.
49+
* <p/>
50+
* For example:
51+
* <pre>assertThat(myBean, hasProperty("foo"))</pre>
52+
*
53+
* @param propertyName
54+
* the name of the JavaBean property that examined beans should possess
55+
*/
4656
@Factory
4757
public static <T> Matcher<T> hasProperty(String propertyName) {
4858
return new HasProperty<T>(propertyName);

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

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
*/
33
package org.hamcrest.beans;
44

5-
import org.hamcrest.*;
6-
75
import java.beans.PropertyDescriptor;
86
import java.lang.reflect.Method;
97

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+
1014
import static org.hamcrest.Condition.matched;
1115
import static org.hamcrest.Condition.notMatched;
1216
import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS;
@@ -20,35 +24,33 @@
2024
* <h2>Example Usage</h2>
2125
* Consider the situation where we have a class representing a person, which
2226
* 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>
2429
* 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>
3943
* public interface PersonGenerationListener {
40-
* public void personGenerated(Person person);
41-
* }
42-
* </code>
44+
* public void personGenerated(Person person);
45+
* }</pre>
46+
*
4347
* In order to check that the code under test generates a person with name
4448
* "Iain" we would do the following:
45-
* <p/>
46-
* <code>
49+
* <pre>
4750
* Mock personGenListenerMock = mock(PersonGenerationListener.class);
4851
* 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+
*
5254
* If an exception is thrown by the getter method for a property, the property
5355
* does not exist, is not readable, or a reflection related exception is thrown
5456
* 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
131133
};
132134
}
133135

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+
*/
134148
@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);
137151
}
138152
}

hamcrest-library/src/main/java/org/hamcrest/beans/SamePropertyValuesAs.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ private static Object readProperty(Method method, Object target) {
120120
}
121121
}
122122

123+
/**
124+
* Creates a matcher that matches when the examined object has values for all of
125+
* its JavaBean properties that are equal to the corresponding values of the
126+
* specified bean.
127+
* <p/>
128+
* For example:
129+
* <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean))</pre>
130+
*
131+
* @param expectedBean
132+
* the bean against which examined beans are compared
133+
*/
123134
@Factory
124135
public static <T> Matcher<T> samePropertyValuesAs(T expectedBean) {
125136
return new SamePropertyValuesAs<T>(expectedBean);

hamcrest-library/src/main/java/org/hamcrest/collection/IsArray.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,15 @@ protected String descriptionEnd() {
8080
}
8181

8282
/**
83-
* Evaluates to true only if each matcher[i] is satisfied by array[i].
83+
* Creates a matcher that matches arrays whose elements are satisfied by the specified matchers. Matches
84+
* positively only if the number of matchers specified is equal to the length of the examined array and
85+
* each matcher[i] is satisfied by array[i].
86+
* <p/>
87+
* For example:
88+
* <pre>assertThat(new Integer[]{1,2,3}, is(array(equalTo(1), equalTo(2), equalTo(3))))</pre>
89+
*
90+
* @param elementMatchers
91+
* the matchers that the elements of examined arrays should satisfy
8492
*/
8593
@Factory
8694
public static <T> IsArray<T> array(Matcher<? super T>... elementMatchers) {

hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayContaining.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public boolean matchesSafely(T[] array) {
3030

3131
@Override
3232
public void describeMismatchSafely(T[] item, Description mismatchDescription) {
33-
super.describeMismatch(Arrays.asList(item), mismatchDescription);
33+
super.describeMismatch(Arrays.asList(item), mismatchDescription);
3434
};
3535

3636
@Override
@@ -41,18 +41,31 @@ public void describeTo(Description description) {
4141
}
4242

4343
/**
44-
* Evaluates to true if any item in an array satisfies the given matcher.
44+
* Creates a matcher for arrays that matches when the examined array contains at least one item
45+
* that is matched by the specified <code>elementMatcher</code>. Whilst matching, the traversal
46+
* of the examined array will stop as soon as a matching element is found.
47+
* <p/>
48+
* For example:
49+
* <pre>assertThat(new String[] {"foo", "bar"}, hasItemInArray(startsWith("ba")))</pre>
50+
*
51+
* @param elementMatcher
52+
* the matcher to apply to elements in examined arrays
4553
*/
4654
@Factory
4755
public static <T> Matcher<T[]> hasItemInArray(Matcher<? super T> elementMatcher) {
4856
return new IsArrayContaining<T>(elementMatcher);
4957
}
5058

5159
/**
52-
* This is a shortcut to the frequently used hasItemInArray(equalTo(x)).
53-
*
54-
* For example, assertThat(hasItemInArray(equal_to(x)))
55-
* vs. assertThat(hasItemInArray(x))
60+
* A shortcut to the frequently used <code>hasItemInArray(equalTo(x))</code>.
61+
* <p/>
62+
* For example:
63+
* <pre>assertThat(hasItemInArray(x))</pre>
64+
* instead of:
65+
* <pre>assertThat(hasItemInArray(equalTo(x)))</pre>
66+
*
67+
* @param element
68+
* the element that should be present in examined arrays
5669
*/
5770
@Factory
5871
public static <T> Matcher<T[]> hasItemInArray(T element) {

hamcrest-library/src/main/java/org/hamcrest/collection/IsArrayWithSize.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,42 @@ protected Integer featureValueOf(E[] actual) {
2121
}
2222

2323
/**
24-
* Does array size satisfy a given matcher?
24+
* Creates a matcher for arrays that matches when the <code>length</code> of the array
25+
* satisfies the specified matcher.
26+
* <p/>
27+
* For example:
28+
* <pre>assertThat(new String[]{"foo", "bar"}, arrayWithSize(equalTo(2)))</pre>
29+
*
30+
* @param sizeMatcher
31+
* a matcher for the length of an examined array
2532
*/
2633
@Factory
2734
public static <E> Matcher<E[]> arrayWithSize(Matcher<? super Integer> sizeMatcher) {
2835
return new IsArrayWithSize<E>(sizeMatcher);
2936
}
3037

3138
/**
32-
* This is a shortcut to the frequently used arrayWithSize(equalTo(x)).
33-
*
34-
* For example, assertThat(arrayWithSize(equal_to(x)))
35-
* vs. assertThat(arrayWithSize(x))
39+
* Creates a matcher for arrays that matches when the <code>length</code> of the array
40+
* equals the specified <code>size</code>.
41+
* <p/>
42+
* For example:
43+
* <pre>assertThat(new String[]{"foo", "bar"}, arrayWithSize(2))</pre>
44+
*
45+
* @param size
46+
* the length that an examined array must have for a positive match
3647
*/
3748
@Factory
3849
public static <E> Matcher<E[]> arrayWithSize(int size) {
3950
return arrayWithSize(equalTo(size));
4051
}
4152

4253
/**
43-
* Matches an empty array.
54+
* Creates a matcher for arrays that matches when the <code>length</code> of the array
55+
* is zero.
56+
* <p/>
57+
* For example:
58+
* <pre>assertThat(new String[0], emptyArray())</pre>
59+
*
4460
*/
4561
@Factory
4662
public static <E> Matcher<E[]> emptyArray() {

hamcrest-library/src/main/java/org/hamcrest/collection/IsCollectionWithSize.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,29 @@ protected Integer featureValueOf(Collection<? extends E> actual) {
2222
}
2323

2424
/**
25-
* Does collection size satisfy a given matcher?
25+
* Creates a matcher for {@link Collection}s that matches when the <code>size()</code> method returns
26+
* a value that satisfies the specified matcher.
27+
* <p/>
28+
* For example:
29+
* <pre>assertThat(Arrays.asList("foo", "bar"), hasSize(equalTo(2)))</pre>
30+
*
31+
* @param sizeMatcher
32+
* a matcher for the size of an examined {@link Collection}
2633
*/
2734
@Factory
28-
public static <E> Matcher<Collection<? extends E>> hasSize(Matcher<? super Integer> size) {
29-
return new IsCollectionWithSize<E>(size);
35+
public static <E> Matcher<Collection<? extends E>> hasSize(Matcher<? super Integer> sizeMatcher) {
36+
return new IsCollectionWithSize<E>(sizeMatcher);
3037
}
3138

32-
3339
/**
34-
* This is a shortcut to the frequently used hasSize(equalTo(x)).
35-
*
36-
* For example, assertThat(hasSize(equal_to(x)))
37-
* vs. assertThat(hasSize(x))
40+
* Creates a matcher for {@link Collection}s that matches when the <code>size()</code> method returns
41+
* a value equal to the specified <code>size</code>.
42+
* <p/>
43+
* For example:
44+
* <pre>assertThat(Arrays.asList("foo", "bar"), hasSize(2))</pre>
45+
*
46+
* @param size
47+
* the expected size of an examined {@link Collection}
3848
*/
3949
@Factory
4050
public static <E> Matcher<Collection<? extends E>> hasSize(int size) {

hamcrest-library/src/main/java/org/hamcrest/collection/IsEmptyCollection.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ public void describeTo(Description description) {
2727
description.appendText("an empty collection");
2828
}
2929

30-
/**
31-
* Matches an empty collection.
32-
*/
30+
/**
31+
* Creates a matcher for {@link Collection}s matching examined collections whose <code>isEmpty</code>
32+
* method returns <code>true</code>.
33+
* <p/>
34+
* For example:
35+
* <pre>assertThat(new ArrayList&lt;String&gt;(), is(empty()))</pre>
36+
*
37+
*/
3338
@Factory
3439
public static <E> Matcher<Collection<? extends E>> empty() {
3540
return new IsEmptyCollection<E>();

hamcrest-library/src/main/java/org/hamcrest/collection/IsEmptyIterable.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ public void describeTo(Description description) {
2525
}
2626

2727
/**
28-
* Matches an empty iterable.
28+
* Creates a matcher for {@link Iterable}s matching examined iterables that yield no items.
29+
* <p/>
30+
* For example:
31+
* <pre>assertThat(new ArrayList&lt;String&gt;(), is(emptyIterable()))</pre>
32+
*
2933
*/
3034
@Factory
3135
public static <E> Matcher<Iterable<? extends E>> emptyIterable() {

hamcrest-library/src/main/java/org/hamcrest/collection/IsIn.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,40 @@ public void describeTo(Description buffer) {
3030
buffer.appendValueList("{", ", ", "}", collection);
3131
}
3232

33+
/**
34+
* Creates a matcher that matches when the examined object is found within the
35+
* specified collection.
36+
*
37+
* @param collection
38+
* the collection in which matching items must be found
39+
*
40+
*/
3341
@Factory
3442
public static <T> Matcher<T> isIn(Collection<T> collection) {
3543
return new IsIn<T>(collection);
3644
}
37-
45+
46+
/**
47+
* Creates a matcher that matches when the examined object is found within the
48+
* specified array.
49+
*
50+
* @param elements
51+
* the array in which matching items must be found
52+
*
53+
*/
3854
@Factory
3955
public static <T> Matcher<T> isIn(T[] elements) {
4056
return new IsIn<T>(elements);
4157
}
4258

59+
/**
60+
* Creates a matcher that matches when the examined object is equal to one of the
61+
* specified elements.
62+
*
63+
* @param elements
64+
* the elements amongst which matching items will be found
65+
*
66+
*/
4367
@Factory
4468
public static <T> Matcher<T> isOneOf(T... elements) {
4569
return isIn(elements);

0 commit comments

Comments
 (0)