Skip to content

Commit c4bddd2

Browse files
committed
Starting to declare syntactic sugar factory methods in domain-specific classes.
The naming convention I've adopted is MatchXxxs, inspired by Colin Vipurs' idea on the mailing list of how to help people find matchers with autocomplete. E.g. MatchStrings for string matchers, MatchIterables for iterable matchers, and so on. The factory classes are typically (but not always) in the same subpackage of the matchers that they provide. E.g. MatchStrings is in org.hamcrest.text, MatchBeans in org.hamcrest.beans. The core matchers fall do not fit this scheme. I'm not yet sure what to do about allOf, anyOf, not, equalTo, anything(), etc.. For now, equalTo and anything() are on MatchObjects but maybe the core matchers should remain on Matchers, and all the other factory methods be deprecated, I haven't deprecated any of the existing factory methods on the Matcher subclasses yet.
1 parent d03571a commit c4bddd2

31 files changed

+1322
-136
lines changed

hamcrest-core/src/main/java/org/hamcrest/internal/NullSafety.java renamed to hamcrest-core/src/main/java/org/hamcrest/internal/Wrapping.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@
66
import java.util.ArrayList;
77
import java.util.List;
88

9-
public class NullSafety {
9+
import static org.hamcrest.core.IsEqual.equalTo;
10+
11+
public class Wrapping {
12+
@SafeVarargs
13+
public static <E> List<Matcher<? super E>> asEqualToMatchers(E... items) {
14+
List<Matcher<? super E>> matchers = new ArrayList<>();
15+
for (E item : items) {
16+
matchers.add(equalTo(item));
17+
}
18+
return matchers;
19+
}
20+
1021
@SuppressWarnings("unchecked")
11-
public static <E> List<Matcher<? super E>> nullSafe(Matcher<? super E>[] itemMatchers) {
22+
@SafeVarargs
23+
public static <E> List<Matcher<? super E>> nullSafe(Matcher<? super E>... itemMatchers) {
1224
final List<Matcher<? super E>> matchers = new ArrayList<>(itemMatchers.length);
1325
for (final Matcher<? super E> itemMatcher : itemMatchers) {
1426
matchers.add((Matcher<? super E>) (itemMatcher == null ? IsNull.nullValue() : itemMatcher));
1527
}
1628
return matchers;
1729
}
30+
1831
}

hamcrest-core/src/test/java/org/hamcrest/core/IsEqualTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import org.junit.Test;
77

88
import static org.hamcrest.AbstractMatcherTest.*;
9-
import static org.hamcrest.core.IsEqual.equalTo;
10-
import static org.hamcrest.core.IsEqual.equalToObject;
9+
import static org.hamcrest.object.MatchObjects.equalTo;
10+
import static org.hamcrest.object.MatchObjects.equalToObject;
1111

1212
public final class IsEqualTest {
1313

hamcrest-library/src/main/java/org/hamcrest/Matchers.java

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
package org.hamcrest;
22

3-
import org.hamcrest.collection.HasSubsequence;
4-
5-
import java.util.ArrayList;
6-
import java.util.List;
7-
8-
import static java.util.Arrays.asList;
3+
import org.hamcrest.text.MatchStrings;
94

105
@SuppressWarnings("UnusedDeclaration")
116
public class Matchers {
12-
137
/**
148
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
159
* For example:
@@ -29,51 +23,6 @@ public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T>.
2923
return org.hamcrest.core.AllOf.allOf(matchers);
3024
}
3125

32-
/**
33-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
34-
* For example:
35-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
36-
*/
37-
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second) {
38-
return org.hamcrest.core.AllOf.allOf(first, second);
39-
}
40-
41-
/**
42-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
43-
* For example:
44-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
45-
*/
46-
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third) {
47-
return org.hamcrest.core.AllOf.allOf(first, second, third);
48-
}
49-
50-
/**
51-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
52-
* For example:
53-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
54-
*/
55-
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth) {
56-
return org.hamcrest.core.AllOf.allOf(first, second, third, fourth);
57-
}
58-
59-
/**
60-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
61-
* For example:
62-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
63-
*/
64-
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth) {
65-
return org.hamcrest.core.AllOf.allOf(first, second, third, fourth, fifth);
66-
}
67-
68-
/**
69-
* Creates a matcher that matches if the examined object matches <b>ALL</b> of the specified matchers.
70-
* For example:
71-
* <pre>assertThat("myValue", allOf(startsWith("my"), containsString("Val")))</pre>
72-
*/
73-
public static <T> org.hamcrest.Matcher<T> allOf(org.hamcrest.Matcher<? super T> first, org.hamcrest.Matcher<? super T> second, org.hamcrest.Matcher<? super T> third, org.hamcrest.Matcher<? super T> fourth, org.hamcrest.Matcher<? super T> fifth, org.hamcrest.Matcher<? super T> sixth) {
74-
return org.hamcrest.core.AllOf.allOf(first, second, third, fourth, fifth, sixth);
75-
}
76-
7726
/**
7827
* Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
7928
* For example:
@@ -1359,15 +1308,15 @@ public static org.hamcrest.Matcher<java.lang.String> blankString() {
13591308
* exactly matches the given {@link java.util.regex.Pattern}.
13601309
*/
13611310
public static org.hamcrest.Matcher<java.lang.String> matchesPattern(java.util.regex.Pattern pattern) {
1362-
return org.hamcrest.text.MatchesPattern.matchesPattern(pattern);
1311+
return MatchStrings.matchesPattern(pattern);
13631312
}
13641313

13651314
/**
13661315
* Creates a matcher of {@link java.lang.String} that matches when the examined string
13671316
* exactly matches the given regular expression, treated as a {@link java.util.regex.Pattern}.
13681317
*/
13691318
public static org.hamcrest.Matcher<java.lang.String> matchesPattern(java.lang.String regex) {
1370-
return org.hamcrest.text.MatchesPattern.matchesPattern(regex);
1319+
return MatchStrings.matchesPattern(regex);
13711320
}
13721321

13731322
/**
@@ -1548,25 +1497,4 @@ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String x
15481497
return org.hamcrest.xml.HasXPath.hasXPath(xPath, namespaceContext);
15491498
}
15501499

1551-
public static <T> Matcher<Iterable<? extends T>> hasSubsequence(List<Matcher<? super T>> matchers) {
1552-
return new HasSubsequence<>(matchers);
1553-
}
1554-
1555-
@SafeVarargs
1556-
public static <T> Matcher<Iterable<? extends T>> hasSubsequence(Matcher<? super T>... matchers) {
1557-
return hasSubsequence(asList(matchers));
1558-
}
1559-
1560-
public static <T> Matcher<Iterable<? extends T>> hasSubsequence(Iterable<? extends T> items) {
1561-
List<Matcher<? super T>> matchers = new ArrayList<>();
1562-
for (Object item : items) {
1563-
matchers.add(equalTo(item));
1564-
}
1565-
return new HasSubsequence<>(matchers);
1566-
}
1567-
1568-
@SafeVarargs
1569-
public static <T> Matcher<Iterable<? extends T>> hasSubsequence(T... elements) {
1570-
return hasSubsequence(asList(elements));
1571-
}
15721500
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.hamcrest.beans;
2+
3+
public class MatchBeans {
4+
/**
5+
* Creates a matcher that matches when the examined object has a JavaBean property
6+
* with the specified name.
7+
* For example:
8+
* <pre>assertThat(myBean, hasProperty("foo"))</pre>
9+
*
10+
* @param propertyName the name of the JavaBean property that examined beans should possess
11+
*/
12+
public static <T> org.hamcrest.Matcher<T> hasProperty(java.lang.String propertyName) {
13+
return org.hamcrest.beans.HasProperty.hasProperty(propertyName);
14+
}
15+
16+
/**
17+
* Creates a matcher that matches when the examined object has a JavaBean property
18+
* with the specified name whose value satisfies the specified matcher.
19+
* For example:
20+
* <pre>assertThat(myBean, hasProperty("foo", equalTo("bar"))</pre>
21+
*
22+
* @param propertyName the name of the JavaBean property that examined beans should possess
23+
* @param valueMatcher a matcher for the value of the specified property of the examined bean
24+
*/
25+
public static <T> org.hamcrest.Matcher<T> hasProperty(java.lang.String propertyName, org.hamcrest.Matcher<?> valueMatcher) {
26+
return org.hamcrest.beans.HasPropertyWithValue.hasProperty(propertyName, valueMatcher);
27+
}
28+
29+
/**
30+
* Creates a matcher that matches when the examined object has values for all of
31+
* its JavaBean properties that are equal to the corresponding values of the
32+
* specified bean.
33+
* For example:
34+
* <pre>assertThat(myBean, samePropertyValuesAs(myExpectedBean))</pre>
35+
*
36+
* @param expectedBean the bean against which examined beans are compared
37+
*/
38+
public static <T> org.hamcrest.Matcher<T> samePropertyValuesAs(T expectedBean) {
39+
return org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs(expectedBean);
40+
}
41+
42+
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.hamcrest.Description;
44
import org.hamcrest.Matcher;
55
import org.hamcrest.TypeSafeMatcher;
6-
import org.hamcrest.internal.NullSafety;
6+
import org.hamcrest.internal.Wrapping;
77

88
import java.util.ArrayList;
99
import java.util.Collection;
@@ -67,11 +67,7 @@ public static <E> Matcher<E[]> arrayContaining(E... items) {
6767
*/
6868
@SafeVarargs
6969
public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatchers) {
70-
//required for JDK 1.6
71-
//noinspection RedundantTypeArguments
72-
final List<Matcher<? super E>> nullSafeWithExplicitTypeMatchers = NullSafety.<E>nullSafe(itemMatchers);
73-
74-
return arrayContaining(nullSafeWithExplicitTypeMatchers);
70+
return arrayContaining(Wrapping.<E>nullSafe(itemMatchers));
7571
}
7672

7773
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.hamcrest.Description;
44
import org.hamcrest.Matcher;
55
import org.hamcrest.TypeSafeDiagnosingMatcher;
6-
import org.hamcrest.internal.NullSafety;
6+
import org.hamcrest.internal.Wrapping;
77

88
import java.util.ArrayList;
99
import java.util.List;
@@ -133,7 +133,7 @@ public static <E> Matcher<Iterable<? extends E>> contains(final Matcher<? super
133133
public static <E> Matcher<Iterable<? extends E>> contains(Matcher<? super E>... itemMatchers) {
134134
// required for JDK 1.6
135135
//noinspection RedundantTypeArguments
136-
final List<Matcher<? super E>> nullSafeWithExplicitTypeMatchers = NullSafety.<E>nullSafe(itemMatchers);
136+
final List<Matcher<? super E>> nullSafeWithExplicitTypeMatchers = Wrapping.<E>nullSafe(itemMatchers);
137137
return contains(nullSafeWithExplicitTypeMatchers);
138138
}
139139

0 commit comments

Comments
 (0)