Skip to content

Commit

Permalink
isEmpty(String) must not return false for whitespace-only values
Browse files Browse the repository at this point in the history
This closes #214
  • Loading branch information
kwin committed Sep 12, 2022
1 parent 8a9147a commit 2a6073d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
17 changes: 6 additions & 11 deletions src/main/java/org/codehaus/plexus/util/StringUtils.java
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 whitespace.
*
* @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
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

0 comments on commit 2a6073d

Please sign in to comment.