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  
20  package org.apache.commons.csv.issues;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  import java.io.IOException;
25  import java.io.StringReader;
26  import java.util.Iterator;
27  
28  import org.apache.commons.csv.CSVFormat;
29  import org.apache.commons.csv.CSVParser;
30  import org.apache.commons.csv.CSVRecord;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests [CSV-265] {@link CSVRecord#getCharacterPosition()} returns the correct position after encountering a comment.
35   */
36  class JiraCsv265Test {
37  
38      @Test
39      void testCharacterPositionWithComments() throws IOException {
40          // @formatter:off
41          final String csv =
42                  "# Comment1\n" +
43                  "Header1,Header2\n" +
44                  "# Comment2\n" +
45                  "Value1,Value2\n" +
46                  "# Comment3\n" +
47                  "Value3,Value4\n" +
48                  "# Comment4\n";
49          final CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
50              .setCommentMarker('#')
51              .setHeader()
52              .setSkipHeaderRecord(true)
53              .get();
54          // @formatter:on
55          try (CSVParser parser = csvFormat.parse(new StringReader(csv))) {
56              final Iterator<CSVRecord> itr = parser.iterator();
57              final CSVRecord record1 = itr.next();
58              assertEquals(csv.indexOf("# Comment2"), record1.getCharacterPosition());
59              final CSVRecord record2 = itr.next();
60              assertEquals(csv.indexOf("# Comment3"), record2.getCharacterPosition());
61          }
62      }
63  
64      @Test
65      void testCharacterPositionWithCommentsSpanningMultipleLines() throws IOException {
66          // @formatter:off
67          final String csv =
68                  "# Comment1\n" +
69                  "# Comment2\n" +
70                  "Header1,Header2\n" +
71                  "# Comment3\n" +
72                  "# Comment4\n" +
73                  "Value1,Value2\n" +
74                  "# Comment5\n" +
75                  "# Comment6\n" +
76                  "Value3,Value4";
77          final CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
78              .setCommentMarker('#')
79              .setHeader()
80              .setSkipHeaderRecord(true)
81              .get();
82          // @formatter:on
83          try (CSVParser parser = csvFormat.parse(new StringReader(csv))) {
84              final Iterator<CSVRecord> itr = parser.iterator();
85              final CSVRecord record1 = itr.next();
86              assertEquals(csv.indexOf("# Comment3"), record1.getCharacterPosition());
87              final CSVRecord record2 = itr.next();
88              assertEquals(csv.indexOf("# Comment5"), record2.getCharacterPosition());
89          }
90      }
91  
92  }