|
| 1 | +package com.baeldung.bomfilewriter; |
| 2 | + |
| 3 | +import org.apache.commons.io.FileUtils; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.apache.commons.lang3.ArrayUtils; |
| 6 | + |
| 7 | +import java.io.*; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 13 | + |
| 14 | +public class BomFileWriterUnitTest { |
| 15 | + private static final String TEST_CONTENT = "This is the content of the file."; |
| 16 | + private static final byte[] UTF8_BOM = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; |
| 17 | + private static final String UTF8_BOM_UNICODE = "\uFEFF"; |
| 18 | + private static final String FILE_PATH_OUTPUT_STREAM = "output_with_bom_output_stream.txt"; |
| 19 | + private static final String FILE_PATH_BUFFERED_WRITER = "output_with_bom_buffered.txt"; |
| 20 | + private static final String FILE_PATH_PRINT_WRITER = "output_with_bom_print_writer.txt"; |
| 21 | + private static final String FILE_PATH_COMMONS_IO = "output_with_bom_commons_io.txt"; |
| 22 | + |
| 23 | + @Test |
| 24 | + public void givenText_whenAddingBomWithFileOutputStream_thenBOMAdded() throws IOException { |
| 25 | + try (FileOutputStream fos = new FileOutputStream(FILE_PATH_OUTPUT_STREAM)) { |
| 26 | + fos.write(UTF8_BOM); |
| 27 | + fos.write(TEST_CONTENT.getBytes(StandardCharsets.UTF_8)); |
| 28 | + } |
| 29 | + |
| 30 | + String result = Files.readString(Path.of(FILE_PATH_OUTPUT_STREAM), StandardCharsets.UTF_8); |
| 31 | + assertTrue(result.startsWith(UTF8_BOM_UNICODE)); |
| 32 | + assertTrue(result.contains(TEST_CONTENT)); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void givenText_whenAddingBomWithBufferedWriter_thenBOMAdded() throws IOException { |
| 37 | + try (OutputStreamWriter osw = new OutputStreamWriter( |
| 38 | + new FileOutputStream(FILE_PATH_BUFFERED_WRITER), StandardCharsets.UTF_8); |
| 39 | + BufferedWriter writer = new BufferedWriter(osw)) { |
| 40 | + |
| 41 | + writer.write(UTF8_BOM_UNICODE); |
| 42 | + writer.write(TEST_CONTENT); |
| 43 | + } |
| 44 | + |
| 45 | + String result = Files.readString(Path.of(FILE_PATH_BUFFERED_WRITER), StandardCharsets.UTF_8); |
| 46 | + assertTrue(result.startsWith(UTF8_BOM_UNICODE)); |
| 47 | + assertTrue(result.contains(TEST_CONTENT)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void givenText_whenUsingPrintWriter_thenBOMAdded() throws IOException { |
| 52 | + try (PrintWriter writer = new PrintWriter( |
| 53 | + new OutputStreamWriter(new FileOutputStream(FILE_PATH_PRINT_WRITER), StandardCharsets.UTF_8))) { |
| 54 | + writer.write(UTF8_BOM_UNICODE); |
| 55 | + writer.println(TEST_CONTENT); |
| 56 | + } |
| 57 | + |
| 58 | + String result = Files.readString(Path.of(FILE_PATH_PRINT_WRITER), StandardCharsets.UTF_8); |
| 59 | + assertTrue(result.startsWith(UTF8_BOM_UNICODE)); |
| 60 | + assertTrue(result.contains(TEST_CONTENT)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void givenText_whenUsingCommonsIO_thenBOMAdded() throws IOException { |
| 65 | + byte[] bomAndContent = ArrayUtils.addAll(UTF8_BOM, TEST_CONTENT.getBytes(StandardCharsets.UTF_8)); |
| 66 | + FileUtils.writeByteArrayToFile(new File(FILE_PATH_COMMONS_IO), bomAndContent); |
| 67 | + |
| 68 | + String result = FileUtils.readFileToString(new File(FILE_PATH_COMMONS_IO), StandardCharsets.UTF_8); |
| 69 | + assertTrue(result.startsWith(UTF8_BOM_UNICODE)); |
| 70 | + assertTrue(result.contains(TEST_CONTENT)); |
| 71 | + } |
| 72 | +} |
0 commit comments