-
Notifications
You must be signed in to change notification settings - Fork 303
Quote null value that starts a record in minimal quote mode #629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -188,6 +188,14 @@ private Connection getH2Connection() throws SQLException, ClassNotFoundException | |
| return DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", ""); | ||
| } | ||
|
|
||
| private String printNullRecord(final CSVFormat format) throws IOException { | ||
| final StringWriter sw = new StringWriter(); | ||
| try (CSVPrinter printer = new CSVPrinter(sw, format)) { | ||
| printer.printRecord((Object) null); | ||
| } | ||
| return sw.toString(); | ||
| } | ||
|
|
||
| private CSVPrinter printWithHeaderComments(final StringWriter sw, final Date now, final CSVFormat baseFormat) throws IOException { | ||
| // Use withHeaderComments first to test CSV-145 | ||
| // @formatter:off | ||
|
|
@@ -1721,6 +1729,31 @@ void testPrinter7() throws IOException { | |
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testPrintNullValueStartingRecord() throws IOException { | ||
|
Comment on lines
+1732
to
+1733
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an |
||
| final StringWriter sw = new StringWriter(); | ||
| try (CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) { | ||
| printer.printRecord("a"); | ||
| printer.printRecord((Object) null); | ||
| printer.printRecord("b"); | ||
| } | ||
| final String csv = sw.toString(); | ||
| assertEquals("a" + RECORD_SEPARATOR + "\"\"" + RECORD_SEPARATOR + "b" + RECORD_SEPARATOR, csv); | ||
| try (CSVParser parser = CSVParser.parse(csv, CSVFormat.DEFAULT)) { | ||
| final List<CSVRecord> records = parser.getRecords(); | ||
| assertEquals(3, records.size()); | ||
| assertArrayEquals(new String[] { "" }, records.get(1).values()); | ||
| } | ||
| // An explicit MINIMAL quote mode encapsulates it too. | ||
| assertEquals("\"\"" + RECORD_SEPARATOR, printNullRecord(CSVFormat.DEFAULT.builder().setQuoteMode(QuoteMode.MINIMAL).get())); | ||
| // ALL encodes null as the bare empty field, distinct from a quoted empty string (CSV-203). | ||
| assertEquals(RECORD_SEPARATOR, printNullRecord(CSVFormat.DEFAULT.builder().setQuoteMode(QuoteMode.ALL).get())); | ||
| // ALL_NON_NULL encodes null as the bare empty field, so it must stay unquoted. | ||
| assertEquals(RECORD_SEPARATOR, printNullRecord(CSVFormat.DEFAULT.builder().setQuoteMode(QuoteMode.ALL_NON_NULL).get())); | ||
| // Without a quote character there is nothing to encapsulate with. | ||
| assertEquals(RECORD_SEPARATOR, printNullRecord(CSVFormat.DEFAULT.builder().setQuote(null).get())); | ||
|
Comment on lines
+1747
to
+1754
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Covered by the new |
||
| } | ||
|
|
||
| @Test | ||
| void testPrintNullValues() throws IOException { | ||
| final StringWriter sw = new StringWriter(); | ||
|
|
@@ -1854,8 +1887,9 @@ void testPrintRecordsWithObjectArray() throws IOException { | |
| printer.printRecords(objectArray); | ||
| assertEquals(objectArray.length, printer.getRecordCount()); | ||
| } | ||
| assertEquals(6, charArrayWriter.size()); | ||
| assertEquals("\n\n\n\n\n\n", charArrayWriter.toString()); | ||
| final String expected = "\"\"\n\"\"\n\"\"\n\n\"\"\n\"\"\n"; | ||
| assertEquals(expected.length(), charArrayWriter.size()); | ||
| assertEquals(expected, charArrayWriter.toString()); | ||
| } | ||
|
|
||
| @Test | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extending this to
ALLbreaksJiraCsv203Test.testWithoutNullString(CSV-203), which pins thatALLwrites null as the bare empty field so it stays distinct from a quoted"". That's the same encoding rationale asALL_NON_NULL, soALLstays excluded. Updated the code comment to say so and added a test assertion pinning it.