diff --git a/src/test/java/org/apache/commons/text/OssFuzzTest.java b/src/test/java/org/apache/commons/text/OssFuzzTest.java index 0b4e1a555a..c3772e5a37 100644 Binary files a/src/test/java/org/apache/commons/text/OssFuzzTest.java and b/src/test/java/org/apache/commons/text/OssFuzzTest.java differ diff --git a/src/test/java/org/apache/commons/text/StringSubstitutorTest.java b/src/test/java/org/apache/commons/text/StringSubstitutorTest.java index 75f112ba6b..4022fc3da1 100644 --- a/src/test/java/org/apache/commons/text/StringSubstitutorTest.java +++ b/src/test/java/org/apache/commons/text/StringSubstitutorTest.java @@ -193,7 +193,7 @@ public void setUp() throws Exception { } @AfterEach - public void tearDown() throws Exception { + public void tearDown() { values = null; } @@ -1085,4 +1085,10 @@ void testSubstitutePreserveEscape() throws IOException { assertEqualsCharSeq("value $${escaped}", replace(sub, org)); } + @Test + void testToString() { + final StringSubstitutor s = new StringSubstitutor(null, "prefix", "suffix"); + String str = s.toString(); + assertTrue(str.contains("\"prefix\""), "Had: " + str); + } } diff --git a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java index ff2cc7d00b..1900c19d68 100644 --- a/src/test/java/org/apache/commons/text/TextStringBuilderTest.java +++ b/src/test/java/org/apache/commons/text/TextStringBuilderTest.java @@ -550,6 +550,13 @@ void testConstructorCharSequence() { assertEquals(length, sb.toCharArray().length); } + @Test + void testConstructorCharSequenceNull() { + final TextStringBuilder sb = new TextStringBuilder((CharSequence) null); + assertEquals(TextStringBuilder.CAPACITY, sb.capacity()); + assertEquals(0, sb.toCharArray().length); + } + @Test void testConstructorDefault() { final TextStringBuilder sb = new TextStringBuilder(); diff --git a/src/test/java/org/apache/commons/text/WordUtilsTest.java b/src/test/java/org/apache/commons/text/WordUtilsTest.java index facbf31c33..5ae96de47a 100644 --- a/src/test/java/org/apache/commons/text/WordUtilsTest.java +++ b/src/test/java/org/apache/commons/text/WordUtilsTest.java @@ -177,14 +177,16 @@ void testConstructor() { @Test void testContainsAllWords_StringString() { - assertFalse(WordUtils.containsAllWords(null, (String) null)); + assertFalse(WordUtils.containsAllWords(null)); assertFalse(WordUtils.containsAllWords(null, "")); assertFalse(WordUtils.containsAllWords(null, "ab")); + assertFalse(WordUtils.containsAllWords("")); assertFalse(WordUtils.containsAllWords("", (String) null)); assertFalse(WordUtils.containsAllWords("", "")); assertFalse(WordUtils.containsAllWords("", "ab")); + assertFalse(WordUtils.containsAllWords("foo")); assertFalse(WordUtils.containsAllWords("foo", (String) null)); assertFalse(WordUtils.containsAllWords("bar", "")); assertFalse(WordUtils.containsAllWords("zzabyycdxx", "by")); @@ -321,6 +323,30 @@ void testInitialsSurrogatePairs() { new char[] { '\uD800', '\uDF14', '\uD800', '\uDF18' })); } + @Test + void testIsDelimiter() { + assertFalse(WordUtils.isDelimiter('.', null)); + assertTrue(WordUtils.isDelimiter(' ', null)); + + assertFalse(WordUtils.isDelimiter(' ', new char[] { '.' })); + assertTrue(WordUtils.isDelimiter('.', new char[] { '.' })); + + assertFalse(WordUtils.isDelimiter(' ', new char[] { '.', '_', 'a' })); + assertTrue(WordUtils.isDelimiter('.', new char[] { '.', '_', 'a', '.' })); + } + + @Test + void testIsDelimiterCodePoint() { + assertFalse(WordUtils.isDelimiter((int) '.', null)); + assertTrue(WordUtils.isDelimiter((int) ' ', null)); + + assertFalse(WordUtils.isDelimiter((int) ' ', new char[] { '.' })); + assertTrue(WordUtils.isDelimiter((int) '.', new char[] { '.' })); + + assertFalse(WordUtils.isDelimiter((int) ' ', new char[] { '.', '_', 'a' })); + assertTrue(WordUtils.isDelimiter((int) '.', new char[] { '.', '_', 'a', '.' })); + } + @Test void testLANG1292() { // Prior to fix, this was throwing StringIndexOutOfBoundsException diff --git a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java index bdc6cac1f3..43e6f237f1 100644 --- a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java +++ b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java @@ -257,6 +257,14 @@ void testSingletons() { assertSame(XmlEncoderStringLookup.INSTANCE, stringLookupFactory.xmlEncoderStringLookup()); } + /** + * Tests that we return the singleton. + */ + @Test + void testDeprecatedSingletons() { + assertSame(StringLookupFactory.INSTANCE_BASE64_DECODER, StringLookupFactory.INSTANCE.base64StringLookup()); + } + @Test void testXmlStringLookup() { final StringLookupFactory stringLookupFactory = StringLookupFactory.INSTANCE; @@ -279,4 +287,9 @@ void testXmlStringLookupExternalEntityOn() { assertEquals(XmlStringLookupTest.DATA, StringLookupFactory.INSTANCE.xmlStringLookup(XmlStringLookupTest.EMPTY_MAP).apply(key).trim()); } + @Test + void testClear() { + // this will clear out the global cache in ConstantStringLookup + StringLookupFactory.clear(); + } } diff --git a/src/test/java/org/apache/commons/text/similarity/IntersectionResultTest.java b/src/test/java/org/apache/commons/text/similarity/IntersectionResultTest.java index 5bb15d6b87..70230b86cb 100644 --- a/src/test/java/org/apache/commons/text/similarity/IntersectionResultTest.java +++ b/src/test/java/org/apache/commons/text/similarity/IntersectionResultTest.java @@ -36,7 +36,7 @@ void testEquals() { }; // Test a different instance with same values - Assertions.assertEquals(results[0], new IntersectionResult(0, 0, 0)); + Assertions.assertEquals(new IntersectionResult(0, 0, 0), results[0]); final Object something = new Object(); for (int i = 0; i < results.length; i++) { @@ -45,6 +45,9 @@ void testEquals() { for (int j = 0; j < results.length; j++) { Assertions.assertEquals(results[i].equals(results[j]), i == j); } + + Assertions.assertFalse(results[i].equals(null), "Should not be Equal to null"); + Assertions.assertNotEquals("Test", results[i], "Should not be Equal to a different type of object"); } } diff --git a/src/test/java/org/apache/commons/text/similarity/LevenshteinResultsTest.java b/src/test/java/org/apache/commons/text/similarity/LevenshteinResultsTest.java index af8f5d92ba..ac93db0605 100644 --- a/src/test/java/org/apache/commons/text/similarity/LevenshteinResultsTest.java +++ b/src/test/java/org/apache/commons/text/similarity/LevenshteinResultsTest.java @@ -40,6 +40,15 @@ void testEqualsReturningFalse() { assertFalse(levenshteinResults.equals(levenshteinResultsTwo)); } + @Test + void testEqualsDifferentDistance() { + final Integer integerOne = 1662; + final Integer integerTwo = 1164; + final LevenshteinResults levenshteinResults = new LevenshteinResults(integerOne, integerOne, integerOne, integerOne); + final LevenshteinResults levenshteinResultsTwo = new LevenshteinResults(integerTwo, integerOne, integerOne, integerOne); + assertFalse(levenshteinResults.equals(levenshteinResultsTwo)); + } + @Test void testEqualsSameObject() { final Integer integer = 1662; @@ -62,4 +71,11 @@ void testEqualsWithNull() { assertFalse(levenshteinResults.equals(null)); } + @Test + void testEqualsWithDifferentObject() { + final Integer integer = -647; + final LevenshteinResults levenshteinResults = new LevenshteinResults(integer, null, null, integer); + assertFalse(levenshteinResults.equals("Test")); + } + } diff --git a/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java b/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java index 2ca0a4b994..8063c911e7 100644 --- a/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java +++ b/src/test/java/org/apache/commons/text/similarity/LongestCommonSubsequenceTest.java @@ -89,6 +89,8 @@ void testLogestCommonSubsequence() { assertEquals("", subject.logestCommonSubsequence("", "")); assertEquals("", subject.logestCommonSubsequence("left", "")); assertEquals("", subject.logestCommonSubsequence("", "right")); + assertEquals("", subject.logestCommonSubsequence("l", "a")); + assertEquals("", subject.logestCommonSubsequence("left", "a")); assertEquals("fog", subject.logestCommonSubsequence("frog", "fog")); assertEquals("", subject.logestCommonSubsequence("fly", "ant")); assertEquals("h", subject.logestCommonSubsequence("elephant", "hippo")); @@ -106,6 +108,8 @@ void testLongestCommonSubsequence() { assertEquals("", subject.longestCommonSubsequence("", "")); assertEquals("", subject.longestCommonSubsequence("left", "")); assertEquals("", subject.longestCommonSubsequence("", "right")); + assertEquals("", subject.longestCommonSubsequence("l", "a")); + assertEquals("", subject.longestCommonSubsequence("left", "a")); assertEquals("fog", subject.longestCommonSubsequence("frog", "fog")); assertEquals("", subject.longestCommonSubsequence("fly", "ant")); assertEquals("h", subject.longestCommonSubsequence("elephant", "hippo")); diff --git a/src/test/java/org/apache/commons/text/translate/CharSequenceTranslatorTest.java b/src/test/java/org/apache/commons/text/translate/CharSequenceTranslatorTest.java index 7585f71df1..3bb3328377 100644 --- a/src/test/java/org/apache/commons/text/translate/CharSequenceTranslatorTest.java +++ b/src/test/java/org/apache/commons/text/translate/CharSequenceTranslatorTest.java @@ -17,9 +17,11 @@ package org.apache.commons.text.translate; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.IOException; +import java.io.UncheckedIOException; import java.io.Writer; import org.junit.jupiter.api.Test; @@ -46,8 +48,20 @@ void testWith() throws IOException { final CharSequenceTranslator charSequenceTranslatorThree = new TestCharSequenceTranslator(); final CharSequenceTranslator aggregatedTranslator = charSequenceTranslatorOne.with(charSequenceTranslatorTwo, charSequenceTranslatorThree); aggregatedTranslator.translate("", 0, null); - assertTrue(aggregatedTranslator instanceof AggregateTranslator); + assertInstanceOf(AggregateTranslator.class, aggregatedTranslator); assertEquals(3, translateInvocationCounter); } + @Test + void testIOException() { + final CharSequenceTranslator translator = new CharSequenceTranslator() { + @Override + public int translate(CharSequence input, int index, Writer writer) throws IOException { + throw new IOException("Test exception"); + } + }; + + assertThrows(UncheckedIOException.class, + () -> translator.translate(".")); + } } diff --git a/src/test/java/org/apache/commons/text/translate/OctalUnescaperTest.java b/src/test/java/org/apache/commons/text/translate/OctalUnescaperTest.java index e9b78b44b7..1f7d4b7be3 100644 --- a/src/test/java/org/apache/commons/text/translate/OctalUnescaperTest.java +++ b/src/test/java/org/apache/commons/text/translate/OctalUnescaperTest.java @@ -38,6 +38,10 @@ void testBetween() { result = oue.translate(input); assertEquals("\377", result, "Failed to unescape octal characters via the between method"); + input = "\\777"; + result = oue.translate(input); + assertEquals("\777", result, "Failed to unescape octal characters via the between method"); + input = "\\377 and"; result = oue.translate(input); assertEquals("\377 and", result, "Failed to unescape octal characters via the between method"); @@ -79,4 +83,31 @@ void testBetween() { assertEquals("\\999", result, "Failed to ignore an out of range octal character via the between method"); } + @Test + void testInvalid() { + final OctalUnescaper oue = new OctalUnescaper(); + final String input = "\\4a"; + final String result = oue.translate(input); + assertEquals("\4a", result, "Failed to unescape octal characters via the between method"); + } + + @Test + void testHighLowSurrogate() { + final OctalUnescaper oue = new OctalUnescaper(); + String input = "\\377\uD800and"; + String result = oue.translate(input); + assertEquals("\377\uD800and", result, "Failed to unescape octal characters via the between method"); + + input = "\\377\uD83D\uDE80and"; + result = oue.translate(input); + assertEquals("\377\uD83D\uDE80and", result, "Failed to unescape octal characters via the between method"); + + input = "\\377\uD83D\uDC00and"; + result = oue.translate(input); + assertEquals("\377\uD83D\uDC00and", result, "Failed to unescape octal characters via the between method"); + + input = "\\377\uD83D"; + result = oue.translate(input); + assertEquals("\377\uD83D", result, "Failed to unescape octal characters via the between method"); + } } diff --git a/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java b/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java index 37e6fdebd0..12ade3f325 100644 --- a/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java +++ b/src/test/java/org/apache/commons/text/translate/SinglePassTranslatorTest.java @@ -19,7 +19,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import java.io.IOException; import java.io.StringWriter; import java.io.Writer; @@ -34,7 +33,7 @@ class SinglePassTranslatorTest { private final SinglePassTranslator dummyTranslator = new SinglePassTranslator() { @Override - void translateWhole(final CharSequence input, final Writer writer) throws IOException { + void translateWhole(final CharSequence input, final Writer writer) { // noop } }; @@ -63,4 +62,15 @@ void testTranslateThrowsIllegalArgumentException() { assertThrows(IllegalArgumentException.class, () -> dummyTranslator.translate("(,Fk", 647, null)); } + @Test + void testTranslateThrowsIllegalArgumentExceptionWithNonAnonymousClass() { + assertThrows(IllegalArgumentException.class, () -> new TestTranslator().translate("(,Fk", 647, null)); + } + + private static final class TestTranslator extends SinglePassTranslator { + @Override + void translateWhole(final CharSequence input, final Writer writer) { + // noop + } + } } diff --git a/src/test/java/org/apache/commons/text/translate/UnicodeUnescaperTest.java b/src/test/java/org/apache/commons/text/translate/UnicodeUnescaperTest.java index cd106c66c4..f20d710678 100644 --- a/src/test/java/org/apache/commons/text/translate/UnicodeUnescaperTest.java +++ b/src/test/java/org/apache/commons/text/translate/UnicodeUnescaperTest.java @@ -49,4 +49,9 @@ void testUuuuu() { final String result = escaper.translate(input); assertEquals("G", result, "Failed to unescape Unicode characters with many 'u' characters"); } + + @Test + void testTooShort() { + assertThrows(IllegalArgumentException.class, () -> new UnicodeUnescaper().translate("\\u")); + } }