From 73a6c2c8326982d0206221f2fa887ca98a0f1132 Mon Sep 17 00:00:00 2001
From: panos-kakos <102670093+panos-kakos@users.noreply.github.com>
Date: Thu, 23 Feb 2023 19:12:26 +0200
Subject: [PATCH] =?UTF-8?q?[JAVA-18754]=20Upgraded=20to=20the=20latest=20v?=
=?UTF-8?q?ersion=20of=20commons-csv=20+=20Replaced=E2=80=A6=20(#13496)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* [JAVA-18754] Upgraded to the latest version of commons-csv + Replaced deprecated method
* [JAVA-18754] Removed setUp method
---
libraries-apache-commons-io/pom.xml | 2 +-
.../io/csv/CSVReaderWriterUnitTest.java | 54 +++++++++++++++----
2 files changed, 46 insertions(+), 10 deletions(-)
diff --git a/libraries-apache-commons-io/pom.xml b/libraries-apache-commons-io/pom.xml
index b45572ddad..7cac50a8e2 100644
--- a/libraries-apache-commons-io/pom.xml
+++ b/libraries-apache-commons-io/pom.xml
@@ -26,7 +26,7 @@
- 1.9.0
+ 1.10.0
\ No newline at end of file
diff --git a/libraries-apache-commons-io/src/test/java/com/baeldung/commons/io/csv/CSVReaderWriterUnitTest.java b/libraries-apache-commons-io/src/test/java/com/baeldung/commons/io/csv/CSVReaderWriterUnitTest.java
index b99f4e8bc3..b37613e962 100644
--- a/libraries-apache-commons-io/src/test/java/com/baeldung/commons/io/csv/CSVReaderWriterUnitTest.java
+++ b/libraries-apache-commons-io/src/test/java/com/baeldung/commons/io/csv/CSVReaderWriterUnitTest.java
@@ -1,9 +1,11 @@
package com.baeldung.commons.io.csv;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import org.junit.jupiter.api.Test;
+
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;
@@ -13,9 +15,7 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
-import static org.junit.Assert.assertEquals;
-
-public class CSVReaderWriterUnitTest {
+class CSVReaderWriterUnitTest {
public static final Map AUTHOR_BOOK_MAP = Collections.unmodifiableMap(new LinkedHashMap() {
{
@@ -24,12 +24,24 @@ public class CSVReaderWriterUnitTest {
}
});
public static final String[] HEADERS = { "author", "title" };
+
+ enum BookHeaders{
+ 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 {
+ void givenCSVFile_whenReadWithArrayHeader_thenContentsAsExpected() throws IOException {
Reader in = new FileReader("src/test/resources/book.csv");
- Iterable records = CSVFormat.DEFAULT.withHeader(HEADERS).withFirstRecordAsHeader().parse(in);
+
+ CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
+ .setHeader(HEADERS)
+ .setSkipHeaderRecord(true)
+ .build();
+
+ Iterable records = csvFormat.parse(in);
+
for (CSVRecord record : records) {
String author = record.get("author");
String title = record.get("title");
@@ -38,9 +50,32 @@ public class CSVReaderWriterUnitTest {
}
@Test
- public void givenAuthorBookMap_whenWrittenToStream_thenOutputStreamAsExpected() throws IOException {
+ void givenCSVFile_whenReadWithEnumHeader_thenContentsAsExpected() throws IOException {
+ Reader in = new FileReader("src/test/resources/book.csv");
+
+ CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
+ .setHeader(BookHeaders.class)
+ .setSkipHeaderRecord(true)
+ .build();
+
+ Iterable records = csvFormat.parse(in);
+
+ for (CSVRecord record : records) {
+ String author = record.get(BookHeaders.author);
+ String title = record.get(BookHeaders.title);
+ assertEquals(AUTHOR_BOOK_MAP.get(author), title);
+ }
+ }
+
+ @Test
+ void givenAuthorBookMap_whenWrittenToStream_thenOutputStreamAsExpected() throws IOException {
StringWriter sw = new StringWriter();
- try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withHeader(HEADERS))) {
+
+ CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
+ .setHeader(BookHeaders.class)
+ .build();
+
+ try (final CSVPrinter printer = new CSVPrinter(sw, csvFormat)) {
AUTHOR_BOOK_MAP.forEach((author, title) -> {
try {
printer.printRecord(author, title);
@@ -49,7 +84,8 @@ public class CSVReaderWriterUnitTest {
}
});
}
- assertEquals(EXPECTED_FILESTREAM, sw.toString().trim());
+ assertEquals(EXPECTED_FILESTREAM, sw.toString()
+ .trim());
}
}