From 9308baeb9c6ca5b449746bb02935f69391e3746c Mon Sep 17 00:00:00 2001 From: Mohit Sinha Date: Tue, 15 Aug 2017 14:52:38 +0530 Subject: [PATCH] BAEL-1105: Apache Commons CSV, Test cases --- libraries/pom.xml | 8 ++- .../commons/csv/CSVReaderWriterTest.java | 60 +++++++++++++++++++ libraries/src/test/resources/book.csv | 3 + 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 libraries/src/test/java/com/baeldung/commons/csv/CSVReaderWriterTest.java create mode 100644 libraries/src/test/resources/book.csv diff --git a/libraries/pom.xml b/libraries/pom.xml index a16a4de59d..c70db4ffb2 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -212,6 +212,11 @@ commons-chain ${commons-chain.version} + + org.apache.commons + commons-csv + ${commons-csv.version} + commons-dbutils commons-dbutils @@ -480,6 +485,7 @@ 1.1 1.9.3 1.2 + 1.4 1.9.2 1.2 3.21.0-GA @@ -515,4 +521,4 @@ 1.0 8.2.0 - \ No newline at end of file + diff --git a/libraries/src/test/java/com/baeldung/commons/csv/CSVReaderWriterTest.java b/libraries/src/test/java/com/baeldung/commons/csv/CSVReaderWriterTest.java new file mode 100644 index 0000000000..deab15a812 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/csv/CSVReaderWriterTest.java @@ -0,0 +1,60 @@ +package com.baeldung.commons.csv; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; +import org.apache.commons.csv.CSVRecord; +import org.junit.Test; + +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; +import java.io.StringWriter; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class CSVReaderWriterTest { + + public static final Map AUTHOR_BOOK_MAP = Collections.unmodifiableMap(new LinkedHashMap() { + { + put("Dan Simmons", "Hyperion"); + put("Douglas Adams", "The Hitchhiker's Guide to the Galaxy"); + } + }); + public static final String[] HEADERS = { "author", "title" }; + public static final String EXPECTED_FILESTREAM = "author,title\r\n" + "Dan Simmons,Hyperion\r\n" + "Douglas Adams,The Hitchhiker's Guide to the Galaxy"; + + @Test + public void givenCSVFile_whenRead_thenContentsAsExpected() throws IOException { + Reader in = new FileReader("src/test/resources/book.csv"); + Iterable records = CSVFormat.DEFAULT + .withHeader(HEADERS) + .withFirstRecordAsHeader() + .parse(in); + for (CSVRecord record : records) { + String author = record.get("author"); + String title = record.get("title"); + assertEquals(AUTHOR_BOOK_MAP.get(author), title); + } + } + + @Test + public void givenAuthorBookMap_whenProcessed_thenFileCreatedAsExpected() throws IOException { + StringWriter sw = new StringWriter(); + try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withHeader(HEADERS))) { + AUTHOR_BOOK_MAP.forEach((author, title) -> { + try { + printer.printRecord(author, title); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + assertEquals(EXPECTED_FILESTREAM, sw + .toString() + .trim()); + } + +} diff --git a/libraries/src/test/resources/book.csv b/libraries/src/test/resources/book.csv new file mode 100644 index 0000000000..d709152a5e --- /dev/null +++ b/libraries/src/test/resources/book.csv @@ -0,0 +1,3 @@ +author,title +Dan Simmons,Hyperion +Douglas Adams,The Hitchhiker's Guide to the Galaxy