CSVWriter.writeValue does not use value delimiter (SANDBOX-243).

git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1037647 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joerg Schaible 2010-11-22 09:47:55 +00:00
parent 6eddaf8b0c
commit b1edd537c7
2 changed files with 120 additions and 13 deletions

View File

@ -103,13 +103,14 @@ public class CSVWriter {
// value to big.. // value to big..
value = value.substring(0, field.getSize()); value = value.substring(0, field.getSize());
} }
}
if (!config.isValueDelimiterIgnored()) { if (!config.isValueDelimiterIgnored()) {
// add the value delimiter.. // add the value delimiter..
value = config.getValueDelimiter()+value+config.getValueDelimiter(); value = config.getValueDelimiter()+value+config.getValueDelimiter();
} }
}
return value; return value;
} }
/** /**
* @return the CVSConfig or null if not present * @return the CVSConfig or null if not present
*/ */

View File

@ -32,6 +32,16 @@ import junit.framework.TestCase;
*/ */
public class CSVWriterTest extends TestCase { public class CSVWriterTest extends TestCase {
private Map map;
protected void setUp() throws Exception {
super.setUp();
map = new HashMap();
map.put("field1", "12345");
map.put("field2", "1234");
}
public void testCSVConfig() { public void testCSVConfig() {
CSVWriter writer = new CSVWriter(); CSVWriter writer = new CSVWriter();
assertEquals(null, writer.getConfig()); assertEquals(null, writer.getConfig());
@ -42,18 +52,114 @@ public class CSVWriterTest extends TestCase {
assertEquals(config, writer.getConfig()); assertEquals(config, writer.getConfig());
} }
public void testWriter() { public void testWriterDefaults() throws Exception {
CSVWriter writer = new CSVWriter(); CSVWriter writer = new CSVWriter();
CSVConfig config = new CSVConfig(); CSVConfig config = getConfig();
config.addField(new CSVField("field1", 5));
config.addField(new CSVField("field2", 4));
writer.setConfig(config); writer.setConfig(config);
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
writer.setWriter(sw); writer.setWriter(sw);
Map map = new HashMap();
map.put("field1", "12345");
map.put("field2", "1234");
writer.writeRecord(map); writer.writeRecord(map);
assertEquals("12345,1234\n",sw.toString()); assertEquals("12345,1234\n", sw.toString());
}
public void testWriterWithExplicitDelimiter() throws Exception {
CSVWriter writer = new CSVWriter();
CSVConfig config = getConfig();
config.setDelimiter(';');
config.setIgnoreDelimiter(false);
writer.setConfig(config);
StringWriter sw = new StringWriter();
writer.setWriter(sw);
writer.writeRecord(map);
assertEquals("12345;1234\n", sw.toString());
}
public void testWriterIgnoringDelimiter() throws Exception {
CSVWriter writer = new CSVWriter();
CSVConfig config = getConfig();
config.setDelimiter(';');
config.setIgnoreDelimiter(true);
writer.setConfig(config);
StringWriter sw = new StringWriter();
writer.setWriter(sw);
writer.writeRecord(map);
assertEquals("123451234\n", sw.toString());
}
public void testWriterWithExplicitValueDelimiter() throws Exception {
CSVWriter writer = new CSVWriter();
CSVConfig config = getConfig();
config.setValueDelimiter('"');
config.setIgnoreValueDelimiter(false);
writer.setConfig(config);
StringWriter sw = new StringWriter();
writer.setWriter(sw);
writer.writeRecord(map);
assertEquals("\"12345\",\"1234\"\n", sw.toString());
}
public void testWriterIgnoringValueDelimiter() throws Exception {
CSVWriter writer = new CSVWriter();
CSVConfig config = getConfig();
config.setValueDelimiter('"');
config.setIgnoreValueDelimiter(true);
writer.setConfig(config);
StringWriter sw = new StringWriter();
writer.setWriter(sw);
writer.writeRecord(map);
assertEquals("12345,1234\n", sw.toString());
}
public void testWriterWithoutHeader() throws Exception {
CSVWriter writer = new CSVWriter();
CSVConfig config = getConfig();
config.setFieldHeader(false);
writer.setConfig(config);
StringWriter sw = new StringWriter();
writer.setWriter(sw);
writer.writeRecord(map);
assertEquals("12345,1234\n", sw.toString());
}
// TODO: SANDBOX-324
public void todoTestWriterWithHeader() throws Exception {
CSVWriter writer = new CSVWriter();
CSVConfig config = getConfig();
config.setFieldHeader(true);
writer.setConfig(config);
StringWriter sw = new StringWriter();
writer.setWriter(sw);
writer.writeRecord(map);
assertEquals("field1,field2\n12345,1234\n", sw.toString());
}
public void testWriterWithExplicitRowDelimiterLF() throws Exception {
CSVWriter writer = new CSVWriter();
CSVConfig config = getConfig();
config.setRowDelimiter("\n");
writer.setConfig(config);
StringWriter sw = new StringWriter();
writer.setWriter(sw);
writer.writeRecord(map);
assertEquals("12345,1234\n", sw.toString());
}
public void testWriterWithExplicitRowDelimiterCRLF() throws Exception {
CSVWriter writer = new CSVWriter();
CSVConfig config = getConfig();
config.setRowDelimiter("\r\n");
writer.setConfig(config);
StringWriter sw = new StringWriter();
writer.setWriter(sw);
writer.writeRecord(map);
assertEquals("12345,1234\r\n", sw.toString());
}
private CSVConfig getConfig() {
CSVConfig config = new CSVConfig();
config.addField(new CSVField("field1", 5));
config.addField(new CSVField("field2", 4));
return config;
} }
} }