1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.csv;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.io.Reader;
26 import java.nio.charset.StandardCharsets;
27
28 import org.junit.jupiter.api.Test;
29
30 public class JiraCsv196Test {
31
32 private Reader getTestInput(final String path) {
33 return new InputStreamReader(ClassLoader.getSystemClassLoader().getResourceAsStream(path));
34 }
35
36 @Test
37 public void testParseFourBytes() throws IOException {
38 final CSVFormat format = CSVFormat.Builder.create().setDelimiter(',').setQuote('\'').get();
39 try (CSVParser parser = new CSVParser.Builder().setFormat(format).setReader(getTestInput("org/apache/commons/csv/CSV-196/emoji.csv"))
40 .setCharset(StandardCharsets.UTF_8).setTrackBytes(true).get()) {
41 final long[] charByteKey = { 0, 84, 701, 1318, 1935 };
42 int idx = 0;
43 for (CSVRecord record : parser) {
44 assertEquals(charByteKey[idx++], record.getBytePosition(), "index " + idx);
45 }
46 }
47 }
48
49 @Test
50 public void testParseThreeBytes() throws IOException {
51 final CSVFormat format = CSVFormat.Builder.create().setDelimiter(',').setQuote('\'').get();
52 try (CSVParser parser = new CSVParser.Builder().setFormat(format).setReader(getTestInput("org/apache/commons/csv/CSV-196/japanese.csv"))
53 .setCharset(StandardCharsets.UTF_8).setTrackBytes(true).get()) {
54 final long[] charByteKey = { 0, 89, 242, 395 };
55 int idx = 0;
56 for (CSVRecord record : parser) {
57 assertEquals(charByteKey[idx++], record.getBytePosition(), "index " + idx);
58 }
59 }
60 }
61 }