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