11package com .baeldung .removebomfromfile ;
22
3- import org .apache .commons .io .input .BOMInputStream ;
43import org .junit .Test ;
54
5+ import org .apache .commons .io .input .BOMInputStream ;
6+
67import java .io .*;
78import java .net .URISyntaxException ;
89import java .nio .ByteBuffer ;
1213import java .util .Objects ;
1314
1415import static org .junit .Assert .assertEquals ;
16+ import static org .junit .Assert .assertTrue ;
1517
1618public 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 }
0 commit comments