diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java index caf5c0d0e..25b918d26 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java @@ -99,7 +99,7 @@ public class TextFile { } } - public static void stringToFileWithBom(final String content, final String path) throws IOException { + public static void stringToFileWithBOM(final String content, final String path) throws IOException { final File file = new CSFile(path); stringToFileWithBOM(content, file); } diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/TextFileTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/TextFileTest.java new file mode 100644 index 000000000..a706cad3b --- /dev/null +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/TextFileTest.java @@ -0,0 +1,129 @@ +package org.hl7.fhir.utilities; + + import org.junit.jupiter.api.*; + + import java.io.File; + import java.io.IOException; + import java.nio.charset.StandardCharsets; + import java.nio.file.Files; + import java.util.ArrayList; + import java.util.Arrays; + import java.util.List; + + import static org.junit.jupiter.api.Assertions.*; + + /** + * Test bench for {@link TextFile}. + * + * @author Quentin Ligier + **/ + class TextFileTest { + + private static final String SAMPLE_CONTENT = "Line 1\nLine 2\nLine 3"; + private static final List SAMPLE_CONTENT_LINES = List.of("Line 1", "Line 2", "Line 3"); + private static final String BOM = "\uFEFF"; + + private static File readFile; + private final static List createdFiles = new ArrayList<>(4); + + @BeforeAll + static void setUp() throws IOException { + readFile = createTempFile(); + readFile.deleteOnExit(); + Files.writeString(readFile.toPath(), SAMPLE_CONTENT); + } + + @AfterAll + static void tearDown() throws IOException { + for (final var file : createdFiles) { + Files.deleteIfExists(file.toPath()); + } + } + + @Test + void testReadAllLines() throws IOException { + final var readLines = TextFile.readAllLines(readFile.getAbsolutePath()); + assertEquals(3, readLines.size()); + assertEquals(SAMPLE_CONTENT_LINES, readLines); + } + + @Test + void testBytesToString1() throws IOException { + final var converted = TextFile.bytesToString(SAMPLE_CONTENT.getBytes(StandardCharsets.UTF_8)); + assertEquals(SAMPLE_CONTENT, converted); + } + + @Test + void testBytesToString2() throws IOException { + final var bytesWithoutBom = SAMPLE_CONTENT.getBytes(StandardCharsets.UTF_8); + final var bomBytes = BOM.getBytes(StandardCharsets.UTF_8); + final var bytesWithBom = Arrays.copyOf(bomBytes, bomBytes.length + bytesWithoutBom.length); + System.arraycopy(bytesWithoutBom, 0, bytesWithBom, bomBytes.length, bytesWithoutBom.length); + + var converted = TextFile.bytesToString(bytesWithoutBom, true); + assertEquals(SAMPLE_CONTENT, converted); + + converted = TextFile.bytesToString(bytesWithoutBom, false); + assertEquals(SAMPLE_CONTENT, converted); + + converted = TextFile.bytesToString(bytesWithBom, true); + assertEquals(SAMPLE_CONTENT, converted); + + converted = TextFile.bytesToString(bytesWithBom, false); + assertEquals(BOM + SAMPLE_CONTENT, converted); + } + + @Test + void testFileToString1() throws IOException { + final var read = TextFile.fileToString(readFile); + assertEquals(SAMPLE_CONTENT, read); + } + + @Test + void testFileToString2() throws IOException { + final var read = TextFile.fileToString(readFile.getAbsolutePath()); + assertEquals(SAMPLE_CONTENT, read); + } + + @Test + void testFileToBytes1() throws IOException { + final var read = TextFile.fileToBytes(readFile); + assertArrayEquals(SAMPLE_CONTENT.getBytes(StandardCharsets.UTF_8), read); + } + + @Test + void testFileToBytesNCS() throws IOException { + final var read = TextFile.fileToBytesNCS(readFile.getAbsolutePath()); + assertArrayEquals(SAMPLE_CONTENT.getBytes(StandardCharsets.UTF_8), read); + } + + @Test + void testFileToBytes2() throws IOException { + final var read = TextFile.fileToBytes(readFile.getAbsolutePath()); + assertArrayEquals(SAMPLE_CONTENT.getBytes(StandardCharsets.UTF_8), read); + } + + @Test + void testStringToFile() throws IOException { + final var writeFile = createTempFile(); + TextFile.stringToFileWithBOM(SAMPLE_CONTENT, writeFile); + assertEquals(BOM + SAMPLE_CONTENT, Files.readString(writeFile.toPath())); + + TextFile.stringToFile(SAMPLE_CONTENT, writeFile); + assertEquals(SAMPLE_CONTENT, Files.readString(writeFile.toPath())); + } + + @Test + void testWriteAllLines() throws IOException { + final var writeFile = createTempFile(); + TextFile.writeAllLines(writeFile.getAbsolutePath(), SAMPLE_CONTENT_LINES); + assertEquals(SAMPLE_CONTENT_LINES, Files.readAllLines(writeFile.toPath())); + } + + private static File createTempFile() throws IOException { + final var file = Files.createTempFile("test_fhir_utilities_", ".txt").toFile(); + file.deleteOnExit(); + createdFiles.add(file); + return file; + } + } \ No newline at end of file