diff --git a/src/test/java/org/apache/commons/text/AlphabetConverterPartitionTests.java b/src/test/java/org/apache/commons/text/AlphabetConverterPartitionTests.java new file mode 100644 index 0000000000..ee83c446e0 --- /dev/null +++ b/src/test/java/org/apache/commons/text/AlphabetConverterPartitionTests.java @@ -0,0 +1,74 @@ +package org.apache.commons.text; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.UnsupportedEncodingException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class AlphabetConverterPartitionTests { + private static AlphabetConverter converter; + + @BeforeAll + static void initConverter() { + Character[] original = {'a', 'b', 'c', 'd'}; + Character[] encodings = {'0', '1', '2', 'd'}; + Character[] doNotEncode = {'d'}; + converter = AlphabetConverter.createConverterFromChars(original, encodings, doNotEncode); + } + + @Test + void encodeSimpleString() throws UnsupportedEncodingException { + String input = "abc"; + assertEquals("012", converter.encode(input)); + } + + @Test + void decodeSimpleString() throws UnsupportedEncodingException { + String input = "012"; + assertEquals("abc", converter.decode(input)); + } + + @Test + void encodeAndDecodeEmptyString() throws UnsupportedEncodingException { + String input = ""; + assertEquals("", converter.encode(input)); + assertEquals("", converter.decode(input)); + } + + @Test + void handlesSingleChar() throws UnsupportedEncodingException { + String input = "a"; + String encoded = "0"; + assertEquals("0", converter.encode(input)); + assertEquals("a", converter.decode(encoded)); + } + + @Test + void doesNotHandleDoNotEncodeChars() throws UnsupportedEncodingException { + String input = "d"; + String encoded = "d"; + assertEquals("d", converter.encode(input)); + assertEquals("d", converter.decode(encoded)); + } + + @Test + void throwsOnUnknownChars() { + assertThrows(UnsupportedEncodingException.class, () -> converter.encode("z")); + } + + @Test + void throwsOnUnknownNums() { + assertThrows(UnsupportedEncodingException.class, () -> converter.decode("3")); + } + + @Test + void handlesMixedValidChars() throws UnsupportedEncodingException { + String input = "abcd"; + String encoded = "012d"; + assertEquals("012d", converter.encode(input)); + assertEquals("abcd", converter.decode(encoded)); + } +} diff --git a/src/test/java/org/apache/commons/text/CaseUtilsPartitionTests.java b/src/test/java/org/apache/commons/text/CaseUtilsPartitionTests.java new file mode 100644 index 0000000000..aeb7bd6cdf --- /dev/null +++ b/src/test/java/org/apache/commons/text/CaseUtilsPartitionTests.java @@ -0,0 +1,73 @@ +package org.apache.commons.text; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class CaseUtilsPartitionTests { + + @Test + void testNullInput() { + assertNull(CaseUtils.toCamelCase(null, false)); + } + + @Test + void testEmptyString() { + assertEquals("", CaseUtils.toCamelCase("", false)); + } + + @Test + void testOnlyDelimiters() { + assertEquals("", CaseUtils.toCamelCase(" ", false)); + assertEquals("", CaseUtils.toCamelCase("@@@", false, '@')); + } + + @Test + void testNoDelimiters() { + assertEquals("teststring", CaseUtils.toCamelCase("teststring", false)); + assertEquals("Teststring", CaseUtils.toCamelCase("teststring", true)); + } + + @Test + void testDelimitersAtStartMiddleEnd() { + assertEquals("ohHiThere", CaseUtils.toCamelCase(" oh hi there ", false)); + assertEquals("OhHiThere", CaseUtils.toCamelCase(" oh hi there ", true)); + assertEquals("ohHiThere", CaseUtils.toCamelCase("-oh-hi-there-", false, '-')); + } + + @Test + void testCapitalizeFirstLetterTrue() { + assertEquals("TacoCat", CaseUtils.toCamelCase("taco cat", true)); + } + + @Test + void testCapitalizeFirstLetterFalse() { + assertEquals("tacoCat", CaseUtils.toCamelCase("taco cat", false)); + } + + @Test + void tesTNullDelimiters() { + assertEquals("tacoCat", CaseUtils.toCamelCase("taco cat", false, null)); + } + + @Test + void testEmptyDelimiterArray() { + assertEquals("tacocat", CaseUtils.toCamelCase("tacocat", false, new char[0])); + } + + @Test + void testCustomDelimiters() { + assertEquals("ohHiThere", CaseUtils.toCamelCase("oh@hi@there", false, '@')); + assertEquals("ohHiThere", CaseUtils.toCamelCase("oh.hi.there", false, '.')); + assertEquals("ohHiThere", CaseUtils.toCamelCase("oh-hi-there", false, '-')); + } + + @Test + void testMixedDelimiters() { + assertEquals("ohHiThere", CaseUtils.toCamelCase("oh-hi.there", false, '-', '.')); + } + +} diff --git a/src/test/java/org/apache/commons/text/CompositeFormatMockingTests.java b/src/test/java/org/apache/commons/text/CompositeFormatMockingTests.java new file mode 100644 index 0000000000..1a169b8849 --- /dev/null +++ b/src/test/java/org/apache/commons/text/CompositeFormatMockingTests.java @@ -0,0 +1,58 @@ +package org.apache.commons.text; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.text.FieldPosition; +import java.text.Format; +import java.text.ParseException; +import java.text.ParsePosition; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class CompositeFormatMockingTests { + + Format mockParser = mock(Format.class); + Format mockFormatter = mock(Format.class); + CompositeFormat composite = new CompositeFormat(mockParser, mockFormatter); + + @Test + void testFormatDelegatesToFormatter() { + StringBuffer buffer = new StringBuffer(); + FieldPosition pos = new FieldPosition(0); + + when(mockFormatter.format(any(), any(StringBuffer.class), any(FieldPosition.class))) + .thenAnswer(invocation -> { + StringBuffer b = invocation.getArgument(1); + b.append("formatted"); + return b; + }); + + StringBuffer result = composite.format("input", buffer, pos); + + assertEquals("formatted", result.toString()); + } + + @Test + void testParseObjectDelegatesToParser() { + ParsePosition parsePosition = new ParsePosition(0); + when(mockParser.parseObject(anyString(), any(ParsePosition.class))).thenReturn("parsedObject"); + + Object parsed = composite.parseObject("inputString", parsePosition); + + assertEquals("parsedObject", parsed); + } + + @Test + void testReformatParseThrowsException() throws ParseException { + when(mockParser.parseObject(anyString())).thenThrow(new ParseException("fail", 0)); + + assertThrows(ParseException.class, () -> composite.reformat("badinput")); + } +} diff --git a/src/test/java/org/apache/commons/text/RandomStringGeneratorPartitionTests.java b/src/test/java/org/apache/commons/text/RandomStringGeneratorPartitionTests.java new file mode 100644 index 0000000000..0ea8bfffef --- /dev/null +++ b/src/test/java/org/apache/commons/text/RandomStringGeneratorPartitionTests.java @@ -0,0 +1,52 @@ +package org.apache.commons.text; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class RandomStringGeneratorPartitionTests { + @Test + void testZeroLength() { + RandomStringGenerator generator = RandomStringGenerator.builder().build(); + assertEquals("", generator.generate(0)); + } + + @Test + void testNegativeLengthThrows() { + RandomStringGenerator generator = RandomStringGenerator.builder().build(); + assertThrows(IllegalArgumentException.class, () -> generator.generate(-1)); + } + + @Test + void testPositiveLength() { + RandomStringGenerator generator = RandomStringGenerator.builder().withinRange('a', 'z').build(); + String result = generator.generate(10); + assertEquals(10, result.length()); + assertTrue(result.chars().allMatch(c -> c >= 'a' && c <= 'z')); + } + + @Test + void testGenerateWithCharacterList() { + RandomStringGenerator generator = RandomStringGenerator.builder().selectFrom('X', 'Y', 'Z').build(); + String result = generator.generate(20); + assertTrue(result.chars().allMatch(c -> c == 'X' || c == 'Y' || c == 'Z')); + } + + @Test + void testGenerateFilteredByPredicate() { + CharacterPredicate vowelPredicate = c -> "aeiouAEIOU".indexOf(c) != -1; + RandomStringGenerator generator = RandomStringGenerator.builder().withinRange('a', 'z').filteredBy(vowelPredicate).build(); + String result = generator.generate(30); + assertTrue(result.chars().allMatch(c -> "aeiouAEIOU".indexOf(c) != -1)); + } + + @Test + void testWithinRangeSingleCharacter() { + RandomStringGenerator generator = RandomStringGenerator.builder().withinRange('M', 'M').build(); + String result = generator.generate(5); + assertEquals("MMMMM", result); + } +} diff --git a/src/test/java/org/apache/commons/text/StringSubstitutorReplacePartitionTests.java b/src/test/java/org/apache/commons/text/StringSubstitutorReplacePartitionTests.java new file mode 100644 index 0000000000..a551cb4e5d --- /dev/null +++ b/src/test/java/org/apache/commons/text/StringSubstitutorReplacePartitionTests.java @@ -0,0 +1,72 @@ +package org.apache.commons.text; + +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class StringSubstitutorReplacePartitionTests { + + @Test + void testReplaceFuncWithSimpleVars() { + Map input = new HashMap<>(); + input.put("cat", "stand"); + StringSubstitutor sub = new StringSubstitutor(input); + assertEquals("Taco stand", sub.replace("Taco ${cat}")); + } + + @Test + void testReplaceFuncWithNoVars() { + StringSubstitutor sub = new StringSubstitutor(); + assertEquals("Taco cat", sub.replace("Taco cat")); + } + + @Test + void testReplaceFuncWithNullVars() { + StringSubstitutor sub = new StringSubstitutor(); + assertNull(sub.replace((String) null)); + } + + @Test + void testReplaceFuncWithEmptyVars() { + StringSubstitutor sub = new StringSubstitutor(); + assertEquals("", sub.replace("")); + } + + @Test + void testReplaceFuncWithInvalidVars() { + Map input = new HashMap<>(); + input.put("cat", "stand"); + StringSubstitutor sub = new StringSubstitutor(input); + assertEquals("Taco ${bell}", sub.replace("Taco ${bell}")); + } + + @Test + void testReplaceWithEscapeChar() { + Map input = new HashMap<>(); + input.put("cat", "stand"); + StringSubstitutor sub = new StringSubstitutor(input); + assertEquals("Taco ${cat}", sub.replace("Taco $${cat}")); + } + + @Test + void testReplaceWithDefaultVal() { + Map input = new HashMap<>(); + input.put("cat", "stand"); + StringSubstitutor sub = new StringSubstitutor(input); + assertEquals("Taco stand goes to taco bell", sub.replace("Taco ${cat} goes to taco ${restaurant:-bell}")); + } + + @Test + void testReplaceWithDefaultValFilled() { + Map input = new HashMap<>(); + input.put("cat", "stand"); + input.put("restaurant", "emporium"); + StringSubstitutor sub = new StringSubstitutor(input); + assertEquals("Taco stand goes to taco emporium", sub.replace("Taco ${cat} goes to taco ${restaurant:-bell}")); + } +} diff --git a/src/test/java/org/apache/commons/text/WordUtilsAbbreviatePartitionTests.java b/src/test/java/org/apache/commons/text/WordUtilsAbbreviatePartitionTests.java new file mode 100644 index 0000000000..52d09d509e --- /dev/null +++ b/src/test/java/org/apache/commons/text/WordUtilsAbbreviatePartitionTests.java @@ -0,0 +1,39 @@ +package org.apache.commons.text; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.*; + +public class WordUtilsAbbreviatePartitionTests { + + @ParameterizedTest + @CsvSource({ + "'Once upon a time there was a witch', 'Once...', 0, 40, '...'", + "'Once upon a time there was a witch', 'Once upon a...', 10, 40, '...'", + "'The end', 'The...', 0, 40, '...'", + "'Once upon a time there was a witch', 'Once upon a...', 10, -1, '...'", + "'Finale', 'Finale', 0, 10, '...'", + "'', '', 10, -1, '...'", + + }) + void abbreviateTestsWithAnOutputs(String input, String res, int lowerLimit, int upperLimit, String appendStr) { + assertEquals(res, WordUtils.abbreviate(input, lowerLimit, upperLimit, appendStr)); + } + + @Test + void abbreviateTestWithNullReturns() { + assertNull(WordUtils.abbreviate(null, 0, 10, "...")); + } + + @Test + void imbalancedLimitsThrows() { + assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate("input", 10, 5, "...")); + } + + @Test + void tooManyNegativeThrows() { + assertThrows(IllegalArgumentException.class, () -> WordUtils.abbreviate("input", 5, -2, "...")); + } +} diff --git a/src/test/java/org/apache/commons/text/WordUtilsCapitalizePartitionTests.java b/src/test/java/org/apache/commons/text/WordUtilsCapitalizePartitionTests.java new file mode 100644 index 0000000000..72a8c77bb6 --- /dev/null +++ b/src/test/java/org/apache/commons/text/WordUtilsCapitalizePartitionTests.java @@ -0,0 +1,41 @@ +package org.apache.commons.text; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class WordUtilsCapitalizePartitionTests { + @Test + void testCapitalizeWhitespaceDelimited() { + String input = "once upon a time"; + String result = WordUtils.capitalize(input); + assertEquals("Once Upon A Time", result); + } + + @Test + void testCapitalizeWithCustomDelimiters() { + String input = "once uPon.a time"; + String result = WordUtils.capitalize(input, '.'); + assertEquals("Once uPon.A time", result); + } + + @Test + void testCapitalizeEmptyString() { + assertEquals("", WordUtils.capitalize("")); + } + + @Test + void testCapitalizeNullInput() { + assertNull(WordUtils.capitalize(null)); + } + + @Test + void testCapitalizeWithAllCaps() { + String input = "ONCE UPON A TIME"; + String result = WordUtils.capitalize(input); + assertEquals("ONCE UPON A TIME", result); + } +} diff --git a/src/test/java/org/apache/commons/text/WordUtilsIniitialsPartitionTests.java b/src/test/java/org/apache/commons/text/WordUtilsIniitialsPartitionTests.java new file mode 100644 index 0000000000..91bcdd1133 --- /dev/null +++ b/src/test/java/org/apache/commons/text/WordUtilsIniitialsPartitionTests.java @@ -0,0 +1,53 @@ +package org.apache.commons.text; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class WordUtilsIniitialsPartitionTests { + @Test + void testInitialsWhitespaceDelimited() { + String input = "Jake B Lee"; + String result = WordUtils.initials(input); + assertEquals("JBL", result); + } + + @Test + void testInitialsWithCustomDelimiters() { + String input = "Jake.B.Lee"; + String result = WordUtils.initials(input, ' ', '.'); + assertEquals("JBL", result); + } + + @Test + void testInitialsWithOneWord() { + String input = "Jake"; + String result = WordUtils.initials(input); + assertEquals("J", result); + } + + @Test + void testInitialsWithMoreThanThreeWords() { + String input = "Jake B Lee Jr The Third But Without His Shoes"; + String result = WordUtils.initials(input); + assertEquals("JBLJTTBWHS", result); + } + + @Test + void testInitialsEmptyString() { + assertEquals("", WordUtils.initials("")); + } + + @Test + void testInitialsNullInput() { + assertNull(WordUtils.initials(null)); + } + + @Test + void testInitialsOnlyDelimiters() { + String input = " . . "; + String result = WordUtils.initials(input, ' ', '.'); + assertEquals("", result); + } +} diff --git a/src/test/java/org/apache/commons/text/numbers/ParsedDecimalBoundaryTests.java b/src/test/java/org/apache/commons/text/numbers/ParsedDecimalBoundaryTests.java new file mode 100644 index 0000000000..17e45d0f49 --- /dev/null +++ b/src/test/java/org/apache/commons/text/numbers/ParsedDecimalBoundaryTests.java @@ -0,0 +1,91 @@ +package org.apache.commons.text.numbers; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class ParsedDecimalBoundaryTests { + @Test + void zeroParsesCorrectly() { + ParsedDecimal pd = ParsedDecimal.from(0.0); + assertFalse(pd.negative); + assertEquals(0, pd.digits[0]); + assertEquals(0, pd.getExponent()); + } + + @Test + void negativeZeroParsesCorrectly() { + ParsedDecimal pd = ParsedDecimal.from(-0.0); + assertTrue(pd.negative); + assertEquals(0, pd.digits[0]); + assertEquals(0, pd.getExponent()); + } + + @Test + void basicDecimalParsesCorrectly() { + ParsedDecimal pd = ParsedDecimal.from(1.7); + assertFalse(pd.negative); + assertArrayEquals(new int[]{1, 7}, pd.digits); + assertEquals(-1, pd.getExponent()); + } + + @Test + void negativeDecimalParsesCorrectly() { + ParsedDecimal pd = ParsedDecimal.from(-2.104); + assertTrue(pd.negative); + assertArrayEquals(new int[]{2,1,0,4}, pd.digits); + assertEquals(-3, pd.getExponent()); + } + + @Test + void largeDecimalParsesCorrectly() { + ParsedDecimal pd = ParsedDecimal.from(1.7976931348623157E306); + assertFalse(pd.negative); + assertArrayEquals(new int[]{1,7,9,7,6,9,3,1,3,4,8,6,2,3,1,5,6,0,0,0,0}, pd.digits); + assertEquals(290, pd.getExponent()); + } + + @Test + void maxDecimalParsesCorrectly() { + ParsedDecimal pd = ParsedDecimal.from(Double.MAX_VALUE); + assertFalse(pd.negative); + assertArrayEquals(new int[]{1,7,9,7,6,9,3,1,3,4,8,6,2,3,1,5,7,0,0,0,0}, pd.digits); + assertEquals(292, pd.getExponent()); + } + + @Test + void maxNegativeDecimalParsesCorrectly() { + ParsedDecimal pd = ParsedDecimal.from(-Double.MAX_VALUE); + assertTrue(pd.negative); + assertArrayEquals(new int[]{1,7,9,7,6,9,3,1,3,4,8,6,2,3,1,5,7,0,0,0,0}, pd.digits); + assertEquals(292, pd.getExponent()); + } + + @Test + void minDecimalParsesCorrectly() { + ParsedDecimal pd = ParsedDecimal.from(Double.MIN_VALUE); + assertFalse(pd.negative); + assertArrayEquals(new int[]{4,9,0,0,0,0,0}, pd.digits); + assertEquals(-325, pd.getExponent()); + } + + @Test + void minNegativeDecimalParsesCorrectly() { + ParsedDecimal pd = ParsedDecimal.from(-Double.MIN_VALUE); + assertTrue(pd.negative); + assertArrayEquals(new int[]{4,9,0,0,0,0,0}, pd.digits); + assertEquals(-325, pd.getExponent()); + } + + @Test + void maxDecimalTimes10Throws() { + assertThrows(IllegalArgumentException.class, () -> ParsedDecimal.from(Double.MAX_VALUE*10)); + } + + @Test + void minDecimalDividedby10IsParsedAs0() { + ParsedDecimal pd = ParsedDecimal.from(Double.MIN_VALUE/10); + assertFalse(pd.negative); + assertArrayEquals(new int[]{0}, pd.digits); + assertEquals(0, pd.getExponent()); } +} diff --git a/src/test/java/org/apache/commons/text/similarity/LongestCommonSequencePartitionTests.java b/src/test/java/org/apache/commons/text/similarity/LongestCommonSequencePartitionTests.java new file mode 100644 index 0000000000..bc3b6c648f --- /dev/null +++ b/src/test/java/org/apache/commons/text/similarity/LongestCommonSequencePartitionTests.java @@ -0,0 +1,56 @@ +package org.apache.commons.text.similarity; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.openjdk.jmh.annotations.Param; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class LongestCommonSequencePartitionTests { + private final LongestCommonSubsequence sequenceObject = new LongestCommonSubsequence(); + + @Test + void applyThrowsOnNull() { + assertThrows(IllegalArgumentException.class, () -> sequenceObject.apply(null, null)); + assertThrows(IllegalArgumentException.class, () -> sequenceObject.apply(null, "DOG")); + } + + @ParameterizedTest + @CsvSource({ + "'dog', 'dog', 3", + "'dog', 'do', 2", + "'dog', 'cat', 0", + "'abracadabra', 'acada', 5", + "'abracadabra', 'academy', 4", + "'DOG', 'dog', 0", + "'DOG', 'DOG', 3", + "'this dog', 'is dog', 6", + "'', 'dog', 0", + "' ', ' ', 1", + "'', ' ', 0", + }) + void lengthTests(String str1, String str2, int res){ + assertEquals(sequenceObject.apply(str1, str2), res); + } + + @ParameterizedTest + @CsvSource({ + "'dog', 'dog', 'dog'", + "'dog', 'do', 'do'", + "'dog', 'cat', ''", + "'abracadabra', 'acada', 'acada'", + "'abracadabra', 'academy', 'acad'", + "'DOG', 'dog', ''", + "'DOG', 'DOG', 'DOG'", + "'this dog', 'is dog', 'is dog'", + "'', 'dog', ''", + "' ', ' ', ' '", + "'', ' ', ''", + }) + void stringResultTests(String str1, String str2, String res){ + assertEquals(sequenceObject.longestCommonSubsequence(str1, str2), res); + } +}