1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.csv.issues;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertNotNull;
23
24 import java.io.IOException;
25 import java.io.StringReader;
26
27 import org.apache.commons.csv.CSVFormat;
28 import org.apache.commons.csv.CSVParser;
29 import org.apache.commons.csv.CSVRecord;
30 import org.junit.jupiter.api.Test;
31
32 class JiraCsv149Test {
33
34 private static final String CR_LF = "\r\n";
35
36 @Test
37 void testJiraCsv149EndWithEOL() throws IOException {
38 testJiraCsv149EndWithEolAtEof(true);
39 }
40
41 private void testJiraCsv149EndWithEolAtEof(final boolean eolAtEof) throws IOException {
42 String source = "A,B,C,D" + CR_LF + "a1,b1,c1,d1" + CR_LF + "a2,b2,c2,d2";
43 if (eolAtEof) {
44 source += CR_LF;
45 }
46 final StringReader reader = new StringReader(source);
47
48 final CSVFormat format = CSVFormat.RFC4180.builder()
49 .setHeader()
50 .setSkipHeaderRecord(true)
51 .setQuote('"')
52 .get();
53
54 int lineCounter = 2;
55 try (CSVParser parser = CSVParser.builder().setReader(reader).setFormat(format).get()) {
56 for (final CSVRecord record : parser) {
57 assertNotNull(record);
58 assertEquals(lineCounter++, parser.getCurrentLineNumber());
59 }
60 }
61 }
62
63 @Test
64 void testJiraCsv149EndWithoutEOL() throws IOException {
65 testJiraCsv149EndWithEolAtEof(false);
66 }
67 }