View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.csv.issues;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.Reader;
22  import java.nio.charset.StandardCharsets;
23  import java.nio.file.Files;
24  
25  import org.apache.commons.csv.CSVFormat;
26  import org.apache.commons.csv.CSVParser;
27  import org.apache.commons.csv.QuoteMode;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests https://issues.apache.org/jira/browse/CSV-213
32   * <p>
33   * This is normal behavior with the current architecture: The iterator() API presents an object that is backed by data
34   * in the CSVParser as the parser is streaming over the file. The CSVParser is like a forward-only stream. When you
35   * create a new Iterator you are only created a new view on the same position in the parser's stream. For the behavior
36   * you want, you need to open a new CSVParser.
37   * </p>
38   */
39  public class JiraCsv213Test {
40  
41      private void createEndChannel(final File csvFile) {
42          // @formatter:off
43          final CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
44                      .setDelimiter(';')
45                      .setHeader()
46                      .setSkipHeaderRecord(true)
47                      .setRecordSeparator('\n')
48                      .setQuoteMode(QuoteMode.ALL)
49                      .build();
50          // @formatter:on
51          try (Reader reader = Files.newBufferedReader(csvFile.toPath(), StandardCharsets.UTF_8);
52              CSVParser parser = csvFormat.parse(reader)) {
53              if (parser.iterator().hasNext()) {
54                  // System.out.println(parser.getCurrentLineNumber());
55                  // System.out.println(parser.getRecordNumber());
56                  // get only first record we don't need other's
57                  parser.iterator().next(); // this fails
58              }
59          } catch (final IOException e) {
60              throw new IllegalStateException("Error while adding end channel to CSV", e);
61          }
62      }
63  
64      @Test
65      public void test() {
66          createEndChannel(new File("src/test/resources/org/apache/commons/csv/CSV-213/999751170.patch.csv"));
67      }
68  }