[CSV-263] Print from Reader with embedded quotes generates incorrect

output.

- Resolve conflicts from PR #78 by Jason A. Guild.
- Don't use depreacted methods.
- Javadoc.
- Use final.
This commit is contained in:
Gary Gregory 2021-07-07 19:18:12 -04:00
parent 3dad2eef41
commit a4237345a1
4 changed files with 81 additions and 3 deletions

View File

@ -59,6 +59,7 @@
<action type="fix" dev="ggregory" due-to="Gary Gregory">Update CSVParser.parse(File, Charset, CSVFormat) from IO to NIO.</action>
<action issue="CSV-271" type="fix" dev="ggregory" due-to="Amar Prakash Pandey">Missing separator with print(object) followed by printRecord(Object[]) #157.</action>
<action issue="CSV-158" type="fix" dev="ggregory" due-to="Alexander Bondarev, Benedikt Ritter, Gary Gregory, Chen">Fix EOL checking for read array in ExtendedBufferedReader #5.</action>
<action issue="CSV-263" type="fix" dev="ggregory" due-to="Jason A. Guild, Gary Gregory">Print from Reader with embedded quotes generates incorrect output #78.</action>
<!-- ADD -->
<action issue="CSV-275" type="add" dev="ggregory" due-to="Michael Wyraz, Gary Gregory">Make CSVRecord#toList() public.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVRecord#stream().</action>

View File

@ -1747,7 +1747,7 @@ public final class CSVFormat implements Serializable {
/**
* Prints the {@code value} as the next value on the line to {@code out}. The value will be escaped or encapsulated as needed. Useful when one wants to
* avoid creating CSVPrinters. Trims the value if {@link #getTrim()} is true
* avoid creating CSVPrinters. Trims the value if {@link #getTrim()} is true.
*
* @param value value to output.
* @param out where to print the value.
@ -2120,11 +2120,11 @@ public final class CSVFormat implements Serializable {
// write out segment up until this char
if (pos > 0) {
append(builder.substring(0, pos), appendable);
append(quote, appendable);
builder.setLength(0);
pos = -1;
}
append(quote, appendable);
append((char) c, appendable);
}
pos++;

View File

@ -971,7 +971,7 @@ public class CSVFormatTest {
final Appendable out = new StringBuilder();
final CSVFormat format = CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC);
format.print(in, out, true);
assertEquals("\"\"\"\"a,b,c\r\nx,y,z\"", out.toString());
assertEquals("\"\"\"a,b,c\r\nx,y,z\"", out.toString());
}
@Test

View File

@ -0,0 +1,77 @@
/*
* 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.IOException;
import java.io.Reader;
import java.io.StringReader;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.QuoteMode;
import org.junit.jupiter.api.Test;
/**
* Tests [CSV-263] Print from Reader with embedded quotes generates incorrect output.
*/
public class JiraCsv263Test {
@Test
public void testPrintFromReaderWithQuotes() throws IOException {
// @formatter:off
final CSVFormat format = CSVFormat.RFC4180.builder()
.setDelimiter(',')
.setQuote('"')
.setEscape('?')
.setQuoteMode(QuoteMode.NON_NUMERIC)
.build();
// @formatter:on
final StringBuilder out = new StringBuilder();
final Reader atStartOnly = new StringReader("\"a,b,c\r\nx,y,z");
format.print(atStartOnly, out, true);
assertEquals("\"\"\"a,b,c\r\nx,y,z\"", out.toString());
final Reader atEndOnly = new StringReader("a,b,c\r\nx,y,z\"");
out.setLength(0);
format.print(atEndOnly, out, true);
assertEquals("\"a,b,c\r\nx,y,z\"\"\"", out.toString());
final Reader atBeginEnd = new StringReader("\"a,b,c\r\nx,y,z\"");
out.setLength(0);
format.print(atBeginEnd, out, true);
assertEquals("\"\"\"a,b,c\r\nx,y,z\"\"\"", out.toString());
final Reader embeddedBeginMiddle = new StringReader("\"a\",b,c\r\nx,\"y\",z");
out.setLength(0);
format.print(embeddedBeginMiddle, out, true);
assertEquals("\"\"\"a\"\",b,c\r\nx,\"\"y\"\",z\"", out.toString());
final Reader embeddedMiddleEnd = new StringReader("a,\"b\",c\r\nx,y,\"z\"");
out.setLength(0);
format.print(embeddedMiddleEnd, out, true);
assertEquals("\"a,\"\"b\"\",c\r\nx,y,\"\"z\"\"\"", out.toString());
final Reader nested = new StringReader("a,\"b \"and\" c\",d");
out.setLength(0);
format.print(nested, out, true);
assertEquals("\"a,\"\"b \"\"and\"\" c\"\",d\"", out.toString());
}
}