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
This commit is contained in:
Gary D. Gregory 2015-12-27 00:02:30 +00:00
parent ce9768fb0c
commit 54597a8b8e
3 changed files with 82 additions and 0 deletions

View File

@ -161,6 +161,16 @@ public final class CSVRecord implements Serializable, Iterable<String> {
return mapping == null || mapping.size() == values.length;
}
/**
* Checks whether this record is a comment, false otherwise.
*
* @return true if this record is a comment, false otherwise
* @since 1.3
*/
public boolean isComment() {
return comment != null;
}
/**
* Checks whether a given column is mapped, i.e. its name has been defined to the parser.
*

View File

@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.csv;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
public class JiraCsv167Test {
@Test
@Ignore("Fails")
public void parse() throws IOException {
File csvData = new File("src/test/resources/csv-167/sample1.csv");
CSVFormat format = CSVFormat.DEFAULT;
//
format = format.withAllowMissingColumnNames(false);
format = format.withCommentMarker('#');
format = format.withDelimiter(',');
format = format.withEscape('\\');
format = format.withHeader("author", "title", "publishDate");
format = format.withHeaderComments("headerComment");
format = format.withNullString("NULL");
format = format.withIgnoreEmptyLines(true);
format = format.withIgnoreSurroundingSpaces(true);
format = format.withQuote('"');
format = format.withQuoteMode(QuoteMode.ALL);
format = format.withRecordSeparator('\n');
format = format.withSkipHeaderRecord(false);
//
CSVParser parser = CSVParser.parse(csvData, Charset.defaultCharset(), format);
int comments = 0;
int records = 0;
for (CSVRecord csvRecord : parser) {
if (csvRecord.isComment()) {
comments++;
} else {
records++;
// System.out.println("[" + csvRecord.toString() + "]");
}
}
// Comment lines are concatenated, in this example 4 lines become 2 comments.
Assert.assertEquals(2, comments);
Assert.assertEquals(3, records);
}
}

View File

@ -0,0 +1,8 @@
# Comment before header
author,title,publishDate
Dan Simmons,Hyperion,"1989"
# Comment Line 1
# Comment Line 2
# Comment Line 3
Douglas Adams,The Hitchhiker's \"Guide\" to the Galaxy,1979
Douglas John,The Hitchhiker's \"Guide\" to the Mars,1979
Can't render this file because it has a wrong number of fields in line 2.