forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-2236: Reading a CSV file into a array
- Loading branch information
1 parent
8c3598b
commit daa1de2
Showing
3 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
core-java-io/src/test/java/com/baeldung/csv/ReadCSVInArrayUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Mary Kom,Unbreakable | ||
Kapil Isapuari,Farishta |