11package com .baeldung .commons .io .csv ;
22
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import org .junit .jupiter .api .Test ;
5+
36import org .apache .commons .csv .CSVFormat ;
47import org .apache .commons .csv .CSVPrinter ;
58import org .apache .commons .csv .CSVRecord ;
6- import org .junit .Test ;
79
810import java .io .FileReader ;
911import java .io .IOException ;
1315import java .util .LinkedHashMap ;
1416import 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