Skip to content

Commit 61350e7

Browse files
This PR is related to BAEL-6944 (#16752)
* This commit is related to BAEL-6944 This commit aims to override file.txt. * Update RemoveBOMUnitTest.java
1 parent 2ad0c9e commit 61350e7

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

core-java-modules/core-java-io-6/src/test/java/com/baeldung/removebomfromfile/RemoveBOMUnitTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.baeldung.removebomfromfile;
22

3-
import org.apache.commons.io.input.BOMInputStream;
43
import org.junit.Test;
54

5+
import org.apache.commons.io.input.BOMInputStream;
6+
67
import java.io.*;
78
import java.net.URISyntaxException;
89
import java.nio.ByteBuffer;
@@ -12,10 +13,11 @@
1213
import java.util.Objects;
1314

1415
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertTrue;
1517

1618
public class RemoveBOMUnitTest {
1719
String filePath;
18-
String expectedContent = "This is a test file with BOM.";
20+
String expectedContent = "This is a test file with a UTF-8 BOM.";
1921

2022
{
2123
try {
@@ -27,24 +29,27 @@ public class RemoveBOMUnitTest {
2729

2830
@Test
2931
public void givenFileWithBOM_whenUsingInputStreamAndReader_thenRemoveBOM() throws IOException {
30-
try (InputStream is = new FileInputStream(filePath);
31-
Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
32-
32+
try (InputStream is = new FileInputStream(filePath)) {
3333
byte[] bom = new byte[3];
3434
int n = is.read(bom, 0, bom.length);
3535

36+
Reader reader;
3637
if (n == 3 && (bom[0] & 0xFF) == 0xEF && (bom[1] & 0xFF) == 0xBB && (bom[2] & 0xFF) == 0xBF) {
37-
assertEquals(expectedContent, readFully(reader));
38+
reader = new InputStreamReader(is, StandardCharsets.UTF_8);
3839
} else {
39-
assertEquals(expectedContent, readFully(new InputStreamReader(new FileInputStream(filePath), StandardCharsets.UTF_8)));
40+
reader = new InputStreamReader(new FileInputStream(filePath), StandardCharsets.UTF_8);
4041
}
42+
assertEquals(expectedContent, readFully(reader));
4143
}
4244
}
4345

4446
@Test
4547
public void givenFileWithBOM_whenUsingApacheCommonsIO_thenRemoveBOM() throws IOException {
4648
try (BOMInputStream bomInputStream = new BOMInputStream(new FileInputStream(filePath));
4749
Reader reader = new InputStreamReader(bomInputStream, StandardCharsets.UTF_8)) {
50+
51+
assertTrue(bomInputStream.hasBOM());
52+
4853
assertEquals(expectedContent, readFully(reader));
4954
}
5055
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
This is a test file with BOM.
1+
This is a test file with a UTF-8 BOM.

0 commit comments

Comments
 (0)