This commit is related to BAEL-7024 (#15564)

This commit aims to add a new test class "ReplaceNonPrintableCharsUnitTest".
This commit is contained in:
Mo Helmy 2024-01-08 05:01:27 +02:00 committed by GitHub
parent 36edee6be7
commit b41a78fb39
1 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package com.baeldung.replacenonprintablecharacters;
import org.junit.jupiter.api.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ReplaceNonPrintableCharsUnitTest {
@Test
public void givenTextWithNonPrintableChars_whenUsingRegularExpression_thenGetSanitizedText() {
String originalText = "\n\nWelcome \n\n\n\tto Baeldung!\n\t";
String expected = "Welcome to Baeldung!";
String regex = "[\\p{C}]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(originalText);
String sanitizedText = matcher.replaceAll("");
assertEquals(expected, sanitizedText);
}
@Test
public void givenTextWithNonPrintableChars_whenCustomImplementation_thenGetSanitizedText() {
String originalText = "\n\nWelcome \n\n\n\tto Baeldung!\n\t";
String expected = "Welcome to Baeldung!";
StringBuilder strBuilder = new StringBuilder();
originalText.codePoints().forEach((i) -> {
if (i >= 32 && i != 127) {
strBuilder.append(Character.toChars(i));
}
});
assertEquals(expected, strBuilder.toString());
}
}