From 6e78b38f055516bfef2b7b1e2b83e96a7a67f2ec Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 26 Sep 2024 11:01:53 -0400 Subject: [PATCH] Avoid JDK-8015417 in Strings.equals() --- .../org/apache/commons/lang3/Strings.java | 13 ++++++++-- .../org/apache/commons/lang3/StringsTest.java | 26 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/Strings.java b/src/main/java/org/apache/commons/lang3/Strings.java index fa3814f17a5..d89fbae90d2 100644 --- a/src/main/java/org/apache/commons/lang3/Strings.java +++ b/src/main/java/org/apache/commons/lang3/Strings.java @@ -140,7 +140,7 @@ public boolean equals(final CharSequence cs1, final CharSequence cs2) { @Override public boolean equals(final String s1, final String s2) { - return s1.equalsIgnoreCase(s2); + return s1 == null ? s2 == null : s1.equalsIgnoreCase(s2); } @Override @@ -247,7 +247,7 @@ public boolean equals(final CharSequence cs1, final CharSequence cs2) { @Override public boolean equals(final String s1, final String s2) { - return s1.equals(s2); + return eq(s1, s2); } @Override @@ -305,6 +305,15 @@ private static boolean containsAny(final ToBooleanBiFunction stringsFactory() { + return Stream.of(Strings.CS, Strings.CI); + } + @Test public void testBuilder() { assertTrue(Strings.builder().setIgnoreCase(false).get().isCaseSensitive()); @@ -54,4 +62,22 @@ public void testCaseSensitiveConstant() { assertNotNull(Strings.CS); assertTrue(Strings.CS.isCaseSensitive()); } + + @ParameterizedTest + @MethodSource("stringsFactory") + public void testEqualsStrings(final Strings strings) { + final String nullStr = null; + assertTrue(strings.equals(nullStr, nullStr)); + assertFalse(strings.equals(nullStr, "")); + assertFalse(strings.equals("", nullStr)); + } + + @ParameterizedTest + @MethodSource("stringsFactory") + public void testEqualsCharSequence(final Strings strings) { + final CharSequence nullCharSequence = null; + assertTrue(strings.equals(nullCharSequence, nullCharSequence)); + assertFalse(strings.equals(nullCharSequence, "")); + assertFalse(strings.equals("", nullCharSequence)); + } }