diff --git a/core-java-modules/core-java-io-conversions/pom.xml b/core-java-modules/core-java-io-conversions/pom.xml index b3cb86e09d37..9760d4baf42a 100644 --- a/core-java-modules/core-java-io-conversions/pom.xml +++ b/core-java-modules/core-java-io-conversions/pom.xml @@ -38,7 +38,7 @@ - 5.8 + 5.9 - \ No newline at end of file + diff --git a/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/CustomCSVParserUnitTest.java b/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/CustomCSVParserUnitTest.java new file mode 100644 index 000000000000..e5f48d36649d --- /dev/null +++ b/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/CustomCSVParserUnitTest.java @@ -0,0 +1,79 @@ +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.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + + + +public class CustomCSVParserUnitTest { + public static final String COMMA_DELIMITER = ","; + + public static final String CSV_FILE = "src/test/resources/book3.csv"; + + public static final List> EXPECTED_ARRAY = Collections.unmodifiableList(new ArrayList>() { + { + add(new ArrayList() { + { + add("Kom, Mary"); + add("Unbreakable"); + } + }); + add(new ArrayList() { + { + add("Isapuari, Kapil"); + add("Farishta"); + } + }); + } + }); + + @Test + public void givenCSVFileWithCommaInValues_whenCustomCSVParser_thenContentsAsExpected() throws IOException { + List> records = new ArrayList>(); + try (BufferedReader br = new BufferedReader(new FileReader(CSV_FILE))) { + String line = ""; + while ((line = br.readLine()) != null) { + records.add(parseLine(line)); + } + } 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()); + } + } + + private static List parseLine(String line) { + List values = new ArrayList<>(); + boolean inQuotes = false; + StringBuilder currentValue = new StringBuilder(); + + for (char c : line.toCharArray()) { + if (c == '"') { + inQuotes = !inQuotes; + } else if (c == ',' && !inQuotes) { + values.add(currentValue.toString()); + currentValue = new StringBuilder(); + } else { + currentValue.append(c); + } + } + values.add(currentValue.toString()); + return values; + } +} diff --git a/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/ReadCSVInArrayUnitTest.java b/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/ReadCSVInArrayUnitTest.java index 5ef66e10458c..794c50156a0a 100644 --- a/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/ReadCSVInArrayUnitTest.java +++ b/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/ReadCSVInArrayUnitTest.java @@ -40,6 +40,7 @@ public class ReadCSVInArrayUnitTest { } }); + @Test public void givenCSVFile_whenBufferedReader_thenContentsAsExpected() throws IOException { List> records = new ArrayList>(); diff --git a/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/ReadCSVWithCommaInValuesIntoArrayUnitTest.java b/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/ReadCSVWithCommaInValuesIntoArrayUnitTest.java new file mode 100644 index 000000000000..38f1c9d7add6 --- /dev/null +++ b/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/csv/ReadCSVWithCommaInValuesIntoArrayUnitTest.java @@ -0,0 +1,176 @@ +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.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Scanner; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.Assert; +import org.junit.Test; + +import com.opencsv.CSVReader; + +public class ReadCSVWithCommaInValuesIntoArrayUnitTest { + public static final String COMMA_DELIMITER = "\\|"; + public static final String CSV_FILE = "src/test/resources/book2.csv"; + public static final String CSV_FILE_OpenCSV = "src/test/resources/book3.csv"; + + public static final List> EXPECTED_ARRAY = Collections.unmodifiableList(new ArrayList>() { + { + add(new ArrayList() { + { + add("\"Kom, Mary\""); + add("Unbreakable"); + } + }); + add(new ArrayList() { + { + add("\"Isapuari, Kapil\""); + add("Farishta"); + } + }); + } + }); + + public static final List> EXPECTED_ARRAY_OpenCSV = Collections.unmodifiableList(new ArrayList>() { + { + add(new ArrayList() { + { + add("Kom, Mary"); + add("Unbreakable"); + } + }); + add(new ArrayList() { + { + add("Isapuari, Kapil"); + add("Farishta"); + } + }); + } + }); + + + @Test + public void givenCSVFileWithCommaInValues_whenOpencsv_thenContentsAsExpected() throws IOException { + List> records = new ArrayList>(); + try (CSVReader csvReader = new CSVReader(new FileReader(CSV_FILE_OpenCSV));) { + 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_OpenCSV.size(); i++) { + Assert.assertArrayEquals(EXPECTED_ARRAY_OpenCSV.get(i) + .toArray(), + records.get(i) + .toArray()); + } + } + + @Test + public void givenCSVFileWithCommaInValues_whenBufferedReader_thenContentsAsExpected() throws IOException { + List> records = new ArrayList>(); + 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 givenCSVFileWithCommaInValues_whenScanner_thenContentsAsExpected() throws IOException { + List> records = new ArrayList>(); + 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 getRecordFromLine(String line) { + List values = new ArrayList(); + try (Scanner rowScanner = new Scanner(line)) { + rowScanner.useDelimiter(COMMA_DELIMITER); + while (rowScanner.hasNext()) { + values.add(rowScanner.next()); + } + } + return values; + } + + @Test + public void givenCSVFileWithCommaInValues_whenUsingFilesReadAllLinesMethod_thenContentsAsExpected() throws IOException { + List> records = Files.readAllLines(Paths.get(CSV_FILE)) + .stream() + .map(line -> Arrays.asList(line.split(COMMA_DELIMITER))) + .collect(Collectors.toList()); + + for (int i = 0; i < EXPECTED_ARRAY.size(); i++) { + Assert.assertArrayEquals(EXPECTED_ARRAY.get(i) + .toArray(), + records.get(i) + .toArray()); + } + } + + @Test + public void givenCSVFileWithCommaInValues_whenUsingFilesNewBufferedReaderMethod_thenContentsAsExpected() throws IOException { + try (BufferedReader reader = Files.newBufferedReader(Paths.get(CSV_FILE))) { + List> records = reader.lines() + .map(line -> Arrays.asList(line.split(COMMA_DELIMITER))) + .collect(Collectors.toList()); + + for (int i = 0; i < EXPECTED_ARRAY.size(); i++) { + Assert.assertArrayEquals(EXPECTED_ARRAY.get(i) + .toArray(), + records.get(i) + .toArray()); + } + } + } + + @Test + public void givenCSVFileWithCommaInValues_whenUsingFilesLinesMethod_thenContentsAsExpected() throws IOException { + try (Stream lines = Files.lines(Paths.get(CSV_FILE))) { + List> records = lines.map(line -> Arrays.asList(line.split(COMMA_DELIMITER))) + .collect(Collectors.toList()); + + for (int i = 0; i < EXPECTED_ARRAY.size(); i++) { + Assert.assertArrayEquals(EXPECTED_ARRAY.get(i) + .toArray(), + records.get(i) + .toArray()); + } + } + } +} diff --git a/core-java-modules/core-java-io-conversions/src/test/resources/book2.csv b/core-java-modules/core-java-io-conversions/src/test/resources/book2.csv new file mode 100644 index 000000000000..53c2e13f8976 --- /dev/null +++ b/core-java-modules/core-java-io-conversions/src/test/resources/book2.csv @@ -0,0 +1,2 @@ +"Kom, Mary"|Unbreakable +"Isapuari, Kapil"|Farishta diff --git a/core-java-modules/core-java-io-conversions/src/test/resources/book3.csv b/core-java-modules/core-java-io-conversions/src/test/resources/book3.csv new file mode 100644 index 000000000000..8e92097095af --- /dev/null +++ b/core-java-modules/core-java-io-conversions/src/test/resources/book3.csv @@ -0,0 +1,2 @@ +"Kom, Mary",Unbreakable +"Isapuari, Kapil",Farishta