Skip to content

Commit bc7f1f9

Browse files
committed
Enhance the IsEmptyString matcher to match blank strings too.
1 parent a385b93 commit bc7f1f9

2 files changed

Lines changed: 52 additions & 12 deletions

File tree

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

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

7+
import java.util.regex.Pattern;
8+
79
import org.hamcrest.BaseMatcher;
810
import org.hamcrest.Description;
911
import org.hamcrest.Factory;
1012
import org.hamcrest.Matcher;
1113

1214
/**
13-
* Matches empty Strings (and null).
15+
* Provides matchers that match null, empty, or blank Strings, or combinations thereof.
1416
*/
1517
public final class IsEmptyString extends BaseMatcher<String> {
16-
private static final IsEmptyString INSTANCE = new IsEmptyString();
18+
private static final IsEmptyString EMPTY_INSTANCE = new IsEmptyString(false);
19+
private static final IsEmptyString BLANK_INSTANCE = new IsEmptyString(true);
1720
@SuppressWarnings("unchecked")
18-
private static final Matcher<String> NULL_OR_EMPTY_INSTANCE = anyOf(nullValue(), INSTANCE);
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;
26+
27+
private IsEmptyString(boolean allowWhitespace) {
28+
this.allowWhitespace = allowWhitespace;
29+
}
1930

2031
@Override
2132
public boolean matches(Object item) {
22-
return item != null && item instanceof String && ((String) item).equals("");
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("");
2338
}
2439

2540
@Override
2641
public void describeTo(Description description) {
27-
description.appendText("an empty string");
42+
description.appendText(allowWhitespace ? "a blank" : "an empty").appendText(" string");
2843
}
2944

3045
/**
@@ -40,7 +55,7 @@ public void describeTo(Description description) {
4055
public static Matcher<String> isEmptyString() {
4156
return emptyString();
4257
}
43-
58+
4459
/**
4560
* Creates a matcher of {@link String} that matches when the examined string has zero length.
4661
* <p/>
@@ -50,7 +65,19 @@ public static Matcher<String> isEmptyString() {
5065
*/
5166
@Factory
5267
public static Matcher<String> emptyString() {
53-
return INSTANCE;
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;
5481
}
5582

5683
/**
@@ -68,7 +95,7 @@ public static Matcher<String> emptyString() {
6895
public static Matcher<String> isEmptyOrNullString() {
6996
return emptyOrNullString();
7097
}
71-
98+
7299
/**
73100
* Creates a matcher of {@link String} that matches when the examined string is <code>null</code>, or
74101
* has zero length.

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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;
1112

1213
import org.hamcrest.Matcher;
1314
import org.junit.Test;
@@ -22,33 +23,45 @@ public final class IsEmptyStringTest {
2223
assertUnknownTypeSafe(matcher);
2324
}
2425

26+
@Test public void
27+
matchesEmptyString() {
28+
assertMatches("didn't match", emptyOrNullString(), "");
29+
assertMatches("didn't match", emptyString(), "");
30+
assertMatches("didn't match", blankString(), "");
31+
}
32+
2533
@Test public void
2634
matchesNullAppropriately() {
2735
assertMatches("didn't match", emptyOrNullString(), null);
2836
assertDoesNotMatch("matched unexpectedly", emptyString(), null);
37+
assertDoesNotMatch("matched unexpectedly", blankString(), null);
2938
}
3039

3140
@Test public void
32-
matchesEmptyString() {
33-
assertMatches("didn't match", emptyOrNullString(), "");
34-
assertMatches("didn't match", emptyString(), "");
41+
matchesBlankStringAppropriately() {
42+
assertMatches("didn't match", blankString(), " ");
43+
assertDoesNotMatch("matched unexpectedly", emptyString(), " ");
44+
assertDoesNotMatch("matched unexpectedly", emptyOrNullString(), " ");
3545
}
3646

3747
@Test public void
3848
doesNotmatchFilledString() {
39-
assertDoesNotMatch("matched unexpectedly", emptyOrNullString(), "a");
4049
assertDoesNotMatch("matched unexpectedly", emptyString(), "a");
50+
assertDoesNotMatch("matched unexpectedly", blankString(), "a");
51+
assertDoesNotMatch("matched unexpectedly", emptyOrNullString(), "a");
4152
}
4253

4354
@Test public void
4455
describesItself() {
4556
assertDescription("an empty string", emptyString());
57+
assertDescription("a blank string", blankString());
4658
assertDescription("(null or an empty string)", emptyOrNullString());
4759
}
4860

4961
@Test public void
5062
describesAMismatch() {
5163
assertMismatchDescription("was \"a\"", emptyString(), "a");
64+
assertMismatchDescription("was \"a\"", blankString(), "a");
5265
assertMismatchDescription("was \"a\"", emptyOrNullString(), "a");
5366
}
5467
}

0 commit comments

Comments
 (0)