BAEL-1105: Apache Commons CSV, Test cases

This commit is contained in:
Mohit Sinha 2017-08-15 14:52:38 +05:30
parent 290e759d4a
commit 9308baeb9c
3 changed files with 70 additions and 1 deletions

View File

@ -212,6 +212,11 @@
<artifactId>commons-chain</artifactId>
<version>${commons-chain.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>${commons-csv.version}</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
@ -480,6 +485,7 @@
<commons-text.version>1.1</commons-text.version>
<commons-beanutils.version>1.9.3</commons-beanutils.version>
<commons-chain.version>1.2</commons-chain.version>
<commons-csv.version>1.4</commons-csv.version>
<jasypt.version>1.9.2</jasypt.version>
<javatuples.version>1.2</javatuples.version>
<javaassist.version>3.21.0-GA</javaassist.version>
@ -515,4 +521,4 @@
<rome.version>1.0</rome.version>
<eclipse-collections.version>8.2.0</eclipse-collections.version>
</properties>
</project>
</project>

View File

@ -0,0 +1,60 @@
package com.baeldung.commons.csv;
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;
import java.io.Reader;
import java.io.StringWriter;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class CSVReaderWriterTest {
public static final Map<String, String> AUTHOR_BOOK_MAP = Collections.unmodifiableMap(new LinkedHashMap<String, String>() {
{
put("Dan Simmons", "Hyperion");
put("Douglas Adams", "The Hitchhiker's Guide to the Galaxy");
}
});
public static final String[] HEADERS = { "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 {
Reader in = new FileReader("src/test/resources/book.csv");
Iterable<CSVRecord> records = CSVFormat.DEFAULT
.withHeader(HEADERS)
.withFirstRecordAsHeader()
.parse(in);
for (CSVRecord record : records) {
String author = record.get("author");
String title = record.get("title");
assertEquals(AUTHOR_BOOK_MAP.get(author), title);
}
}
@Test
public void givenAuthorBookMap_whenProcessed_thenFileCreatedAsExpected() throws IOException {
StringWriter sw = new StringWriter();
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withHeader(HEADERS))) {
AUTHOR_BOOK_MAP.forEach((author, title) -> {
try {
printer.printRecord(author, title);
} catch (IOException e) {
e.printStackTrace();
}
});
}
assertEquals(EXPECTED_FILESTREAM, sw
.toString()
.trim());
}
}

View File

@ -0,0 +1,3 @@
author,title
Dan Simmons,Hyperion
Douglas Adams,The Hitchhiker's Guide to the Galaxy
1 author title
2 Dan Simmons Hyperion
3 Douglas Adams The Hitchhiker's Guide to the Galaxy