|
| 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.nio.file.Files; |
| 9 | +import java.nio.file.Paths; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Scanner; |
| 15 | +import java.util.stream.Collectors; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import com.opencsv.CSVReader; |
| 22 | + |
| 23 | +public class ReadCSVWithCommaInValuesIntoArrayUnitTest { |
| 24 | + public static final String COMMA_DELIMITER = "\\|"; |
| 25 | + public static final String CSV_FILE = "src/test/resources/book2.csv"; |
| 26 | + public static final String CSV_FILE_OpenCSV = "src/test/resources/book3.csv"; |
| 27 | + |
| 28 | + public static final List<List<String>> EXPECTED_ARRAY = Collections.unmodifiableList(new ArrayList<List<String>>() { |
| 29 | + { |
| 30 | + add(new ArrayList<String>() { |
| 31 | + { |
| 32 | + add("\"Kom, Mary\""); |
| 33 | + add("Unbreakable"); |
| 34 | + } |
| 35 | + }); |
| 36 | + add(new ArrayList<String>() { |
| 37 | + { |
| 38 | + add("\"Isapuari, Kapil\""); |
| 39 | + add("Farishta"); |
| 40 | + } |
| 41 | + }); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + public static final List<List<String>> EXPECTED_ARRAY_OpenCSV = Collections.unmodifiableList(new ArrayList<List<String>>() { |
| 46 | + { |
| 47 | + add(new ArrayList<String>() { |
| 48 | + { |
| 49 | + add("Kom, Mary"); |
| 50 | + add("Unbreakable"); |
| 51 | + } |
| 52 | + }); |
| 53 | + add(new ArrayList<String>() { |
| 54 | + { |
| 55 | + add("Isapuari, Kapil"); |
| 56 | + add("Farishta"); |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + |
| 63 | + @Test |
| 64 | + public void givenCSVFileWithCommaInValues_whenOpencsv_thenContentsAsExpected() throws IOException { |
| 65 | + List<List<String>> records = new ArrayList<List<String>>(); |
| 66 | + try (CSVReader csvReader = new CSVReader(new FileReader(CSV_FILE_OpenCSV));) { |
| 67 | + String[] values = null; |
| 68 | + while ((values = csvReader.readNext()) != null) { |
| 69 | + records.add(Arrays.asList(values)); |
| 70 | + } |
| 71 | + } catch (Exception e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + for (int i = 0; i < EXPECTED_ARRAY_OpenCSV.size(); i++) { |
| 75 | + Assert.assertArrayEquals(EXPECTED_ARRAY_OpenCSV.get(i) |
| 76 | + .toArray(), |
| 77 | + records.get(i) |
| 78 | + .toArray()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void givenCSVFileWithCommaInValues_whenBufferedReader_thenContentsAsExpected() throws IOException { |
| 84 | + List<List<String>> records = new ArrayList<List<String>>(); |
| 85 | + try (BufferedReader br = new BufferedReader(new FileReader(CSV_FILE))) { |
| 86 | + String line = ""; |
| 87 | + while ((line = br.readLine()) != null) { |
| 88 | + String[] values = line.split(COMMA_DELIMITER); |
| 89 | + records.add(Arrays.asList(values)); |
| 90 | + } |
| 91 | + } catch (Exception e) { |
| 92 | + e.printStackTrace(); |
| 93 | + } |
| 94 | + for (int i = 0; i < EXPECTED_ARRAY.size(); i++) { |
| 95 | + Assert.assertArrayEquals(EXPECTED_ARRAY.get(i) |
| 96 | + .toArray(), |
| 97 | + records.get(i) |
| 98 | + .toArray()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void givenCSVFileWithCommaInValues_whenScanner_thenContentsAsExpected() throws IOException { |
| 104 | + List<List<String>> records = new ArrayList<List<String>>(); |
| 105 | + try (Scanner scanner = new Scanner(new File(CSV_FILE));) { |
| 106 | + while (scanner.hasNextLine()) { |
| 107 | + records.add(getRecordFromLine(scanner.nextLine())); |
| 108 | + } |
| 109 | + } catch (FileNotFoundException e) { |
| 110 | + e.printStackTrace(); |
| 111 | + } |
| 112 | + for (int i = 0; i < EXPECTED_ARRAY.size(); i++) { |
| 113 | + Assert.assertArrayEquals(EXPECTED_ARRAY.get(i) |
| 114 | + .toArray(), |
| 115 | + records.get(i) |
| 116 | + .toArray()); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private List<String> getRecordFromLine(String line) { |
| 121 | + List<String> values = new ArrayList<String>(); |
| 122 | + try (Scanner rowScanner = new Scanner(line)) { |
| 123 | + rowScanner.useDelimiter(COMMA_DELIMITER); |
| 124 | + while (rowScanner.hasNext()) { |
| 125 | + values.add(rowScanner.next()); |
| 126 | + } |
| 127 | + } |
| 128 | + return values; |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void givenCSVFileWithCommaInValues_whenUsingFilesReadAllLinesMethod_thenContentsAsExpected() throws IOException { |
| 133 | + List<List<String>> records = Files.readAllLines(Paths.get(CSV_FILE)) |
| 134 | + .stream() |
| 135 | + .map(line -> Arrays.asList(line.split(COMMA_DELIMITER))) |
| 136 | + .collect(Collectors.toList()); |
| 137 | + |
| 138 | + for (int i = 0; i < EXPECTED_ARRAY.size(); i++) { |
| 139 | + Assert.assertArrayEquals(EXPECTED_ARRAY.get(i) |
| 140 | + .toArray(), |
| 141 | + records.get(i) |
| 142 | + .toArray()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void givenCSVFileWithCommaInValues_whenUsingFilesNewBufferedReaderMethod_thenContentsAsExpected() throws IOException { |
| 148 | + try (BufferedReader reader = Files.newBufferedReader(Paths.get(CSV_FILE))) { |
| 149 | + List<List<String>> records = reader.lines() |
| 150 | + .map(line -> Arrays.asList(line.split(COMMA_DELIMITER))) |
| 151 | + .collect(Collectors.toList()); |
| 152 | + |
| 153 | + for (int i = 0; i < EXPECTED_ARRAY.size(); i++) { |
| 154 | + Assert.assertArrayEquals(EXPECTED_ARRAY.get(i) |
| 155 | + .toArray(), |
| 156 | + records.get(i) |
| 157 | + .toArray()); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void givenCSVFileWithCommaInValues_whenUsingFilesLinesMethod_thenContentsAsExpected() throws IOException { |
| 164 | + try (Stream<String> lines = Files.lines(Paths.get(CSV_FILE))) { |
| 165 | + List<List<String>> records = lines.map(line -> Arrays.asList(line.split(COMMA_DELIMITER))) |
| 166 | + .collect(Collectors.toList()); |
| 167 | + |
| 168 | + for (int i = 0; i < EXPECTED_ARRAY.size(); i++) { |
| 169 | + Assert.assertArrayEquals(EXPECTED_ARRAY.get(i) |
| 170 | + .toArray(), |
| 171 | + records.get(i) |
| 172 | + .toArray()); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments