2
2
3
3
import org .hamcrest .Description ;
4
4
import org .hamcrest .Matcher ;
5
- import org .hamcrest .Matchers ;
5
+ import org .hamcrest .TypeSafeMatcher ;
6
6
7
7
import java .util .Arrays ;
8
8
import java .util .regex .Pattern ;
9
9
10
10
import static org .hamcrest .core .AnyOf .anyOf ;
11
11
import static org .hamcrest .core .IsNull .nullValue ;
12
- import static org .hamcrest .text .IsEmptyString .IS_EMPTY_STRING ;
13
12
14
13
public class MatchStrings {
14
+ public static final Matcher <String > IS_EMPTY_STRING = new TypeSafeMatcher <String >() {
15
+ @ Override public void describeTo (Description description ) { description .appendText ("an empty string" ); }
16
+ @ Override protected boolean matchesSafely (String item ) { return item .isEmpty (); }
17
+ };
18
+ public static final Matcher <String > IS_NULL_OR_EMPTY_STRING = anyOf (IS_EMPTY_STRING , nullValue ());
19
+
20
+
15
21
private static final Pattern REGEX_WHITESPACE = Pattern .compile ("\\ s*" );
16
22
public static final Matcher <String > IS_BLANK_STRING = new MatchesPattern (REGEX_WHITESPACE ) {
17
23
@ Override
@@ -29,7 +35,7 @@ public void describeTo(Description description) {
29
35
*
30
36
* @param expectedString the expected value of matched strings
31
37
*/
32
- public static Matcher <String > equalToIgnoringCase (java . lang . String expectedString ) {
38
+ public static Matcher <String > equalToIgnoringCase (String expectedString ) {
33
39
return new IsEqualIgnoringCase (expectedString );
34
40
}
35
41
@@ -46,7 +52,7 @@ public static Matcher<String> equalToIgnoringCase(java.lang.String expectedStrin
46
52
*
47
53
* @param expectedString the expected value of matched strings
48
54
*/
49
- public static Matcher <java . lang . String > equalToIgnoringWhiteSpace (java . lang . String expectedString ) {
55
+ public static Matcher <String > equalToIgnoringWhiteSpace (String expectedString ) {
50
56
return new IsEqualIgnoringWhiteSpace (expectedString );
51
57
}
52
58
@@ -56,16 +62,14 @@ public static Matcher<java.lang.String> equalToIgnoringWhiteSpace(java.lang.Stri
56
62
* For example:
57
63
* <pre>assertThat(((String)null), is(emptyOrNullString()))</pre>
58
64
*/
59
- public static Matcher <java .lang .String > emptyOrNullString () {
60
- return Matchers .anyOf (nullValue (), IS_EMPTY_STRING );
61
- }
65
+ public static Matcher <String > emptyOrNullString () { return IS_NULL_OR_EMPTY_STRING ; }
62
66
63
67
/**
64
68
* Creates a matcher of {@link String} that matches when the examined string has zero length.
65
69
* For example:
66
70
* <pre>assertThat("", is(emptyString()))</pre>
67
71
*/
68
- public static Matcher <java . lang . String > emptyString () {
72
+ public static Matcher <String > emptyString () {
69
73
return IS_EMPTY_STRING ;
70
74
}
71
75
@@ -75,33 +79,31 @@ public static Matcher<java.lang.String> emptyString() {
75
79
* For example:
76
80
* <pre>assertThat(((String)null), is(blankOrNullString()))</pre>
77
81
*/
78
- public static Matcher <java .lang .String > blankOrNullString () {
79
- return IS_BLANK_OR_NULL_STRING ;
80
- }
82
+ public static Matcher <String > blankOrNullString () { return IS_BLANK_OR_NULL_STRING ; }
81
83
82
84
/**
83
85
* Creates a matcher of {@link String} that matches when the examined string contains
84
86
* zero or more whitespace characters and nothing else.
85
87
* For example:
86
88
* <pre>assertThat(" ", is(blankString()))</pre>
87
89
*/
88
- public static Matcher <java . lang . String > blankString () {
90
+ public static Matcher <String > blankString () {
89
91
return IS_BLANK_STRING ;
90
92
}
91
93
92
94
/**
93
- * Creates a matcher of {@link java.lang. String} that matches when the examined string
94
- * exactly matches the given {@link java.util.regex. Pattern}.
95
+ * Creates a matcher of {@link String} that matches when the examined string
96
+ * exactly matches the given {@link Pattern}.
95
97
*/
96
- public static Matcher <java . lang . String > matchesPattern (java . util . regex . Pattern pattern ) {
98
+ public static Matcher <String > matchesPattern (Pattern pattern ) {
97
99
return new MatchesPattern (pattern );
98
100
}
99
101
100
102
/**
101
- * Creates a matcher of {@link java.lang. String} that matches when the examined string
102
- * exactly matches the given regular expression, treated as a {@link java.util.regex. Pattern}.
103
+ * Creates a matcher of {@link String} that matches when the examined string
104
+ * exactly matches the given regular expression, treated as a {@link Pattern}.
103
105
*/
104
- public static Matcher <java . lang . String > matchesPattern (java . lang . String regex ) {
106
+ public static Matcher <String > matchesPattern (String regex ) {
105
107
return new MatchesPattern (Pattern .compile (regex ));
106
108
}
107
109
@@ -114,7 +116,7 @@ public static Matcher<java.lang.String> matchesPattern(java.lang.String regex) {
114
116
*
115
117
* @param substrings the substrings that must be contained within matching strings
116
118
*/
117
- public static Matcher <java . lang . String > stringContainsInOrder (java . lang . Iterable <java . lang . String > substrings ) {
119
+ public static Matcher <String > stringContainsInOrder (Iterable <String > substrings ) {
118
120
return new StringContainsInOrder (substrings );
119
121
}
120
122
@@ -127,7 +129,7 @@ public static Matcher<java.lang.String> stringContainsInOrder(java.lang.Iterable
127
129
*
128
130
* @param substrings the substrings that must be contained within matching strings
129
131
*/
130
- public static Matcher <java . lang . String > stringContainsInOrder (java . lang . String ... substrings ) {
132
+ public static Matcher <String > stringContainsInOrder (String ... substrings ) {
131
133
return new StringContainsInOrder (Arrays .asList (substrings ));
132
134
}
133
135
}
0 commit comments