Skip to content

Commit

Permalink
BAEL-2236: Reading a CSV file into a array
Browse files Browse the repository at this point in the history
  • Loading branch information
PranayJain committed Oct 22, 2018
1 parent 8c3598b commit daa1de2
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
8 changes: 7 additions & 1 deletion core-java-io/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@
<artifactId>async-http-client</artifactId>
<version>${async-http-client.version}</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>${opencsv.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -247,7 +253,7 @@
<protonpack.version>1.13</protonpack.version>
<streamex.version>0.6.5</streamex.version>
<vavr.version>0.9.0</vavr.version>

<opencsv.version>4.1</opencsv.version>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<avaitility.version>1.7.0</avaitility.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.baeldung.csv;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

import org.junit.Assert;
import org.junit.Test;

import com.opencsv.CSVReader;

public class ReadCSVInArrayUnitTest {
public static final String COMMA_DELIMITER = ",";
public static final String CSV_FILE = "src/test/resources/book.csv";
public static final List<List<String>> EXPECTED_ARRAY = Collections.unmodifiableList(new ArrayList<List<String>>() {
{
add(new ArrayList<String>() {
{
add("Mary Kom");
add("Unbreakable");
}
});
add(new ArrayList<String>() {
{
add("Kapil Isapuari");
add("Farishta");
}
});
}
});

@Test
public void givenCSVFile_whenBufferedReader_thenContentsAsExpected() throws IOException {
List<List<String>> records = new ArrayList<List<String>>();
try (BufferedReader br = new BufferedReader(new FileReader(CSV_FILE))) {
String line = "";
while ((line = br.readLine()) != null) {
String[] values = line.split(COMMA_DELIMITER);
records.add(Arrays.asList(values));
}
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < EXPECTED_ARRAY.size(); i++) {
Assert.assertArrayEquals(EXPECTED_ARRAY.get(i)
.toArray(),
records.get(i)
.toArray());
}
}

@Test
public void givenCSVFile_whenScanner_thenContentsAsExpected() throws IOException {
List<List<String>> records = new ArrayList<List<String>>();
try (Scanner scanner = new Scanner(new File(CSV_FILE));) {
while (scanner.hasNextLine()) {
records.add(getRecordFromLine(scanner.nextLine()));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (int i = 0; i < EXPECTED_ARRAY.size(); i++) {
Assert.assertArrayEquals(EXPECTED_ARRAY.get(i)
.toArray(),
records.get(i)
.toArray());
}
}

private List<String> getRecordFromLine(String line) {
List<String> values = new ArrayList<String>();
try (Scanner rowScanner = new Scanner(line)) {
rowScanner.useDelimiter(COMMA_DELIMITER);
while (rowScanner.hasNext()) {
values.add(rowScanner.next());
}
}
return values;
}

@Test
public void givenCSVFile_whenOpencsv_thenContentsAsExpected() throws IOException {
List<List<String>> records = new ArrayList<List<String>>();
try (CSVReader csvReader = new CSVReader(new FileReader(CSV_FILE));) {
String[] values = null;
while ((values = csvReader.readNext()) != null) {
records.add(Arrays.asList(values));
}
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < EXPECTED_ARRAY.size(); i++) {
Assert.assertArrayEquals(EXPECTED_ARRAY.get(i)
.toArray(),
records.get(i)
.toArray());
}
}
}
2 changes: 2 additions & 0 deletions core-java-io/src/test/resources/book.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Mary Kom,Unbreakable
Kapil Isapuari,Farishta

0 comments on commit daa1de2

Please sign in to comment.