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
71 changes: 71 additions & 0 deletions src/test/java/org/apache/commons/text/StringSubstitutor_Test.java
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;

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

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;

public class StringSubstitutor_TestPK {

//test for edge case Null value

@Test
void testReplace_NullInput() {
StringSubstitutor substitutor = new StringSubstitutor();
assertNull(substitutor.replace((String) null), "Expected null when input is null");
}

// Edge case testing by testing empty string
@Test
void testReplace_EmptyString() {
StringSubstitutor substitutor = new StringSubstitutor();
assertEquals("", substitutor.replace(""), "Expected empty string to remain empty");
}
// No placeholder test
@Test
void testReplace_NoPlaceholders() {
StringSubstitutor substitutor = new StringSubstitutor();
String input = "Hello, World!";
assertEquals(input, substitutor.replace(input), "Expected the same string when no placeholders exist");
}
//test valid substitution
@Test
void testReplace_ValidSubstitution() {
Map<String, String> values = new HashMap<>();
values.put("name", "Jacob");
StringSubstitutor substitutor = new StringSubstitutor(values);

String input = "Hello, ${name}!";
String expected = "Hello, Jacob!";
assertEquals(expected, substitutor.replace(input), "Expected placeholder to be replaced");
}
@Test
void testReplace_UnresolvedPlaceholder() {
Map<String, String> values = new HashMap<>();
values.put("name", "Alice");
StringSubstitutor substitutor = new StringSubstitutor(values);

String input = "Hello, ${unknown}!";
String expected = "Hello, ${unknown}!";
assertEquals(expected, substitutor.replace(input), "Expected unresolved placeholder to remain unchanged");
}
}
200 changes: 200 additions & 0 deletions src/test/java/org/apache/commons/text/WordUtils_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* 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 org.apache.commons.lang3.StringUtils;

import org.junit.jupiter.api.Test;

public class WordUtils_TestPk {




//Testing empty string and no deliminitor
@Test
public void testCapitalizePartitionEmptyString() {
//setup

String str="";
char[] delimiters ={};

//test
String result= WordUtils.capitalize(str, delimiters);

//verify

assertEquals("", result);
}

// testing single lower case string
@Test
public void testCapitalizePartitionLowerCaseString(){
//set up
String str= "hello";
char[] delimiters ={};

//test
String result= WordUtils.capitalize(str, delimiters);

//verify

assertEquals("Hello", result);
}
//testing multiple lower case string with no deliminator
@Test
public void testCapitalizePartitionMultipleLowerCaseString(){
//set up
String str= "hello world";
char[] delimiters ={};

//test
String result= WordUtils.capitalize(str, delimiters);

//verify

assertEquals("Hello world", result);
}
// multiple lower case string with a space deliminiter
@Test
public void testCapitalizePartitionMultipleLowerCaseStringWithDeli(){
//set up
String str= "hello world";
char[] delimiters ={' '};

//test
String result= WordUtils.capitalize(str, delimiters);

//verify

assertEquals("Hello World", result);
}
// words already capitalized with a space deliminiter
@Test
public void testCapitalizePartitionUpperCaseStringWithDeli(){
//set up
String str= "Hello World";
char[] delimiters ={' '};

//test
String result= WordUtils.capitalize(str, delimiters);

//verify

assertEquals("Hello World", result);
}
// words all capitalized with a multiple deliminiter
@Test
public void testCapitalizePartitionStringWithMultipleDeli(){
//set up
String str= "hello-world";
char[] delimiters ={' ','-'};

//test
String result= WordUtils.capitalize(str, delimiters);

//verify

assertEquals("Hello-World", result);
}
// words with special character as deliminiter
@Test
public void testCapitalizePartitionWithSpecialDeli(){
//set up
String str= "hello@world";
char[] delimiters ={' '};

//test
String result= WordUtils.capitalize(str, delimiters);

//verify

assertEquals("Hello@world", result);
}
// words already capitalized with a space deliminiter
@Test
public void testCapitalizePartitionAllUpperCaseWithDeli(){
//set up
String str= "HELLO WORLD";
char[] delimiters ={' '};

//test
String result= WordUtils.capitalize(str, delimiters);

//verify

assertEquals("HELLO WORLD", result);
}
// words already capitalized with a space deliminiter
@Test
public void testCapitalizePartitionStringWithNumbersWithDeli(){
//set up
String str= "Hello World";
char[] delimiters ={' '};

//test
String result= WordUtils.capitalize(str, delimiters);

//verify

assertEquals("Hello World", result);
}
// words with a space deliminiter at the start
@Test
public void testCapitalizePartitionWithDeliAtStart(){
//set up
String str= " hello";
char[] delimiters ={' '};

//test
String result= WordUtils.capitalize(str, delimiters);

//verify

assertEquals(" Hello", result);
}
// words with a space deliminiter at the end
@Test
public void testCapitalizePartitionWithDeliAtEnd(){
//set up
String str= "hello ";
char[] delimiters ={' '};

//test
String result= WordUtils.capitalize(str, delimiters);

//verify

assertEquals("Hello ", result);
}
// lower case word only first letter is capital with no deliminiter
@Test
public void testCapitalizePartitionNoDeliminiters(){
//set up
String str= "hello world";
char[] delimiters ={};

//test
String result= WordUtils.capitalize(str, delimiters);

//verify

assertEquals("Hello world", result);
}
}