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.Disabled;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests https://issues.apache.org/jira/browse/CSV-213
33   *
34   * This is normal behavior with the current architecture: The iterator() API presents an object that is backed by data
35   * in the CSVParser as the parser is streaming over the file. The CSVParser is like a forward-only stream. When you
36   * create a new Iterator you are only created a new view on the same position in the parser's stream. For the behavior
37   * you want, you need to open a new CSVParser.
38   */
39  @Disabled
40  public class JiraCsv213Test {
41  
42      private void createEndChannel(final File csvFile) {
43          // @formatter:off
44          final CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
45                      .setDelimiter(';')
46                      .setHeader()
47                      .setSkipHeaderRecord(true)
48                      .setRecordSeparator('\n')
49                      .setQuoteMode(QuoteMode.ALL)
50                      .build();
51          // @formatter:on
52          try (Reader reader = Files.newBufferedReader(csvFile.toPath(), StandardCharsets.UTF_8);
53              CSVParser parser = csvFormat.parse(reader)) {
54              if (parser.iterator().hasNext()) {
55                  // System.out.println(parser.getCurrentLineNumber());
56                  // System.out.println(parser.getRecordNumber());
57                  // get only first record we don't need other's
58                  parser.iterator().next(); // this fails
59              }
60          } catch (final IOException e) {
61              throw new IllegalStateException("Error while adding end channel to CSV", e);
62          }
63      }
64  
65      @Test
66      public void test() {
67          createEndChannel(new File("src/test/resources/CSV-213/999751170.patch.csv"));
68      }
69  }