Sort members

This commit is contained in:
Gary Gregory 2022-09-12 09:53:28 -07:00
parent 0dff1d5582
commit 9797255331
5 changed files with 2034 additions and 2034 deletions

View File

@ -271,17 +271,6 @@ public final class CSVFormat implements Serializable {
return this;
}
/**
* Sets the duplicate header names behavior.
*
* @param duplicateHeaderMode the duplicate header names behavior
* @return This instance.
*/
public Builder setDuplicateHeaderMode(final DuplicateHeaderMode duplicateHeaderMode) {
this.duplicateHeaderMode = duplicateHeaderMode;
return this;
}
/**
* Sets the missing column names behavior, {@code true} to allow missing column names in the header line, {@code false} to cause an
* {@link IllegalArgumentException} to be thrown.
@ -361,6 +350,17 @@ public final class CSVFormat implements Serializable {
return this;
}
/**
* Sets the duplicate header names behavior.
*
* @param duplicateHeaderMode the duplicate header names behavior
* @return This instance.
*/
public Builder setDuplicateHeaderMode(final DuplicateHeaderMode duplicateHeaderMode) {
this.duplicateHeaderMode = duplicateHeaderMode;
return this;
}
/**
* Sets the escape character.
*
@ -1472,16 +1472,6 @@ public final class CSVFormat implements Serializable {
return duplicateHeaderMode == DuplicateHeaderMode.ALLOW_ALL;
}
/**
* Gets how duplicate headers are handled.
*
* @return if duplicate header values are allowed, allowed conditionally, or disallowed.
* @since 1.9.0
*/
public DuplicateHeaderMode getDuplicateHeaderMode() {
return duplicateHeaderMode;
}
/**
* Specifies whether missing column names are allowed when parsing the header line.
*
@ -1530,6 +1520,16 @@ public final class CSVFormat implements Serializable {
return delimiter;
}
/**
* Gets how duplicate headers are handled.
*
* @return if duplicate header values are allowed, allowed conditionally, or disallowed.
* @since 1.9.0
*/
public DuplicateHeaderMode getDuplicateHeaderMode() {
return duplicateHeaderMode;
}
/**
* Returns the escape character.
*

View File

@ -559,6 +559,17 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
return lexer.getFirstEol();
}
/**
* Returns the header comment, if any.
* The header comment appears before the header record.
*
* @return the header comment for this stream, or null if no comment is available.
* @since 1.10.0
*/
public String getHeaderComment() {
return headerComment;
}
/**
* Returns a copy of the header map.
* <p>
@ -605,57 +616,6 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
return Collections.unmodifiableList(headers.headerNames);
}
/**
* Checks whether there is a header comment.
* The header comment appears before the header record.
* Note that if the parser's format has been given an explicit header
* (with {@link CSVFormat.Builder#setHeader(String... )} or another overload)
* and the header record is not being skipped
* ({@link CSVFormat.Builder#setSkipHeaderRecord} is false) then any initial comments
* will be associated with the first record, not the header.
*
* @return true if this parser has seen a header comment, false otherwise
* @since 1.10.0
*/
public boolean hasHeaderComment() {
return headerComment != null;
}
/**
* Returns the header comment, if any.
* The header comment appears before the header record.
*
* @return the header comment for this stream, or null if no comment is available.
* @since 1.10.0
*/
public String getHeaderComment() {
return headerComment;
}
/**
* Checks whether there is a trailer comment.
* Trailer comments are located between the last record and EOF.
* The trailer comments will only be available after the parser has
* finished processing this stream.
*
* @return true if this parser has seen a trailer comment, false otherwise
* @since 1.10.0
*/
public boolean hasTrailerComment() {
return trailerComment != null;
}
/**
* Returns the trailer comment, if any.
* Trailer comments are located between the last record and EOF
*
* @return the trailer comment for this stream, or null if no comment is available.
* @since 1.10.0
*/
public String getTrailerComment() {
return trailerComment;
}
/**
* Returns the current record number in the input stream.
*
@ -691,6 +651,17 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
return records;
}
/**
* Returns the trailer comment, if any.
* Trailer comments are located between the last record and EOF
*
* @return the trailer comment for this stream, or null if no comment is available.
* @since 1.10.0
*/
public String getTrailerComment() {
return trailerComment;
}
/**
* Handle whether input is parsed as null
*
@ -710,6 +681,35 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
return strictQuoteMode && nullString == null && input.isEmpty() && !isQuoted ? null : input;
}
/**
* Checks whether there is a header comment.
* The header comment appears before the header record.
* Note that if the parser's format has been given an explicit header
* (with {@link CSVFormat.Builder#setHeader(String... )} or another overload)
* and the header record is not being skipped
* ({@link CSVFormat.Builder#setSkipHeaderRecord} is false) then any initial comments
* will be associated with the first record, not the header.
*
* @return true if this parser has seen a header comment, false otherwise
* @since 1.10.0
*/
public boolean hasHeaderComment() {
return headerComment != null;
}
/**
* Checks whether there is a trailer comment.
* Trailer comments are located between the last record and EOF.
* The trailer comments will only be available after the parser has
* finished processing this stream.
*
* @return true if this parser has seen a trailer comment, false otherwise
* @since 1.10.0
*/
public boolean hasTrailerComment() {
return trailerComment != null;
}
/**
* Tests whether this parser is closed.
*

File diff suppressed because it is too large Load Diff

View File

@ -78,6 +78,34 @@ public class CSVParserTest {
private static final String[][] RESULT = {{"a", "b", "c", "d"}, {"a", "b", "1 2"}, {"foo baar", "b", ""}, {"foo\n,,\n\",,\n\"", "d", "e"}};
// CSV with no header comments
static private final String CSV_INPUT_NO_COMMENT = "A,B"+CRLF+"1,2"+CRLF;
// CSV with a header comment
static private final String CSV_INPUT_HEADER_COMMENT = "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF;
// CSV with a single line header and trailer comment
static private final String CSV_INPUT_HEADER_TRAILER_COMMENT = "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF + "# comment";
// CSV with a multi-line header and trailer comment
static private final String CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT = "# multi-line" + CRLF + "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF + "# multi-line" + CRLF + "# comment";
// Format with auto-detected header
static private final CSVFormat FORMAT_AUTO_HEADER = CSVFormat.Builder.create(CSVFormat.DEFAULT).setCommentMarker('#').setHeader().build();
// Format with explicit header
static private final CSVFormat FORMAT_EXPLICIT_HEADER = CSVFormat.Builder.create(CSVFormat.DEFAULT)
.setSkipHeaderRecord(true)
.setCommentMarker('#')
.setHeader("A", "B")
.build();
// Format with explicit header that does not skip the header line
CSVFormat FORMAT_EXPLICIT_HEADER_NOSKIP = CSVFormat.Builder.create(CSVFormat.DEFAULT)
.setCommentMarker('#')
.setHeader("A", "B")
.build();
private BOMInputStream createBOMInputStream(final String resource) throws IOException {
final URL url = ClassLoader.getSystemClassLoader().getResource(resource);
return new BOMInputStream(url.openStream());
@ -497,6 +525,76 @@ public class CSVParserTest {
}
}
@Test
public void testGetHeaderComment_HeaderComment1() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_AUTO_HEADER)) {
parser.getRecords();
// Expect a header comment
assertTrue(parser.hasHeaderComment());
assertEquals("header comment", parser.getHeaderComment());
}
}
@Test
public void testGetHeaderComment_HeaderComment2() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER)) {
parser.getRecords();
// Expect a header comment
assertTrue(parser.hasHeaderComment());
assertEquals("header comment", parser.getHeaderComment());
}
}
@Test
public void testGetHeaderComment_HeaderComment3() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) {
parser.getRecords();
// Expect no header comment - the text "comment" is attached to the first record
assertFalse(parser.hasHeaderComment());
assertNull(parser.getHeaderComment());
}
}
@Test
public void testGetHeaderComment_HeaderTrailerComment() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT, FORMAT_AUTO_HEADER)) {
parser.getRecords();
// Expect a header comment
assertTrue(parser.hasHeaderComment());
assertEquals("multi-line"+LF+"header comment", parser.getHeaderComment());
}
}
@Test
public void testGetHeaderComment_NoComment1() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_NO_COMMENT, FORMAT_AUTO_HEADER)) {
parser.getRecords();
// Expect no header comment
assertFalse(parser.hasHeaderComment());
assertNull(parser.getHeaderComment());
}
}
@Test
public void testGetHeaderComment_NoComment2() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_NO_COMMENT, FORMAT_EXPLICIT_HEADER)) {
parser.getRecords();
// Expect no header comment
assertFalse(parser.hasHeaderComment());
assertNull(parser.getHeaderComment());
}
}
@Test
public void testGetHeaderComment_NoComment3() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_NO_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) {
parser.getRecords();
// Expect no header comment
assertFalse(parser.hasHeaderComment());
assertNull(parser.getHeaderComment());
}
}
@Test
public void testGetHeaderMap() throws Exception {
try (final CSVParser parser = CSVParser.parse("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader("A", "B", "C"))) {
@ -668,6 +766,69 @@ public class CSVParserTest {
}
}
@Test
public void testGetTrailerComment_HeaderComment1() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_AUTO_HEADER)) {
parser.getRecords();
assertFalse(parser.hasTrailerComment());
assertNull(parser.getTrailerComment());
}
}
@Test
public void testGetTrailerComment_HeaderComment2() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER)) {
parser.getRecords();
assertFalse(parser.hasTrailerComment());
assertNull(parser.getTrailerComment());
}
}
@Test
public void testGetTrailerComment_HeaderComment3() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) {
parser.getRecords();
assertFalse(parser.hasTrailerComment());
assertNull(parser.getTrailerComment());
}
}
@Test
public void testGetTrailerComment_HeaderTrailerComment1() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_AUTO_HEADER)) {
parser.getRecords();
assertTrue(parser.hasTrailerComment());
assertEquals("comment", parser.getTrailerComment());
}
}
@Test
public void testGetTrailerComment_HeaderTrailerComment2() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_EXPLICIT_HEADER)) {
parser.getRecords();
assertTrue(parser.hasTrailerComment());
assertEquals("comment", parser.getTrailerComment());
}
}
@Test
public void testGetTrailerComment_HeaderTrailerComment3() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) {
parser.getRecords();
assertTrue(parser.hasTrailerComment());
assertEquals("comment", parser.getTrailerComment());
}
}
@Test
public void testGetTrailerComment_MultilineComment() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT, FORMAT_AUTO_HEADER)) {
parser.getRecords();
assertTrue(parser.hasTrailerComment());
assertEquals("multi-line"+LF+"comment", parser.getTrailerComment());
}
}
@Test
public void testHeader() throws Exception {
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
@ -1048,7 +1209,6 @@ public class CSVParserTest {
assertEquals("xyz", csvRecord.get(1));
}
}
@Test
public void testParseWithDelimiterStringWithQuote() throws IOException {
final String source = "'a[|]b[|]c'[|]xyz\r\nabc[abc][|]xyz";
@ -1062,7 +1222,6 @@ public class CSVParserTest {
assertEquals("xyz", csvRecord.get(1));
}
}
@Test
public void testParseWithDelimiterWithEscape() throws IOException {
final String source = "a!,b!,c,xyz";
@ -1073,7 +1232,6 @@ public class CSVParserTest {
assertEquals("xyz", csvRecord.get(1));
}
}
@Test
public void testParseWithDelimiterWithQuote() throws IOException {
final String source = "'a,b,c',xyz";
@ -1084,7 +1242,6 @@ public class CSVParserTest {
assertEquals("xyz", csvRecord.get(1));
}
}
@Test
public void testParseWithQuoteThrowsException() {
final CSVFormat csvFormat = CSVFormat.DEFAULT.withQuote('\'');
@ -1092,7 +1249,6 @@ public class CSVParserTest {
assertThrows(IOException.class, () -> csvFormat.parse(new StringReader("'a,b,c'abc,xyz")).nextRecord());
assertThrows(IOException.class, () -> csvFormat.parse(new StringReader("'abc'a,b,c',xyz")).nextRecord());
}
@Test
public void testParseWithQuoteWithEscape() throws IOException {
final String source = "'a?,b?,c?d',xyz";
@ -1103,7 +1259,6 @@ public class CSVParserTest {
assertEquals("xyz", csvRecord.get(1));
}
}
@Test
public void testProvidedHeader() throws Exception {
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
@ -1350,159 +1505,4 @@ public class CSVParserTest {
parser.close();
}
// CSV with no header comments
static private final String CSV_INPUT_NO_COMMENT = "A,B"+CRLF+"1,2"+CRLF;
// CSV with a header comment
static private final String CSV_INPUT_HEADER_COMMENT = "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF;
// CSV with a single line header and trailer comment
static private final String CSV_INPUT_HEADER_TRAILER_COMMENT = "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF + "# comment";
// CSV with a multi-line header and trailer comment
static private final String CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT = "# multi-line" + CRLF + "# header comment" + CRLF + "A,B" + CRLF + "1,2" + CRLF + "# multi-line" + CRLF + "# comment";
// Format with auto-detected header
static private final CSVFormat FORMAT_AUTO_HEADER = CSVFormat.Builder.create(CSVFormat.DEFAULT).setCommentMarker('#').setHeader().build();
// Format with explicit header
static private final CSVFormat FORMAT_EXPLICIT_HEADER = CSVFormat.Builder.create(CSVFormat.DEFAULT)
.setSkipHeaderRecord(true)
.setCommentMarker('#')
.setHeader("A", "B")
.build();
// Format with explicit header that does not skip the header line
CSVFormat FORMAT_EXPLICIT_HEADER_NOSKIP = CSVFormat.Builder.create(CSVFormat.DEFAULT)
.setCommentMarker('#')
.setHeader("A", "B")
.build();
@Test
public void testGetHeaderComment_NoComment1() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_NO_COMMENT, FORMAT_AUTO_HEADER)) {
parser.getRecords();
// Expect no header comment
assertFalse(parser.hasHeaderComment());
assertNull(parser.getHeaderComment());
}
}
@Test
public void testGetHeaderComment_HeaderComment1() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_AUTO_HEADER)) {
parser.getRecords();
// Expect a header comment
assertTrue(parser.hasHeaderComment());
assertEquals("header comment", parser.getHeaderComment());
}
}
@Test
public void testGetHeaderComment_HeaderTrailerComment() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT, FORMAT_AUTO_HEADER)) {
parser.getRecords();
// Expect a header comment
assertTrue(parser.hasHeaderComment());
assertEquals("multi-line"+LF+"header comment", parser.getHeaderComment());
}
}
@Test
public void testGetHeaderComment_NoComment2() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_NO_COMMENT, FORMAT_EXPLICIT_HEADER)) {
parser.getRecords();
// Expect no header comment
assertFalse(parser.hasHeaderComment());
assertNull(parser.getHeaderComment());
}
}
@Test
public void testGetHeaderComment_HeaderComment2() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER)) {
parser.getRecords();
// Expect a header comment
assertTrue(parser.hasHeaderComment());
assertEquals("header comment", parser.getHeaderComment());
}
}
@Test
public void testGetHeaderComment_NoComment3() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_NO_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) {
parser.getRecords();
// Expect no header comment
assertFalse(parser.hasHeaderComment());
assertNull(parser.getHeaderComment());
}
}
@Test
public void testGetHeaderComment_HeaderComment3() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) {
parser.getRecords();
// Expect no header comment - the text "comment" is attached to the first record
assertFalse(parser.hasHeaderComment());
assertNull(parser.getHeaderComment());
}
}
@Test
public void testGetTrailerComment_HeaderComment1() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_AUTO_HEADER)) {
parser.getRecords();
assertFalse(parser.hasTrailerComment());
assertNull(parser.getTrailerComment());
}
}
@Test
public void testGetTrailerComment_HeaderTrailerComment1() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_AUTO_HEADER)) {
parser.getRecords();
assertTrue(parser.hasTrailerComment());
assertEquals("comment", parser.getTrailerComment());
}
}
@Test
public void testGetTrailerComment_MultilineComment() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT, FORMAT_AUTO_HEADER)) {
parser.getRecords();
assertTrue(parser.hasTrailerComment());
assertEquals("multi-line"+LF+"comment", parser.getTrailerComment());
}
}
@Test
public void testGetTrailerComment_HeaderComment2() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER)) {
parser.getRecords();
assertFalse(parser.hasTrailerComment());
assertNull(parser.getTrailerComment());
}
}
@Test
public void testGetTrailerComment_HeaderTrailerComment2() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_EXPLICIT_HEADER)) {
parser.getRecords();
assertTrue(parser.hasTrailerComment());
assertEquals("comment", parser.getTrailerComment());
}
}
@Test
public void testGetTrailerComment_HeaderComment3() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) {
parser.getRecords();
assertFalse(parser.hasTrailerComment());
assertNull(parser.getTrailerComment());
}
}
@Test
public void testGetTrailerComment_HeaderTrailerComment3() throws IOException {
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) {
parser.getRecords();
assertTrue(parser.hasTrailerComment());
assertEquals("comment", parser.getTrailerComment());
}
}
}

View File

@ -1,230 +1,230 @@
/*
* 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.issues;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.Reader;
import java.io.StringReader;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.junit.jupiter.api.Test;
public class JiraCsv288Test {
@Test
// Before fix:
// expected: <a,b,c,d,,f> but was: <a,b|c,d,|f>
public void testParseWithDoublePipeDelimiter() throws Exception {
final Reader in = new StringReader("a||b||c||d||||f");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f", stringBuilder.toString());
}
}
}
@Test
// Before fix:
// expected: <a,b,c,d,,f> but was: <a,b|c,d,|f>
public void testParseWithTriplePipeDelimiter() throws Exception {
final Reader in = new StringReader("a|||b|||c|||d||||||f");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("|||").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f", stringBuilder.toString());
}
}
}
@Test
// Before fix:
// expected: <a,b,c,d,,f> but was: <a,b,c,d,|f>
public void testParseWithABADelimiter() throws Exception {
final Reader in = new StringReader("a|~|b|~|c|~|d|~||~|f");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("|~|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f", stringBuilder.toString());
}
}
}
@Test
// Before fix:
// expected: <a,b||c,d,,f> but was: <a,b||c,d,|f>
public void testParseWithDoublePipeDelimiterQuoted() throws Exception {
final Reader in = new StringReader("a||\"b||c\"||d||||f");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b||c,d,,f", stringBuilder.toString());
}
}
}
@Test
// Before fix:
// expected: <a,b,c,d,,f,> but was: <a,b|c,d,|f>
public void testParseWithDoublePipeDelimiterEndsWithDelimiter() throws Exception {
final Reader in = new StringReader("a||b||c||d||||f||");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f,", stringBuilder.toString());
}
}
}
@Test
// Before fix:
// expected: <a,b,c,d,,f,> but was: <a,b,c,d,,f>
public void testParseWithTwoCharDelimiterEndsWithDelimiter() throws Exception {
final Reader in = new StringReader("a~|b~|c~|d~|~|f~|");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f,", stringBuilder.toString());
}
}
}
@Test
// Regression, already passed before fix
public void testParseWithDoublePipeDelimiterDoubleCharValue() throws Exception {
final Reader in = new StringReader("a||bb||cc||dd||f");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,bb,cc,dd,f", stringBuilder.toString());
}
}
}
@Test
// Regression, already passed before fix
public void testParseWithTwoCharDelimiter1() throws Exception {
final Reader in = new StringReader("a~|b~|c~|d~|~|f");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f", stringBuilder.toString());
}
}
}
@Test
// Regression, already passed before fix
public void testParseWithTwoCharDelimiter2() throws Exception {
final Reader in = new StringReader("a~|b~|c~|d~|~|f~");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f~", stringBuilder.toString());
}
}
}
@Test
// Regression, already passed before fix
public void testParseWithTwoCharDelimiter3() throws Exception {
final Reader in = new StringReader("a~|b~|c~|d~|~|f|");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f|", stringBuilder.toString());
}
}
}
@Test
// Regression, already passed before fix
public void testParseWithTwoCharDelimiter4() throws Exception {
final Reader in = new StringReader("a~|b~|c~|d~|~|f~~||g");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f~,|g", stringBuilder.toString());
}
}
}
@Test
// Regression, already passed before fix
public void testParseWithSinglePipeDelimiterEndsWithDelimiter() throws Exception {
final Reader in = new StringReader("a|b|c|d||f|");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f,", stringBuilder.toString());
}
}
}
/*
* 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.issues;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.Reader;
import java.io.StringReader;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.junit.jupiter.api.Test;
public class JiraCsv288Test {
@Test
// Before fix:
// expected: <a,b,c,d,,f> but was: <a,b,c,d,|f>
public void testParseWithABADelimiter() throws Exception {
final Reader in = new StringReader("a|~|b|~|c|~|d|~||~|f");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("|~|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f", stringBuilder.toString());
}
}
}
@Test
// Before fix:
// expected: <a,b,c,d,,f> but was: <a,b|c,d,|f>
public void testParseWithDoublePipeDelimiter() throws Exception {
final Reader in = new StringReader("a||b||c||d||||f");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f", stringBuilder.toString());
}
}
}
@Test
// Regression, already passed before fix
public void testParseWithDoublePipeDelimiterDoubleCharValue() throws Exception {
final Reader in = new StringReader("a||bb||cc||dd||f");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,bb,cc,dd,f", stringBuilder.toString());
}
}
}
@Test
// Before fix:
// expected: <a,b,c,d,,f,> but was: <a,b|c,d,|f>
public void testParseWithDoublePipeDelimiterEndsWithDelimiter() throws Exception {
final Reader in = new StringReader("a||b||c||d||||f||");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f,", stringBuilder.toString());
}
}
}
@Test
// Before fix:
// expected: <a,b||c,d,,f> but was: <a,b||c,d,|f>
public void testParseWithDoublePipeDelimiterQuoted() throws Exception {
final Reader in = new StringReader("a||\"b||c\"||d||||f");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("||").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b||c,d,,f", stringBuilder.toString());
}
}
}
@Test
// Regression, already passed before fix
public void testParseWithSinglePipeDelimiterEndsWithDelimiter() throws Exception {
final Reader in = new StringReader("a|b|c|d||f|");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f,", stringBuilder.toString());
}
}
}
@Test
// Before fix:
// expected: <a,b,c,d,,f> but was: <a,b|c,d,|f>
public void testParseWithTriplePipeDelimiter() throws Exception {
final Reader in = new StringReader("a|||b|||c|||d||||||f");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("|||").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f", stringBuilder.toString());
}
}
}
@Test
// Regression, already passed before fix
public void testParseWithTwoCharDelimiter1() throws Exception {
final Reader in = new StringReader("a~|b~|c~|d~|~|f");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f", stringBuilder.toString());
}
}
}
@Test
// Regression, already passed before fix
public void testParseWithTwoCharDelimiter2() throws Exception {
final Reader in = new StringReader("a~|b~|c~|d~|~|f~");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f~", stringBuilder.toString());
}
}
}
@Test
// Regression, already passed before fix
public void testParseWithTwoCharDelimiter3() throws Exception {
final Reader in = new StringReader("a~|b~|c~|d~|~|f|");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f|", stringBuilder.toString());
}
}
}
@Test
// Regression, already passed before fix
public void testParseWithTwoCharDelimiter4() throws Exception {
final Reader in = new StringReader("a~|b~|c~|d~|~|f~~||g");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f~,|g", stringBuilder.toString());
}
}
}
@Test
// Before fix:
// expected: <a,b,c,d,,f,> but was: <a,b,c,d,,f>
public void testParseWithTwoCharDelimiterEndsWithDelimiter() throws Exception {
final Reader in = new StringReader("a~|b~|c~|d~|~|f~|");
final StringBuilder stringBuilder = new StringBuilder();
try (CSVPrinter csvPrinter = new CSVPrinter(stringBuilder, CSVFormat.EXCEL);
CSVParser csvParser = CSVParser.parse(in, CSVFormat.Builder.create().setDelimiter("~|").build())) {
for (final CSVRecord csvRecord : csvParser) {
for (int i = 0; i < csvRecord.size(); i++) {
csvPrinter.print(csvRecord.get(i));
}
assertEquals("a,b,c,d,,f,", stringBuilder.toString());
}
}
}
}