Skip to content

Commit 5d20115

Browse files
authored
Merge pull request eugenp#2446 from mohitsinha/BAEL-1105
BAEL 1105- Apache Commons CSV
2 parents 6db067d + 299be4a commit 5d20115

3 files changed

Lines changed: 70 additions & 1 deletion

File tree

libraries/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@
212212
<artifactId>commons-chain</artifactId>
213213
<version>${commons-chain.version}</version>
214214
</dependency>
215+
<dependency>
216+
<groupId>org.apache.commons</groupId>
217+
<artifactId>commons-csv</artifactId>
218+
<version>${commons-csv.version}</version>
219+
</dependency>
215220
<dependency>
216221
<groupId>commons-dbutils</groupId>
217222
<artifactId>commons-dbutils</artifactId>
@@ -480,6 +485,7 @@
480485
<commons-text.version>1.1</commons-text.version>
481486
<commons-beanutils.version>1.9.3</commons-beanutils.version>
482487
<commons-chain.version>1.2</commons-chain.version>
488+
<commons-csv.version>1.4</commons-csv.version>
483489
<jasypt.version>1.9.2</jasypt.version>
484490
<javatuples.version>1.2</javatuples.version>
485491
<javaassist.version>3.21.0-GA</javaassist.version>
@@ -515,4 +521,4 @@
515521
<rome.version>1.0</rome.version>
516522
<eclipse-collections.version>8.2.0</eclipse-collections.version>
517523
</properties>
518-
</project>
524+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.commons.csv;
2+
3+
import org.apache.commons.csv.CSVFormat;
4+
import org.apache.commons.csv.CSVPrinter;
5+
import org.apache.commons.csv.CSVRecord;
6+
import org.junit.Test;
7+
8+
import java.io.FileReader;
9+
import java.io.IOException;
10+
import java.io.Reader;
11+
import java.io.StringWriter;
12+
import java.util.Collections;
13+
import java.util.LinkedHashMap;
14+
import java.util.Map;
15+
16+
import static org.junit.Assert.assertEquals;
17+
18+
public class CSVReaderWriterTest {
19+
20+
public static final Map<String, String> AUTHOR_BOOK_MAP = Collections.unmodifiableMap(new LinkedHashMap<String, String>() {
21+
{
22+
put("Dan Simmons", "Hyperion");
23+
put("Douglas Adams", "The Hitchhiker's Guide to the Galaxy");
24+
}
25+
});
26+
public static final String[] HEADERS = { "author", "title" };
27+
public static final String EXPECTED_FILESTREAM = "author,title\r\n" + "Dan Simmons,Hyperion\r\n" + "Douglas Adams,The Hitchhiker's Guide to the Galaxy";
28+
29+
@Test
30+
public void givenCSVFile_whenRead_thenContentsAsExpected() throws IOException {
31+
Reader in = new FileReader("src/test/resources/book.csv");
32+
Iterable<CSVRecord> records = CSVFormat.DEFAULT
33+
.withHeader(HEADERS)
34+
.withFirstRecordAsHeader()
35+
.parse(in);
36+
for (CSVRecord record : records) {
37+
String author = record.get("author");
38+
String title = record.get("title");
39+
assertEquals(AUTHOR_BOOK_MAP.get(author), title);
40+
}
41+
}
42+
43+
@Test
44+
public void givenAuthorBookMap_whenWrittenToStream_thenOutputStreamAsExpected() throws IOException {
45+
StringWriter sw = new StringWriter();
46+
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withHeader(HEADERS))) {
47+
AUTHOR_BOOK_MAP.forEach((author, title) -> {
48+
try {
49+
printer.printRecord(author, title);
50+
} catch (IOException e) {
51+
e.printStackTrace();
52+
}
53+
});
54+
}
55+
assertEquals(EXPECTED_FILESTREAM, sw
56+
.toString()
57+
.trim());
58+
}
59+
60+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
author,title
2+
Dan Simmons,Hyperion
3+
Douglas Adams,The Hitchhiker's Guide to the Galaxy

0 commit comments

Comments
 (0)