Skip to content

Commit 4a28ba8

Browse files
committed
[CSV-322] CSVFormat.Builder.setQuote() does not refresh quotedNullString
1 parent 7cf896b commit 4a28ba8

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,8 @@ public Builder setQuote(final Character quoteCharacter) {
806806
throw new IllegalArgumentException("The quoteCharacter cannot be a line break");
807807
}
808808
this.quoteCharacter = quoteCharacter;
809+
final Character quote = quoteCharacter != null ? quoteCharacter : Constants.DOUBLE_QUOTE_CHAR;
810+
this.quotedNullString = quote + nullString + quote;
809811
return this;
810812
}
811813

src/test/java/org/apache/commons/csv/CSVFormatTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,30 @@ void testQuoteCharSameAsDelimiterThrowsException_Deprecated() {
10011001
assertThrows(IllegalArgumentException.class, () -> CSVFormat.DEFAULT.withQuote('!').withDelimiter('!'));
10021002
}
10031003

1004+
@Test
1005+
void testQuotedNullStringTracksQuoteCharacter() throws IOException {
1006+
final StringBuilder out = new StringBuilder();
1007+
// @formatter:off
1008+
final Builder builder = CSVFormat.DEFAULT.builder();
1009+
final CSVFormat format = builder
1010+
.setQuoteMode(QuoteMode.ALL)
1011+
.setNullString("NULL")
1012+
.get();
1013+
// @formatter:on
1014+
format.print(null, out, true);
1015+
assertEquals("\"NULL\"", out.toString());
1016+
// set
1017+
out.setLength(0);
1018+
builder.setQuote('\'');
1019+
builder.get().print(null, out, true);
1020+
assertEquals("'NULL'", out.toString());
1021+
// reset
1022+
out.setLength(0);
1023+
builder.setQuote((Character) null);
1024+
builder.get().print(null, out, true);
1025+
assertEquals("\"NULL\"", out.toString());
1026+
}
1027+
10041028
@Test
10051029
void testQuoteModeNoneShouldReturnMeaningfulExceptionMessage() {
10061030
final Exception exception = assertThrows(IllegalArgumentException.class, () ->

0 commit comments

Comments
 (0)