MalformedInputException test functions are added
This commit is contained in:
parent
e8f83431b3
commit
230b2db438
@ -1,10 +1,9 @@
|
|||||||
package com.baeldung.encoding;
|
package com.baeldung.encoding;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.FileInputStream;
|
import java.nio.charset.Charset;
|
||||||
import java.io.IOException;
|
import java.nio.charset.CharsetDecoder;
|
||||||
import java.io.InputStreamReader;
|
import java.nio.charset.CodingErrorAction;
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
|
|
||||||
public class CharacterEncodingExamples {
|
public class CharacterEncodingExamples {
|
||||||
|
|
||||||
@ -29,4 +28,15 @@ public class CharacterEncodingExamples {
|
|||||||
}
|
}
|
||||||
return buffer.toString();
|
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();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,21 @@
|
|||||||
package com.baeldung.encoding;
|
package com.baeldung.encoding;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.nio.charset.*;
|
||||||
|
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.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
|
||||||
|
import static java.nio.file.Files.newInputStream;
|
||||||
|
|
||||||
public class CharacterEncodingExamplesUnitTest {
|
public class CharacterEncodingExamplesUnitTest {
|
||||||
|
|
||||||
@ -58,4 +70,47 @@ public class CharacterEncodingExamplesUnitTest {
|
|||||||
"0 0 10001010 10011110 ");
|
"0 0 10001010 10011110 ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUTF8String_decodeByUS_ASCII_ReplaceMalformedInputSequence() throws IOException {
|
||||||
|
String input = "The façade pattern is a software design pattern.";
|
||||||
|
Assertions.assertEquals(CharacterEncodingExamples.decodeText(input, StandardCharsets.US_ASCII, CodingErrorAction.REPLACE),
|
||||||
|
"The fa<66><61>ade pattern is a software design pattern.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUTF8String_decodeByUS_ASCII_IgnoreMalformedInputSequence() throws IOException {
|
||||||
|
String input = "The façade pattern is a software design pattern.";
|
||||||
|
Assertions.assertEquals(
|
||||||
|
CharacterEncodingExamples.decodeText(input, StandardCharsets.US_ASCII, CodingErrorAction.IGNORE),
|
||||||
|
"The faade pattern is a software design pattern.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUTF8String_decodeByUS_ASCII_ReportMalformedInputSequence() {
|
||||||
|
String input = "The façade pattern is a software design pattern.";
|
||||||
|
Assertions.assertThrows(MalformedInputException.class,
|
||||||
|
() -> CharacterEncodingExamples.decodeText(input, StandardCharsets.US_ASCII, CodingErrorAction.REPORT));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTextFile_FindSuitableCandidateEncodings() {
|
||||||
|
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(newInputStream(path), charsetDecoder);
|
||||||
|
BufferedReader bufferedReader = new BufferedReader(reader);
|
||||||
|
while (bufferedReader.readLine() != null) {
|
||||||
|
}
|
||||||
|
suitableCharsets.add(charset);
|
||||||
|
} catch (MalformedInputException ignored) {
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Assertions.assertEquals(suitableCharsets, Arrays.asList(StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user