Skip to content

Commit 73a6c2c

Browse files
authored
[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
1 parent 0560ac2 commit 73a6c2c

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

libraries-apache-commons-io/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</dependencies>
2727

2828
<properties>
29-
<commons-csv.version>1.9.0</commons-csv.version>
29+
<commons-csv.version>1.10.0</commons-csv.version>
3030
</properties>
3131

3232
</project>
Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.baeldung.commons.io.csv;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import org.junit.jupiter.api.Test;
5+
36
import org.apache.commons.csv.CSVFormat;
47
import org.apache.commons.csv.CSVPrinter;
58
import org.apache.commons.csv.CSVRecord;
6-
import org.junit.Test;
79

810
import java.io.FileReader;
911
import java.io.IOException;
@@ -13,9 +15,7 @@
1315
import java.util.LinkedHashMap;
1416
import java.util.Map;
1517

16-
import static org.junit.Assert.assertEquals;
17-
18-
public class CSVReaderWriterUnitTest {
18+
class CSVReaderWriterUnitTest {
1919

2020
public static final Map<String, String> AUTHOR_BOOK_MAP = Collections.unmodifiableMap(new LinkedHashMap<String, String>() {
2121
{
@@ -24,12 +24,24 @@ public class CSVReaderWriterUnitTest {
2424
}
2525
});
2626
public static final String[] HEADERS = { "author", "title" };
27+
28+
enum BookHeaders{
29+
author, title
30+
}
31+
2732
public static final String EXPECTED_FILESTREAM = "author,title\r\n" + "Dan Simmons,Hyperion\r\n" + "Douglas Adams,The Hitchhiker's Guide to the Galaxy";
2833

2934
@Test
30-
public void givenCSVFile_whenRead_thenContentsAsExpected() throws IOException {
35+
void givenCSVFile_whenReadWithArrayHeader_thenContentsAsExpected() throws IOException {
3136
Reader in = new FileReader("src/test/resources/book.csv");
32-
Iterable<CSVRecord> records = CSVFormat.DEFAULT.withHeader(HEADERS).withFirstRecordAsHeader().parse(in);
37+
38+
CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
39+
.setHeader(HEADERS)
40+
.setSkipHeaderRecord(true)
41+
.build();
42+
43+
Iterable<CSVRecord> records = csvFormat.parse(in);
44+
3345
for (CSVRecord record : records) {
3446
String author = record.get("author");
3547
String title = record.get("title");
@@ -38,9 +50,32 @@ public void givenCSVFile_whenRead_thenContentsAsExpected() throws IOException {
3850
}
3951

4052
@Test
41-
public void givenAuthorBookMap_whenWrittenToStream_thenOutputStreamAsExpected() throws IOException {
53+
void givenCSVFile_whenReadWithEnumHeader_thenContentsAsExpected() throws IOException {
54+
Reader in = new FileReader("src/test/resources/book.csv");
55+
56+
CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
57+
.setHeader(BookHeaders.class)
58+
.setSkipHeaderRecord(true)
59+
.build();
60+
61+
Iterable<CSVRecord> records = csvFormat.parse(in);
62+
63+
for (CSVRecord record : records) {
64+
String author = record.get(BookHeaders.author);
65+
String title = record.get(BookHeaders.title);
66+
assertEquals(AUTHOR_BOOK_MAP.get(author), title);
67+
}
68+
}
69+
70+
@Test
71+
void givenAuthorBookMap_whenWrittenToStream_thenOutputStreamAsExpected() throws IOException {
4272
StringWriter sw = new StringWriter();
43-
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withHeader(HEADERS))) {
73+
74+
CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
75+
.setHeader(BookHeaders.class)
76+
.build();
77+
78+
try (final CSVPrinter printer = new CSVPrinter(sw, csvFormat)) {
4479
AUTHOR_BOOK_MAP.forEach((author, title) -> {
4580
try {
4681
printer.printRecord(author, title);
@@ -49,7 +84,8 @@ public void givenAuthorBookMap_whenWrittenToStream_thenOutputStreamAsExpected()
4984
}
5085
});
5186
}
52-
assertEquals(EXPECTED_FILESTREAM, sw.toString().trim());
87+
assertEquals(EXPECTED_FILESTREAM, sw.toString()
88+
.trim());
5389
}
5490

5591
}

0 commit comments

Comments
 (0)