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