Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified src/test/java/org/apache/commons/text/OssFuzzTest.java
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public void setUp() throws Exception {
}

@AfterEach
public void tearDown() throws Exception {
public void tearDown() {
values = null;
}

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 27 additions & 1 deletion src/test/java/org/apache/commons/text/WordUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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("."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
}
};
Expand Down Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}