Commit 94e614d5 authored by R.W.Majeed's avatar R.W.Majeed

fixed NPE for null values in row array. test case added.

parent 6686062d
......@@ -35,6 +35,7 @@ public class CSVWriter implements ExportWriter{
private String filenameExtension;
private String patientTableName;
private String visitTableName;
private String nullString;
/**
* Create a CSV writer which creates table files
......@@ -49,6 +50,7 @@ public class CSVWriter implements ExportWriter{
this.directory = directory;
this.fieldSeparator = fieldSeparator;
this.filenameExtension = fileSuffix;
this.nullString = ""; // write null values as empty strings
this.patientTableName = "patients";
this.visitTableName = "visits";
if( fieldSeparator == ' ' || fieldSeparator == '\n' ){
......@@ -103,6 +105,9 @@ public class CSVWriter implements ExportWriter{
* @return escaped data field
*/
protected String escapeData(String data){
if( data == null ){
return nullString;
}
// TODO do proper escaping
return data.replace(fieldSeparator, ' ').replace('\n', ' ').replace('\r', ' ');
}
......
package de.sekmi.histream.export.csv;
import java.nio.file.Paths;
import static org.junit.Assert.*;
import org.junit.Test;
public class TestCSVWriter {
@Test
public void verifyEscape(){
CSVWriter w = new CSVWriter(Paths.get("."), '\t', ".txt");
// null is written as empty string (default)
assertEquals("", w.escapeData(null));
// line feeds and carriage returns will be encoded each as single spaces
assertEquals("a b", w.escapeData("a\r\nb"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment