[JAVA-18754] Upgraded to the latest version of commons-csv + Replaced… (#13496)

* [JAVA-18754] Upgraded to the latest version of commons-csv + Replaced deprecated method

* [JAVA-18754]  Removed setUp method
This commit is contained in:
panos-kakos 2023-02-23 19:12:26 +02:00 committed by GitHub
parent 0560ac2d9d
commit 73a6c2c832
2 changed files with 46 additions and 10 deletions

View File

@ -26,7 +26,7 @@
</dependencies>
<properties>
<commons-csv.version>1.9.0</commons-csv.version>
<commons-csv.version>1.10.0</commons-csv.version>
</properties>
</project>

View File

@ -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<String, String> AUTHOR_BOOK_MAP = Collections.unmodifiableMap(new LinkedHashMap<String, String>() {
{
@ -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<CSVRecord> records = CSVFormat.DEFAULT.withHeader(HEADERS).withFirstRecordAsHeader().parse(in);
CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
.setHeader(HEADERS)
.setSkipHeaderRecord(true)
.build();
Iterable<CSVRecord> 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<CSVRecord> 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());
}
}