From 3a771cf051d6f27d55690d5b72f27689149a7742 Mon Sep 17 00:00:00 2001 From: Miguel Paraz Date: Thu, 11 Dec 2014 14:58:57 +1100 Subject: [PATCH] Issue #53 do not attempt to write null fields - will make col1;;col3 --- .../java/org/csveed/row/RowWriterImpl.java | 4 ++++ .../java/org/csveed/api/CsvClientTest.java | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/main/java/org/csveed/row/RowWriterImpl.java b/src/main/java/org/csveed/row/RowWriterImpl.java index bb6c6e79..a8400b0a 100644 --- a/src/main/java/org/csveed/row/RowWriterImpl.java +++ b/src/main/java/org/csveed/row/RowWriterImpl.java @@ -101,6 +101,10 @@ private void writeSeparator() throws IOException { } private void writeCell(String cell) throws IOException { + if (cell == null) { + return; + } + writer.write(rowInstructions.getQuote()); String searchString = Character.toString(rowInstructions.getQuote()); String replaceString = new String(new char[] { rowInstructions.getEscape(), rowInstructions.getQuote() } ); diff --git a/src/test/java/org/csveed/api/CsvClientTest.java b/src/test/java/org/csveed/api/CsvClientTest.java index 0f1b24d6..b8b27cff 100644 --- a/src/test/java/org/csveed/api/CsvClientTest.java +++ b/src/test/java/org/csveed/api/CsvClientTest.java @@ -59,6 +59,26 @@ writer, new BeanInstructionsImpl(BeanWithMultipleStrings.class) writer.getBuffer().toString()); } + @Test + public void writeBeanBasedOnInstructionsWithNull() throws IOException { + StringWriter writer = new StringWriter(); + BeanWithMultipleStrings bean = createBean(null, "row 1, cell 2", "row 1, cell 1"); + CsvClient client = new CsvClientImpl( + writer, new BeanInstructionsImpl(BeanWithMultipleStrings.class) + .mapColumnNameToProperty("alpha", "alpha") + .mapColumnNameToProperty("beta", "beta") + .ignoreProperty("gamma") + ); + client.writeBean(bean); + writer.close(); + assertEquals( + "\"beta\";\"alpha\"\r\n"+ + "\"row 1, cell 2\";\r\n", + writer.getBuffer().toString()); + } + + + private BeanWithMultipleStrings createBean(String alpha, String beta, String gamma) { BeanWithMultipleStrings bean = new BeanWithMultipleStrings(); bean.setAlpha(alpha);