|
| 1 | +package org.apache.commons.csv.issues; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStreamReader; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | + |
| 9 | +import org.apache.commons.csv.CSVFormat; |
| 10 | +import org.apache.commons.csv.CSVParser; |
| 11 | +import org.apache.commons.csv.CSVRecord; |
| 12 | +import org.apache.commons.csv.QuoteMode; |
| 13 | +import org.junit.Ignore; |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +/** |
| 17 | + * Tests https://issues.apache.org/jira/browse/CSV-213 |
| 18 | + * |
| 19 | + * This is normal behavior with the current architecture: The iterator() API presents an object that is backed by data |
| 20 | + * in the CSVParser as the parser is streaming over the file. The CSVParser is like a forward-only stream. When you |
| 21 | + * create a new Iterator you are only created a new view on the same position in the parser's stream. For the behavior |
| 22 | + * you want, you need to open a new CSVParser. |
| 23 | + * |
| 24 | + */ |
| 25 | +@Ignore |
| 26 | +public class JiraCsv213Test { |
| 27 | + |
| 28 | + private void createEndChannel(File csvFile) { |
| 29 | + // @formatter:off |
| 30 | + final CSVFormat csvFormat = |
| 31 | + CSVFormat.DEFAULT |
| 32 | + .withDelimiter(';') |
| 33 | + .withFirstRecordAsHeader() |
| 34 | + .withRecordSeparator('\n') |
| 35 | + .withQuoteMode(QuoteMode.ALL); |
| 36 | + // @formatter:on |
| 37 | + try (CSVParser parser = csvFormat |
| 38 | + .parse(new InputStreamReader(new FileInputStream(csvFile), StandardCharsets.UTF_8))) { |
| 39 | + if (parser.iterator().hasNext()) { |
| 40 | + System.out.println(parser.getCurrentLineNumber()); |
| 41 | + System.out.println(parser.getRecordNumber()); |
| 42 | + // get only first record we don't need other's |
| 43 | + CSVRecord firstRecord = parser.iterator().next(); // this fails |
| 44 | + |
| 45 | + return; |
| 46 | + } |
| 47 | + } catch (IOException e) { |
| 48 | + throw new RuntimeException("Error while adding end channel to csv", e); |
| 49 | + } |
| 50 | + |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void test() { |
| 56 | + createEndChannel(new File("src/test/resources/CSV-213/999751170.patch.csv")); |
| 57 | + } |
| 58 | +} |
0 commit comments