Ruiqi Dong created CSV-322:
------------------------------

             Summary: CSVFormat.Builder.setQuote() does not refresh 
quotedNullString
                 Key: CSV-322
                 URL: https://issues.apache.org/jira/browse/CSV-322
             Project: Commons CSV
          Issue Type: Bug
          Components: Build
    Affects Versions: 1.14.1
            Reporter: Ruiqi Dong


*Summary*
If setNullString() is called before setQuote(), the builder caches 
quotedNullString using the old quote character and never recomputes it. Later 
printing with QuoteMode.ALL uses the stale cached value.
 
*Affected code*
File: src/main/java/org/apache/commons/csv/CSVFormat.java
{code:java}
private String quotedNullString;

public Builder setNullString(final String nullString) {
    this.nullString = nullString;
    this.quotedNullString = quoteCharacter + nullString + quoteCharacter;
    return this;
}

public Builder setQuote(final Character quoteCharacter) {
    if (isLineBreak(quoteCharacter)) {
        throw new IllegalArgumentException("The quoteCharacter cannot be a line 
break");
    }
    this.quoteCharacter = quoteCharacter;
    return this;
}

if (null == nullString) {
    charSequence = Constants.EMPTY;
} else if (QuoteMode.ALL == quoteMode) {
    charSequence = quotedNullString;
} else {
    charSequence = nullString;
} {code}
*Reproducer*
Add the following test to 
src/test/java/org/apache/commons/csv/CSVFormatTest.java:
{code:java}
@Test
void testQuotedNullStringTracksQuoteCharacter() throws IOException {
    final StringBuilder out = new StringBuilder();
    final CSVFormat format = CSVFormat.DEFAULT.builder()
            .setQuoteMode(QuoteMode.ALL)
            .setNullString("NULL")
            .setQuote('\'')
            .get();
    format.print(null, out, true);
    assertEquals("'NULL'", out.toString());
} {code}
Run:
{code:java}
mvn -q -Dtest=org.apache.commons.csv.CSVFormatTest test {code}
Observed behavior:
{code:java}
CSVFormatTest.testQuotedNullStringTracksQuoteCharacter:978
expected: <'NULL'> but was: <"NULL">{code}
Expected behavior:
After changing the quote character to ', printing {{null}} with 
{{QuoteMode.ALL}} should yield 'NULL'.
 
The builder stores a cached quoted null representation but does not keep that 
cache consistent when the quote character changes.
 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to