Skip to content

Commit 389fcd2

Browse files
committed
Testcases for CSVWriter.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/csv/trunk@373943 13f79535-47bb-0310-9956-ffa450edef68
1 parent 366598c commit 389fcd2

4 files changed

Lines changed: 293 additions & 0 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2006 The Apache Software Foundation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.commons.csv.writer;
17+
18+
import java.io.ByteArrayInputStream;
19+
20+
import junit.framework.TestCase;
21+
22+
/**
23+
* Tests for the config guesser.
24+
*
25+
* @author Martin van den Bemt
26+
* @version $Id: $
27+
*/
28+
public class CSVConfigGuesserTest extends TestCase {
29+
30+
public void testSetters() throws Exception {
31+
CSVConfigGuesser guesser = new CSVConfigGuesser();
32+
ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
33+
guesser.setInputStream(in);
34+
assertEquals(in, guesser.getInputStream());
35+
guesser = new CSVConfigGuesser(in);
36+
assertEquals(in, guesser.getInputStream());
37+
assertEquals(false, guesser.hasFieldHeader());
38+
guesser.setHasFieldHeader(true);
39+
assertEquals(true, guesser.hasFieldHeader());
40+
}
41+
/**
42+
* Test a format like
43+
* 1234 ; abcd ; 1234 ;
44+
*
45+
*/
46+
public void testConfigGuess1() {
47+
CSVConfig expected = new CSVConfig();
48+
expected.setDelimiter(';');
49+
expected.setValueDelimiter(' ');
50+
expected.setFill(CSVConfig.FILLRIGHT);
51+
expected.setIgnoreValueDelimiter(false);
52+
expected.setFixedWidth(true);
53+
CSVField field = new CSVField();
54+
field.setSize(4);
55+
expected.addField(field);
56+
expected.addField(field);
57+
StringBuffer sb = new StringBuffer();
58+
sb.append("1234;abcd;1234\n");
59+
sb.append("abcd;1234;abcd");
60+
ByteArrayInputStream in = new ByteArrayInputStream(sb.toString().getBytes());
61+
CSVConfigGuesser guesser = new CSVConfigGuesser(in);
62+
CSVConfig guessed = guesser.guess();
63+
assertEquals(expected.isFixedWidth(), guessed.isFixedWidth());
64+
assertEquals(expected.getFields().length, guessed.getFields().length);
65+
assertEquals(expected.getFields()[0].getSize(), guessed.getFields()[0].getSize());
66+
}
67+
/**
68+
* Test a format like
69+
* 1234,123123,12312312,213123
70+
* 1,2,3,4
71+
*
72+
*/
73+
public void testConfigGuess2() {
74+
CSVConfig expected = new CSVConfig();
75+
expected.setDelimiter(';');
76+
expected.setValueDelimiter(' ');
77+
expected.setFill(CSVConfig.FILLRIGHT);
78+
expected.setIgnoreValueDelimiter(false);
79+
// expected.setFixedWidth(false);
80+
StringBuffer sb = new StringBuffer();
81+
sb.append("1,2,3,4\n");
82+
sb.append("abcd,1234,abcd,1234");
83+
ByteArrayInputStream in = new ByteArrayInputStream(sb.toString().getBytes());
84+
CSVConfigGuesser guesser = new CSVConfigGuesser(in);
85+
CSVConfig guessed = guesser.guess();
86+
assertEquals(expected.isFixedWidth(), guessed.isFixedWidth());
87+
}
88+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2006 The Apache Software Foundation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.commons.csv.writer;
17+
18+
import java.util.Collection;
19+
20+
import junit.framework.TestCase;
21+
22+
/**
23+
* Testcase for the CSVConfig
24+
*
25+
* @author Martin van den Bemt
26+
* @version $Id: $
27+
*/
28+
public class CSVConfigTest extends TestCase {
29+
30+
31+
public void testFixedWith() {
32+
CSVConfig config = new CSVConfig();
33+
assertEquals(false, config.isFixedWidth());
34+
config.setFixedWidth(true);
35+
assertEquals(true, config.isFixedWidth());
36+
}
37+
38+
public void testFields() {
39+
CSVConfig config = new CSVConfig();
40+
assertEquals(0, config.getFields().length);
41+
config.setFields((CSVField[])null);
42+
assertEquals(0, config.getFields().length);
43+
config.setFields((Collection)null);
44+
assertEquals(0, config.getFields().length);
45+
CSVField field = new CSVField();
46+
field.setName("field1");
47+
config.addField(field);
48+
assertEquals(field, config.getFields()[0]);
49+
assertEquals(null, config.getField(null));
50+
assertEquals(null, config.getField("field11"));
51+
assertEquals(field, config.getField("field1"));
52+
}
53+
54+
public void testFill() {
55+
CSVConfig config = new CSVConfig();
56+
assertEquals(CSVConfig.FILLNONE, config.getFill());
57+
config.setFill(CSVConfig.FILLLEFT);
58+
assertEquals(CSVConfig.FILLLEFT, config.getFill());
59+
config.setFill(CSVConfig.FILLRIGHT);
60+
assertEquals(CSVConfig.FILLRIGHT, config.getFill());
61+
assertEquals(' ', config.getFillChar());
62+
config.setFillChar('m');
63+
assertEquals('m', config.getFillChar());
64+
}
65+
66+
public void testDelimiter() {
67+
CSVConfig config = new CSVConfig();
68+
assertEquals(',', config.getDelimiter());
69+
config.setDelimiter(';');
70+
assertEquals(';', config.getDelimiter());
71+
assertEquals(false, config.isDelimiterIgnored());
72+
config.setIgnoreDelimiter(true);
73+
assertEquals(true, config.isDelimiterIgnored());
74+
}
75+
76+
public void testValueDelimiter() {
77+
CSVConfig config = new CSVConfig();
78+
assertEquals('"', config.getValueDelimiter());
79+
config.setValueDelimiter('m');
80+
assertEquals('m', config.getValueDelimiter());
81+
assertEquals(true, config.isValueDelimiterIgnored());
82+
config.setIgnoreValueDelimiter(false);
83+
assertEquals(false, config.isValueDelimiterIgnored());
84+
}
85+
86+
public void testFieldHeader() {
87+
CSVConfig config = new CSVConfig();
88+
assertEquals(false, config.isFieldHeader());
89+
config.setFieldHeader(true);
90+
assertEquals(true, config.isFieldHeader());
91+
}
92+
93+
public void testTrimEnd() {
94+
CSVConfig config = new CSVConfig();
95+
assertEquals(false, config.isEndTrimmed());
96+
config.setEndTrimmed(true);
97+
assertEquals(true, config.isEndTrimmed());
98+
}
99+
100+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2006 The Apache Software Foundation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.commons.csv.writer;
17+
18+
import junit.framework.TestCase;
19+
20+
/**
21+
*
22+
* @author Martin van den Bemt
23+
* @version $Id: $
24+
*/
25+
public class CSVFieldTest extends TestCase {
26+
27+
public void testCSVField() {
28+
CSVField field = new CSVField();
29+
assertEquals(null, field.getName());
30+
field.setName("id");
31+
assertEquals("id", field.getName());
32+
assertEquals(0, field.getSize());
33+
field.setSize(10);
34+
assertEquals(10, field.getSize());
35+
field = new CSVField("name");
36+
assertEquals("name", field.getName());
37+
field = new CSVField("name", 10);
38+
assertEquals("name", field.getName());
39+
assertEquals(10, field.getSize());
40+
}
41+
42+
public void testFill() {
43+
CSVField field = new CSVField();
44+
assertEquals(CSVConfig.FILLNONE, field.getFill());
45+
assertEquals(false, field.overrideFill());
46+
field.setFill(CSVConfig.FILLLEFT);
47+
assertEquals(true, field.overrideFill());
48+
assertEquals(CSVConfig.FILLLEFT, field.getFill());
49+
}
50+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2006 The Apache Software Foundation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/package org.apache.commons.csv.writer;
16+
17+
import java.io.StringWriter;
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
import junit.framework.TestCase;
22+
23+
/**
24+
* The testcase for the csv writer.
25+
*
26+
* @author Martin van den Bemt
27+
* @version $Id: $
28+
*/
29+
public class CSVWriterTest extends TestCase {
30+
31+
public void testCSVConfig() {
32+
CSVWriter writer = new CSVWriter();
33+
assertEquals(null, writer.getConfig());
34+
CSVConfig config = new CSVConfig();
35+
writer.setConfig(config);
36+
assertEquals(config, writer.getConfig());
37+
writer = new CSVWriter(config);
38+
assertEquals(config, writer.getConfig());
39+
}
40+
41+
public void testWriter() {
42+
CSVWriter writer = new CSVWriter();
43+
CSVConfig config = new CSVConfig();
44+
config.addField(new CSVField("field1", 5));
45+
config.addField(new CSVField("field2", 4));
46+
writer.setConfig(config);
47+
StringWriter sw = new StringWriter();
48+
writer.setWriter(sw);
49+
Map map = new HashMap();
50+
map.put("field1", "12345");
51+
map.put("field2", "1234");
52+
writer.writeRecord(map);
53+
assertEquals("12345,1234\n",sw.toString());
54+
}
55+
}

0 commit comments

Comments
 (0)