Skip to content

isEmpty(String) must not return false for whitespace-only values #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions src/main/java/org/codehaus/plexus/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ public static String deleteWhitespace( String str )
}

/**
* <p>
* Checks if a String is non <code>null</code> and is not empty (<code>length &gt; 0</code>).
* </p>
*
* @param str the String to check
* @return true if the String is non-null, and not length zero
Expand All @@ -169,21 +167,18 @@ public static boolean isNotEmpty( String str )
}

/**
* Checks if a String is <code>null</code> or empty.
* <p>
* Checks if a (trimmed) String is <code>null</code> or empty.
* </p>
* <p>
* <strong>Note:</strong> In future releases, this method will no longer trim the input string such that it works
* complementary to {@link #isNotEmpty(String)}. Code that wants to test for whitespace-only strings should be
* migrated to use {@link #isBlank(String)} instead.
* </p>
* <strong>Note:</strong> In releases prior 3.5.0, this method trimmed the input string such that it worked
* the same as {@link #isBlank(String)}. Since release 3.5.0 it no longer returns {@code true} for strings
* containing only whitespace characters.
*
* @param str the String to check
* @return <code>true</code> if the String is <code>null</code>, or length zero once trimmed
* @return <code>true</code> if the String is <code>null</code>, or length zero
*/
public static boolean isEmpty( String str )
{
return ( ( str == null ) || ( str.trim().isEmpty() ) );
return ( ( str == null ) || ( str.isEmpty() ) );
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/test/java/org/codehaus/plexus/util/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testIsEmpty()
{
assertEquals( true, StringUtils.isEmpty( null ) );
assertEquals( true, StringUtils.isEmpty( "" ) );
assertEquals( true, StringUtils.isEmpty( " " ) );
assertEquals( false, StringUtils.isEmpty( " " ) );
assertEquals( false, StringUtils.isEmpty( "foo" ) );
assertEquals( false, StringUtils.isEmpty( " foo " ) );
}
Expand All @@ -61,6 +61,16 @@ public void testIsNotEmpty()
assertEquals( true, StringUtils.isNotEmpty( " foo " ) );
}

@Test
public void testIsNotEmptyNegatesIsEmpty()
{
assertEquals( !StringUtils.isEmpty( null ), StringUtils.isNotEmpty( null ) );
assertEquals( !StringUtils.isEmpty( "" ), StringUtils.isNotEmpty( "" ) );
assertEquals( !StringUtils.isEmpty( " " ), StringUtils.isNotEmpty( " " ) );
assertEquals( !StringUtils.isEmpty( "foo" ), StringUtils.isNotEmpty( "foo" ) );
assertEquals( !StringUtils.isEmpty( " foo " ), StringUtils.isNotEmpty( " foo " ) );
}

/**
* <p>testIsBlank.</p>
*/
Expand Down