Testcases for CSVWriter.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/csv/trunk@373943 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Martin van den Bemt 2006-01-31 23:37:16 +00:00
parent 366598c77c
commit 389fcd218e
4 changed files with 293 additions and 0 deletions

View File

@ -0,0 +1,88 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.csv.writer;
import java.io.ByteArrayInputStream;
import junit.framework.TestCase;
/**
* Tests for the config guesser.
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVConfigGuesserTest extends TestCase {
public void testSetters() throws Exception {
CSVConfigGuesser guesser = new CSVConfigGuesser();
ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
guesser.setInputStream(in);
assertEquals(in, guesser.getInputStream());
guesser = new CSVConfigGuesser(in);
assertEquals(in, guesser.getInputStream());
assertEquals(false, guesser.hasFieldHeader());
guesser.setHasFieldHeader(true);
assertEquals(true, guesser.hasFieldHeader());
}
/**
* Test a format like
* 1234 ; abcd ; 1234 ;
*
*/
public void testConfigGuess1() {
CSVConfig expected = new CSVConfig();
expected.setDelimiter(';');
expected.setValueDelimiter(' ');
expected.setFill(CSVConfig.FILLRIGHT);
expected.setIgnoreValueDelimiter(false);
expected.setFixedWidth(true);
CSVField field = new CSVField();
field.setSize(4);
expected.addField(field);
expected.addField(field);
StringBuffer sb = new StringBuffer();
sb.append("1234;abcd;1234\n");
sb.append("abcd;1234;abcd");
ByteArrayInputStream in = new ByteArrayInputStream(sb.toString().getBytes());
CSVConfigGuesser guesser = new CSVConfigGuesser(in);
CSVConfig guessed = guesser.guess();
assertEquals(expected.isFixedWidth(), guessed.isFixedWidth());
assertEquals(expected.getFields().length, guessed.getFields().length);
assertEquals(expected.getFields()[0].getSize(), guessed.getFields()[0].getSize());
}
/**
* Test a format like
* 1234,123123,12312312,213123
* 1,2,3,4
*
*/
public void testConfigGuess2() {
CSVConfig expected = new CSVConfig();
expected.setDelimiter(';');
expected.setValueDelimiter(' ');
expected.setFill(CSVConfig.FILLRIGHT);
expected.setIgnoreValueDelimiter(false);
// expected.setFixedWidth(false);
StringBuffer sb = new StringBuffer();
sb.append("1,2,3,4\n");
sb.append("abcd,1234,abcd,1234");
ByteArrayInputStream in = new ByteArrayInputStream(sb.toString().getBytes());
CSVConfigGuesser guesser = new CSVConfigGuesser(in);
CSVConfig guessed = guesser.guess();
assertEquals(expected.isFixedWidth(), guessed.isFixedWidth());
}
}

View File

@ -0,0 +1,100 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.csv.writer;
import java.util.Collection;
import junit.framework.TestCase;
/**
* Testcase for the CSVConfig
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVConfigTest extends TestCase {
public void testFixedWith() {
CSVConfig config = new CSVConfig();
assertEquals(false, config.isFixedWidth());
config.setFixedWidth(true);
assertEquals(true, config.isFixedWidth());
}
public void testFields() {
CSVConfig config = new CSVConfig();
assertEquals(0, config.getFields().length);
config.setFields((CSVField[])null);
assertEquals(0, config.getFields().length);
config.setFields((Collection)null);
assertEquals(0, config.getFields().length);
CSVField field = new CSVField();
field.setName("field1");
config.addField(field);
assertEquals(field, config.getFields()[0]);
assertEquals(null, config.getField(null));
assertEquals(null, config.getField("field11"));
assertEquals(field, config.getField("field1"));
}
public void testFill() {
CSVConfig config = new CSVConfig();
assertEquals(CSVConfig.FILLNONE, config.getFill());
config.setFill(CSVConfig.FILLLEFT);
assertEquals(CSVConfig.FILLLEFT, config.getFill());
config.setFill(CSVConfig.FILLRIGHT);
assertEquals(CSVConfig.FILLRIGHT, config.getFill());
assertEquals(' ', config.getFillChar());
config.setFillChar('m');
assertEquals('m', config.getFillChar());
}
public void testDelimiter() {
CSVConfig config = new CSVConfig();
assertEquals(',', config.getDelimiter());
config.setDelimiter(';');
assertEquals(';', config.getDelimiter());
assertEquals(false, config.isDelimiterIgnored());
config.setIgnoreDelimiter(true);
assertEquals(true, config.isDelimiterIgnored());
}
public void testValueDelimiter() {
CSVConfig config = new CSVConfig();
assertEquals('"', config.getValueDelimiter());
config.setValueDelimiter('m');
assertEquals('m', config.getValueDelimiter());
assertEquals(true, config.isValueDelimiterIgnored());
config.setIgnoreValueDelimiter(false);
assertEquals(false, config.isValueDelimiterIgnored());
}
public void testFieldHeader() {
CSVConfig config = new CSVConfig();
assertEquals(false, config.isFieldHeader());
config.setFieldHeader(true);
assertEquals(true, config.isFieldHeader());
}
public void testTrimEnd() {
CSVConfig config = new CSVConfig();
assertEquals(false, config.isEndTrimmed());
config.setEndTrimmed(true);
assertEquals(true, config.isEndTrimmed());
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.csv.writer;
import junit.framework.TestCase;
/**
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVFieldTest extends TestCase {
public void testCSVField() {
CSVField field = new CSVField();
assertEquals(null, field.getName());
field.setName("id");
assertEquals("id", field.getName());
assertEquals(0, field.getSize());
field.setSize(10);
assertEquals(10, field.getSize());
field = new CSVField("name");
assertEquals("name", field.getName());
field = new CSVField("name", 10);
assertEquals("name", field.getName());
assertEquals(10, field.getSize());
}
public void testFill() {
CSVField field = new CSVField();
assertEquals(CSVConfig.FILLNONE, field.getFill());
assertEquals(false, field.overrideFill());
field.setFill(CSVConfig.FILLLEFT);
assertEquals(true, field.overrideFill());
assertEquals(CSVConfig.FILLLEFT, field.getFill());
}
}

View File

@ -0,0 +1,55 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package org.apache.commons.csv.writer;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
/**
* The testcase for the csv writer.
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVWriterTest extends TestCase {
public void testCSVConfig() {
CSVWriter writer = new CSVWriter();
assertEquals(null, writer.getConfig());
CSVConfig config = new CSVConfig();
writer.setConfig(config);
assertEquals(config, writer.getConfig());
writer = new CSVWriter(config);
assertEquals(config, writer.getConfig());
}
public void testWriter() {
CSVWriter writer = new CSVWriter();
CSVConfig config = new CSVConfig();
config.addField(new CSVField("field1", 5));
config.addField(new CSVField("field2", 4));
writer.setConfig(config);
StringWriter sw = new StringWriter();
writer.setWriter(sw);
Map map = new HashMap();
map.put("field1", "12345");
map.put("field2", "1234");
writer.writeRecord(map);
assertEquals("12345,1234\n",sw.toString());
}
}