Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/org/apache/commons/csv/CSVFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,8 @@ public Builder setQuote(final Character quoteCharacter) {
throw new IllegalArgumentException("The quoteCharacter cannot be a line break");
}
this.quoteCharacter = quoteCharacter;
final Character quote = quoteCharacter != null ? quoteCharacter : Constants.DOUBLE_QUOTE_CHAR;
this.quotedNullString = quote + nullString + quote;
return this;
}

Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/apache/commons/csv/CSVFormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,30 @@ void testQuoteCharSameAsDelimiterThrowsException_Deprecated() {
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withDelimiter('!'));
}

@Test
void testQuotedNullStringTracksQuoteCharacter() throws IOException {
final StringBuilder out = new StringBuilder();
// @formatter:off
final Builder builder = CSVFormat.DEFAULT.builder();
final CSVFormat format = builder
.setQuoteMode(QuoteMode.ALL)
.setNullString("NULL")
.get();
// @formatter:on
format.print(null, out, true);
assertEquals("\"NULL\"", out.toString());
// set
out.setLength(0);
builder.setQuote('\'');
builder.get().print(null, out, true);
assertEquals("'NULL'", out.toString());
// reset
out.setLength(0);
builder.setQuote((Character) null);
builder.get().print(null, out, true);
assertEquals("\"NULL\"", out.toString());
}

@Test
void testQuoteModeNoneShouldReturnMeaningfulExceptionMessage() {
final Exception exception = assertThrows(IllegalArgumentException.class, () ->
Expand Down
Loading