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