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());
}
}