File tree Expand file tree Collapse file tree
hamcrest-library/src/main/java/org/hamcrest/text
hamcrest-unit-test/src/main/java/org/hamcrest Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+
2+ package org .hamcrest .text ;
3+
4+ import org .hamcrest .BaseMatcher ;
5+ import org .hamcrest .Description ;
6+ import org .hamcrest .Factory ;
7+ import org .hamcrest .Matcher ;
8+
9+ /**
10+ * Matches empty Strings (and null).
11+ */
12+ public final class IsEmptyOrNullString extends BaseMatcher <String > {
13+ private static final IsEmptyOrNullString INSTANCE = new IsEmptyOrNullString ();
14+
15+ public boolean matches (Object item ) {
16+ return item == null || ((item instanceof String ) && ((String ) item ).equals ("" ));
17+ }
18+
19+ public void describeTo (Description description ) {
20+ description .appendText ("an empty string or null" );
21+ }
22+
23+ /**
24+ * Matches if value is null or zero-length string.
25+ */
26+ @ Factory
27+ public static Matcher <String > isEmptyString () {
28+ return INSTANCE ;
29+ }
30+
31+ /**
32+ * Matches if value is null or zero-length string.
33+ */
34+ @ Factory
35+ public static Matcher <String > isEmptyOrNullString () {
36+ return INSTANCE ;
37+ }
38+ }
Original file line number Diff line number Diff line change @@ -38,6 +38,12 @@ public void testCopesWithUnknownTypes() {
3838 createMatcher ().matches (new UnknownType ());
3939 }
4040
41+ public void testDoesNotHaveAnEmptyDescription () throws Exception {
42+ Description description = new StringDescription ();
43+ description .appendDescriptionOf (createMatcher ());
44+ assertFalse (description .toString ().length () == 0 );
45+ }
46+
4147 public static class UnknownType {
4248 }
4349
Original file line number Diff line number Diff line change 1+ package org .hamcrest .text ;
2+
3+ import org .hamcrest .AbstractMatcherTest ;
4+ import org .hamcrest .Matcher ;
5+
6+ import static org .hamcrest .text .IsEmptyOrNullString .isEmptyString ;
7+
8+ public class IsEmptyOrNullStringTest extends AbstractMatcherTest {
9+
10+ @ Override
11+ protected Matcher <?> createMatcher () {
12+ return IsEmptyOrNullString .isEmptyOrNullString ();
13+ }
14+
15+ public void testMatchesNull () {
16+ assertMatches ("null" , isEmptyString (), null );
17+ }
18+
19+ public void testMatchesEmptyString () {
20+ assertMatches ("empty string" , isEmptyString (), "" );
21+ }
22+
23+ public void testDoesNotMatchNonEmptyString () {
24+ assertDoesNotMatch ("non empty string" , isEmptyString (), "a" );
25+ }
26+
27+ public void testHasAReadableDescription () {
28+ assertDescription ("an empty string or null" , isEmptyString ());
29+ }
30+ }
Original file line number Diff line number Diff line change 3939 <!-- Text -->
4040 <factory class =" org.hamcrest.text.IsEqualIgnoringCase" />
4141 <factory class =" org.hamcrest.text.IsEqualIgnoringWhiteSpace" />
42+ <factory class =" org.hamcrest.text.IsEmptyOrNullString" />
4243 <factory class =" org.hamcrest.text.StringContains" />
4344 <factory class =" org.hamcrest.text.StringEndsWith" />
4445 <factory class =" org.hamcrest.text.StringStartsWith" />
You can’t perform that action at this time.
0 commit comments