From 73cc5246cf789db8f459e2f539831b6e91bedd26 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Thu, 13 Mar 2014 00:16:57 +0000 Subject: [PATCH] [CSV-106] CSVFormat.format always append null. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1577011 13f79535-47bb-0310-9956-ffa450edef68 --- src/main/java/org/apache/commons/csv/CSVPrinter.java | 5 ++++- .../java/org/apache/commons/csv/CSVFormatTest.java | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java index 9771cb03..a8aa33ff 100644 --- a/src/main/java/org/apache/commons/csv/CSVPrinter.java +++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java @@ -321,7 +321,10 @@ public final class CSVPrinter implements Flushable, Closeable { * If an I/O error occurs */ public void println() throws IOException { - out.append(format.getRecordSeparator()); + final String recordSeparator = format.getRecordSeparator(); + if (recordSeparator != null) { + out.append(recordSeparator); + } newRecord = true; } diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index 07ffac60..8b0bfac9 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -55,7 +55,7 @@ public class CSVFormatTest { public void testDelimiterSameAsCommentStartThrowsException() { CSVFormat.DEFAULT.withDelimiter('!').withCommentStart('!').validate(); } - + @Test(expected = IllegalStateException.class) public void testDelimiterSameAsEscapeThrowsException() { CSVFormat.DEFAULT.withDelimiter('!').withEscape('!').validate(); @@ -229,6 +229,14 @@ public class CSVFormatTest { assertNotSame(formatWithHeader.getHeader(), headerCopy); } + @Test + public void testNullRecordSeparatorCsv106() { + final CSVFormat format = CSVFormat.newFormat(';').withSkipHeaderRecord(true).withHeader("H1", "H2"); + final String formatStr = format.format("A", "B"); + assertNotNull(formatStr); + assertFalse(formatStr.endsWith("null")); + } + @Test(expected = IllegalStateException.class) public void testQuoteCharSameAsCommentStartThrowsException() { CSVFormat.DEFAULT.withQuoteChar('!').withCommentStart('!').validate();