Skip to content

Commit 54597a8

Browse files
committed
Unit test for [CSV-167] Comment line hides next record.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1721778 13f79535-47bb-0310-9956-ffa450edef68
1 parent ce9768f commit 54597a8

3 files changed

Lines changed: 82 additions & 0 deletions

File tree

src/main/java/org/apache/commons/csv/CSVRecord.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ public boolean isConsistent() {
161161
return mapping == null || mapping.size() == values.length;
162162
}
163163

164+
/**
165+
* Checks whether this record is a comment, false otherwise.
166+
*
167+
* @return true if this record is a comment, false otherwise
168+
* @since 1.3
169+
*/
170+
public boolean isComment() {
171+
return comment != null;
172+
}
173+
164174
/**
165175
* Checks whether a given column is mapped, i.e. its name has been defined to the parser.
166176
*
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.nio.charset.Charset;
22+
23+
import org.junit.Assert;
24+
import org.junit.Ignore;
25+
import org.junit.Test;
26+
27+
public class JiraCsv167Test {
28+
29+
@Test
30+
@Ignore("Fails")
31+
public void parse() throws IOException {
32+
File csvData = new File("src/test/resources/csv-167/sample1.csv");
33+
CSVFormat format = CSVFormat.DEFAULT;
34+
//
35+
format = format.withAllowMissingColumnNames(false);
36+
format = format.withCommentMarker('#');
37+
format = format.withDelimiter(',');
38+
format = format.withEscape('\\');
39+
format = format.withHeader("author", "title", "publishDate");
40+
format = format.withHeaderComments("headerComment");
41+
format = format.withNullString("NULL");
42+
format = format.withIgnoreEmptyLines(true);
43+
format = format.withIgnoreSurroundingSpaces(true);
44+
format = format.withQuote('"');
45+
format = format.withQuoteMode(QuoteMode.ALL);
46+
format = format.withRecordSeparator('\n');
47+
format = format.withSkipHeaderRecord(false);
48+
//
49+
CSVParser parser = CSVParser.parse(csvData, Charset.defaultCharset(), format);
50+
int comments = 0;
51+
int records = 0;
52+
for (CSVRecord csvRecord : parser) {
53+
if (csvRecord.isComment()) {
54+
comments++;
55+
} else {
56+
records++;
57+
// System.out.println("[" + csvRecord.toString() + "]");
58+
}
59+
}
60+
// Comment lines are concatenated, in this example 4 lines become 2 comments.
61+
Assert.assertEquals(2, comments);
62+
Assert.assertEquals(3, records);
63+
}
64+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Comment before header
2+
author,title,publishDate
3+
Dan Simmons,Hyperion,"1989"
4+
# Comment Line 1
5+
# Comment Line 2
6+
# Comment Line 3
7+
Douglas Adams,The Hitchhiker's \"Guide\" to the Galaxy,1979
8+
Douglas John,The Hitchhiker's \"Guide\" to the Mars,1979

0 commit comments

Comments
 (0)