diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 033a149c..7e488fc0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -38,6 +38,9 @@
Release Notes
+
+ CSVFormat.with* methods clear the header comments
+
QuoteMode.NON_NUMERIC doesn't work with CSVPrinter.printRecords(ResultSet)
CSVFormat#withHeader doesn't work well with #printComment, add withHeaderComments(String...)
diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java
index 319c8612..a7432b98 100644
--- a/src/main/java/org/apache/commons/csv/CSVFormat.java
+++ b/src/main/java/org/apache/commons/csv/CSVFormat.java
@@ -804,8 +804,8 @@ public final class CSVFormat implements Serializable {
throw new IllegalArgumentException("The comment start marker character cannot be a line break");
}
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -822,8 +822,8 @@ public final class CSVFormat implements Serializable {
throw new IllegalArgumentException("The delimiter cannot be a line break");
}
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -853,7 +853,8 @@ public final class CSVFormat implements Serializable {
throw new IllegalArgumentException("The escape character cannot be a line break");
}
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escape, ignoreSurroundingSpaces,
- ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord, allowMissingColumnNames);
+ ignoreEmptyLines, recordSeparator, nullString, headerComments, header, skipHeaderRecord,
+ allowMissingColumnNames);
}
/**
@@ -880,8 +881,8 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withHeader(final String... header) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -948,8 +949,8 @@ public final class CSVFormat implements Serializable {
}
}
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, labels, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, labels,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -994,8 +995,8 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withAllowMissingColumnNames(final boolean allowMissingColumnNames) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -1019,8 +1020,8 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -1044,8 +1045,8 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -1065,8 +1066,8 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withNullString(final String nullString) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -1096,7 +1097,8 @@ public final class CSVFormat implements Serializable {
throw new IllegalArgumentException("The quoteChar cannot be a line break");
}
return new CSVFormat(delimiter, quoteChar, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces,
- ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord, allowMissingColumnNames);
+ ignoreEmptyLines, recordSeparator, nullString, headerComments, header, skipHeaderRecord,
+ allowMissingColumnNames);
}
/**
@@ -1109,8 +1111,8 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withQuoteMode(final QuoteMode quoteModePolicy) {
return new CSVFormat(delimiter, quoteCharacter, quoteModePolicy, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -1147,8 +1149,8 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withRecordSeparator(final String recordSeparator) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
/**
@@ -1174,7 +1176,7 @@ public final class CSVFormat implements Serializable {
*/
public CSVFormat withSkipHeaderRecord(final boolean skipHeaderRecord) {
return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, null, header, skipHeaderRecord,
- allowMissingColumnNames);
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames);
}
}
diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
index caf42553..2d5750a5 100644
--- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
@@ -579,8 +579,11 @@ public class CSVPrinterTest {
private CSVPrinter printWithHeaderComments(final StringWriter sw, final Date now, final CSVFormat baseFormat)
throws IOException {
- CSVFormat format = baseFormat.withCommentMarker('#').withHeader("Col1", "Col2");
+ CSVFormat format = baseFormat;
+ // Use withHeaderComments first to test CSV-145
format = format.withHeaderComments("Generated by Apache Commons CSV 1.1", now);
+ format = format.withCommentMarker('#');
+ format = format.withHeader("Col1", "Col2");
final CSVPrinter csvPrinter = format.print(sw);
csvPrinter.printRecord("A", "B");
csvPrinter.printRecord("C", "D");