Merge pull request #7910 from mnafshin/MalformedInputException

[BAEL-2505] MalformedInputException test functions are added
This commit is contained in:
Eric Martin 2019-10-10 22:19:49 -05:00 committed by GitHub
commit 31b573dee0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 0 deletions

View File

@ -1,10 +1,15 @@
package com.baeldung.encoding;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
public class CharacterEncodingExamples {
@ -29,4 +34,12 @@ public class CharacterEncodingExamples {
}
return buffer.toString();
}
static String decodeText(String input, Charset charset, CodingErrorAction codingErrorAction) throws IOException {
CharsetDecoder charsetDecoder = charset.newDecoder();
charsetDecoder.onMalformedInput(codingErrorAction);
return new BufferedReader(
new InputStreamReader(
new ByteArrayInputStream(input.getBytes()), charsetDecoder)).readLine();
}
}

View File

@ -1,9 +1,24 @@
package com.baeldung.encoding;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.MalformedInputException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
public class CharacterEncodingExamplesUnitTest {
@ -58,4 +73,52 @@ public class CharacterEncodingExamplesUnitTest {
"0 0 10001010 10011110 ");
}
@Test
public void givenUTF8String_whenDecodeByUS_ASCII_thenIgnoreMalformedInputSequence() throws IOException {
Assertions.assertEquals("The faade pattern is a software design pattern.", CharacterEncodingExamples.decodeText("The façade pattern is a software design pattern.", StandardCharsets.US_ASCII, CodingErrorAction.IGNORE));
}
@Test
public void givenUTF8String_whenDecodeByUS_ASCII_thenReplaceMalformedInputSequence() throws IOException {
Assertions.assertEquals(
"The fa<66><61>ade pattern is a software design pattern.",
CharacterEncodingExamples.decodeText(
"The façade pattern is a software design pattern.",
StandardCharsets.US_ASCII,
CodingErrorAction.REPLACE));
}
@Test
public void givenUTF8String_whenDecodeByUS_ASCII_thenReportMalformedInputSequence() {
Assertions.assertThrows(
MalformedInputException.class,
() -> CharacterEncodingExamples.decodeText(
"The façade pattern is a software design pattern.",
StandardCharsets.US_ASCII,
CodingErrorAction.REPORT));
}
@Test
public void givenTextFile_whenLoopOverAllCandidateEncodings_thenProduceSuitableCandidateEncodings() {
Path path = Paths.get("src/test/resources/encoding.txt");
List<Charset> allCandidateCharSets = Arrays.asList(
StandardCharsets.US_ASCII, StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1);
List<Charset> suitableCharsets = new ArrayList<>();
allCandidateCharSets.forEach(charset -> {
try {
CharsetDecoder charsetDecoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT);
Reader reader = new InputStreamReader(Files.newInputStream(path), charsetDecoder);
BufferedReader bufferedReader = new BufferedReader(reader);
bufferedReader.readLine();
suitableCharsets.add(charset);
} catch (MalformedInputException ignored) {
} catch (IOException ex) {
ex.printStackTrace();
}
});
Assertions.assertEquals(suitableCharsets, Arrays.asList(StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1));
}
}