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.issues;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  import java.io.BufferedReader;
24  import java.io.IOException;
25  import java.io.InputStreamReader;
26  import java.io.Reader;
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.apache.commons.csv.QuoteMode;
32  import org.junit.jupiter.api.Test;
33  
34  class JiraCsv167Test {
35  
36      private Reader getTestReader() {
37          return new InputStreamReader(
38              ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv"));
39      }
40  
41      @Test
42      void testParse() throws IOException {
43          int totcomment = 0;
44          int totrecs = 0;
45          try (Reader reader = getTestReader(); BufferedReader br = new BufferedReader(reader)) {
46              String s = null;
47              boolean lastWasComment = false;
48              while ((s = br.readLine()) != null) {
49                  if (s.startsWith("#")) {
50                      if (!lastWasComment) { // comments are merged
51                          totcomment++;
52                      }
53                      lastWasComment = true;
54                  } else {
55                      totrecs++;
56                      lastWasComment = false;
57                  }
58              }
59          }
60          final CSVFormat format = CSVFormat.DEFAULT.builder()
61          // @formatter:off
62              .setAllowMissingColumnNames(false)
63              .setCommentMarker('#')
64              .setDelimiter(',')
65              .setEscape('\\')
66              .setHeader("author", "title", "publishDate")
67              .setHeaderComments("headerComment")
68              .setNullString("NULL")
69              .setIgnoreEmptyLines(true)
70              .setIgnoreSurroundingSpaces(true)
71              .setQuote('"')
72              .setQuoteMode(QuoteMode.ALL)
73              .setRecordSeparator('\n')
74              .setSkipHeaderRecord(false)
75              .get();
76          // @formatter:on
77          int comments = 0;
78          int records = 0;
79          try (Reader reader = getTestReader(); CSVParser parser = format.parse(reader)) {
80              for (final CSVRecord csvRecord : parser) {
81                  records++;
82                  if (csvRecord.hasComment()) {
83                      comments++;
84                  }
85              }
86          }
87          // Comment lines are concatenated, in this example 4 lines become 2 comments.
88          assertEquals(totcomment, comments);
89          assertEquals(totrecs, records); // records includes the header
90      }
91  }