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)); + } }