View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }