@@ -1107,6 +1107,38 @@ void testMultiLineComment() throws IOException {
11071107 }
11081108 }
11091109
1110+ @ Test
1111+ void testMultiLineCommentWithTrailingDelimiter () throws IOException {
1112+ // A comment is not a record, so the trailing delimiter must not follow comment lines, or it becomes
1113+ // part of the comment text on read back. The following data record still gets its trailing delimiter.
1114+ final CSVFormat format = CSVFormat .DEFAULT .builder ().setCommentMarker ('#' ).setTrailingDelimiter (true ).get ();
1115+ final StringWriter sw = new StringWriter ();
1116+ try (CSVPrinter printer = new CSVPrinter (sw , format )) {
1117+ printer .printComment ("This is a comment\n on multiple lines" );
1118+ printer .printRecord ("A" , "B" );
1119+ }
1120+ final String string = sw .toString ();
1121+ assertEquals ("# This is a comment" + RECORD_SEPARATOR + "# on multiple lines" + RECORD_SEPARATOR + "A,B," + RECORD_SEPARATOR , string );
1122+ try (CSVParser parser = CSVParser .parse (string , format )) {
1123+ final List <CSVRecord > records = parser .getRecords ();
1124+ assertEquals (1 , records .size ());
1125+ assertEquals ("This is a comment\n on multiple lines" , records .get (0 ).getComment ());
1126+ assertEquals ("A" , records .get (0 ).get (0 ));
1127+ assertEquals ("B" , records .get (0 ).get (1 ));
1128+ }
1129+ }
1130+
1131+ @ Test
1132+ void testMultiLineCommentWithoutRecordSeparator () throws IOException {
1133+ // A null record separator writes nothing between comment lines, matching the record output path.
1134+ final CSVFormat format = CSVFormat .DEFAULT .builder ().setCommentMarker ('#' ).setRecordSeparator (null ).get ();
1135+ final StringWriter sw = new StringWriter ();
1136+ try (CSVPrinter printer = new CSVPrinter (sw , format )) {
1137+ printer .printComment ("a\n b" );
1138+ }
1139+ assertEquals ("# a# b" , sw .toString ());
1140+ }
1141+
11101142 @ Test
11111143 void testMySqlNullOutput () throws IOException {
11121144 Object [] s = new String [] { "NULL" , null };
0 commit comments