Skip to content

Commit

Permalink
[LANG-1682] Javadoc and test: Use Strings.CI.startsWithAny method ins…
Browse files Browse the repository at this point in the history
…tead (#1299)

* test: add case-insensitive tests for startsWithAny method

* docs: revise javadoc to describe case-sensitivity
  • Loading branch information
ParanoidUser authored Oct 16, 2024
1 parent c330b89 commit f51f015
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
47 changes: 31 additions & 16 deletions src/main/java/org/apache/commons/lang3/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ public boolean endsWith(final CharSequence str, final CharSequence suffix) {
}

/**
* Tests if a CharSequence ends with any of the provided case-sensitive suffixes.
* Tests if a CharSequence ends with any of the provided suffixes.
*
* <p>
* Case-sensitive examples
Expand All @@ -625,10 +625,10 @@ public boolean endsWith(final CharSequence str, final CharSequence suffix) {
* </pre>
*
* @param sequence the CharSequence to check, may be null
* @param searchStrings the case-sensitive CharSequences to find, may be empty or contain {@code null}
* @see StringUtils#endsWith(CharSequence, CharSequence)
* @param searchStrings the CharSequence suffixes to find, may be empty or contain {@code null}
* @see Strings#endsWith(CharSequence, CharSequence)
* @return {@code true} if the input {@code sequence} is {@code null} AND no {@code searchStrings} are provided, or the input {@code sequence} ends in any
* of the provided case-sensitive {@code searchStrings}.
* of the provided {@code searchStrings}.
*/
public boolean endsWithAny(final CharSequence sequence, final CharSequence... searchStrings) {
if (StringUtils.isEmpty(sequence) || ArrayUtils.isEmpty(searchStrings)) {
Expand Down Expand Up @@ -1412,28 +1412,43 @@ public boolean startsWith(final CharSequence str, final CharSequence prefix) {
}

/**
* Tests if a CharSequence starts with any of the provided case-sensitive prefixes.
* Tests if a CharSequence starts with any of the provided prefixes.
*
* <p>
* Case-sensitive examples
* </p>
*
* <pre>
* StringUtils.startsWithAny(null, null) = false
* StringUtils.startsWithAny(null, new String[] {"abc"}) = false
* StringUtils.startsWithAny("abcxyz", null) = false
* StringUtils.startsWithAny("abcxyz", new String[] {""}) = true
* StringUtils.startsWithAny("abcxyz", new String[] {"abc"}) = true
* StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
* StringUtils.startsWithAny("abcxyz", null, "xyz", "ABCX") = false
* StringUtils.startsWithAny("ABCXYZ", null, "xyz", "abc") = false
* Strings.CS.startsWithAny(null, null) = false
* Strings.CS.startsWithAny(null, new String[] {"abc"}) = false
* Strings.CS.startsWithAny("abcxyz", null) = false
* Strings.CS.startsWithAny("abcxyz", new String[] {""}) = true
* Strings.CS.startsWithAny("abcxyz", new String[] {"abc"}) = true
* Strings.CS.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
* Strings.CS.startsWithAny("abcxyz", null, "xyz", "ABCX") = false
* Strings.CS.startsWithAny("ABCXYZ", null, "xyz", "abc") = false
* </pre>
*
* <p>
* Case-insensitive examples
* </p>
*
* <pre>
* Strings.CI.startsWithAny(null, null) = false
* Strings.CI.startsWithAny(null, new String[] {"aBc"}) = false
* Strings.CI.startsWithAny("AbCxYz", null) = false
* Strings.CI.startsWithAny("AbCxYz", new String[] {""}) = true
* Strings.CI.startsWithAny("AbCxYz", new String[] {"aBc"}) = true
* Strings.CI.startsWithAny("AbCxYz", new String[] {null, "XyZ", "aBc"}) = true
* Strings.CI.startsWithAny("abcxyz", null, "xyz", "ABCX") = true
* Strings.CI.startsWithAny("ABCXYZ", null, "xyz", "abc") = true
* </pre>
*
* @param sequence the CharSequence to check, may be null
* @param searchStrings the case-sensitive CharSequence prefixes, may be empty or contain {@code null}
* @see StringUtils#startsWith(CharSequence, CharSequence)
* @param searchStrings the CharSequence prefixes, may be empty or contain {@code null}
* @see Strings#startsWith(CharSequence, CharSequence)
* @return {@code true} if the input {@code sequence} is {@code null} AND no {@code searchStrings} are provided, or the input {@code sequence} begins with
* any of the provided case-sensitive {@code searchStrings}.
* any of the provided {@code searchStrings}.
*/
public boolean startsWithAny(final CharSequence sequence, final CharSequence... searchStrings) {
if (StringUtils.isEmpty(sequence) || ArrayUtils.isEmpty(searchStrings)) {
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/apache/commons/lang3/StringsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ public void testCaseSensitiveConstant() {
assertTrue(Strings.CS.isCaseSensitive());
}

/**
* Expanding the existing test group {@link StringUtilsStartsEndsWithTest#testStartsWithAny()} to include case-insensitive cases
*/
@Test
public void testCaseInsensitiveStartsWithAny() {
// LANG-1682
assertFalse(Strings.CI.startsWithAny(null, (String[]) null));
assertFalse(Strings.CI.startsWithAny(null, "aBc"));
assertFalse(Strings.CI.startsWithAny("AbCxYz", (String[]) null));
assertFalse(Strings.CI.startsWithAny("AbCxYz"));
assertTrue(Strings.CI.startsWithAny("AbCxYz", "aBc"));
assertTrue(Strings.CI.startsWithAny("AbCxYz", null, "XyZ", "aBc"));
assertFalse(Strings.CI.startsWithAny("AbCxYz", null, "XyZ", "aBcD"));
assertTrue(Strings.CI.startsWithAny("AbCxYz", ""));
assertTrue(Strings.CI.startsWithAny("abcxyz", null, "XyZ", "ABCX"));
assertTrue(Strings.CI.startsWithAny("ABCXYZ", null, "XyZ", "abc"));

assertTrue(Strings.CI.startsWithAny("AbCxYz", new StringBuilder("XyZ"), new StringBuffer("aBc")));
assertTrue(Strings.CI.startsWithAny(new StringBuffer("AbCxYz"), new StringBuilder("XyZ"), new StringBuffer("abc")));
}

@ParameterizedTest
@MethodSource("stringsFactory")
public void testEqualsStrings(final Strings strings) {
Expand Down

0 comments on commit f51f015

Please sign in to comment.