This PR is related to BAEL-7174 (#15681)

* This PR is related to BAEL-7174

This PR aims to add a new test class "CheckIfStringContainsInvalidEncodedCharactersUnitTest".

* Update CheckIfStringContainsInvalidEncodedCharactersUnitTest.java

Updating if-else with ternary operator
This commit is contained in:
Mo Helmy 2024-01-18 22:27:04 +02:00 committed by GitHub
parent c97e529deb
commit 5b76e4eace
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,33 @@
package com.baeldung.checkifstringcontainsinvalidcharacters;
import org.junit.jupiter.api.Test;
import java.nio.charset.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class CheckIfStringContainsInvalidEncodedCharactersUnitTest {
public String input = "HÆllo, World!";
@Test
public void givenInputString_whenUsingRegexPattern_thenFindIfInvalidCharacters() {
String regexPattern = "[^\\x00-\\x7F]+";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher = pattern.matcher(input);
assertTrue(matcher.find() ? true : false);
}
@Test
public void givenInputString_whenUsingStringEncoding_thenFindIfInvalidCharacters() {
byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
boolean found = false;
for (byte b : bytes) {
found = (b & 0xFF) > 127 ? true : found;
}
assertTrue(found ? true : false);
}
}