From 2a6073d8684133b891a368cd754f9a823b10900b Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Mon, 12 Sep 2022 09:10:53 +0200 Subject: [PATCH] isEmpty(String) must not return false for whitespace-only values This closes #214 --- .../org/codehaus/plexus/util/StringUtils.java | 17 ++++++----------- .../codehaus/plexus/util/StringUtilsTest.java | 12 +++++++++++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/util/StringUtils.java b/src/main/java/org/codehaus/plexus/util/StringUtils.java index 2e6a2f3f..685a683e 100644 --- a/src/main/java/org/codehaus/plexus/util/StringUtils.java +++ b/src/main/java/org/codehaus/plexus/util/StringUtils.java @@ -156,9 +156,7 @@ public static String deleteWhitespace( String str ) } /** - *

* Checks if a String is non null and is not empty (length > 0). - *

* * @param str the String to check * @return true if the String is non-null, and not length zero @@ -169,21 +167,18 @@ public static boolean isNotEmpty( String str ) } /** + * Checks if a String is null or empty. *

- * Checks if a (trimmed) String is null or empty. - *

- *

- * Note: 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. - *

+ * Note: 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 true if the String is null, or length zero once trimmed + * @return true if the String is null, or length zero */ public static boolean isEmpty( String str ) { - return ( ( str == null ) || ( str.trim().isEmpty() ) ); + return ( ( str == null ) || ( str.isEmpty() ) ); } /** diff --git a/src/test/java/org/codehaus/plexus/util/StringUtilsTest.java b/src/test/java/org/codehaus/plexus/util/StringUtilsTest.java index acabdb9b..b7bd6d72 100644 --- a/src/test/java/org/codehaus/plexus/util/StringUtilsTest.java +++ b/src/test/java/org/codehaus/plexus/util/StringUtilsTest.java @@ -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 " ) ); } @@ -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 " ) ); + } + /** *

testIsBlank.

*/