Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -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));
}
}
73 changes: 73 additions & 0 deletions src/test/java/org/apache/commons/text/CaseUtilsPartitionTests.java
Original file line number Diff line number Diff line change
@@ -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, '-', '.'));
}

}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> input = new HashMap<>();
input.put("cat", "stand");
StringSubstitutor sub = new StringSubstitutor(input);
assertEquals("Taco ${bell}", sub.replace("Taco ${bell}"));
}

@Test
void testReplaceWithEscapeChar() {
Map<String, String> input = new HashMap<>();
input.put("cat", "stand");
StringSubstitutor sub = new StringSubstitutor(input);
assertEquals("Taco ${cat}", sub.replace("Taco $${cat}"));
}

@Test
void testReplaceWithDefaultVal() {
Map<String, String> 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<String, String> 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}"));
}
}
Original file line number Diff line number Diff line change
@@ -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, "..."));
}
}
Loading