|
| 1 | +package com.baeldung.csv; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.FileReader; |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.Collections; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Scanner; |
| 13 | + |
| 14 | +import org.junit.Assert; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import com.opencsv.CSVReader; |
| 18 | + |
| 19 | +public class ReadCSVInArrayUnitTest { |
| 20 | + public static final String COMMA_DELIMITER = ","; |
| 21 | + public static final String CSV_FILE = "src/test/resources/book.csv"; |
| 22 | + public static final List<List<String>> EXPECTED_ARRAY = Collections.unmodifiableList(new ArrayList<List<String>>() { |
| 23 | + { |
| 24 | + add(new ArrayList<String>() { |
| 25 | + { |
| 26 | + add("Mary Kom"); |
| 27 | + add("Unbreakable"); |
| 28 | + } |
| 29 | + }); |
| 30 | + add(new ArrayList<String>() { |
| 31 | + { |
| 32 | + add("Kapil Isapuari"); |
| 33 | + add("Farishta"); |
| 34 | + } |
| 35 | + }); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + @Test |
| 40 | + public void givenCSVFile_whenBufferedReader_thenContentsAsExpected() throws IOException { |
| 41 | + List<List<String>> records = new ArrayList<List<String>>(); |
| 42 | + try (BufferedReader br = new BufferedReader(new FileReader(CSV_FILE))) { |
| 43 | + String line = ""; |
| 44 | + while ((line = br.readLine()) != null) { |
| 45 | + String[] values = line.split(COMMA_DELIMITER); |
| 46 | + records.add(Arrays.asList(values)); |
| 47 | + } |
| 48 | + } catch (Exception e) { |
| 49 | + e.printStackTrace(); |
| 50 | + } |
| 51 | + for (int i = 0; i < EXPECTED_ARRAY.size(); i++) { |
| 52 | + Assert.assertArrayEquals(EXPECTED_ARRAY.get(i) |
| 53 | + .toArray(), |
| 54 | + records.get(i) |
| 55 | + .toArray()); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void givenCSVFile_whenScanner_thenContentsAsExpected() throws IOException { |
| 61 | + List<List<String>> records = new ArrayList<List<String>>(); |
| 62 | + try (Scanner scanner = new Scanner(new File(CSV_FILE));) { |
| 63 | + while (scanner.hasNextLine()) { |
| 64 | + records.add(getRecordFromLine(scanner.nextLine())); |
| 65 | + } |
| 66 | + } catch (FileNotFoundException e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + for (int i = 0; i < EXPECTED_ARRAY.size(); i++) { |
| 70 | + Assert.assertArrayEquals(EXPECTED_ARRAY.get(i) |
| 71 | + .toArray(), |
| 72 | + records.get(i) |
| 73 | + .toArray()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private List<String> getRecordFromLine(String line) { |
| 78 | + List<String> values = new ArrayList<String>(); |
| 79 | + try (Scanner rowScanner = new Scanner(line)) { |
| 80 | + rowScanner.useDelimiter(COMMA_DELIMITER); |
| 81 | + while (rowScanner.hasNext()) { |
| 82 | + values.add(rowScanner.next()); |
| 83 | + } |
| 84 | + } |
| 85 | + return values; |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void givenCSVFile_whenOpencsv_thenContentsAsExpected() throws IOException { |
| 90 | + List<List<String>> records = new ArrayList<List<String>>(); |
| 91 | + try (CSVReader csvReader = new CSVReader(new FileReader(CSV_FILE));) { |
| 92 | + String[] values = null; |
| 93 | + while ((values = csvReader.readNext()) != null) { |
| 94 | + records.add(Arrays.asList(values)); |
| 95 | + } |
| 96 | + } catch (Exception e) { |
| 97 | + e.printStackTrace(); |
| 98 | + } |
| 99 | + for (int i = 0; i < EXPECTED_ARRAY.size(); i++) { |
| 100 | + Assert.assertArrayEquals(EXPECTED_ARRAY.get(i) |
| 101 | + .toArray(), |
| 102 | + records.get(i) |
| 103 | + .toArray()); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments