package diergo.csv;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;

import static diergo.csv.CsvReaderBuilder.toCsvStream;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

public class CsvReaderCompatibilityIntegrationTest {

    public static final String UNI_VELOCITY_CORRECTNESS =
        "https://raw.githubusercontent.com/uniVocity/csv-parsers-comparison/master/src/main/resources/correctness.csv";
    public static final String MAXMIND_WORLD_CITIES_POP =
        "http://www.maxmind.com/download/worldcities/worldcitiespop.txt.gz";

    @Test
    public void readerIsCompatibleToUniVocityData() throws IOException {
        InputStream in = new URL(UNI_VELOCITY_CORRECTNESS).openStream();
        InputStreamReader csv = new InputStreamReader(in, StandardCharsets.ISO_8859_1);

        List<Row> rows = toCsvStream(csv).separatedBy(',').treatEmptyAsNull().build().collect(toList());

        assertThat(rows.size(), is(6));
        assertThat(rows, is(asList(
            new Columns("Year", "Make", "Model", "Description", "Price"),
            new Columns("1997", "Ford", "E350", "ac, abs, moon", "3000.00"),
            new Columns("1999", "Chevy", "Venture \"Extended Edition\"", null, "4900.00"),
            new Columns("1996", "Jeep", "Grand Cherokee", "MUST SELL!\nair, moon roof, loaded", "4799.00"),
            new Columns("1999", "Chevy", "Venture \"Extended Edition, Very Large\"", null, "5000.00"),
            new Columns(null, null, "Venture \"Extended Edition\"", null, "4900.00")
        )));
    }

    @Test
    public void readerHandlesHugeAmountOfData() throws IOException {
        InputStream in = new GZIPInputStream(new URL(MAXMIND_WORLD_CITIES_POP).openStream());
        InputStreamReader worldCitiesPopulation = new InputStreamReader(in, StandardCharsets.UTF_8);

        Map<Integer, Long> countByColumns = toCsvStream(worldCitiesPopulation).separatedBy(',').laxMode().treatEmptyAsNull().build().collect(Collectors.groupingBy(row -> row.getLength(), Collectors.counting()));

        assertThat(countByColumns.size(), is(1));
        assertThat(countByColumns.get(7), greaterThan(3000000L));
    }
}

@@ -14,16 +14,46 @@
 o.csv;%0A%0A
+import org.junit.BeforeClass;%0A
 import o
@@ -83,27 +83,31 @@
 java.io.
-IOException
+FileInputStream
 ;%0Aimport
@@ -116,26 +116,26 @@
 ava.io.I
-nputStream
+OException
 ;%0Aimport
@@ -241,17 +241,22 @@
 ava.
-util.List
+nio.file.Files
 ;%0Aim
@@ -269,16 +269,21 @@
 ava.
-util.Map
+nio.file.Path
 ;%0Aim
@@ -297,33 +297,20 @@
 va.util.
+Li
 st
-ream.Collectors
 ;%0Aimport
@@ -393,24 +393,89 @@
 oCsvStream;%0A
+import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;%0A
 import stati
@@ -1052,16 +1052,107 @@
 t.gz%22;%0A%0A
+    private static Path UNIVOCITY_CORRECTNESS;%0A    private static Path WORLDS_CITIES_POP;%0A%0A
     @Tes
@@ -1249,118 +1249,94 @@
 ream
- in = new URL(UNI_VELOCITY_CORRECTNESS).openStream();%0A        InputStreamReader csv = new InputStreamReader(in
+Reader csv = new InputStreamReader(new FileInputStream(UNIVOCITY_CORRECTNESS.toFile())
 , St
@@ -2206,226 +2206,203 @@
 ream
- in = new GZIPInputStream(new URL(MAXMIND_WORLD_CITIES_POP).openStream());%0A        InputStreamReader worldCitiesPopulation = new InputStreamReader(in, StandardCharsets.UTF_8);%0A%0A        Map%3CInteger, L
+Reader worldCitiesPopulation = new InputStreamReader(new FileInputStream(WORLDS_CITIES_POP.toFile()), StandardCharsets.UTF_8);%0A%0A        long start = System.currentTimeMillis();%0A        l
 ong
-%3E
  count
-ByColumns
  = t
@@ -2466,115 +2466,164 @@
 e().
-treatEmptyAsNull().build().collect(Collectors.groupingBy(row -%3E row.getLength(), Collectors.counting())
+build().count();%0A        long time = (System.currentTimeMillis() - start);%0A        System.out.println(%22took %22 + time + %22 ms to read %22 + count + %22 rows. %22
 );%0A
-%0A
     
@@ -2646,97 +2646,506 @@
 ount
-ByColumns.size(), is(1));%0A        assertThat(countByColumns.get(7), greaterThan(3000000L)
+, greaterThan(3000000L));%0A    %7D%0A%0A    @BeforeClass%0A    public static void cacheFilesLocally() throws IOException %7B%0A        WORLDS_CITIES_POP = Files.createTempFile(%22worldcitiespop%22, %22txt%22);%0A        Files.copy(new GZIPInputStream(new URL(MAXMIND_WORLD_CITIES_POP).openStream()), WORLDS_CITIES_POP, REPLACE_EXISTING);%0A%0A        UNIVOCITY_CORRECTNESS = Files.createTempFile(%22correctness%22, %22csv%22);%0A        Files.copy(new URL(UNI_VELOCITY_CORRECTNESS).openStream(), UNIVOCITY_CORRECTNESS, REPLACE_EXISTING
 );%0A 

use local files to check performance