Skip to content

Commit 31b573d

Browse files
authored
Merge pull request eugenp#7910 from mnafshin/MalformedInputException
[BAEL-2505] MalformedInputException test functions are added
2 parents 36322c2 + 3997dcc commit 31b573d

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

core-java-modules/core-java/src/main/java/com/baeldung/encoding/CharacterEncodingExamples.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.baeldung.encoding;
22

3+
import java.io.BufferedReader;
4+
import java.io.ByteArrayInputStream;
35
import java.io.File;
46
import java.io.FileInputStream;
57
import java.io.IOException;
68
import java.io.InputStreamReader;
79
import java.io.UnsupportedEncodingException;
10+
import java.nio.charset.Charset;
11+
import java.nio.charset.CharsetDecoder;
12+
import java.nio.charset.CodingErrorAction;
813

914
public class CharacterEncodingExamples {
1015

@@ -29,4 +34,12 @@ static String convertToBinary(String input, String encoding) throws UnsupportedE
2934
}
3035
return buffer.toString();
3136
}
37+
38+
static String decodeText(String input, Charset charset, CodingErrorAction codingErrorAction) throws IOException {
39+
CharsetDecoder charsetDecoder = charset.newDecoder();
40+
charsetDecoder.onMalformedInput(codingErrorAction);
41+
return new BufferedReader(
42+
new InputStreamReader(
43+
new ByteArrayInputStream(input.getBytes()), charsetDecoder)).readLine();
44+
}
3245
}

core-java-modules/core-java/src/test/java/com/baeldung/encoding/CharacterEncodingExamplesUnitTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
package com.baeldung.encoding;
22

3+
import java.io.BufferedReader;
34
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.io.Reader;
7+
import java.nio.charset.Charset;
8+
import java.nio.charset.CharsetDecoder;
9+
import java.nio.charset.CodingErrorAction;
10+
import java.nio.charset.MalformedInputException;
11+
import java.nio.charset.StandardCharsets;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
import java.nio.file.Paths;
15+
import java.util.ArrayList;
16+
import java.util.Arrays;
17+
import java.util.List;
418

519
import org.junit.Assert;
620
import org.junit.Test;
21+
import org.junit.jupiter.api.Assertions;
722

823
public class CharacterEncodingExamplesUnitTest {
924

@@ -58,4 +73,52 @@ public void givenCharacterCh_whenConvertedtoBinaryWithEncodingUTF32_thenProduceR
5873
"0 0 10001010 10011110 ");
5974
}
6075

76+
@Test
77+
public void givenUTF8String_whenDecodeByUS_ASCII_thenIgnoreMalformedInputSequence() throws IOException {
78+
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));
79+
}
80+
81+
@Test
82+
public void givenUTF8String_whenDecodeByUS_ASCII_thenReplaceMalformedInputSequence() throws IOException {
83+
Assertions.assertEquals(
84+
"The fa��ade pattern is a software design pattern.",
85+
CharacterEncodingExamples.decodeText(
86+
"The façade pattern is a software design pattern.",
87+
StandardCharsets.US_ASCII,
88+
CodingErrorAction.REPLACE));
89+
}
90+
91+
@Test
92+
public void givenUTF8String_whenDecodeByUS_ASCII_thenReportMalformedInputSequence() {
93+
Assertions.assertThrows(
94+
MalformedInputException.class,
95+
() -> CharacterEncodingExamples.decodeText(
96+
"The façade pattern is a software design pattern.",
97+
StandardCharsets.US_ASCII,
98+
CodingErrorAction.REPORT));
99+
}
100+
101+
@Test
102+
public void givenTextFile_whenLoopOverAllCandidateEncodings_thenProduceSuitableCandidateEncodings() {
103+
Path path = Paths.get("src/test/resources/encoding.txt");
104+
List<Charset> allCandidateCharSets = Arrays.asList(
105+
StandardCharsets.US_ASCII, StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1);
106+
107+
List<Charset> suitableCharsets = new ArrayList<>();
108+
allCandidateCharSets.forEach(charset -> {
109+
try {
110+
CharsetDecoder charsetDecoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT);
111+
Reader reader = new InputStreamReader(Files.newInputStream(path), charsetDecoder);
112+
BufferedReader bufferedReader = new BufferedReader(reader);
113+
bufferedReader.readLine();
114+
suitableCharsets.add(charset);
115+
} catch (MalformedInputException ignored) {
116+
} catch (IOException ex) {
117+
ex.printStackTrace();
118+
}
119+
});
120+
121+
Assertions.assertEquals(suitableCharsets, Arrays.asList(StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1));
122+
}
123+
61124
}

0 commit comments

Comments
 (0)