From 4f5dea0d694951660956df66639bf1baa450f171 Mon Sep 17 00:00:00 2001 From: MSaifAsif Date: Thu, 26 Sep 2024 16:21:27 +0200 Subject: [PATCH 1/2] LANG-1752 seperated test cases of Strings from StringUtils --- .../lang3/StringUtilsContainsTest.java | 131 ------ .../lang3/StringUtilsEqualsIndexOfTest.java | 302 ------------- .../lang3/StringUtilsStartsEndsWithTest.java | 113 ----- .../commons/lang3/StringsContainsTest.java | 173 ++++++++ .../lang3/StringsEqualsIndexOfTest.java | 407 ++++++++++++++++++ .../lang3/StringsStartsEndsWithTest.java | 150 +++++++ 6 files changed, 730 insertions(+), 546 deletions(-) create mode 100644 src/test/java/org/apache/commons/lang3/StringsContainsTest.java create mode 100644 src/test/java/org/apache/commons/lang3/StringsEqualsIndexOfTest.java create mode 100644 src/test/java/org/apache/commons/lang3/StringsStartsEndsWithTest.java diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsContainsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsContainsTest.java index 063085327d5..12fd98f425f 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsContainsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsContainsTest.java @@ -24,10 +24,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.Locale; - import org.junit.jupiter.api.Test; -import org.junitpioneer.jupiter.DefaultLocale; /** * Unit tests {@link StringUtils} - Contains methods @@ -37,28 +34,12 @@ public class StringUtilsContainsTest extends AbstractLangTest { public void testContains_Char() { assertFalse(StringUtils.contains(null, ' ')); assertFalse(StringUtils.contains("", ' ')); - assertFalse(StringUtils.contains("", null)); - assertFalse(StringUtils.contains(null, null)); assertTrue(StringUtils.contains("abc", 'a')); assertTrue(StringUtils.contains("abc", 'b')); assertTrue(StringUtils.contains("abc", 'c')); assertFalse(StringUtils.contains("abc", 'z')); } - @Test - public void testContains_String() { - assertFalse(StringUtils.contains(null, null)); - assertFalse(StringUtils.contains(null, "")); - assertFalse(StringUtils.contains(null, "a")); - assertFalse(StringUtils.contains("", null)); - assertTrue(StringUtils.contains("", "")); - assertFalse(StringUtils.contains("", "a")); - assertTrue(StringUtils.contains("abc", "a")); - assertTrue(StringUtils.contains("abc", "b")); - assertTrue(StringUtils.contains("abc", "c")); - assertTrue(StringUtils.contains("abc", "abc")); - assertFalse(StringUtils.contains("abc", "z")); - } /** * See https://www.oracle.com/technical-resources/articles/javase/supplementary.html @@ -66,25 +47,9 @@ public void testContains_String() { @Test public void testContains_StringWithBadSupplementaryChars() { // Test edge case: 1/2 of a (broken) supplementary char - assertFalse(StringUtils.contains(CharUSuppCharHigh, CharU20001)); - assertFalse(StringUtils.contains(CharUSuppCharLow, CharU20001)); - assertFalse(StringUtils.contains(CharU20001, CharUSuppCharHigh)); assertEquals(0, CharU20001.indexOf(CharUSuppCharLow)); - assertTrue(StringUtils.contains(CharU20001, CharUSuppCharLow)); - assertTrue(StringUtils.contains(CharU20001 + CharUSuppCharLow + "a", "a")); - assertTrue(StringUtils.contains(CharU20001 + CharUSuppCharHigh + "a", "a")); } - /** - * See https://www.oracle.com/technical-resources/articles/javase/supplementary.html - */ - @Test - public void testContains_StringWithSupplementaryChars() { - assertTrue(StringUtils.contains(CharU20000 + CharU20001, CharU20000)); - assertTrue(StringUtils.contains(CharU20000 + CharU20001, CharU20001)); - assertTrue(StringUtils.contains(CharU20000, CharU20000)); - assertFalse(StringUtils.contains(CharU20000, CharU20001)); - } @Test public void testContainsAny_StringCharArray() { @@ -157,27 +122,6 @@ public void testContainsAny_StringString() { assertFalse(StringUtils.containsAny("ab", "z")); } - @Test - public void testContainsAny_StringStringArray() { - assertFalse(StringUtils.containsAny(null, (String[]) null)); - assertFalse(StringUtils.containsAny(null, new String[0])); - assertFalse(StringUtils.containsAny(null, new String[] { "hello" })); - assertFalse(StringUtils.containsAny("", (String[]) null)); - assertFalse(StringUtils.containsAny("", new String[0])); - assertFalse(StringUtils.containsAny("", new String[] { "hello" })); - assertFalse(StringUtils.containsAny("hello, goodbye", (String[]) null)); - assertFalse(StringUtils.containsAny("hello, goodbye", new String[0])); - assertTrue(StringUtils.containsAny("hello, goodbye", new String[] { "hello", "goodbye" })); - assertTrue(StringUtils.containsAny("hello, goodbye", new String[] { "hello", "Goodbye" })); - assertFalse(StringUtils.containsAny("hello, goodbye", new String[] { "Hello", "Goodbye" })); - assertFalse(StringUtils.containsAny("hello, goodbye", new String[] { "Hello", null })); - assertFalse(StringUtils.containsAny("hello, null", new String[] { "Hello", null })); - // Javadoc examples: - assertTrue(StringUtils.containsAny("abcd", "ab", null)); - assertTrue(StringUtils.containsAny("abcd", "ab", "cd")); - assertTrue(StringUtils.containsAny("abc", "d", "abc")); - } - /** * See https://www.oracle.com/technical-resources/articles/javase/supplementary.html */ @@ -209,81 +153,6 @@ public void testContainsAny_StringWithSupplementaryChars() { assertFalse(StringUtils.containsAny(CharU20001, CharU20000)); } - @Test - public void testContainsAnyIgnoreCase_StringStringArray() { - assertFalse(StringUtils.containsAnyIgnoreCase(null, (String[]) null)); - assertFalse(StringUtils.containsAnyIgnoreCase(null, new String[0])); - assertFalse(StringUtils.containsAnyIgnoreCase(null, new String[] { "hello" })); - assertFalse(StringUtils.containsAnyIgnoreCase("", (String[]) null)); - assertFalse(StringUtils.containsAnyIgnoreCase("", new String[0])); - assertFalse(StringUtils.containsAnyIgnoreCase("", new String[] { "hello" })); - assertFalse(StringUtils.containsAnyIgnoreCase("hello, goodbye", (String[]) null)); - assertFalse(StringUtils.containsAnyIgnoreCase("hello, goodbye", new String[0])); - assertTrue(StringUtils.containsAnyIgnoreCase("hello, goodbye", new String[] { "hello", "goodbye" })); - assertTrue(StringUtils.containsAnyIgnoreCase("hello, goodbye", new String[] { "hello", "Goodbye" })); - assertTrue(StringUtils.containsAnyIgnoreCase("hello, goodbye", new String[] { "Hello", "Goodbye" })); - assertTrue(StringUtils.containsAnyIgnoreCase("hello, goodbye", new String[] { "Hello", null })); - assertTrue(StringUtils.containsAnyIgnoreCase("hello, null", new String[] { "Hello", null })); - // Javadoc examples: - assertTrue(StringUtils.containsAnyIgnoreCase("abcd", "ab", null)); - assertTrue(StringUtils.containsAnyIgnoreCase("abcd", "ab", "cd")); - assertTrue(StringUtils.containsAnyIgnoreCase("abc", "d", "abc")); - } - - @DefaultLocale(language = "de", country = "DE") - @Test - public void testContainsIgnoreCase_LocaleIndependence() { - final Locale[] locales = { Locale.ENGLISH, new Locale("tr"), Locale.getDefault() }; - - final String[][] tdata = { { "i", "I" }, { "I", "i" }, { "\u03C2", "\u03C3" }, { "\u03A3", "\u03C2" }, { "\u03A3", "\u03C3" }, }; - - final String[][] fdata = { { "\u00DF", "SS" }, }; - - for (final Locale testLocale : locales) { - Locale.setDefault(testLocale); - for (int j = 0; j < tdata.length; j++) { - assertTrue(StringUtils.containsIgnoreCase(tdata[j][0], tdata[j][1]), Locale.getDefault() + ": " + j + " " + tdata[j][0] + " " + tdata[j][1]); - } - for (int j = 0; j < fdata.length; j++) { - assertFalse(StringUtils.containsIgnoreCase(fdata[j][0], fdata[j][1]), Locale.getDefault() + ": " + j + " " + fdata[j][0] + " " + fdata[j][1]); - } - } - } - - @Test - public void testContainsIgnoreCase_StringString() { - assertFalse(StringUtils.containsIgnoreCase(null, null)); - - // Null tests - assertFalse(StringUtils.containsIgnoreCase(null, "")); - assertFalse(StringUtils.containsIgnoreCase(null, "a")); - assertFalse(StringUtils.containsIgnoreCase(null, "abc")); - - assertFalse(StringUtils.containsIgnoreCase("", null)); - assertFalse(StringUtils.containsIgnoreCase("a", null)); - assertFalse(StringUtils.containsIgnoreCase("abc", null)); - - // Match len = 0 - assertTrue(StringUtils.containsIgnoreCase("", "")); - assertTrue(StringUtils.containsIgnoreCase("a", "")); - assertTrue(StringUtils.containsIgnoreCase("abc", "")); - - // Match len = 1 - assertFalse(StringUtils.containsIgnoreCase("", "a")); - assertTrue(StringUtils.containsIgnoreCase("a", "a")); - assertTrue(StringUtils.containsIgnoreCase("abc", "a")); - assertFalse(StringUtils.containsIgnoreCase("", "A")); - assertTrue(StringUtils.containsIgnoreCase("a", "A")); - assertTrue(StringUtils.containsIgnoreCase("abc", "A")); - - // Match len > 1 - assertFalse(StringUtils.containsIgnoreCase("", "abc")); - assertFalse(StringUtils.containsIgnoreCase("a", "abc")); - assertTrue(StringUtils.containsIgnoreCase("xabcz", "abc")); - assertFalse(StringUtils.containsIgnoreCase("", "ABC")); - assertFalse(StringUtils.containsIgnoreCase("a", "ABC")); - assertTrue(StringUtils.containsIgnoreCase("xabcz", "ABC")); - } @Test public void testContainsNone_CharArray() { diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java index 6101ea8d2d0..f65c41dc2a1 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java @@ -20,11 +20,9 @@ import static org.apache.commons.lang3.Supplementary.CharU20001; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.nio.CharBuffer; -import java.util.Locale; import org.hamcrest.core.IsNot; import org.junit.jupiter.api.Test; @@ -88,21 +86,6 @@ public String toString() { private static final String[] FOOBAR_SUB_ARRAY = {"ob", "ba"}; - @Test - public void testCompare_StringString() { - assertEquals(0, StringUtils.compare(null, null)); - assertTrue(StringUtils.compare(null, "a") < 0); - assertTrue(StringUtils.compare("a", null) > 0); - assertEquals(0, StringUtils.compare("abc", "abc")); - assertTrue(StringUtils.compare("a", "b") < 0); - assertTrue(StringUtils.compare("b", "a") > 0); - assertTrue(StringUtils.compare("a", "B") > 0); - assertTrue(StringUtils.compare("abc", "abd") < 0); - assertTrue(StringUtils.compare("ab", "abc") < 0); - assertTrue(StringUtils.compare("ab", "ab ") < 0); - assertTrue(StringUtils.compare("abc", "ab ") > 0); - } - @Test public void testCompare_StringStringBoolean() { assertEquals(0, StringUtils.compare(null, null, false)); @@ -120,23 +103,6 @@ public void testCompare_StringStringBoolean() { assertTrue(StringUtils.compare("abc", "ab ", false) > 0); } - @Test - public void testCompareIgnoreCase_StringString() { - assertEquals(0, StringUtils.compareIgnoreCase(null, null)); - assertTrue(StringUtils.compareIgnoreCase(null, "a") < 0); - assertTrue(StringUtils.compareIgnoreCase("a", null) > 0); - assertEquals(0, StringUtils.compareIgnoreCase("abc", "abc")); - assertEquals(0, StringUtils.compareIgnoreCase("abc", "ABC")); - assertTrue(StringUtils.compareIgnoreCase("a", "b") < 0); - assertTrue(StringUtils.compareIgnoreCase("b", "a") > 0); - assertTrue(StringUtils.compareIgnoreCase("a", "B") < 0); - assertTrue(StringUtils.compareIgnoreCase("A", "b") < 0); - assertTrue(StringUtils.compareIgnoreCase("abc", "ABD") < 0); - assertTrue(StringUtils.compareIgnoreCase("ab", "ABC") < 0); - assertTrue(StringUtils.compareIgnoreCase("ab", "AB ") < 0); - assertTrue(StringUtils.compareIgnoreCase("abc", "AB ") > 0); - } - @Test public void testCompareIgnoreCase_StringStringBoolean() { assertEquals(0, StringUtils.compareIgnoreCase(null, null, false)); @@ -160,104 +126,8 @@ public void testCompareIgnoreCase_StringStringBoolean() { public void testCustomCharSequence() { assertThat(new CustomCharSequence(FOO), IsNot.not(FOO)); assertThat(FOO, IsNot.not(new CustomCharSequence(FOO))); - assertEquals(new CustomCharSequence(FOO), new CustomCharSequence(FOO)); } - @Test - public void testEquals() { - final CharSequence fooCs = new StringBuilder(FOO), barCs = new StringBuilder(BAR), foobarCs = new StringBuilder(FOOBAR); - assertTrue(StringUtils.equals(null, null)); - assertTrue(StringUtils.equals(fooCs, fooCs)); - assertTrue(StringUtils.equals(fooCs, new StringBuilder(FOO))); - assertTrue(StringUtils.equals(fooCs, new String(new char[] { 'f', 'o', 'o' }))); - assertTrue(StringUtils.equals(fooCs, new CustomCharSequence(FOO))); - assertTrue(StringUtils.equals(new CustomCharSequence(FOO), fooCs)); - assertFalse(StringUtils.equals(fooCs, new String(new char[] { 'f', 'O', 'O' }))); - assertFalse(StringUtils.equals(fooCs, barCs)); - assertFalse(StringUtils.equals(fooCs, null)); - assertFalse(StringUtils.equals(null, fooCs)); - assertFalse(StringUtils.equals(fooCs, foobarCs)); - assertFalse(StringUtils.equals(foobarCs, fooCs)); - } - - @Test - public void testEqualsAny() { - assertFalse(StringUtils.equalsAny(FOO)); - assertFalse(StringUtils.equalsAny(FOO, new String[]{})); - - assertTrue(StringUtils.equalsAny(FOO, FOO)); - assertTrue(StringUtils.equalsAny(FOO, BAR, new String(new char[] { 'f', 'o', 'o' }))); - assertFalse(StringUtils.equalsAny(FOO, BAR, new String(new char[] { 'f', 'O', 'O' }))); - assertFalse(StringUtils.equalsAny(FOO, BAR)); - assertFalse(StringUtils.equalsAny(FOO, BAR, null)); - assertFalse(StringUtils.equalsAny(null, FOO)); - assertFalse(StringUtils.equalsAny(FOO, FOOBAR)); - assertFalse(StringUtils.equalsAny(FOOBAR, FOO)); - - assertTrue(StringUtils.equalsAny(null, null, null)); - assertFalse(StringUtils.equalsAny(null, FOO, BAR, FOOBAR)); - assertFalse(StringUtils.equalsAny(FOO, null, BAR)); - assertTrue(StringUtils.equalsAny(FOO, BAR, null, "", FOO, BAR)); - assertFalse(StringUtils.equalsAny(FOO, FOO.toUpperCase(Locale.ROOT))); - - assertFalse(StringUtils.equalsAny(null, (CharSequence[]) null)); - assertTrue(StringUtils.equalsAny(FOO, new CustomCharSequence("foo"))); - assertTrue(StringUtils.equalsAny(FOO, new StringBuilder("foo"))); - assertFalse(StringUtils.equalsAny(FOO, new CustomCharSequence("fOo"))); - assertFalse(StringUtils.equalsAny(FOO, new StringBuilder("fOo"))); - } - - @Test - public void testEqualsAnyIgnoreCase() { - assertFalse(StringUtils.equalsAnyIgnoreCase(FOO)); - assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, new String[]{})); - - assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, FOO)); - assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, FOO.toUpperCase(Locale.ROOT))); - assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, FOO, new String(new char[]{'f', 'o', 'o'}))); - assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, BAR, new String(new char[]{'f', 'O', 'O'}))); - assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, BAR)); - assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, BAR, null)); - assertFalse(StringUtils.equalsAnyIgnoreCase(null, FOO)); - assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, FOOBAR)); - assertFalse(StringUtils.equalsAnyIgnoreCase(FOOBAR, FOO)); - - assertTrue(StringUtils.equalsAnyIgnoreCase(null, null, null)); - assertFalse(StringUtils.equalsAnyIgnoreCase(null, FOO, BAR, FOOBAR)); - assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, null, BAR)); - assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, BAR, null, "", FOO.toUpperCase(Locale.ROOT), BAR)); - assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, FOO.toUpperCase(Locale.ROOT))); - - assertFalse(StringUtils.equalsAnyIgnoreCase(null, (CharSequence[]) null)); - assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, new CustomCharSequence("fOo"))); - assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, new StringBuilder("fOo"))); - } - - @Test - public void testEqualsIgnoreCase() { - assertTrue(StringUtils.equalsIgnoreCase(null, null)); - assertTrue(StringUtils.equalsIgnoreCase(FOO, FOO)); - assertTrue(StringUtils.equalsIgnoreCase(FOO, new String(new char[] { 'f', 'o', 'o' }))); - assertTrue(StringUtils.equalsIgnoreCase(FOO, new String(new char[] { 'f', 'O', 'O' }))); - assertFalse(StringUtils.equalsIgnoreCase(FOO, BAR)); - assertFalse(StringUtils.equalsIgnoreCase(FOO, null)); - assertFalse(StringUtils.equalsIgnoreCase(null, FOO)); - assertTrue(StringUtils.equalsIgnoreCase("", "")); - assertFalse(StringUtils.equalsIgnoreCase("abcd", "abcd ")); - } - - @Test - public void testEqualsOnStrings() { - assertTrue(StringUtils.equals(null, null)); - assertTrue(StringUtils.equals(FOO, FOO)); - assertTrue(StringUtils.equals(FOO, new String(new char[] { 'f', 'o', 'o' }))); - assertFalse(StringUtils.equals(FOO, new String(new char[] { 'f', 'O', 'O' }))); - assertFalse(StringUtils.equals(FOO, BAR)); - assertFalse(StringUtils.equals(FOO, null)); - assertFalse(StringUtils.equals(null, FOO)); - assertFalse(StringUtils.equals(FOO, FOOBAR)); - assertFalse(StringUtils.equals(FOOBAR, FOO)); - } @Test public void testIndexOf_char() { @@ -306,50 +176,6 @@ public void testIndexOf_charInt() { assertEquals(-1, StringUtils.indexOf(builder.toString(), CODE_POINT, 2)); } - @Test - public void testIndexOf_String() { - assertEquals(-1, StringUtils.indexOf(null, null)); - assertEquals(-1, StringUtils.indexOf("", null)); - assertEquals(0, StringUtils.indexOf("", "")); - assertEquals(0, StringUtils.indexOf("aabaabaa", "a")); - assertEquals(2, StringUtils.indexOf("aabaabaa", "b")); - assertEquals(1, StringUtils.indexOf("aabaabaa", "ab")); - assertEquals(0, StringUtils.indexOf("aabaabaa", "")); - - assertEquals(2, StringUtils.indexOf(new StringBuilder("aabaabaa"), "b")); - } - - @Test - public void testIndexOf_StringInt() { - assertEquals(-1, StringUtils.indexOf(null, null, 0)); - assertEquals(-1, StringUtils.indexOf(null, null, -1)); - assertEquals(-1, StringUtils.indexOf(null, "", 0)); - assertEquals(-1, StringUtils.indexOf(null, "", -1)); - assertEquals(-1, StringUtils.indexOf("", null, 0)); - assertEquals(-1, StringUtils.indexOf("", null, -1)); - assertEquals(0, StringUtils.indexOf("", "", 0)); - assertEquals(0, StringUtils.indexOf("", "", -1)); - assertEquals(0, StringUtils.indexOf("", "", 9)); - assertEquals(0, StringUtils.indexOf("abc", "", 0)); - assertEquals(0, StringUtils.indexOf("abc", "", -1)); - assertEquals(3, StringUtils.indexOf("abc", "", 9)); - assertEquals(3, StringUtils.indexOf("abc", "", 3)); - assertEquals(0, StringUtils.indexOf("aabaabaa", "a", 0)); - assertEquals(2, StringUtils.indexOf("aabaabaa", "b", 0)); - assertEquals(1, StringUtils.indexOf("aabaabaa", "ab", 0)); - assertEquals(5, StringUtils.indexOf("aabaabaa", "b", 3)); - assertEquals(-1, StringUtils.indexOf("aabaabaa", "b", 9)); - assertEquals(2, StringUtils.indexOf("aabaabaa", "b", -1)); - assertEquals(2, StringUtils.indexOf("aabaabaa", "", 2)); - - // Test that startIndex works correctly, i.e. cannot match before startIndex - assertEquals(7, StringUtils.indexOf("12345678", "8", 5)); - assertEquals(7, StringUtils.indexOf("12345678", "8", 6)); - assertEquals(7, StringUtils.indexOf("12345678", "8", 7)); // 7 is last index - assertEquals(-1, StringUtils.indexOf("12345678", "8", 8)); - - assertEquals(5, StringUtils.indexOf(new StringBuilder("aabaabaa"), "b", 3)); - } @Test public void testIndexOfAny_StringCharArray() { @@ -476,39 +302,6 @@ public void testIndexOfAnyBut_StringStringWithSupplementaryChars() { assertEquals(0, StringUtils.indexOfAnyBut(CharU20000, CharU20001)); } - @Test - public void testIndexOfIgnoreCase_String() { - assertEquals(-1, StringUtils.indexOfIgnoreCase(null, null)); - assertEquals(-1, StringUtils.indexOfIgnoreCase(null, "")); - assertEquals(-1, StringUtils.indexOfIgnoreCase("", null)); - assertEquals(0, StringUtils.indexOfIgnoreCase("", "")); - assertEquals(0, StringUtils.indexOfIgnoreCase("aabaabaa", "a")); - assertEquals(0, StringUtils.indexOfIgnoreCase("aabaabaa", "A")); - assertEquals(2, StringUtils.indexOfIgnoreCase("aabaabaa", "b")); - assertEquals(2, StringUtils.indexOfIgnoreCase("aabaabaa", "B")); - assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "ab")); - assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB")); - assertEquals(0, StringUtils.indexOfIgnoreCase("aabaabaa", "")); - } - - @Test - public void testIndexOfIgnoreCase_StringInt() { - assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", -1)); - assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 0)); - assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 1)); - assertEquals(4, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 2)); - assertEquals(4, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 3)); - assertEquals(4, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 4)); - assertEquals(-1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 5)); - assertEquals(-1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 6)); - assertEquals(-1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 7)); - assertEquals(-1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 8)); - assertEquals(1, StringUtils.indexOfIgnoreCase("aab", "AB", 1)); - assertEquals(5, StringUtils.indexOfIgnoreCase("aabaabaa", "", 5)); - assertEquals(-1, StringUtils.indexOfIgnoreCase("ab", "AAB", 0)); - assertEquals(-1, StringUtils.indexOfIgnoreCase("aab", "AAB", 1)); - assertEquals(-1, StringUtils.indexOfIgnoreCase("abc", "", 9)); - } @Test public void testLANG1193() { @@ -586,58 +379,6 @@ public void testLastIndexOf_charInt() { assertEquals(StringUtils.INDEX_NOT_FOUND, StringUtils.lastIndexOf(CharBuffer.wrap("[%{.c.0rro"), -1738, 982)); } - @Test - public void testLastIndexOf_String() { - assertEquals(-1, StringUtils.lastIndexOf(null, null)); - assertEquals(-1, StringUtils.lastIndexOf("", null)); - assertEquals(-1, StringUtils.lastIndexOf("", "a")); - assertEquals(0, StringUtils.lastIndexOf("", "")); - assertEquals(8, StringUtils.lastIndexOf("aabaabaa", "")); - assertEquals(7, StringUtils.lastIndexOf("aabaabaa", "a")); - assertEquals(5, StringUtils.lastIndexOf("aabaabaa", "b")); - assertEquals(4, StringUtils.lastIndexOf("aabaabaa", "ab")); - - assertEquals(4, StringUtils.lastIndexOf(new StringBuilder("aabaabaa"), "ab")); - } - - @Test - public void testLastIndexOf_StringInt() { - assertEquals(-1, StringUtils.lastIndexOf(null, null, 0)); - assertEquals(-1, StringUtils.lastIndexOf(null, null, -1)); - assertEquals(-1, StringUtils.lastIndexOf(null, "", 0)); - assertEquals(-1, StringUtils.lastIndexOf(null, "", -1)); - assertEquals(-1, StringUtils.lastIndexOf("", null, 0)); - assertEquals(-1, StringUtils.lastIndexOf("", null, -1)); - assertEquals(0, StringUtils.lastIndexOf("", "", 0)); - assertEquals(-1, StringUtils.lastIndexOf("", "", -1)); - assertEquals(0, StringUtils.lastIndexOf("", "", 9)); - assertEquals(0, StringUtils.lastIndexOf("abc", "", 0)); - assertEquals(-1, StringUtils.lastIndexOf("abc", "", -1)); - assertEquals(3, StringUtils.lastIndexOf("abc", "", 9)); - assertEquals(7, StringUtils.lastIndexOf("aabaabaa", "a", 8)); - assertEquals(5, StringUtils.lastIndexOf("aabaabaa", "b", 8)); - assertEquals(4, StringUtils.lastIndexOf("aabaabaa", "ab", 8)); - assertEquals(2, StringUtils.lastIndexOf("aabaabaa", "b", 3)); - assertEquals(5, StringUtils.lastIndexOf("aabaabaa", "b", 9)); - assertEquals(-1, StringUtils.lastIndexOf("aabaabaa", "b", -1)); - assertEquals(-1, StringUtils.lastIndexOf("aabaabaa", "b", 0)); - assertEquals(0, StringUtils.lastIndexOf("aabaabaa", "a", 0)); - assertEquals(-1, StringUtils.lastIndexOf("aabaabaa", "a", -1)); - - // Test that fromIndex works correctly, i.e. cannot match after fromIndex - assertEquals(7, StringUtils.lastIndexOf("12345678", "8", 9)); - assertEquals(7, StringUtils.lastIndexOf("12345678", "8", 8)); - assertEquals(7, StringUtils.lastIndexOf("12345678", "8", 7)); // 7 is last index - assertEquals(-1, StringUtils.lastIndexOf("12345678", "8", 6)); - - assertEquals(-1, StringUtils.lastIndexOf("aabaabaa", "b", 1)); - assertEquals(2, StringUtils.lastIndexOf("aabaabaa", "b", 2)); - assertEquals(2, StringUtils.lastIndexOf("aabaabaa", "ba", 2)); - assertEquals(2, StringUtils.lastIndexOf("aabaabaa", "ba", 3)); - - assertEquals(2, StringUtils.lastIndexOf(new StringBuilder("aabaabaa"), "b", 3)); - } - @Test public void testLastIndexOfAny_StringStringArray() { assertEquals(-1, StringUtils.lastIndexOfAny(null, (CharSequence) null)); // test both types of ... @@ -660,49 +401,6 @@ public void testLastIndexOfAny_StringStringArray() { assertEquals(-1, StringUtils.lastIndexOfAny(null, new String[] {null})); } - @Test - public void testLastIndexOfIgnoreCase_String() { - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, null)); - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", null)); - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, "")); - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", "a")); - assertEquals(0, StringUtils.lastIndexOfIgnoreCase("", "")); - assertEquals(8, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "")); - assertEquals(7, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "a")); - assertEquals(7, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A")); - assertEquals(5, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "b")); - assertEquals(5, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B")); - assertEquals(4, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "ab")); - assertEquals(4, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "AB")); - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("ab", "AAB")); - assertEquals(0, StringUtils.lastIndexOfIgnoreCase("aab", "AAB")); - } - - @Test - public void testLastIndexOfIgnoreCase_StringInt() { - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, null, 0)); - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, null, -1)); - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, "", 0)); - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, "", -1)); - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", null, 0)); - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", null, -1)); - assertEquals(0, StringUtils.lastIndexOfIgnoreCase("", "", 0)); - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", "", -1)); - assertEquals(0, StringUtils.lastIndexOfIgnoreCase("", "", 9)); - assertEquals(0, StringUtils.lastIndexOfIgnoreCase("abc", "", 0)); - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("abc", "", -1)); - assertEquals(3, StringUtils.lastIndexOfIgnoreCase("abc", "", 9)); - assertEquals(7, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 8)); - assertEquals(5, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 8)); - assertEquals(4, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "AB", 8)); - assertEquals(2, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 3)); - assertEquals(5, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 9)); - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", -1)); - assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 0)); - assertEquals(0, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 0)); - assertEquals(1, StringUtils.lastIndexOfIgnoreCase("aab", "AB", 1)); - } - @Test public void testLastOrdinalIndexOf() { assertEquals(-1, StringUtils.lastOrdinalIndexOf(null, "*", 42) ); diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java index aae687db543..5207b388e7b 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java @@ -32,36 +32,6 @@ public class StringUtilsStartsEndsWithTest extends AbstractLangTest { private static final String BAR = "BAR"; private static final String FOOBAR = "FOOBAR"; - /** - * Test StringUtils.endsWith() - */ - @Test - public void testEndsWith() { - assertTrue(StringUtils.endsWith(null, null), "endsWith(null, null)"); - assertFalse(StringUtils.endsWith(FOOBAR, null), "endsWith(FOOBAR, null)"); - assertFalse(StringUtils.endsWith(null, FOO), "endsWith(null, FOO)"); - assertTrue(StringUtils.endsWith(FOOBAR, ""), "endsWith(FOOBAR, \"\")"); - - assertFalse(StringUtils.endsWith(foobar, foo), "endsWith(foobar, foo)"); - assertFalse(StringUtils.endsWith(FOOBAR, FOO), "endsWith(FOOBAR, FOO)"); - assertFalse(StringUtils.endsWith(foobar, FOO), "endsWith(foobar, FOO)"); - assertFalse(StringUtils.endsWith(FOOBAR, foo), "endsWith(FOOBAR, foo)"); - - assertFalse(StringUtils.endsWith(foo, foobar), "endsWith(foo, foobar)"); - assertFalse(StringUtils.endsWith(bar, foobar), "endsWith(foo, foobar)"); - - assertTrue(StringUtils.endsWith(foobar, bar), "endsWith(foobar, bar)"); - assertTrue(StringUtils.endsWith(FOOBAR, BAR), "endsWith(FOOBAR, BAR)"); - assertFalse(StringUtils.endsWith(foobar, BAR), "endsWith(foobar, BAR)"); - assertFalse(StringUtils.endsWith(FOOBAR, bar), "endsWith(FOOBAR, bar)"); - - // "alpha, beta, gamma, delta".endsWith("delta") - assertTrue(StringUtils.endsWith("\u03B1\u03B2\u03B3\u03B4", "\u03B4"), - "endsWith(\u03B1\u03B2\u03B3\u03B4, \u03B4)"); - // "alpha, beta, gamma, delta".endsWith("gamma, DELTA") - assertFalse(StringUtils.endsWith("\u03B1\u03B2\u03B3\u03B4", "\u03B3\u0394"), - "endsWith(\u03B1\u03B2\u03B3\u03B4, \u03B3\u0394)"); - } @Test public void testEndsWithAny() { @@ -92,65 +62,6 @@ public void testEndsWithAny() { assertTrue(StringUtils.endsWithAny(new StringBuffer("abcxyz"), new StringBuilder("abc"), new StringBuffer("xyz")), "StringUtils.endsWithAny(StringBuffer(abcxyz), StringBuilder(abc), StringBuffer(xyz))"); } - /** - * Test StringUtils.endsWithIgnoreCase() - */ - @Test - public void testEndsWithIgnoreCase() { - assertTrue(StringUtils.endsWithIgnoreCase(null, null), "endsWithIgnoreCase(null, null)"); - assertFalse(StringUtils.endsWithIgnoreCase(FOOBAR, null), "endsWithIgnoreCase(FOOBAR, null)"); - assertFalse(StringUtils.endsWithIgnoreCase(null, FOO), "endsWithIgnoreCase(null, FOO)"); - assertTrue(StringUtils.endsWithIgnoreCase(FOOBAR, ""), "endsWithIgnoreCase(FOOBAR, \"\")"); - - assertFalse(StringUtils.endsWithIgnoreCase(foobar, foo), "endsWithIgnoreCase(foobar, foo)"); - assertFalse(StringUtils.endsWithIgnoreCase(FOOBAR, FOO), "endsWithIgnoreCase(FOOBAR, FOO)"); - assertFalse(StringUtils.endsWithIgnoreCase(foobar, FOO), "endsWithIgnoreCase(foobar, FOO)"); - assertFalse(StringUtils.endsWithIgnoreCase(FOOBAR, foo), "endsWithIgnoreCase(FOOBAR, foo)"); - - assertFalse(StringUtils.endsWithIgnoreCase(foo, foobar), "endsWithIgnoreCase(foo, foobar)"); - assertFalse(StringUtils.endsWithIgnoreCase(bar, foobar), "endsWithIgnoreCase(foo, foobar)"); - - assertTrue(StringUtils.endsWithIgnoreCase(foobar, bar), "endsWithIgnoreCase(foobar, bar)"); - assertTrue(StringUtils.endsWithIgnoreCase(FOOBAR, BAR), "endsWithIgnoreCase(FOOBAR, BAR)"); - assertTrue(StringUtils.endsWithIgnoreCase(foobar, BAR), "endsWithIgnoreCase(foobar, BAR)"); - assertTrue(StringUtils.endsWithIgnoreCase(FOOBAR, bar), "endsWithIgnoreCase(FOOBAR, bar)"); - - // javadoc - assertTrue(StringUtils.endsWithIgnoreCase("abcdef", "def")); - assertTrue(StringUtils.endsWithIgnoreCase("ABCDEF", "def")); - assertFalse(StringUtils.endsWithIgnoreCase("ABCDEF", "cde")); - - // "alpha, beta, gamma, delta".endsWith("DELTA") - assertTrue(StringUtils.endsWithIgnoreCase("\u03B1\u03B2\u03B3\u03B4", "\u0394"), - "endsWith(\u03B1\u03B2\u03B3\u03B4, \u0394)"); - // "alpha, beta, gamma, delta".endsWith("GAMMA") - assertFalse(StringUtils.endsWithIgnoreCase("\u03B1\u03B2\u03B3\u03B4", "\u0393"), - "endsWith(\u03B1\u03B2\u03B3\u03B4, \u0393)"); - } - - /** - * Test StringUtils.startsWith() - */ - @Test - public void testStartsWith() { - assertTrue(StringUtils.startsWith(null, null), "startsWith(null, null)"); - assertFalse(StringUtils.startsWith(FOOBAR, null), "startsWith(FOOBAR, null)"); - assertFalse(StringUtils.startsWith(null, FOO), "startsWith(null, FOO)"); - assertTrue(StringUtils.startsWith(FOOBAR, ""), "startsWith(FOOBAR, \"\")"); - - assertTrue(StringUtils.startsWith(foobar, foo), "startsWith(foobar, foo)"); - assertTrue(StringUtils.startsWith(FOOBAR, FOO), "startsWith(FOOBAR, FOO)"); - assertFalse(StringUtils.startsWith(foobar, FOO), "startsWith(foobar, FOO)"); - assertFalse(StringUtils.startsWith(FOOBAR, foo), "startsWith(FOOBAR, foo)"); - - assertFalse(StringUtils.startsWith(foo, foobar), "startsWith(foo, foobar)"); - assertFalse(StringUtils.startsWith(bar, foobar), "startsWith(foo, foobar)"); - - assertFalse(StringUtils.startsWith(foobar, bar), "startsWith(foobar, bar)"); - assertFalse(StringUtils.startsWith(FOOBAR, BAR), "startsWith(FOOBAR, BAR)"); - assertFalse(StringUtils.startsWith(foobar, BAR), "startsWith(foobar, BAR)"); - assertFalse(StringUtils.startsWith(FOOBAR, bar), "startsWith(FOOBAR, bar)"); - } @Test public void testStartsWithAny() { @@ -169,28 +80,4 @@ public void testStartsWithAny() { assertTrue(StringUtils.startsWithAny(new StringBuffer("abcxyz"), new StringBuilder("xyz"), new StringBuffer("abc")), "StringUtils.startsWithAny(StringBuffer(abcxyz), StringBuilder(xyz), StringBuffer(abc))"); } - /** - * Test StringUtils.testStartsWithIgnoreCase() - */ - @Test - public void testStartsWithIgnoreCase() { - assertTrue(StringUtils.startsWithIgnoreCase(null, null), "startsWithIgnoreCase(null, null)"); - assertFalse(StringUtils.startsWithIgnoreCase(FOOBAR, null), "startsWithIgnoreCase(FOOBAR, null)"); - assertFalse(StringUtils.startsWithIgnoreCase(null, FOO), "startsWithIgnoreCase(null, FOO)"); - assertTrue(StringUtils.startsWithIgnoreCase(FOOBAR, ""), "startsWithIgnoreCase(FOOBAR, \"\")"); - - assertTrue(StringUtils.startsWithIgnoreCase(foobar, foo), "startsWithIgnoreCase(foobar, foo)"); - assertTrue(StringUtils.startsWithIgnoreCase(FOOBAR, FOO), "startsWithIgnoreCase(FOOBAR, FOO)"); - assertTrue(StringUtils.startsWithIgnoreCase(foobar, FOO), "startsWithIgnoreCase(foobar, FOO)"); - assertTrue(StringUtils.startsWithIgnoreCase(FOOBAR, foo), "startsWithIgnoreCase(FOOBAR, foo)"); - - assertFalse(StringUtils.startsWithIgnoreCase(foo, foobar), "startsWithIgnoreCase(foo, foobar)"); - assertFalse(StringUtils.startsWithIgnoreCase(bar, foobar), "startsWithIgnoreCase(foo, foobar)"); - - assertFalse(StringUtils.startsWithIgnoreCase(foobar, bar), "startsWithIgnoreCase(foobar, bar)"); - assertFalse(StringUtils.startsWithIgnoreCase(FOOBAR, BAR), "startsWithIgnoreCase(FOOBAR, BAR)"); - assertFalse(StringUtils.startsWithIgnoreCase(foobar, BAR), "startsWithIgnoreCase(foobar, BAR)"); - assertFalse(StringUtils.startsWithIgnoreCase(FOOBAR, bar), "startsWithIgnoreCase(FOOBAR, bar)"); - } - } diff --git a/src/test/java/org/apache/commons/lang3/StringsContainsTest.java b/src/test/java/org/apache/commons/lang3/StringsContainsTest.java new file mode 100644 index 00000000000..f69b7920c5d --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/StringsContainsTest.java @@ -0,0 +1,173 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3; + +import org.junit.jupiter.api.Test; +import org.junitpioneer.jupiter.DefaultLocale; + +import java.util.Locale; + +import static org.apache.commons.lang3.Supplementary.*; +import static org.junit.jupiter.api.Assertions.*; + +/** + * Unit tests {@link Strings} - Contains methods + */ +public class StringsContainsTest extends AbstractLangTest { + @Test + public void testContains_Char() { + assertFalse(Strings.CS.contains("", null)); + assertFalse(Strings.CS.contains(null, null)); + } + + @Test + public void testContains_String() { + assertFalse(Strings.CS.contains(null, null)); + assertFalse(Strings.CS.contains(null, "")); + assertFalse(Strings.CS.contains(null, "a")); + assertFalse(Strings.CS.contains("", null)); + assertTrue(Strings.CS.contains("", "")); + assertFalse(Strings.CS.contains("", "a")); + assertTrue(Strings.CS.contains("abc", "a")); + assertTrue(Strings.CS.contains("abc", "b")); + assertTrue(Strings.CS.contains("abc", "c")); + assertTrue(Strings.CS.contains("abc", "abc")); + assertFalse(Strings.CS.contains("abc", "z")); + } + + /** + * See https://www.oracle.com/technical-resources/articles/javase/supplementary.html + */ + @Test + public void testContains_StringWithBadSupplementaryChars() { + // Test edge case: 1/2 of a (broken) supplementary char + assertFalse(Strings.CS.contains(CharUSuppCharHigh, CharU20001)); + assertFalse(Strings.CS.contains(CharUSuppCharLow, CharU20001)); + assertFalse(Strings.CS.contains(CharU20001, CharUSuppCharHigh)); + assertTrue(Strings.CS.contains(CharU20001, CharUSuppCharLow)); + assertTrue(Strings.CS.contains(CharU20001 + CharUSuppCharLow + "a", "a")); + assertTrue(Strings.CS.contains(CharU20001 + CharUSuppCharHigh + "a", "a")); + } + + /** + * See https://www.oracle.com/technical-resources/articles/javase/supplementary.html + */ + @Test + public void testContains_StringWithSupplementaryChars() { + assertTrue(Strings.CS.contains(CharU20000 + CharU20001, CharU20000)); + assertTrue(Strings.CS.contains(CharU20000 + CharU20001, CharU20001)); + assertTrue(Strings.CS.contains(CharU20000, CharU20000)); + assertFalse(Strings.CS.contains(CharU20000, CharU20001)); + } + + @Test + public void testContainsAny_StringStringArray() { + assertFalse(Strings.CS.containsAny(null, (String[]) null)); + assertFalse(Strings.CS.containsAny(null, new String[0])); + assertFalse(Strings.CS.containsAny(null, new String[] { "hello" })); + assertFalse(Strings.CS.containsAny("", (String[]) null)); + assertFalse(Strings.CS.containsAny("", new String[0])); + assertFalse(Strings.CS.containsAny("", new String[] { "hello" })); + assertFalse(Strings.CS.containsAny("hello, goodbye", (String[]) null)); + assertFalse(Strings.CS.containsAny("hello, goodbye", new String[0])); + assertTrue(Strings.CS.containsAny("hello, goodbye", new String[] { "hello", "goodbye" })); + assertTrue(Strings.CS.containsAny("hello, goodbye", new String[] { "hello", "Goodbye" })); + assertFalse(Strings.CS.containsAny("hello, goodbye", new String[] { "Hello", "Goodbye" })); + assertFalse(Strings.CS.containsAny("hello, goodbye", new String[] { "Hello", null })); + assertFalse(Strings.CS.containsAny("hello, null", new String[] { "Hello", null })); + // Javadoc examples: + assertTrue(Strings.CS.containsAny("abcd", "ab", null)); + assertTrue(Strings.CS.containsAny("abcd", "ab", "cd")); + assertTrue(Strings.CS.containsAny("abc", "d", "abc")); + } + + @Test + public void testContainsAnyIgnoreCase_StringStringArray() { + assertFalse(Strings.CI.containsAny(null, (String[]) null)); + assertFalse(Strings.CI.containsAny(null, new String[0])); + assertFalse(Strings.CI.containsAny(null, new String[] { "hello" })); + assertFalse(Strings.CI.containsAny("", (String[]) null)); + assertFalse(Strings.CI.containsAny("", new String[0])); + assertFalse(Strings.CI.containsAny("", new String[] { "hello" })); + assertFalse(Strings.CI.containsAny("hello, goodbye", (String[]) null)); + assertFalse(Strings.CI.containsAny("hello, goodbye", new String[0])); + assertTrue(Strings.CI.containsAny("hello, goodbye", new String[] { "hello", "goodbye" })); + assertTrue(Strings.CI.containsAny("hello, goodbye", new String[] { "hello", "Goodbye" })); + assertTrue(Strings.CI.containsAny("hello, goodbye", new String[] { "Hello", "Goodbye" })); + assertTrue(Strings.CI.containsAny("hello, goodbye", new String[] { "Hello", null })); + assertTrue(Strings.CI.containsAny("hello, null", new String[] { "Hello", null })); + // Javadoc examples: + assertTrue(Strings.CI.containsAny("abcd", "ab", null)); + assertTrue(Strings.CI.containsAny("abcd", "ab", "cd")); + assertTrue(Strings.CI.containsAny("abc", "d", "abc")); + } + + @DefaultLocale(language = "de", country = "DE") + @Test + public void testContainsIgnoreCase_LocaleIndependence() { + final Locale[] locales = { Locale.ENGLISH, new Locale("tr"), Locale.getDefault() }; + + final String[][] tdata = { { "i", "I" }, { "I", "i" }, { "\u03C2", "\u03C3" }, { "\u03A3", "\u03C2" }, { "\u03A3", "\u03C3" }, }; + + final String[][] fdata = { { "\u00DF", "SS" }, }; + + for (final Locale testLocale : locales) { + Locale.setDefault(testLocale); + for (int j = 0; j < tdata.length; j++) { + assertTrue(Strings.CI.contains(tdata[j][0], tdata[j][1]), Locale.getDefault() + ": " + j + " " + tdata[j][0] + " " + tdata[j][1]); + } + for (int j = 0; j < fdata.length; j++) { + assertFalse(Strings.CI.contains(fdata[j][0], fdata[j][1]), Locale.getDefault() + ": " + j + " " + fdata[j][0] + " " + fdata[j][1]); + } + } + } + + @Test + public void testContainsIgnoreCase_StringString() { + assertFalse(Strings.CI.contains(null, null)); + + // Null tests + assertFalse(Strings.CI.contains(null, "")); + assertFalse(Strings.CI.contains(null, "a")); + assertFalse(Strings.CI.contains(null, "abc")); + + assertFalse(Strings.CI.contains("", null)); + assertFalse(Strings.CI.contains("a", null)); + assertFalse(Strings.CI.contains("abc", null)); + + // Match len = 0 + assertTrue(Strings.CI.contains("", "")); + assertTrue(Strings.CI.contains("a", "")); + assertTrue(Strings.CI.contains("abc", "")); + + // Match len = 1 + assertFalse(Strings.CI.contains("", "a")); + assertTrue(Strings.CI.contains("a", "a")); + assertTrue(Strings.CI.contains("abc", "a")); + assertFalse(Strings.CI.contains("", "A")); + assertTrue(Strings.CI.contains("a", "A")); + assertTrue(Strings.CI.contains("abc", "A")); + + // Match len > 1 + assertFalse(Strings.CI.contains("", "abc")); + assertFalse(Strings.CI.contains("a", "abc")); + assertTrue(Strings.CI.contains("xabcz", "abc")); + assertFalse(Strings.CI.contains("", "ABC")); + assertFalse(Strings.CI.contains("a", "ABC")); + assertTrue(Strings.CI.contains("xabcz", "ABC")); + } +} diff --git a/src/test/java/org/apache/commons/lang3/StringsEqualsIndexOfTest.java b/src/test/java/org/apache/commons/lang3/StringsEqualsIndexOfTest.java new file mode 100644 index 00000000000..9bf5b5e576c --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/StringsEqualsIndexOfTest.java @@ -0,0 +1,407 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3; + +import org.junit.jupiter.api.Test; + +import java.util.Locale; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Unit tests {@link Strings} - Equals/IndexOf methods + */ +public class StringsEqualsIndexOfTest extends AbstractLangTest { + + // The purpose of this class is to test StringUtils#equals(CharSequence, CharSequence) + // with a CharSequence implementation whose equals(Object) override requires that the + // other object be an instance of CustomCharSequence, even though, as char sequences, + // `seq` may equal the other object. + private static final class CustomCharSequence implements CharSequence { + private final CharSequence seq; + + CustomCharSequence(final CharSequence seq) { + this.seq = seq; + } + + @Override + public char charAt(final int index) { + return seq.charAt(index); + } + + @Override + public boolean equals(final Object obj) { + if (!(obj instanceof CustomCharSequence)) { + return false; + } + final CustomCharSequence other = (CustomCharSequence) obj; + return seq.equals(other.seq); + } + + @Override + public int hashCode() { + return seq.hashCode(); + } + + @Override + public int length() { + return seq.length(); + } + + @Override + public CharSequence subSequence(final int start, final int end) { + return new CustomCharSequence(seq.subSequence(start, end)); + } + + @Override + public String toString() { + return seq.toString(); + } + } + + private static final String BAR = "bar"; + + private static final String FOO = "foo"; + + private static final String FOOBAR = "foobar"; + + private static final String[] FOOBAR_SUB_ARRAY = {"ob", "ba"}; + + @Test + public void testCompare_StringString() { + assertEquals(0, Strings.CS.compare(null, null)); + assertTrue(Strings.CS.compare(null, "a") < 0); + assertTrue(Strings.CS.compare("a", null) > 0); + assertEquals(0, Strings.CS.compare("abc", "abc")); + assertTrue(Strings.CS.compare("a", "b") < 0); + assertTrue(Strings.CS.compare("b", "a") > 0); + assertTrue(Strings.CS.compare("a", "B") > 0); + assertTrue(Strings.CS.compare("abc", "abd") < 0); + assertTrue(Strings.CS.compare("ab", "abc") < 0); + assertTrue(Strings.CS.compare("ab", "ab ") < 0); + assertTrue(Strings.CS.compare("abc", "ab ") > 0); + } + + + @Test + public void testCompareIgnoreCase_StringString() { + assertEquals(0, Strings.CI.compare(null, null)); + assertTrue(Strings.CI.compare(null, "a") < 0); + assertTrue(Strings.CI.compare("a", null) > 0); + assertEquals(0, Strings.CI.compare("abc", "abc")); + assertEquals(0, Strings.CI.compare("abc", "ABC")); + assertTrue(Strings.CI.compare("a", "b") < 0); + assertTrue(Strings.CI.compare("b", "a") > 0); + assertTrue(Strings.CI.compare("a", "B") < 0); + assertTrue(Strings.CI.compare("A", "b") < 0); + assertTrue(Strings.CI.compare("abc", "ABD") < 0); + assertTrue(Strings.CI.compare("ab", "ABC") < 0); + assertTrue(Strings.CI.compare("ab", "AB ") < 0); + assertTrue(Strings.CI.compare("abc", "AB ") > 0); + } + + + @Test + public void testCustomCharSequence() { + assertEquals(new CustomCharSequence(FOO), new CustomCharSequence(FOO)); + } + + @Test + public void testEqualsNull() { + final CharSequence fooCs = new StringBuilder(FOO), barCs = new StringBuilder(BAR); + assertTrue(StringUtils.equals(null, null)); + assertFalse(StringUtils.equals(null, fooCs)); + assertFalse(StringUtils.equals(fooCs, null)); + + assertTrue(Strings.CS.equals(null, null)); + assertFalse(Strings.CS.equals(null, barCs)); + assertFalse(Strings.CS.equals(fooCs, null)); + + } + + @Test + public void testEquals() { + final CharSequence fooCs = new StringBuilder(FOO), barCs = new StringBuilder(BAR), foobarCs = new StringBuilder(FOOBAR); + assertTrue(Strings.CS.equals(fooCs, fooCs)); + assertTrue(Strings.CS.equals(fooCs, new StringBuilder(FOO))); + assertTrue(Strings.CS.equals(fooCs, new String(new char[] { 'f', 'o', 'o' }))); + assertTrue(Strings.CS.equals(fooCs, new CustomCharSequence(FOO))); + assertTrue(Strings.CS.equals(new CustomCharSequence(FOO), fooCs)); + assertFalse(Strings.CS.equals(fooCs, new String(new char[] { 'f', 'O', 'O' }))); + assertFalse(Strings.CS.equals(fooCs, barCs)); + assertFalse(Strings.CS.equals(fooCs, null)); + assertFalse(Strings.CS.equals(null, fooCs)); + assertFalse(Strings.CS.equals(fooCs, foobarCs)); + assertFalse(Strings.CS.equals(foobarCs, fooCs)); + } + + @Test + public void testEqualsAny() { + assertFalse(Strings.CS.equalsAny(FOO)); + assertFalse(Strings.CS.equalsAny(FOO, new String[]{})); + + assertTrue(Strings.CS.equalsAny(FOO, FOO)); + assertTrue(Strings.CS.equalsAny(FOO, BAR, new String(new char[] { 'f', 'o', 'o' }))); + assertFalse(Strings.CS.equalsAny(FOO, BAR, new String(new char[] { 'f', 'O', 'O' }))); + assertFalse(Strings.CS.equalsAny(FOO, BAR)); + assertFalse(Strings.CS.equalsAny(FOO, BAR, null)); + assertFalse(Strings.CS.equalsAny(null, FOO)); + assertFalse(Strings.CS.equalsAny(FOO, FOOBAR)); + assertFalse(Strings.CS.equalsAny(FOOBAR, FOO)); + + assertTrue(Strings.CS.equalsAny(null, null, null)); + assertFalse(Strings.CS.equalsAny(null, FOO, BAR, FOOBAR)); + assertFalse(Strings.CS.equalsAny(FOO, null, BAR)); + assertTrue(Strings.CS.equalsAny(FOO, BAR, null, "", FOO, BAR)); + assertFalse(Strings.CS.equalsAny(FOO, FOO.toUpperCase(Locale.ROOT))); + + assertFalse(Strings.CS.equalsAny(null, (CharSequence[]) null)); + assertTrue(Strings.CS.equalsAny(FOO, new CustomCharSequence("foo"))); + assertTrue(Strings.CS.equalsAny(FOO, new StringBuilder("foo"))); + assertFalse(Strings.CS.equalsAny(FOO, new CustomCharSequence("fOo"))); + assertFalse(Strings.CS.equalsAny(FOO, new StringBuilder("fOo"))); + } + + @Test + public void testEqualsAnyIgnoreCase() { + assertFalse(Strings.CI.equalsAny(FOO)); + assertFalse(Strings.CI.equalsAny(FOO, new String[]{})); + + assertTrue(Strings.CI.equalsAny(FOO, FOO)); + assertTrue(Strings.CI.equalsAny(FOO, FOO.toUpperCase(Locale.ROOT))); + assertTrue(Strings.CI.equalsAny(FOO, FOO, new String(new char[]{'f', 'o', 'o'}))); + assertTrue(Strings.CI.equalsAny(FOO, BAR, new String(new char[]{'f', 'O', 'O'}))); + assertFalse(Strings.CI.equalsAny(FOO, BAR)); + assertFalse(Strings.CI.equalsAny(FOO, BAR, null)); + assertFalse(Strings.CI.equalsAny(null, FOO)); + assertFalse(Strings.CI.equalsAny(FOO, FOOBAR)); + assertFalse(Strings.CI.equalsAny(FOOBAR, FOO)); + + assertTrue(Strings.CI.equalsAny(null, null, null)); + assertFalse(Strings.CI.equalsAny(null, FOO, BAR, FOOBAR)); + assertFalse(Strings.CI.equalsAny(FOO, null, BAR)); + assertTrue(Strings.CI.equalsAny(FOO, BAR, null, "", FOO.toUpperCase(Locale.ROOT), BAR)); + assertTrue(Strings.CI.equalsAny(FOO, FOO.toUpperCase(Locale.ROOT))); + + assertFalse(Strings.CI.equalsAny(null, (CharSequence[]) null)); + assertTrue(Strings.CI.equalsAny(FOO, new CustomCharSequence("fOo"))); + assertTrue(Strings.CI.equalsAny(FOO, new StringBuilder("fOo"))); + } + + @Test + public void testEqualsIgnoreCase() { + assertTrue(StringUtils.equals(null, null)); + assertTrue(Strings.CI.equals(FOO, FOO)); + assertTrue(Strings.CI.equals(FOO, new String(new char[] { 'f', 'o', 'o' }))); + assertTrue(Strings.CI.equals(FOO, new String(new char[] { 'f', 'O', 'O' }))); + assertFalse(Strings.CI.equals(FOO, BAR)); + assertFalse(Strings.CI.equals(FOO, null)); + assertFalse(StringUtils.equals(null, FOO)); + assertTrue(Strings.CI.equals("", "")); + assertFalse(Strings.CI.equals("abcd", "abcd ")); + } + + @Test + public void testEqualsOnStrings() { + assertTrue(StringUtils.equals(null, null)); + assertTrue(Strings.CS.equals(FOO, FOO)); + assertTrue(Strings.CS.equals(FOO, new String(new char[] { 'f', 'o', 'o' }))); + assertFalse(Strings.CS.equals(FOO, new String(new char[] { 'f', 'O', 'O' }))); + assertFalse(Strings.CS.equals(FOO, BAR)); + assertFalse(Strings.CS.equals(FOO, null)); + assertFalse(StringUtils.equals(null, FOO)); + assertFalse(Strings.CS.equals(FOO, FOOBAR)); + assertFalse(Strings.CS.equals(FOOBAR, FOO)); + } + + + @Test + public void testIndexOf_String() { + assertEquals(-1, Strings.CS.indexOf(null, null)); + assertEquals(-1, Strings.CS.indexOf("", null)); + assertEquals(0, Strings.CS.indexOf("", "")); + assertEquals(0, Strings.CS.indexOf("aabaabaa", "a")); + assertEquals(2, Strings.CS.indexOf("aabaabaa", "b")); + assertEquals(1, Strings.CS.indexOf("aabaabaa", "ab")); + assertEquals(0, Strings.CS.indexOf("aabaabaa", "")); + + assertEquals(2, Strings.CS.indexOf(new StringBuilder("aabaabaa"), "b")); + } + + @Test + public void testIndexOf_StringInt() { + assertEquals(-1, Strings.CS.indexOf(null, null, 0)); + assertEquals(-1, Strings.CS.indexOf(null, null, -1)); + assertEquals(-1, Strings.CS.indexOf(null, "", 0)); + assertEquals(-1, Strings.CS.indexOf(null, "", -1)); + assertEquals(-1, Strings.CS.indexOf("", null, 0)); + assertEquals(-1, Strings.CS.indexOf("", null, -1)); + assertEquals(0, Strings.CS.indexOf("", "", 0)); + assertEquals(0, Strings.CS.indexOf("", "", -1)); + assertEquals(0, Strings.CS.indexOf("", "", 9)); + assertEquals(0, Strings.CS.indexOf("abc", "", 0)); + assertEquals(0, Strings.CS.indexOf("abc", "", -1)); + assertEquals(3, Strings.CS.indexOf("abc", "", 9)); + assertEquals(3, Strings.CS.indexOf("abc", "", 3)); + assertEquals(0, Strings.CS.indexOf("aabaabaa", "a", 0)); + assertEquals(2, Strings.CS.indexOf("aabaabaa", "b", 0)); + assertEquals(1, Strings.CS.indexOf("aabaabaa", "ab", 0)); + assertEquals(5, Strings.CS.indexOf("aabaabaa", "b", 3)); + assertEquals(-1, Strings.CS.indexOf("aabaabaa", "b", 9)); + assertEquals(2, Strings.CS.indexOf("aabaabaa", "b", -1)); + assertEquals(2, Strings.CS.indexOf("aabaabaa", "", 2)); + + // Test that startIndex works correctly, i.e. cannot match before startIndex + assertEquals(7, Strings.CS.indexOf("12345678", "8", 5)); + assertEquals(7, Strings.CS.indexOf("12345678", "8", 6)); + assertEquals(7, Strings.CS.indexOf("12345678", "8", 7)); // 7 is last index + assertEquals(-1, Strings.CS.indexOf("12345678", "8", 8)); + + assertEquals(5, Strings.CS.indexOf(new StringBuilder("aabaabaa"), "b", 3)); + } + + @Test + public void testIndexOfIgnoreCase_String() { + assertEquals(-1, Strings.CI.indexOf(null, null)); + assertEquals(-1, Strings.CI.indexOf(null, "")); + assertEquals(-1, Strings.CI.indexOf("", null)); + assertEquals(0, Strings.CI.indexOf("", "")); + assertEquals(0, Strings.CI.indexOf("aabaabaa", "a")); + assertEquals(0, Strings.CI.indexOf("aabaabaa", "A")); + assertEquals(2, Strings.CI.indexOf("aabaabaa", "b")); + assertEquals(2, Strings.CI.indexOf("aabaabaa", "B")); + assertEquals(1, Strings.CI.indexOf("aabaabaa", "ab")); + assertEquals(1, Strings.CI.indexOf("aabaabaa", "AB")); + assertEquals(0, Strings.CI.indexOf("aabaabaa", "")); + } + + @Test + public void testIndexOfIgnoreCase_StringInt() { + assertEquals(1, Strings.CI.indexOf("aabaabaa", "AB", -1)); + assertEquals(1, Strings.CI.indexOf("aabaabaa", "AB", 0)); + assertEquals(1, Strings.CI.indexOf("aabaabaa", "AB", 1)); + assertEquals(4, Strings.CI.indexOf("aabaabaa", "AB", 2)); + assertEquals(4, Strings.CI.indexOf("aabaabaa", "AB", 3)); + assertEquals(4, Strings.CI.indexOf("aabaabaa", "AB", 4)); + assertEquals(-1, Strings.CI.indexOf("aabaabaa", "AB", 5)); + assertEquals(-1, Strings.CI.indexOf("aabaabaa", "AB", 6)); + assertEquals(-1, Strings.CI.indexOf("aabaabaa", "AB", 7)); + assertEquals(-1, Strings.CI.indexOf("aabaabaa", "AB", 8)); + assertEquals(1, Strings.CI.indexOf("aab", "AB", 1)); + assertEquals(5, Strings.CI.indexOf("aabaabaa", "", 5)); + assertEquals(-1, Strings.CI.indexOf("ab", "AAB", 0)); + assertEquals(-1, Strings.CI.indexOf("aab", "AAB", 1)); + assertEquals(-1, Strings.CI.indexOf("abc", "", 9)); + } + + + @Test + public void testLastIndexOf_String() { + assertEquals(-1, Strings.CS.lastIndexOf(null, null)); + assertEquals(-1, Strings.CS.lastIndexOf("", null)); + assertEquals(-1, Strings.CS.lastIndexOf("", "a")); + assertEquals(0, Strings.CS.lastIndexOf("", "")); + assertEquals(8, Strings.CS.lastIndexOf("aabaabaa", "")); + assertEquals(7, Strings.CS.lastIndexOf("aabaabaa", "a")); + assertEquals(5, Strings.CS.lastIndexOf("aabaabaa", "b")); + assertEquals(4, Strings.CS.lastIndexOf("aabaabaa", "ab")); + + assertEquals(4, Strings.CS.lastIndexOf(new StringBuilder("aabaabaa"), "ab")); + } + + @Test + public void testLastIndexOf_StringInt() { + assertEquals(-1, Strings.CS.lastIndexOf(null, null, 0)); + assertEquals(-1, Strings.CS.lastIndexOf(null, null, -1)); + assertEquals(-1, Strings.CS.lastIndexOf(null, "", 0)); + assertEquals(-1, Strings.CS.lastIndexOf(null, "", -1)); + assertEquals(-1, Strings.CS.lastIndexOf("", null, 0)); + assertEquals(-1, Strings.CS.lastIndexOf("", null, -1)); + assertEquals(0, Strings.CS.lastIndexOf("", "", 0)); + assertEquals(-1, Strings.CS.lastIndexOf("", "", -1)); + assertEquals(0, Strings.CS.lastIndexOf("", "", 9)); + assertEquals(0, Strings.CS.lastIndexOf("abc", "", 0)); + assertEquals(-1, Strings.CS.lastIndexOf("abc", "", -1)); + assertEquals(3, Strings.CS.lastIndexOf("abc", "", 9)); + assertEquals(7, Strings.CS.lastIndexOf("aabaabaa", "a", 8)); + assertEquals(5, Strings.CS.lastIndexOf("aabaabaa", "b", 8)); + assertEquals(4, Strings.CS.lastIndexOf("aabaabaa", "ab", 8)); + assertEquals(2, Strings.CS.lastIndexOf("aabaabaa", "b", 3)); + assertEquals(5, Strings.CS.lastIndexOf("aabaabaa", "b", 9)); + assertEquals(-1, Strings.CS.lastIndexOf("aabaabaa", "b", -1)); + assertEquals(-1, Strings.CS.lastIndexOf("aabaabaa", "b", 0)); + assertEquals(0, Strings.CS.lastIndexOf("aabaabaa", "a", 0)); + assertEquals(-1, Strings.CS.lastIndexOf("aabaabaa", "a", -1)); + + // Test that fromIndex works correctly, i.e. cannot match after fromIndex + assertEquals(7, Strings.CS.lastIndexOf("12345678", "8", 9)); + assertEquals(7, Strings.CS.lastIndexOf("12345678", "8", 8)); + assertEquals(7, Strings.CS.lastIndexOf("12345678", "8", 7)); // 7 is last index + assertEquals(-1, Strings.CS.lastIndexOf("12345678", "8", 6)); + + assertEquals(-1, Strings.CS.lastIndexOf("aabaabaa", "b", 1)); + assertEquals(2, Strings.CS.lastIndexOf("aabaabaa", "b", 2)); + assertEquals(2, Strings.CS.lastIndexOf("aabaabaa", "ba", 2)); + assertEquals(2, Strings.CS.lastIndexOf("aabaabaa", "ba", 3)); + + assertEquals(2, Strings.CS.lastIndexOf(new StringBuilder("aabaabaa"), "b", 3)); + } + + @Test + public void testLastIndexOfIgnoreCase_String() { + assertEquals(-1, Strings.CI.lastIndexOf(null, null)); + assertEquals(-1, Strings.CI.lastIndexOf("", null)); + assertEquals(-1, Strings.CI.lastIndexOf(null, "")); + assertEquals(-1, Strings.CI.lastIndexOf("", "a")); + assertEquals(0, Strings.CI.lastIndexOf("", "")); + assertEquals(8, Strings.CI.lastIndexOf("aabaabaa", "")); + assertEquals(7, Strings.CI.lastIndexOf("aabaabaa", "a")); + assertEquals(7, Strings.CI.lastIndexOf("aabaabaa", "A")); + assertEquals(5, Strings.CI.lastIndexOf("aabaabaa", "b")); + assertEquals(5, Strings.CI.lastIndexOf("aabaabaa", "B")); + assertEquals(4, Strings.CI.lastIndexOf("aabaabaa", "ab")); + assertEquals(4, Strings.CI.lastIndexOf("aabaabaa", "AB")); + assertEquals(-1, Strings.CI.lastIndexOf("ab", "AAB")); + assertEquals(0, Strings.CI.lastIndexOf("aab", "AAB")); + } + + @Test + public void testLastIndexOfIgnoreCase_StringInt() { + assertEquals(-1, Strings.CI.lastIndexOf(null, null, 0)); + assertEquals(-1, Strings.CI.lastIndexOf(null, null, -1)); + assertEquals(-1, Strings.CI.lastIndexOf(null, "", 0)); + assertEquals(-1, Strings.CI.lastIndexOf(null, "", -1)); + assertEquals(-1, Strings.CI.lastIndexOf("", null, 0)); + assertEquals(-1, Strings.CI.lastIndexOf("", null, -1)); + assertEquals(0, Strings.CI.lastIndexOf("", "", 0)); + assertEquals(-1, Strings.CI.lastIndexOf("", "", -1)); + assertEquals(0, Strings.CI.lastIndexOf("", "", 9)); + assertEquals(0, Strings.CI.lastIndexOf("abc", "", 0)); + assertEquals(-1, Strings.CI.lastIndexOf("abc", "", -1)); + assertEquals(3, Strings.CI.lastIndexOf("abc", "", 9)); + assertEquals(7, Strings.CI.lastIndexOf("aabaabaa", "A", 8)); + assertEquals(5, Strings.CI.lastIndexOf("aabaabaa", "B", 8)); + assertEquals(4, Strings.CI.lastIndexOf("aabaabaa", "AB", 8)); + assertEquals(2, Strings.CI.lastIndexOf("aabaabaa", "B", 3)); + assertEquals(5, Strings.CI.lastIndexOf("aabaabaa", "B", 9)); + assertEquals(-1, Strings.CI.lastIndexOf("aabaabaa", "B", -1)); + assertEquals(-1, Strings.CI.lastIndexOf("aabaabaa", "B", 0)); + assertEquals(0, Strings.CI.lastIndexOf("aabaabaa", "A", 0)); + assertEquals(1, Strings.CI.lastIndexOf("aab", "AB", 1)); + } + +} diff --git a/src/test/java/org/apache/commons/lang3/StringsStartsEndsWithTest.java b/src/test/java/org/apache/commons/lang3/StringsStartsEndsWithTest.java new file mode 100644 index 00000000000..6ca76e73c1b --- /dev/null +++ b/src/test/java/org/apache/commons/lang3/StringsStartsEndsWithTest.java @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.lang3; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Unit tests {@link Strings} - StartsWith/EndsWith methods + */ +public class StringsStartsEndsWithTest extends AbstractLangTest { + private static final String foo = "foo"; + private static final String bar = "bar"; + private static final String foobar = "foobar"; + private static final String FOO = "FOO"; + private static final String BAR = "BAR"; + private static final String FOOBAR = "FOOBAR"; + + /** + * Test Strings.CS.endsWith() + */ + @Test + public void testEndsWith() { + assertTrue(Strings.CS.endsWith(null, null), "endsWith(null, null)"); + assertFalse(Strings.CS.endsWith(FOOBAR, null), "endsWith(FOOBAR, null)"); + assertFalse(Strings.CS.endsWith(null, FOO), "endsWith(null, FOO)"); + assertTrue(Strings.CS.endsWith(FOOBAR, ""), "endsWith(FOOBAR, \"\")"); + + assertFalse(Strings.CS.endsWith(foobar, foo), "endsWith(foobar, foo)"); + assertFalse(Strings.CS.endsWith(FOOBAR, FOO), "endsWith(FOOBAR, FOO)"); + assertFalse(Strings.CS.endsWith(foobar, FOO), "endsWith(foobar, FOO)"); + assertFalse(Strings.CS.endsWith(FOOBAR, foo), "endsWith(FOOBAR, foo)"); + + assertFalse(Strings.CS.endsWith(foo, foobar), "endsWith(foo, foobar)"); + assertFalse(Strings.CS.endsWith(bar, foobar), "endsWith(foo, foobar)"); + + assertTrue(Strings.CS.endsWith(foobar, bar), "endsWith(foobar, bar)"); + assertTrue(Strings.CS.endsWith(FOOBAR, BAR), "endsWith(FOOBAR, BAR)"); + assertFalse(Strings.CS.endsWith(foobar, BAR), "endsWith(foobar, BAR)"); + assertFalse(Strings.CS.endsWith(FOOBAR, bar), "endsWith(FOOBAR, bar)"); + + // "alpha, beta, gamma, delta".endsWith("delta") + assertTrue(Strings.CS.endsWith("\u03B1\u03B2\u03B3\u03B4", "\u03B4"), + "endsWith(\u03B1\u03B2\u03B3\u03B4, \u03B4)"); + // "alpha, beta, gamma, delta".endsWith("gamma, DELTA") + assertFalse(Strings.CS.endsWith("\u03B1\u03B2\u03B3\u03B4", "\u03B3\u0394"), + "endsWith(\u03B1\u03B2\u03B3\u03B4, \u03B3\u0394)"); + } + + /** + * Test Strings.CI.endsWith() + */ + @Test + public void testEndsWithIgnoreCase() { + assertTrue(Strings.CI.endsWith(null, null), "endsWithIgnoreCase(null, null)"); + assertFalse(Strings.CI.endsWith(FOOBAR, null), "endsWithIgnoreCase(FOOBAR, null)"); + assertFalse(Strings.CI.endsWith(null, FOO), "endsWithIgnoreCase(null, FOO)"); + assertTrue(Strings.CI.endsWith(FOOBAR, ""), "endsWithIgnoreCase(FOOBAR, \"\")"); + + assertFalse(Strings.CI.endsWith(foobar, foo), "endsWithIgnoreCase(foobar, foo)"); + assertFalse(Strings.CI.endsWith(FOOBAR, FOO), "endsWithIgnoreCase(FOOBAR, FOO)"); + assertFalse(Strings.CI.endsWith(foobar, FOO), "endsWithIgnoreCase(foobar, FOO)"); + assertFalse(Strings.CI.endsWith(FOOBAR, foo), "endsWithIgnoreCase(FOOBAR, foo)"); + + assertFalse(Strings.CI.endsWith(foo, foobar), "endsWithIgnoreCase(foo, foobar)"); + assertFalse(Strings.CI.endsWith(bar, foobar), "endsWithIgnoreCase(foo, foobar)"); + + assertTrue(Strings.CI.endsWith(foobar, bar), "endsWithIgnoreCase(foobar, bar)"); + assertTrue(Strings.CI.endsWith(FOOBAR, BAR), "endsWithIgnoreCase(FOOBAR, BAR)"); + assertTrue(Strings.CI.endsWith(foobar, BAR), "endsWithIgnoreCase(foobar, BAR)"); + assertTrue(Strings.CI.endsWith(FOOBAR, bar), "endsWithIgnoreCase(FOOBAR, bar)"); + + // javadoc + assertTrue(Strings.CI.endsWith("abcdef", "def")); + assertTrue(Strings.CI.endsWith("ABCDEF", "def")); + assertFalse(Strings.CI.endsWith("ABCDEF", "cde")); + + // "alpha, beta, gamma, delta".endsWith("DELTA") + assertTrue(Strings.CI.endsWith("\u03B1\u03B2\u03B3\u03B4", "\u0394"), + "endsWith(\u03B1\u03B2\u03B3\u03B4, \u0394)"); + // "alpha, beta, gamma, delta".endsWith("GAMMA") + assertFalse(Strings.CI.endsWith("\u03B1\u03B2\u03B3\u03B4", "\u0393"), + "endsWith(\u03B1\u03B2\u03B3\u03B4, \u0393)"); + } + + /** + * Test Strings.CS.startsWith() + */ + @Test + public void testStartsWith() { + assertTrue(Strings.CS.startsWith(null, null), "startsWith(null, null)"); + assertFalse(Strings.CS.startsWith(FOOBAR, null), "startsWith(FOOBAR, null)"); + assertFalse(Strings.CS.startsWith(null, FOO), "startsWith(null, FOO)"); + assertTrue(Strings.CS.startsWith(FOOBAR, ""), "startsWith(FOOBAR, \"\")"); + + assertTrue(Strings.CS.startsWith(foobar, foo), "startsWith(foobar, foo)"); + assertTrue(Strings.CS.startsWith(FOOBAR, FOO), "startsWith(FOOBAR, FOO)"); + assertFalse(Strings.CS.startsWith(foobar, FOO), "startsWith(foobar, FOO)"); + assertFalse(Strings.CS.startsWith(FOOBAR, foo), "startsWith(FOOBAR, foo)"); + + assertFalse(Strings.CS.startsWith(foo, foobar), "startsWith(foo, foobar)"); + assertFalse(Strings.CS.startsWith(bar, foobar), "startsWith(foo, foobar)"); + + assertFalse(Strings.CS.startsWith(foobar, bar), "startsWith(foobar, bar)"); + assertFalse(Strings.CS.startsWith(FOOBAR, BAR), "startsWith(FOOBAR, BAR)"); + assertFalse(Strings.CS.startsWith(foobar, BAR), "startsWith(foobar, BAR)"); + assertFalse(Strings.CS.startsWith(FOOBAR, bar), "startsWith(FOOBAR, bar)"); + } + + /** + * Test StringUtils.testStartsWithIgnoreCase() + */ + @Test + public void testStartsWithIgnoreCase() { + assertTrue(Strings.CI.startsWith(null, null), "startsWithIgnoreCase(null, null)"); + assertFalse(Strings.CI.startsWith(FOOBAR, null), "startsWithIgnoreCase(FOOBAR, null)"); + assertFalse(Strings.CI.startsWith(null, FOO), "startsWithIgnoreCase(null, FOO)"); + assertTrue(Strings.CI.startsWith(FOOBAR, ""), "startsWithIgnoreCase(FOOBAR, \"\")"); + + assertTrue(Strings.CI.startsWith(foobar, foo), "startsWithIgnoreCase(foobar, foo)"); + assertTrue(Strings.CI.startsWith(FOOBAR, FOO), "startsWithIgnoreCase(FOOBAR, FOO)"); + assertTrue(Strings.CI.startsWith(foobar, FOO), "startsWithIgnoreCase(foobar, FOO)"); + assertTrue(Strings.CI.startsWith(FOOBAR, foo), "startsWithIgnoreCase(FOOBAR, foo)"); + + assertFalse(Strings.CI.startsWith(foo, foobar), "startsWithIgnoreCase(foo, foobar)"); + assertFalse(Strings.CI.startsWith(bar, foobar), "startsWithIgnoreCase(foo, foobar)"); + + assertFalse(Strings.CI.startsWith(foobar, bar), "startsWithIgnoreCase(foobar, bar)"); + assertFalse(Strings.CI.startsWith(FOOBAR, BAR), "startsWithIgnoreCase(FOOBAR, BAR)"); + assertFalse(Strings.CI.startsWith(foobar, BAR), "startsWithIgnoreCase(foobar, BAR)"); + assertFalse(Strings.CI.startsWith(FOOBAR, bar), "startsWithIgnoreCase(FOOBAR, bar)"); + } + +} From 34388240c080e9b6d18aa0e95fd05467103a0e0a Mon Sep 17 00:00:00 2001 From: MSaifAsif Date: Thu, 26 Sep 2024 20:17:42 +0200 Subject: [PATCH 2/2] Bringing back existing test cases --- .../lang3/StringUtilsContainsTest.java | 131 ++++++++ .../lang3/StringUtilsEqualsIndexOfTest.java | 302 ++++++++++++++++++ .../lang3/StringUtilsStartsEndsWithTest.java | 113 +++++++ 3 files changed, 546 insertions(+) diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsContainsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsContainsTest.java index 12fd98f425f..063085327d5 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsContainsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsContainsTest.java @@ -24,7 +24,10 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.Locale; + import org.junit.jupiter.api.Test; +import org.junitpioneer.jupiter.DefaultLocale; /** * Unit tests {@link StringUtils} - Contains methods @@ -34,12 +37,28 @@ public class StringUtilsContainsTest extends AbstractLangTest { public void testContains_Char() { assertFalse(StringUtils.contains(null, ' ')); assertFalse(StringUtils.contains("", ' ')); + assertFalse(StringUtils.contains("", null)); + assertFalse(StringUtils.contains(null, null)); assertTrue(StringUtils.contains("abc", 'a')); assertTrue(StringUtils.contains("abc", 'b')); assertTrue(StringUtils.contains("abc", 'c')); assertFalse(StringUtils.contains("abc", 'z')); } + @Test + public void testContains_String() { + assertFalse(StringUtils.contains(null, null)); + assertFalse(StringUtils.contains(null, "")); + assertFalse(StringUtils.contains(null, "a")); + assertFalse(StringUtils.contains("", null)); + assertTrue(StringUtils.contains("", "")); + assertFalse(StringUtils.contains("", "a")); + assertTrue(StringUtils.contains("abc", "a")); + assertTrue(StringUtils.contains("abc", "b")); + assertTrue(StringUtils.contains("abc", "c")); + assertTrue(StringUtils.contains("abc", "abc")); + assertFalse(StringUtils.contains("abc", "z")); + } /** * See https://www.oracle.com/technical-resources/articles/javase/supplementary.html @@ -47,9 +66,25 @@ public void testContains_Char() { @Test public void testContains_StringWithBadSupplementaryChars() { // Test edge case: 1/2 of a (broken) supplementary char + assertFalse(StringUtils.contains(CharUSuppCharHigh, CharU20001)); + assertFalse(StringUtils.contains(CharUSuppCharLow, CharU20001)); + assertFalse(StringUtils.contains(CharU20001, CharUSuppCharHigh)); assertEquals(0, CharU20001.indexOf(CharUSuppCharLow)); + assertTrue(StringUtils.contains(CharU20001, CharUSuppCharLow)); + assertTrue(StringUtils.contains(CharU20001 + CharUSuppCharLow + "a", "a")); + assertTrue(StringUtils.contains(CharU20001 + CharUSuppCharHigh + "a", "a")); } + /** + * See https://www.oracle.com/technical-resources/articles/javase/supplementary.html + */ + @Test + public void testContains_StringWithSupplementaryChars() { + assertTrue(StringUtils.contains(CharU20000 + CharU20001, CharU20000)); + assertTrue(StringUtils.contains(CharU20000 + CharU20001, CharU20001)); + assertTrue(StringUtils.contains(CharU20000, CharU20000)); + assertFalse(StringUtils.contains(CharU20000, CharU20001)); + } @Test public void testContainsAny_StringCharArray() { @@ -122,6 +157,27 @@ public void testContainsAny_StringString() { assertFalse(StringUtils.containsAny("ab", "z")); } + @Test + public void testContainsAny_StringStringArray() { + assertFalse(StringUtils.containsAny(null, (String[]) null)); + assertFalse(StringUtils.containsAny(null, new String[0])); + assertFalse(StringUtils.containsAny(null, new String[] { "hello" })); + assertFalse(StringUtils.containsAny("", (String[]) null)); + assertFalse(StringUtils.containsAny("", new String[0])); + assertFalse(StringUtils.containsAny("", new String[] { "hello" })); + assertFalse(StringUtils.containsAny("hello, goodbye", (String[]) null)); + assertFalse(StringUtils.containsAny("hello, goodbye", new String[0])); + assertTrue(StringUtils.containsAny("hello, goodbye", new String[] { "hello", "goodbye" })); + assertTrue(StringUtils.containsAny("hello, goodbye", new String[] { "hello", "Goodbye" })); + assertFalse(StringUtils.containsAny("hello, goodbye", new String[] { "Hello", "Goodbye" })); + assertFalse(StringUtils.containsAny("hello, goodbye", new String[] { "Hello", null })); + assertFalse(StringUtils.containsAny("hello, null", new String[] { "Hello", null })); + // Javadoc examples: + assertTrue(StringUtils.containsAny("abcd", "ab", null)); + assertTrue(StringUtils.containsAny("abcd", "ab", "cd")); + assertTrue(StringUtils.containsAny("abc", "d", "abc")); + } + /** * See https://www.oracle.com/technical-resources/articles/javase/supplementary.html */ @@ -153,6 +209,81 @@ public void testContainsAny_StringWithSupplementaryChars() { assertFalse(StringUtils.containsAny(CharU20001, CharU20000)); } + @Test + public void testContainsAnyIgnoreCase_StringStringArray() { + assertFalse(StringUtils.containsAnyIgnoreCase(null, (String[]) null)); + assertFalse(StringUtils.containsAnyIgnoreCase(null, new String[0])); + assertFalse(StringUtils.containsAnyIgnoreCase(null, new String[] { "hello" })); + assertFalse(StringUtils.containsAnyIgnoreCase("", (String[]) null)); + assertFalse(StringUtils.containsAnyIgnoreCase("", new String[0])); + assertFalse(StringUtils.containsAnyIgnoreCase("", new String[] { "hello" })); + assertFalse(StringUtils.containsAnyIgnoreCase("hello, goodbye", (String[]) null)); + assertFalse(StringUtils.containsAnyIgnoreCase("hello, goodbye", new String[0])); + assertTrue(StringUtils.containsAnyIgnoreCase("hello, goodbye", new String[] { "hello", "goodbye" })); + assertTrue(StringUtils.containsAnyIgnoreCase("hello, goodbye", new String[] { "hello", "Goodbye" })); + assertTrue(StringUtils.containsAnyIgnoreCase("hello, goodbye", new String[] { "Hello", "Goodbye" })); + assertTrue(StringUtils.containsAnyIgnoreCase("hello, goodbye", new String[] { "Hello", null })); + assertTrue(StringUtils.containsAnyIgnoreCase("hello, null", new String[] { "Hello", null })); + // Javadoc examples: + assertTrue(StringUtils.containsAnyIgnoreCase("abcd", "ab", null)); + assertTrue(StringUtils.containsAnyIgnoreCase("abcd", "ab", "cd")); + assertTrue(StringUtils.containsAnyIgnoreCase("abc", "d", "abc")); + } + + @DefaultLocale(language = "de", country = "DE") + @Test + public void testContainsIgnoreCase_LocaleIndependence() { + final Locale[] locales = { Locale.ENGLISH, new Locale("tr"), Locale.getDefault() }; + + final String[][] tdata = { { "i", "I" }, { "I", "i" }, { "\u03C2", "\u03C3" }, { "\u03A3", "\u03C2" }, { "\u03A3", "\u03C3" }, }; + + final String[][] fdata = { { "\u00DF", "SS" }, }; + + for (final Locale testLocale : locales) { + Locale.setDefault(testLocale); + for (int j = 0; j < tdata.length; j++) { + assertTrue(StringUtils.containsIgnoreCase(tdata[j][0], tdata[j][1]), Locale.getDefault() + ": " + j + " " + tdata[j][0] + " " + tdata[j][1]); + } + for (int j = 0; j < fdata.length; j++) { + assertFalse(StringUtils.containsIgnoreCase(fdata[j][0], fdata[j][1]), Locale.getDefault() + ": " + j + " " + fdata[j][0] + " " + fdata[j][1]); + } + } + } + + @Test + public void testContainsIgnoreCase_StringString() { + assertFalse(StringUtils.containsIgnoreCase(null, null)); + + // Null tests + assertFalse(StringUtils.containsIgnoreCase(null, "")); + assertFalse(StringUtils.containsIgnoreCase(null, "a")); + assertFalse(StringUtils.containsIgnoreCase(null, "abc")); + + assertFalse(StringUtils.containsIgnoreCase("", null)); + assertFalse(StringUtils.containsIgnoreCase("a", null)); + assertFalse(StringUtils.containsIgnoreCase("abc", null)); + + // Match len = 0 + assertTrue(StringUtils.containsIgnoreCase("", "")); + assertTrue(StringUtils.containsIgnoreCase("a", "")); + assertTrue(StringUtils.containsIgnoreCase("abc", "")); + + // Match len = 1 + assertFalse(StringUtils.containsIgnoreCase("", "a")); + assertTrue(StringUtils.containsIgnoreCase("a", "a")); + assertTrue(StringUtils.containsIgnoreCase("abc", "a")); + assertFalse(StringUtils.containsIgnoreCase("", "A")); + assertTrue(StringUtils.containsIgnoreCase("a", "A")); + assertTrue(StringUtils.containsIgnoreCase("abc", "A")); + + // Match len > 1 + assertFalse(StringUtils.containsIgnoreCase("", "abc")); + assertFalse(StringUtils.containsIgnoreCase("a", "abc")); + assertTrue(StringUtils.containsIgnoreCase("xabcz", "abc")); + assertFalse(StringUtils.containsIgnoreCase("", "ABC")); + assertFalse(StringUtils.containsIgnoreCase("a", "ABC")); + assertTrue(StringUtils.containsIgnoreCase("xabcz", "ABC")); + } @Test public void testContainsNone_CharArray() { diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java index f65c41dc2a1..6101ea8d2d0 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java @@ -20,9 +20,11 @@ import static org.apache.commons.lang3.Supplementary.CharU20001; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.nio.CharBuffer; +import java.util.Locale; import org.hamcrest.core.IsNot; import org.junit.jupiter.api.Test; @@ -86,6 +88,21 @@ public String toString() { private static final String[] FOOBAR_SUB_ARRAY = {"ob", "ba"}; + @Test + public void testCompare_StringString() { + assertEquals(0, StringUtils.compare(null, null)); + assertTrue(StringUtils.compare(null, "a") < 0); + assertTrue(StringUtils.compare("a", null) > 0); + assertEquals(0, StringUtils.compare("abc", "abc")); + assertTrue(StringUtils.compare("a", "b") < 0); + assertTrue(StringUtils.compare("b", "a") > 0); + assertTrue(StringUtils.compare("a", "B") > 0); + assertTrue(StringUtils.compare("abc", "abd") < 0); + assertTrue(StringUtils.compare("ab", "abc") < 0); + assertTrue(StringUtils.compare("ab", "ab ") < 0); + assertTrue(StringUtils.compare("abc", "ab ") > 0); + } + @Test public void testCompare_StringStringBoolean() { assertEquals(0, StringUtils.compare(null, null, false)); @@ -103,6 +120,23 @@ public void testCompare_StringStringBoolean() { assertTrue(StringUtils.compare("abc", "ab ", false) > 0); } + @Test + public void testCompareIgnoreCase_StringString() { + assertEquals(0, StringUtils.compareIgnoreCase(null, null)); + assertTrue(StringUtils.compareIgnoreCase(null, "a") < 0); + assertTrue(StringUtils.compareIgnoreCase("a", null) > 0); + assertEquals(0, StringUtils.compareIgnoreCase("abc", "abc")); + assertEquals(0, StringUtils.compareIgnoreCase("abc", "ABC")); + assertTrue(StringUtils.compareIgnoreCase("a", "b") < 0); + assertTrue(StringUtils.compareIgnoreCase("b", "a") > 0); + assertTrue(StringUtils.compareIgnoreCase("a", "B") < 0); + assertTrue(StringUtils.compareIgnoreCase("A", "b") < 0); + assertTrue(StringUtils.compareIgnoreCase("abc", "ABD") < 0); + assertTrue(StringUtils.compareIgnoreCase("ab", "ABC") < 0); + assertTrue(StringUtils.compareIgnoreCase("ab", "AB ") < 0); + assertTrue(StringUtils.compareIgnoreCase("abc", "AB ") > 0); + } + @Test public void testCompareIgnoreCase_StringStringBoolean() { assertEquals(0, StringUtils.compareIgnoreCase(null, null, false)); @@ -126,8 +160,104 @@ public void testCompareIgnoreCase_StringStringBoolean() { public void testCustomCharSequence() { assertThat(new CustomCharSequence(FOO), IsNot.not(FOO)); assertThat(FOO, IsNot.not(new CustomCharSequence(FOO))); + assertEquals(new CustomCharSequence(FOO), new CustomCharSequence(FOO)); } + @Test + public void testEquals() { + final CharSequence fooCs = new StringBuilder(FOO), barCs = new StringBuilder(BAR), foobarCs = new StringBuilder(FOOBAR); + assertTrue(StringUtils.equals(null, null)); + assertTrue(StringUtils.equals(fooCs, fooCs)); + assertTrue(StringUtils.equals(fooCs, new StringBuilder(FOO))); + assertTrue(StringUtils.equals(fooCs, new String(new char[] { 'f', 'o', 'o' }))); + assertTrue(StringUtils.equals(fooCs, new CustomCharSequence(FOO))); + assertTrue(StringUtils.equals(new CustomCharSequence(FOO), fooCs)); + assertFalse(StringUtils.equals(fooCs, new String(new char[] { 'f', 'O', 'O' }))); + assertFalse(StringUtils.equals(fooCs, barCs)); + assertFalse(StringUtils.equals(fooCs, null)); + assertFalse(StringUtils.equals(null, fooCs)); + assertFalse(StringUtils.equals(fooCs, foobarCs)); + assertFalse(StringUtils.equals(foobarCs, fooCs)); + } + + @Test + public void testEqualsAny() { + assertFalse(StringUtils.equalsAny(FOO)); + assertFalse(StringUtils.equalsAny(FOO, new String[]{})); + + assertTrue(StringUtils.equalsAny(FOO, FOO)); + assertTrue(StringUtils.equalsAny(FOO, BAR, new String(new char[] { 'f', 'o', 'o' }))); + assertFalse(StringUtils.equalsAny(FOO, BAR, new String(new char[] { 'f', 'O', 'O' }))); + assertFalse(StringUtils.equalsAny(FOO, BAR)); + assertFalse(StringUtils.equalsAny(FOO, BAR, null)); + assertFalse(StringUtils.equalsAny(null, FOO)); + assertFalse(StringUtils.equalsAny(FOO, FOOBAR)); + assertFalse(StringUtils.equalsAny(FOOBAR, FOO)); + + assertTrue(StringUtils.equalsAny(null, null, null)); + assertFalse(StringUtils.equalsAny(null, FOO, BAR, FOOBAR)); + assertFalse(StringUtils.equalsAny(FOO, null, BAR)); + assertTrue(StringUtils.equalsAny(FOO, BAR, null, "", FOO, BAR)); + assertFalse(StringUtils.equalsAny(FOO, FOO.toUpperCase(Locale.ROOT))); + + assertFalse(StringUtils.equalsAny(null, (CharSequence[]) null)); + assertTrue(StringUtils.equalsAny(FOO, new CustomCharSequence("foo"))); + assertTrue(StringUtils.equalsAny(FOO, new StringBuilder("foo"))); + assertFalse(StringUtils.equalsAny(FOO, new CustomCharSequence("fOo"))); + assertFalse(StringUtils.equalsAny(FOO, new StringBuilder("fOo"))); + } + + @Test + public void testEqualsAnyIgnoreCase() { + assertFalse(StringUtils.equalsAnyIgnoreCase(FOO)); + assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, new String[]{})); + + assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, FOO)); + assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, FOO.toUpperCase(Locale.ROOT))); + assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, FOO, new String(new char[]{'f', 'o', 'o'}))); + assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, BAR, new String(new char[]{'f', 'O', 'O'}))); + assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, BAR)); + assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, BAR, null)); + assertFalse(StringUtils.equalsAnyIgnoreCase(null, FOO)); + assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, FOOBAR)); + assertFalse(StringUtils.equalsAnyIgnoreCase(FOOBAR, FOO)); + + assertTrue(StringUtils.equalsAnyIgnoreCase(null, null, null)); + assertFalse(StringUtils.equalsAnyIgnoreCase(null, FOO, BAR, FOOBAR)); + assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, null, BAR)); + assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, BAR, null, "", FOO.toUpperCase(Locale.ROOT), BAR)); + assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, FOO.toUpperCase(Locale.ROOT))); + + assertFalse(StringUtils.equalsAnyIgnoreCase(null, (CharSequence[]) null)); + assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, new CustomCharSequence("fOo"))); + assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, new StringBuilder("fOo"))); + } + + @Test + public void testEqualsIgnoreCase() { + assertTrue(StringUtils.equalsIgnoreCase(null, null)); + assertTrue(StringUtils.equalsIgnoreCase(FOO, FOO)); + assertTrue(StringUtils.equalsIgnoreCase(FOO, new String(new char[] { 'f', 'o', 'o' }))); + assertTrue(StringUtils.equalsIgnoreCase(FOO, new String(new char[] { 'f', 'O', 'O' }))); + assertFalse(StringUtils.equalsIgnoreCase(FOO, BAR)); + assertFalse(StringUtils.equalsIgnoreCase(FOO, null)); + assertFalse(StringUtils.equalsIgnoreCase(null, FOO)); + assertTrue(StringUtils.equalsIgnoreCase("", "")); + assertFalse(StringUtils.equalsIgnoreCase("abcd", "abcd ")); + } + + @Test + public void testEqualsOnStrings() { + assertTrue(StringUtils.equals(null, null)); + assertTrue(StringUtils.equals(FOO, FOO)); + assertTrue(StringUtils.equals(FOO, new String(new char[] { 'f', 'o', 'o' }))); + assertFalse(StringUtils.equals(FOO, new String(new char[] { 'f', 'O', 'O' }))); + assertFalse(StringUtils.equals(FOO, BAR)); + assertFalse(StringUtils.equals(FOO, null)); + assertFalse(StringUtils.equals(null, FOO)); + assertFalse(StringUtils.equals(FOO, FOOBAR)); + assertFalse(StringUtils.equals(FOOBAR, FOO)); + } @Test public void testIndexOf_char() { @@ -176,6 +306,50 @@ public void testIndexOf_charInt() { assertEquals(-1, StringUtils.indexOf(builder.toString(), CODE_POINT, 2)); } + @Test + public void testIndexOf_String() { + assertEquals(-1, StringUtils.indexOf(null, null)); + assertEquals(-1, StringUtils.indexOf("", null)); + assertEquals(0, StringUtils.indexOf("", "")); + assertEquals(0, StringUtils.indexOf("aabaabaa", "a")); + assertEquals(2, StringUtils.indexOf("aabaabaa", "b")); + assertEquals(1, StringUtils.indexOf("aabaabaa", "ab")); + assertEquals(0, StringUtils.indexOf("aabaabaa", "")); + + assertEquals(2, StringUtils.indexOf(new StringBuilder("aabaabaa"), "b")); + } + + @Test + public void testIndexOf_StringInt() { + assertEquals(-1, StringUtils.indexOf(null, null, 0)); + assertEquals(-1, StringUtils.indexOf(null, null, -1)); + assertEquals(-1, StringUtils.indexOf(null, "", 0)); + assertEquals(-1, StringUtils.indexOf(null, "", -1)); + assertEquals(-1, StringUtils.indexOf("", null, 0)); + assertEquals(-1, StringUtils.indexOf("", null, -1)); + assertEquals(0, StringUtils.indexOf("", "", 0)); + assertEquals(0, StringUtils.indexOf("", "", -1)); + assertEquals(0, StringUtils.indexOf("", "", 9)); + assertEquals(0, StringUtils.indexOf("abc", "", 0)); + assertEquals(0, StringUtils.indexOf("abc", "", -1)); + assertEquals(3, StringUtils.indexOf("abc", "", 9)); + assertEquals(3, StringUtils.indexOf("abc", "", 3)); + assertEquals(0, StringUtils.indexOf("aabaabaa", "a", 0)); + assertEquals(2, StringUtils.indexOf("aabaabaa", "b", 0)); + assertEquals(1, StringUtils.indexOf("aabaabaa", "ab", 0)); + assertEquals(5, StringUtils.indexOf("aabaabaa", "b", 3)); + assertEquals(-1, StringUtils.indexOf("aabaabaa", "b", 9)); + assertEquals(2, StringUtils.indexOf("aabaabaa", "b", -1)); + assertEquals(2, StringUtils.indexOf("aabaabaa", "", 2)); + + // Test that startIndex works correctly, i.e. cannot match before startIndex + assertEquals(7, StringUtils.indexOf("12345678", "8", 5)); + assertEquals(7, StringUtils.indexOf("12345678", "8", 6)); + assertEquals(7, StringUtils.indexOf("12345678", "8", 7)); // 7 is last index + assertEquals(-1, StringUtils.indexOf("12345678", "8", 8)); + + assertEquals(5, StringUtils.indexOf(new StringBuilder("aabaabaa"), "b", 3)); + } @Test public void testIndexOfAny_StringCharArray() { @@ -302,6 +476,39 @@ public void testIndexOfAnyBut_StringStringWithSupplementaryChars() { assertEquals(0, StringUtils.indexOfAnyBut(CharU20000, CharU20001)); } + @Test + public void testIndexOfIgnoreCase_String() { + assertEquals(-1, StringUtils.indexOfIgnoreCase(null, null)); + assertEquals(-1, StringUtils.indexOfIgnoreCase(null, "")); + assertEquals(-1, StringUtils.indexOfIgnoreCase("", null)); + assertEquals(0, StringUtils.indexOfIgnoreCase("", "")); + assertEquals(0, StringUtils.indexOfIgnoreCase("aabaabaa", "a")); + assertEquals(0, StringUtils.indexOfIgnoreCase("aabaabaa", "A")); + assertEquals(2, StringUtils.indexOfIgnoreCase("aabaabaa", "b")); + assertEquals(2, StringUtils.indexOfIgnoreCase("aabaabaa", "B")); + assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "ab")); + assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB")); + assertEquals(0, StringUtils.indexOfIgnoreCase("aabaabaa", "")); + } + + @Test + public void testIndexOfIgnoreCase_StringInt() { + assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", -1)); + assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 0)); + assertEquals(1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 1)); + assertEquals(4, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 2)); + assertEquals(4, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 3)); + assertEquals(4, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 4)); + assertEquals(-1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 5)); + assertEquals(-1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 6)); + assertEquals(-1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 7)); + assertEquals(-1, StringUtils.indexOfIgnoreCase("aabaabaa", "AB", 8)); + assertEquals(1, StringUtils.indexOfIgnoreCase("aab", "AB", 1)); + assertEquals(5, StringUtils.indexOfIgnoreCase("aabaabaa", "", 5)); + assertEquals(-1, StringUtils.indexOfIgnoreCase("ab", "AAB", 0)); + assertEquals(-1, StringUtils.indexOfIgnoreCase("aab", "AAB", 1)); + assertEquals(-1, StringUtils.indexOfIgnoreCase("abc", "", 9)); + } @Test public void testLANG1193() { @@ -379,6 +586,58 @@ public void testLastIndexOf_charInt() { assertEquals(StringUtils.INDEX_NOT_FOUND, StringUtils.lastIndexOf(CharBuffer.wrap("[%{.c.0rro"), -1738, 982)); } + @Test + public void testLastIndexOf_String() { + assertEquals(-1, StringUtils.lastIndexOf(null, null)); + assertEquals(-1, StringUtils.lastIndexOf("", null)); + assertEquals(-1, StringUtils.lastIndexOf("", "a")); + assertEquals(0, StringUtils.lastIndexOf("", "")); + assertEquals(8, StringUtils.lastIndexOf("aabaabaa", "")); + assertEquals(7, StringUtils.lastIndexOf("aabaabaa", "a")); + assertEquals(5, StringUtils.lastIndexOf("aabaabaa", "b")); + assertEquals(4, StringUtils.lastIndexOf("aabaabaa", "ab")); + + assertEquals(4, StringUtils.lastIndexOf(new StringBuilder("aabaabaa"), "ab")); + } + + @Test + public void testLastIndexOf_StringInt() { + assertEquals(-1, StringUtils.lastIndexOf(null, null, 0)); + assertEquals(-1, StringUtils.lastIndexOf(null, null, -1)); + assertEquals(-1, StringUtils.lastIndexOf(null, "", 0)); + assertEquals(-1, StringUtils.lastIndexOf(null, "", -1)); + assertEquals(-1, StringUtils.lastIndexOf("", null, 0)); + assertEquals(-1, StringUtils.lastIndexOf("", null, -1)); + assertEquals(0, StringUtils.lastIndexOf("", "", 0)); + assertEquals(-1, StringUtils.lastIndexOf("", "", -1)); + assertEquals(0, StringUtils.lastIndexOf("", "", 9)); + assertEquals(0, StringUtils.lastIndexOf("abc", "", 0)); + assertEquals(-1, StringUtils.lastIndexOf("abc", "", -1)); + assertEquals(3, StringUtils.lastIndexOf("abc", "", 9)); + assertEquals(7, StringUtils.lastIndexOf("aabaabaa", "a", 8)); + assertEquals(5, StringUtils.lastIndexOf("aabaabaa", "b", 8)); + assertEquals(4, StringUtils.lastIndexOf("aabaabaa", "ab", 8)); + assertEquals(2, StringUtils.lastIndexOf("aabaabaa", "b", 3)); + assertEquals(5, StringUtils.lastIndexOf("aabaabaa", "b", 9)); + assertEquals(-1, StringUtils.lastIndexOf("aabaabaa", "b", -1)); + assertEquals(-1, StringUtils.lastIndexOf("aabaabaa", "b", 0)); + assertEquals(0, StringUtils.lastIndexOf("aabaabaa", "a", 0)); + assertEquals(-1, StringUtils.lastIndexOf("aabaabaa", "a", -1)); + + // Test that fromIndex works correctly, i.e. cannot match after fromIndex + assertEquals(7, StringUtils.lastIndexOf("12345678", "8", 9)); + assertEquals(7, StringUtils.lastIndexOf("12345678", "8", 8)); + assertEquals(7, StringUtils.lastIndexOf("12345678", "8", 7)); // 7 is last index + assertEquals(-1, StringUtils.lastIndexOf("12345678", "8", 6)); + + assertEquals(-1, StringUtils.lastIndexOf("aabaabaa", "b", 1)); + assertEquals(2, StringUtils.lastIndexOf("aabaabaa", "b", 2)); + assertEquals(2, StringUtils.lastIndexOf("aabaabaa", "ba", 2)); + assertEquals(2, StringUtils.lastIndexOf("aabaabaa", "ba", 3)); + + assertEquals(2, StringUtils.lastIndexOf(new StringBuilder("aabaabaa"), "b", 3)); + } + @Test public void testLastIndexOfAny_StringStringArray() { assertEquals(-1, StringUtils.lastIndexOfAny(null, (CharSequence) null)); // test both types of ... @@ -401,6 +660,49 @@ public void testLastIndexOfAny_StringStringArray() { assertEquals(-1, StringUtils.lastIndexOfAny(null, new String[] {null})); } + @Test + public void testLastIndexOfIgnoreCase_String() { + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, null)); + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", null)); + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, "")); + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", "a")); + assertEquals(0, StringUtils.lastIndexOfIgnoreCase("", "")); + assertEquals(8, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "")); + assertEquals(7, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "a")); + assertEquals(7, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A")); + assertEquals(5, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "b")); + assertEquals(5, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B")); + assertEquals(4, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "ab")); + assertEquals(4, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "AB")); + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("ab", "AAB")); + assertEquals(0, StringUtils.lastIndexOfIgnoreCase("aab", "AAB")); + } + + @Test + public void testLastIndexOfIgnoreCase_StringInt() { + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, null, 0)); + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, null, -1)); + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, "", 0)); + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase(null, "", -1)); + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", null, 0)); + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", null, -1)); + assertEquals(0, StringUtils.lastIndexOfIgnoreCase("", "", 0)); + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("", "", -1)); + assertEquals(0, StringUtils.lastIndexOfIgnoreCase("", "", 9)); + assertEquals(0, StringUtils.lastIndexOfIgnoreCase("abc", "", 0)); + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("abc", "", -1)); + assertEquals(3, StringUtils.lastIndexOfIgnoreCase("abc", "", 9)); + assertEquals(7, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 8)); + assertEquals(5, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 8)); + assertEquals(4, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "AB", 8)); + assertEquals(2, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 3)); + assertEquals(5, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 9)); + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", -1)); + assertEquals(-1, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "B", 0)); + assertEquals(0, StringUtils.lastIndexOfIgnoreCase("aabaabaa", "A", 0)); + assertEquals(1, StringUtils.lastIndexOfIgnoreCase("aab", "AB", 1)); + } + @Test public void testLastOrdinalIndexOf() { assertEquals(-1, StringUtils.lastOrdinalIndexOf(null, "*", 42) ); diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java index 5207b388e7b..aae687db543 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java @@ -32,6 +32,36 @@ public class StringUtilsStartsEndsWithTest extends AbstractLangTest { private static final String BAR = "BAR"; private static final String FOOBAR = "FOOBAR"; + /** + * Test StringUtils.endsWith() + */ + @Test + public void testEndsWith() { + assertTrue(StringUtils.endsWith(null, null), "endsWith(null, null)"); + assertFalse(StringUtils.endsWith(FOOBAR, null), "endsWith(FOOBAR, null)"); + assertFalse(StringUtils.endsWith(null, FOO), "endsWith(null, FOO)"); + assertTrue(StringUtils.endsWith(FOOBAR, ""), "endsWith(FOOBAR, \"\")"); + + assertFalse(StringUtils.endsWith(foobar, foo), "endsWith(foobar, foo)"); + assertFalse(StringUtils.endsWith(FOOBAR, FOO), "endsWith(FOOBAR, FOO)"); + assertFalse(StringUtils.endsWith(foobar, FOO), "endsWith(foobar, FOO)"); + assertFalse(StringUtils.endsWith(FOOBAR, foo), "endsWith(FOOBAR, foo)"); + + assertFalse(StringUtils.endsWith(foo, foobar), "endsWith(foo, foobar)"); + assertFalse(StringUtils.endsWith(bar, foobar), "endsWith(foo, foobar)"); + + assertTrue(StringUtils.endsWith(foobar, bar), "endsWith(foobar, bar)"); + assertTrue(StringUtils.endsWith(FOOBAR, BAR), "endsWith(FOOBAR, BAR)"); + assertFalse(StringUtils.endsWith(foobar, BAR), "endsWith(foobar, BAR)"); + assertFalse(StringUtils.endsWith(FOOBAR, bar), "endsWith(FOOBAR, bar)"); + + // "alpha, beta, gamma, delta".endsWith("delta") + assertTrue(StringUtils.endsWith("\u03B1\u03B2\u03B3\u03B4", "\u03B4"), + "endsWith(\u03B1\u03B2\u03B3\u03B4, \u03B4)"); + // "alpha, beta, gamma, delta".endsWith("gamma, DELTA") + assertFalse(StringUtils.endsWith("\u03B1\u03B2\u03B3\u03B4", "\u03B3\u0394"), + "endsWith(\u03B1\u03B2\u03B3\u03B4, \u03B3\u0394)"); + } @Test public void testEndsWithAny() { @@ -62,6 +92,65 @@ public void testEndsWithAny() { assertTrue(StringUtils.endsWithAny(new StringBuffer("abcxyz"), new StringBuilder("abc"), new StringBuffer("xyz")), "StringUtils.endsWithAny(StringBuffer(abcxyz), StringBuilder(abc), StringBuffer(xyz))"); } + /** + * Test StringUtils.endsWithIgnoreCase() + */ + @Test + public void testEndsWithIgnoreCase() { + assertTrue(StringUtils.endsWithIgnoreCase(null, null), "endsWithIgnoreCase(null, null)"); + assertFalse(StringUtils.endsWithIgnoreCase(FOOBAR, null), "endsWithIgnoreCase(FOOBAR, null)"); + assertFalse(StringUtils.endsWithIgnoreCase(null, FOO), "endsWithIgnoreCase(null, FOO)"); + assertTrue(StringUtils.endsWithIgnoreCase(FOOBAR, ""), "endsWithIgnoreCase(FOOBAR, \"\")"); + + assertFalse(StringUtils.endsWithIgnoreCase(foobar, foo), "endsWithIgnoreCase(foobar, foo)"); + assertFalse(StringUtils.endsWithIgnoreCase(FOOBAR, FOO), "endsWithIgnoreCase(FOOBAR, FOO)"); + assertFalse(StringUtils.endsWithIgnoreCase(foobar, FOO), "endsWithIgnoreCase(foobar, FOO)"); + assertFalse(StringUtils.endsWithIgnoreCase(FOOBAR, foo), "endsWithIgnoreCase(FOOBAR, foo)"); + + assertFalse(StringUtils.endsWithIgnoreCase(foo, foobar), "endsWithIgnoreCase(foo, foobar)"); + assertFalse(StringUtils.endsWithIgnoreCase(bar, foobar), "endsWithIgnoreCase(foo, foobar)"); + + assertTrue(StringUtils.endsWithIgnoreCase(foobar, bar), "endsWithIgnoreCase(foobar, bar)"); + assertTrue(StringUtils.endsWithIgnoreCase(FOOBAR, BAR), "endsWithIgnoreCase(FOOBAR, BAR)"); + assertTrue(StringUtils.endsWithIgnoreCase(foobar, BAR), "endsWithIgnoreCase(foobar, BAR)"); + assertTrue(StringUtils.endsWithIgnoreCase(FOOBAR, bar), "endsWithIgnoreCase(FOOBAR, bar)"); + + // javadoc + assertTrue(StringUtils.endsWithIgnoreCase("abcdef", "def")); + assertTrue(StringUtils.endsWithIgnoreCase("ABCDEF", "def")); + assertFalse(StringUtils.endsWithIgnoreCase("ABCDEF", "cde")); + + // "alpha, beta, gamma, delta".endsWith("DELTA") + assertTrue(StringUtils.endsWithIgnoreCase("\u03B1\u03B2\u03B3\u03B4", "\u0394"), + "endsWith(\u03B1\u03B2\u03B3\u03B4, \u0394)"); + // "alpha, beta, gamma, delta".endsWith("GAMMA") + assertFalse(StringUtils.endsWithIgnoreCase("\u03B1\u03B2\u03B3\u03B4", "\u0393"), + "endsWith(\u03B1\u03B2\u03B3\u03B4, \u0393)"); + } + + /** + * Test StringUtils.startsWith() + */ + @Test + public void testStartsWith() { + assertTrue(StringUtils.startsWith(null, null), "startsWith(null, null)"); + assertFalse(StringUtils.startsWith(FOOBAR, null), "startsWith(FOOBAR, null)"); + assertFalse(StringUtils.startsWith(null, FOO), "startsWith(null, FOO)"); + assertTrue(StringUtils.startsWith(FOOBAR, ""), "startsWith(FOOBAR, \"\")"); + + assertTrue(StringUtils.startsWith(foobar, foo), "startsWith(foobar, foo)"); + assertTrue(StringUtils.startsWith(FOOBAR, FOO), "startsWith(FOOBAR, FOO)"); + assertFalse(StringUtils.startsWith(foobar, FOO), "startsWith(foobar, FOO)"); + assertFalse(StringUtils.startsWith(FOOBAR, foo), "startsWith(FOOBAR, foo)"); + + assertFalse(StringUtils.startsWith(foo, foobar), "startsWith(foo, foobar)"); + assertFalse(StringUtils.startsWith(bar, foobar), "startsWith(foo, foobar)"); + + assertFalse(StringUtils.startsWith(foobar, bar), "startsWith(foobar, bar)"); + assertFalse(StringUtils.startsWith(FOOBAR, BAR), "startsWith(FOOBAR, BAR)"); + assertFalse(StringUtils.startsWith(foobar, BAR), "startsWith(foobar, BAR)"); + assertFalse(StringUtils.startsWith(FOOBAR, bar), "startsWith(FOOBAR, bar)"); + } @Test public void testStartsWithAny() { @@ -80,4 +169,28 @@ public void testStartsWithAny() { assertTrue(StringUtils.startsWithAny(new StringBuffer("abcxyz"), new StringBuilder("xyz"), new StringBuffer("abc")), "StringUtils.startsWithAny(StringBuffer(abcxyz), StringBuilder(xyz), StringBuffer(abc))"); } + /** + * Test StringUtils.testStartsWithIgnoreCase() + */ + @Test + public void testStartsWithIgnoreCase() { + assertTrue(StringUtils.startsWithIgnoreCase(null, null), "startsWithIgnoreCase(null, null)"); + assertFalse(StringUtils.startsWithIgnoreCase(FOOBAR, null), "startsWithIgnoreCase(FOOBAR, null)"); + assertFalse(StringUtils.startsWithIgnoreCase(null, FOO), "startsWithIgnoreCase(null, FOO)"); + assertTrue(StringUtils.startsWithIgnoreCase(FOOBAR, ""), "startsWithIgnoreCase(FOOBAR, \"\")"); + + assertTrue(StringUtils.startsWithIgnoreCase(foobar, foo), "startsWithIgnoreCase(foobar, foo)"); + assertTrue(StringUtils.startsWithIgnoreCase(FOOBAR, FOO), "startsWithIgnoreCase(FOOBAR, FOO)"); + assertTrue(StringUtils.startsWithIgnoreCase(foobar, FOO), "startsWithIgnoreCase(foobar, FOO)"); + assertTrue(StringUtils.startsWithIgnoreCase(FOOBAR, foo), "startsWithIgnoreCase(FOOBAR, foo)"); + + assertFalse(StringUtils.startsWithIgnoreCase(foo, foobar), "startsWithIgnoreCase(foo, foobar)"); + assertFalse(StringUtils.startsWithIgnoreCase(bar, foobar), "startsWithIgnoreCase(foo, foobar)"); + + assertFalse(StringUtils.startsWithIgnoreCase(foobar, bar), "startsWithIgnoreCase(foobar, bar)"); + assertFalse(StringUtils.startsWithIgnoreCase(FOOBAR, BAR), "startsWithIgnoreCase(FOOBAR, BAR)"); + assertFalse(StringUtils.startsWithIgnoreCase(foobar, BAR), "startsWithIgnoreCase(foobar, BAR)"); + assertFalse(StringUtils.startsWithIgnoreCase(FOOBAR, bar), "startsWithIgnoreCase(FOOBAR, bar)"); + } + }