Skip to content

Commit bda1db3

Browse files
committed
pull the IsBlankString matchers out into a separate class.
1 parent 60d350a commit bda1db3

5 files changed

Lines changed: 129 additions & 40 deletions

File tree

‎hamcrest-library/matchers.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<factory class="org.hamcrest.text.IsEqualIgnoringCase"/>
4444
<factory class="org.hamcrest.text.IsEqualIgnoringWhiteSpace"/>
4545
<factory class="org.hamcrest.text.IsEmptyString"/>
46+
<factory class="org.hamcrest.text.IsBlankString"/>
4647
<factory class="org.hamcrest.text.StringContainsInOrder"/>
4748

4849
<!-- Object -->
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
package org.hamcrest.text;
3+
4+
import static org.hamcrest.core.AnyOf.anyOf;
5+
import static org.hamcrest.core.IsNull.nullValue;
6+
7+
import java.util.regex.Pattern;
8+
9+
import org.hamcrest.BaseMatcher;
10+
import org.hamcrest.Description;
11+
import org.hamcrest.Factory;
12+
import org.hamcrest.Matcher;
13+
14+
/**
15+
* Matches blank Strings (and null).
16+
*/
17+
public final class IsBlankString extends BaseMatcher<String> {
18+
private static final IsBlankString BLANK_INSTANCE = new IsBlankString();
19+
@SuppressWarnings("unchecked")
20+
private static final Matcher<String> NULL_OR_BLANK_INSTANCE = anyOf(nullValue(), BLANK_INSTANCE);
21+
22+
private static final Pattern REGEX_WHITESPACE = Pattern.compile("\\s*");
23+
24+
private IsBlankString() { }
25+
26+
@Override
27+
public boolean matches(Object item) {
28+
return item != null && item instanceof String && REGEX_WHITESPACE.matcher((String)item).matches();
29+
}
30+
31+
@Override
32+
public void describeTo(Description description) {
33+
description.appendText("a blank string");
34+
}
35+
36+
/**
37+
* Creates a matcher of {@link String} that matches when the examined string contains
38+
* zero or more whitespace characters and nothing else.
39+
* <p/>
40+
* For example:
41+
* <pre>assertThat(" ", is(blankString()))</pre>
42+
*/
43+
@Factory
44+
public static Matcher<String> blankString() {
45+
return BLANK_INSTANCE;
46+
}
47+
48+
/**
49+
* Creates a matcher of {@link String} that matches when the examined string is <code>null</code>, or
50+
* contains zero or more whitespace characters and nothing else.
51+
* <p/>
52+
* For example:
53+
* <pre>assertThat(((String)null), is(blankOrNullString()))</pre>
54+
*
55+
*/
56+
@Factory
57+
public static Matcher<String> blankOrNullString() {
58+
return NULL_OR_BLANK_INSTANCE;
59+
}
60+
}

‎hamcrest-library/src/main/java/org/hamcrest/text/IsEmptyString.java‎

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,29 @@
44
import static org.hamcrest.core.AnyOf.anyOf;
55
import static org.hamcrest.core.IsNull.nullValue;
66

7-
import java.util.regex.Pattern;
8-
97
import org.hamcrest.BaseMatcher;
108
import org.hamcrest.Description;
119
import org.hamcrest.Factory;
1210
import org.hamcrest.Matcher;
1311

1412
/**
15-
* Provides matchers that match null, empty, or blank Strings, or combinations thereof.
13+
* Matches empty Strings (and null).
1614
*/
1715
public final class IsEmptyString extends BaseMatcher<String> {
18-
private static final IsEmptyString EMPTY_INSTANCE = new IsEmptyString(false);
19-
private static final IsEmptyString BLANK_INSTANCE = new IsEmptyString(true);
16+
private static final IsEmptyString INSTANCE = new IsEmptyString();
2017
@SuppressWarnings("unchecked")
21-
private static final Matcher<String> NULL_OR_EMPTY_INSTANCE = anyOf(nullValue(), EMPTY_INSTANCE);
22-
23-
private static final Pattern REGEX_WHITESPACE = Pattern.compile("\\s*");
24-
25-
private final boolean allowWhitespace;
18+
private static final Matcher<String> NULL_OR_EMPTY_INSTANCE = anyOf(nullValue(), INSTANCE);
2619

27-
private IsEmptyString(boolean allowWhitespace) {
28-
this.allowWhitespace = allowWhitespace;
29-
}
20+
private IsEmptyString() { }
3021

3122
@Override
3223
public boolean matches(Object item) {
33-
if (item == null || !(item instanceof String)) {
34-
return false;
35-
}
36-
final String candidate = (String)item;
37-
return allowWhitespace ? REGEX_WHITESPACE.matcher(candidate).matches() : candidate.equals("");
24+
return item != null && item instanceof String && ((String) item).equals("");
3825
}
3926

4027
@Override
4128
public void describeTo(Description description) {
42-
description.appendText(allowWhitespace ? "a blank" : "an empty").appendText(" string");
29+
description.appendText("an empty string");
4330
}
4431

4532
/**
@@ -65,19 +52,7 @@ public static Matcher<String> isEmptyString() {
6552
*/
6653
@Factory
6754
public static Matcher<String> emptyString() {
68-
return EMPTY_INSTANCE;
69-
}
70-
71-
/**
72-
* Creates a matcher of {@link String} that matches when the examined string contains
73-
* zero or more whitespace characters and nothing else.
74-
* <p/>
75-
* For example:
76-
* <pre>assertThat(" ", is(emptyString()))</pre>
77-
*/
78-
@Factory
79-
public static Matcher<String> blankString() {
80-
return BLANK_INSTANCE;
55+
return INSTANCE;
8156
}
8257

8358
/**
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.hamcrest.text;
2+
3+
import static org.hamcrest.AbstractMatcherTest.assertDescription;
4+
import static org.hamcrest.AbstractMatcherTest.assertDoesNotMatch;
5+
import static org.hamcrest.AbstractMatcherTest.assertMatches;
6+
import static org.hamcrest.AbstractMatcherTest.assertMismatchDescription;
7+
import static org.hamcrest.AbstractMatcherTest.assertNullSafe;
8+
import static org.hamcrest.AbstractMatcherTest.assertUnknownTypeSafe;
9+
import static org.hamcrest.text.IsBlankString.blankOrNullString;
10+
import static org.hamcrest.text.IsBlankString.blankString;
11+
12+
import org.hamcrest.Matcher;
13+
import org.junit.Test;
14+
15+
public final class IsBlankStringTest {
16+
17+
@Test public void
18+
copesWithNullsAndUnknownTypes() {
19+
Matcher<String> matcher = blankString();
20+
21+
assertNullSafe(matcher);
22+
assertUnknownTypeSafe(matcher);
23+
}
24+
25+
@Test public void
26+
matchesEmptyString() {
27+
assertMatches("didn't match", blankOrNullString(), "");
28+
assertMatches("didn't match", blankString(), "");
29+
}
30+
31+
@Test public void
32+
matchesNullAppropriately() {
33+
assertMatches("didn't match", blankOrNullString(), null);
34+
assertDoesNotMatch("matched unexpectedly", blankString(), null);
35+
}
36+
37+
@Test public void
38+
matchesBlankStringAppropriately() {
39+
assertMatches("didn't match", blankString(), " \t");
40+
assertMatches("didn't match", blankOrNullString(), " \t");
41+
}
42+
43+
@Test public void
44+
doesNotMatchFilledString() {
45+
assertDoesNotMatch("matched unexpectedly", blankString(), "a");
46+
assertDoesNotMatch("matched unexpectedly", blankOrNullString(), "a");
47+
}
48+
49+
@Test public void
50+
describesItself() {
51+
assertDescription("a blank string", blankString());
52+
assertDescription("(null or a blank string)", blankOrNullString());
53+
}
54+
55+
@Test public void
56+
describesAMismatch() {
57+
assertMismatchDescription("was \"a\"", blankString(), "a");
58+
assertMismatchDescription("was \"a\"", blankOrNullString(), "a");
59+
}
60+
}

‎hamcrest-library/src/test/java/org/hamcrest/text/IsEmptyStringTest.java‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import static org.hamcrest.AbstractMatcherTest.assertUnknownTypeSafe;
99
import static org.hamcrest.text.IsEmptyString.emptyOrNullString;
1010
import static org.hamcrest.text.IsEmptyString.emptyString;
11-
import static org.hamcrest.text.IsEmptyString.blankString;
1211

1312
import org.hamcrest.Matcher;
1413
import org.junit.Test;
@@ -17,7 +16,7 @@ public final class IsEmptyStringTest {
1716

1817
@Test public void
1918
copesWithNullsAndUnknownTypes() {
20-
Matcher<String> matcher = emptyOrNullString();
19+
Matcher<String> matcher = emptyString();
2120

2221
assertNullSafe(matcher);
2322
assertUnknownTypeSafe(matcher);
@@ -27,41 +26,35 @@ public final class IsEmptyStringTest {
2726
matchesEmptyString() {
2827
assertMatches("didn't match", emptyOrNullString(), "");
2928
assertMatches("didn't match", emptyString(), "");
30-
assertMatches("didn't match", blankString(), "");
3129
}
3230

3331
@Test public void
3432
matchesNullAppropriately() {
3533
assertMatches("didn't match", emptyOrNullString(), null);
3634
assertDoesNotMatch("matched unexpectedly", emptyString(), null);
37-
assertDoesNotMatch("matched unexpectedly", blankString(), null);
3835
}
3936

4037
@Test public void
4138
matchesBlankStringAppropriately() {
42-
assertMatches("didn't match", blankString(), " ");
4339
assertDoesNotMatch("matched unexpectedly", emptyString(), " ");
4440
assertDoesNotMatch("matched unexpectedly", emptyOrNullString(), " ");
4541
}
4642

4743
@Test public void
4844
doesNotMatchFilledString() {
4945
assertDoesNotMatch("matched unexpectedly", emptyString(), "a");
50-
assertDoesNotMatch("matched unexpectedly", blankString(), "a");
5146
assertDoesNotMatch("matched unexpectedly", emptyOrNullString(), "a");
5247
}
5348

5449
@Test public void
5550
describesItself() {
5651
assertDescription("an empty string", emptyString());
57-
assertDescription("a blank string", blankString());
5852
assertDescription("(null or an empty string)", emptyOrNullString());
5953
}
6054

6155
@Test public void
6256
describesAMismatch() {
6357
assertMismatchDescription("was \"a\"", emptyString(), "a");
64-
assertMismatchDescription("was \"a\"", blankString(), "a");
6558
assertMismatchDescription("was \"a\"", emptyOrNullString(), "a");
6659
}
6760
}

0 commit comments

Comments
 (0)