From 0e726b75766dba946eb3e42bb47f2d30d2bbd4fa Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Sat, 18 Nov 2023 22:29:58 +0100 Subject: [PATCH] Added some tests for CsvPrinter.java --- .../de/jplag/reporting/csv/CsvPrinter.java | 26 ++++++++++++-- .../jplag/reporting/csv/CsvPrinterTest.java | 35 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 core/src/test/java/de/jplag/reporting/csv/CsvPrinterTest.java diff --git a/core/src/main/java/de/jplag/reporting/csv/CsvPrinter.java b/core/src/main/java/de/jplag/reporting/csv/CsvPrinter.java index ee4fe1224..330535503 100644 --- a/core/src/main/java/de/jplag/reporting/csv/CsvPrinter.java +++ b/core/src/main/java/de/jplag/reporting/csv/CsvPrinter.java @@ -1,7 +1,9 @@ package de.jplag.reporting.csv; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.io.OutputStreamWriter; import java.io.Writer; import java.util.ArrayList; import java.util.Arrays; @@ -79,11 +81,29 @@ public void setLineEnd(String lineEnd) { */ public void printToFile(File file) throws IOException { try (Writer writer = FileUtils.openFileWriter(file)) { - this.writeTitleRow(writer); + this.printCsv(writer); + } + } + + public String printToString() throws IOException { + String csv; - for (String[] datum : this.data) { - this.printRow(writer, datum); + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { + try (Writer writer = new OutputStreamWriter(outputStream)) { + this.printCsv(writer); } + + csv = outputStream.toString(); + } + + return csv; + } + + private void printCsv(Writer writer) throws IOException { + this.writeTitleRow(writer); + + for (String[] datum : this.data) { + this.printRow(writer, datum); } } diff --git a/core/src/test/java/de/jplag/reporting/csv/CsvPrinterTest.java b/core/src/test/java/de/jplag/reporting/csv/CsvPrinterTest.java new file mode 100644 index 000000000..1ff98f8fd --- /dev/null +++ b/core/src/test/java/de/jplag/reporting/csv/CsvPrinterTest.java @@ -0,0 +1,35 @@ +package de.jplag.reporting.csv; + +import java.io.IOException; +import java.util.List; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class CsvPrinterTest { + private static final String EXPECTED_CSV_TEXT = "1,test1\r\n2,\"test2,\"\"x\"\"\"\r\n"; + private static final List TEST_ITEMS = List.of(new CsvTestItem(1, "test1"), new CsvTestItem(2, "test2,\"x\"")); + + @Test + void testPrintWithReflectiveMapper() throws IOException { + CsvDataMapper mapper = new ReflectiveCsvDataMapper<>(CsvTestItem.class); + CsvPrinter printer = new CsvPrinter<>(mapper); + + printer.addRows(TEST_ITEMS); + + Assertions.assertEquals(EXPECTED_CSV_TEXT, printer.printToString()); + } + + @Test + void testPrintWithHardcodedMapper() throws IOException { + CsvDataMapper mapper = new HardcodedCsvDataMapper<>(2, item -> new Object[] {item.number(), item.text()}); + CsvPrinter printer = new CsvPrinter<>(mapper); + + printer.addRows(TEST_ITEMS); + + Assertions.assertEquals(EXPECTED_CSV_TEXT, printer.printToString()); + } + + private record CsvTestItem(@CsvValue(1) int number, @CsvValue(2) String text) { + } +}