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:
parent
c97e529deb
commit
5b76e4eace
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue