[LANG-1682] Javadoc and test: Use Strings.CI.startsWithAny method instead (#1299)

* test: add case-insensitive tests for startsWithAny method

* docs: revise javadoc to describe case-sensitivity
This commit is contained in:
Capt. Cutlass 2024-10-16 13:34:39 -04:00 committed by GitHub
parent c330b890b9
commit f51f015e3d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 16 deletions

View File

@ -607,7 +607,7 @@ public abstract class Strings {
}
/**
* 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
@ -625,10 +625,10 @@ public abstract class Strings {
* </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)) {
@ -1412,28 +1412,43 @@ public abstract class Strings {
}
/**
* 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)) {

View File

@ -63,6 +63,27 @@ public class StringsTest {
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) {