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,126 @@
/*
* 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.text;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.UnsupportedEncodingException;

import org.junit.jupiter.api.Test;

public class AlphabetConverterPartitionTest {
// 1. Valid input: Basic encoding/decoding
@Test
void testSimpleEncodeDecode() throws UnsupportedEncodingException {
Character[] original = {'a', 'b', 'c'};
Character[] encoding = {'0', '1', '2'};
Character[] doNotEncode = {};
AlphabetConverter converter = AlphabetConverter.createConverterFromChars(original, encoding, doNotEncode);

assertEquals("012", converter.encode("abc"));
assertEquals("abc", converter.decode("012"));
}

// 2. Valid input: Encoding with doNotEncode characters
@Test
void testDoNotEncodeCharacters() throws UnsupportedEncodingException {
Character[] original = {'a', 'b', 'c'};
Character[] encoding = {'0', '1', 'c'};
Character[] doNotEncode = {'c'};
AlphabetConverter converter = AlphabetConverter.createConverterFromChars(original, encoding, doNotEncode);

assertEquals("00c", converter.encode("aac"));
assertEquals("aac", converter.decode("00c"));
}

// 3. Invalid input: doNotEncode character not in original alphabet
@Test
void testDoNotEncodeMissingFromOriginal() {
Character[] original = {'a', 'b'};
Character[] encoding = {'0', '1', 'c'};
Character[] doNotEncode = {'c'}; // c not in original

assertThrows(IllegalArgumentException.class, () ->
AlphabetConverter.createConverterFromChars(original, encoding, doNotEncode)
);
}

// 4. Invalid input: decode contains unknown encoded group
@Test
void testDecodeWithInvalidGroup() {
Character[] original = {'a', 'b', 'c'};
Character[] encoding = {'0', '1', '2'};
Character[] doNotEncode = {};
AlphabetConverter converter = AlphabetConverter.createConverterFromChars(original, encoding, doNotEncode);

assertThrows(UnsupportedEncodingException.class, () ->
converter.decode("xyz")
);
}

// 5. Valid input: Empty string encoding/decoding
@Test
void testEmptyString() throws UnsupportedEncodingException {
Character[] original = {'a', 'b', 'c'};
Character[] encoding = {'0', '1', '2'};
Character[] doNotEncode = {};
AlphabetConverter converter = AlphabetConverter.createConverterFromChars(original, encoding, doNotEncode);

assertEquals("", converter.encode(""));
assertEquals("", converter.decode(""));
}

// 6. Valid input: null passed to encode/decode should return null
@Test
void testNullInputEncodeDecode() throws UnsupportedEncodingException {
Character[] original = {'a', 'b', 'c'};
Character[] encoding = {'0', '1', '2'};
Character[] doNotEncode = {};
AlphabetConverter converter = AlphabetConverter.createConverterFromChars(original, encoding, doNotEncode);

assertNull(converter.encode(null));
assertNull(converter.decode(null));
}

// 7. Invalid input: encoding alphabet too small
@Test
void testEncodingAlphabetTooSmallThrows() {
Character[] original = {'a', 'b', 'c'};
Character[] encoding = {'x'}; // too small
Character[] doNotEncode = {};

assertThrows(IllegalArgumentException.class, () ->
AlphabetConverter.createConverterFromChars(original, encoding, doNotEncode)
);
}

// 8. Valid input: repeated characters in original or encoding should be ignored
@Test
void testRepeatedCharactersAreIgnored() throws UnsupportedEncodingException {
Character[] original = {'a', 'b', 'a', 'c'};
Character[] encoding = {'0', '1', '0', '2'};
Character[] doNotEncode = {};
AlphabetConverter converter = AlphabetConverter.createConverterFromChars(original, encoding, doNotEncode);

String encoded = converter.encode("abc");
String decoded = converter.decode(encoded);
assertEquals("abc", decoded);
}
}
83 changes: 83 additions & 0 deletions src/test/java/org/apache/commons/text/CaseUtilsPartitionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.text;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class CaseUtilsPartitionTest {
// Null input string
@Test
void testNullInputReturnsNull() {
assertNull(CaseUtils.toCamelCase(null, true, null));
}

// Empty input string
@Test
void testEmptyInputReturnsEmpty() {
assertEquals("", CaseUtils.toCamelCase("", true, null));
}

// Only delimiters
@Test
void testOnlyDelimitersReturnsEmpty() {
assertEquals("", CaseUtils.toCamelCase(" @ @", false, new char[]{'@'}));
}

// No delimiters (just case change)
@Test
void testNoDelimitersCapitalizationTrue() {
assertEquals("Hello", CaseUtils.toCamelCase("hello", true, null));
}

@Test
void testNoDelimitersCapitalizationFalse() {
assertEquals("hello", CaseUtils.toCamelCase("HELLO", false, null));
}

// Capitalize first letter flag
@Test
void testCapitalizeFirstTrue() {
assertEquals("FooBar", CaseUtils.toCamelCase("foo bar", true, null));
}

@Test
void testCapitalizeFirstFalse() {
assertEquals("fooBar", CaseUtils.toCamelCase("foo bar", false, null));
}

// Null delimiters (uses space as default)
@Test
void testNullDelimitersUsesSpace() {
assertEquals("FooBar", CaseUtils.toCamelCase("foo bar", true, null));
}

// Empty delimiters (also uses space)
@Test
void testEmptyDelimitersUsesSpace() {
assertEquals("FooBar", CaseUtils.toCamelCase("foo bar", true, new char[0]));
}

// Custom delimiter characters
@Test
void testCustomDelimiters() {
assertEquals("FooBarBaz", CaseUtils.toCamelCase("foo-bar_baz", true, new char[]{'-', '_'}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.text.lookup;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;

import org.junit.jupiter.api.Test;


public class DateStringLookupPartitionTest {

private final DateStringLookup lookup = DateStringLookup.INSTANCE;

// Partition 1: Valid format strings
@Test
public void testValidFormat_yyyyMMdd() {
String result = lookup.lookup("yyyy-MM-dd");
assertNotNull(result);
assertTrue(result.matches("\\d{4}-\\d{2}-\\d{2}"));
}

@Test
public void testValidFormat_fullDateTime() {
String result = lookup.lookup("yyyy-MM-dd HH:mm:ss");
assertNotNull(result);
assertTrue(result.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}"));
}

@Test
public void testValidFormat_customText() {
String result = lookup.lookup("'Today is' EEEE");
assertNotNull(result);
assertTrue(result.startsWith("Today is"));
}

// Partition 2: Null format (default format)
@Test
public void testNullFormat_usesDefault() {
String result = lookup.lookup(null);
assertNotNull(result);
// Since default FastDateFormat output may vary, just check it's non-empty
assertFalse(result.isEmpty());
}

// Partition 3: Invalid format strings
@Test
public void testInvalidFormat_throwsException() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
lookup.lookup("invalid_format_%%");
});
assertTrue(exception.getMessage().contains("Invalid date format"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.text.lookup;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.jupiter.api.Test;

public class FileStringLookupPartitionTest {
private final FileStringLookup lookup = new FileStringLookup();

@Test
void testNullKeyReturnsNull() {
assertNull(lookup.lookup(null));
}

@Test
void testKeyMissingColonThrows() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
lookup.lookup("UTF-8")
);
assertTrue(ex.getMessage().contains("Bad file key format"));
}

@Test
void testInvalidCharsetThrows() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
lookup.lookup("FAKECHARSET:/some/fake/file.txt")
);
assertTrue(ex.getMessage().contains("Error looking up file"));
}

@Test
void testNonexistentFileThrows() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
lookup.lookup("UTF-8:/definitely/does/not/exist.txt")
);
assertTrue(ex.getMessage().contains("Error looking up file"));
}

@Test
void testValidKeyReturnsFileContents() throws Exception {
Path tempFile = Files.createTempFile("test-file", ".txt");
Files.write(tempFile, "Hello, world!".getBytes(StandardCharsets.UTF_8));

String key = "UTF-8:" + tempFile.toString();
String result = lookup.lookup(key);
assertEquals("Hello, world!", result);
}

@Test
void testMultipleColonsStillWorksOrThrowsMeaningfully() {
// File doesn't exist, but structure is valid
String key = "UTF-8:/fake/path:ignored";
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
lookup.lookup(key)
);
assertTrue(ex.getMessage().contains("Error looking up file"));
}
}
Loading