3232 */
3333public class CSVWriterTest extends TestCase {
3434
35+ private Map map ;
36+
37+ protected void setUp () throws Exception {
38+ super .setUp ();
39+
40+ map = new HashMap ();
41+ map .put ("field1" , "12345" );
42+ map .put ("field2" , "1234" );
43+ }
44+
3545 public void testCSVConfig () {
3646 CSVWriter writer = new CSVWriter ();
3747 assertEquals (null , writer .getConfig ());
@@ -41,19 +51,115 @@ public void testCSVConfig() {
4151 writer = new CSVWriter (config );
4252 assertEquals (config , writer .getConfig ());
4353 }
44-
45- public void testWriter () {
54+
55+ public void testWriterDefaults () throws Exception {
4656 CSVWriter writer = new CSVWriter ();
47- CSVConfig config = new CSVConfig ();
48- config .addField (new CSVField ("field1" , 5 ));
49- config .addField (new CSVField ("field2" , 4 ));
57+ CSVConfig config = getConfig ();
58+ writer .setConfig (config );
59+ StringWriter sw = new StringWriter ();
60+ writer .setWriter (sw );
61+ writer .writeRecord (map );
62+ assertEquals ("12345,1234\n " , sw .toString ());
63+ }
64+
65+ public void testWriterWithExplicitDelimiter () throws Exception {
66+ CSVWriter writer = new CSVWriter ();
67+ CSVConfig config = getConfig ();
68+ config .setDelimiter (';' );
69+ config .setIgnoreDelimiter (false );
70+ writer .setConfig (config );
71+ StringWriter sw = new StringWriter ();
72+ writer .setWriter (sw );
73+ writer .writeRecord (map );
74+ assertEquals ("12345;1234\n " , sw .toString ());
75+ }
76+
77+ public void testWriterIgnoringDelimiter () throws Exception {
78+ CSVWriter writer = new CSVWriter ();
79+ CSVConfig config = getConfig ();
80+ config .setDelimiter (';' );
81+ config .setIgnoreDelimiter (true );
82+ writer .setConfig (config );
83+ StringWriter sw = new StringWriter ();
84+ writer .setWriter (sw );
85+ writer .writeRecord (map );
86+ assertEquals ("123451234\n " , sw .toString ());
87+ }
88+
89+ public void testWriterWithExplicitValueDelimiter () throws Exception {
90+ CSVWriter writer = new CSVWriter ();
91+ CSVConfig config = getConfig ();
92+ config .setValueDelimiter ('"' );
93+ config .setIgnoreValueDelimiter (false );
94+ writer .setConfig (config );
95+ StringWriter sw = new StringWriter ();
96+ writer .setWriter (sw );
97+ writer .writeRecord (map );
98+ assertEquals ("\" 12345\" ,\" 1234\" \n " , sw .toString ());
99+ }
100+
101+ public void testWriterIgnoringValueDelimiter () throws Exception {
102+ CSVWriter writer = new CSVWriter ();
103+ CSVConfig config = getConfig ();
104+ config .setValueDelimiter ('"' );
105+ config .setIgnoreValueDelimiter (true );
106+ writer .setConfig (config );
107+ StringWriter sw = new StringWriter ();
108+ writer .setWriter (sw );
109+ writer .writeRecord (map );
110+ assertEquals ("12345,1234\n " , sw .toString ());
111+ }
112+
113+ public void testWriterWithoutHeader () throws Exception {
114+ CSVWriter writer = new CSVWriter ();
115+ CSVConfig config = getConfig ();
116+ config .setFieldHeader (false );
50117 writer .setConfig (config );
51118 StringWriter sw = new StringWriter ();
52119 writer .setWriter (sw );
53- Map map = new HashMap ();
54- map .put ("field1" , "12345" );
55- map .put ("field2" , "1234" );
56120 writer .writeRecord (map );
57- assertEquals ("12345,1234\n " ,sw .toString ());
121+ assertEquals ("12345,1234\n " , sw .toString ());
122+ }
123+
124+ // TODO: SANDBOX-324
125+ public void todoTestWriterWithHeader () throws Exception {
126+ CSVWriter writer = new CSVWriter ();
127+ CSVConfig config = getConfig ();
128+ config .setFieldHeader (true );
129+ writer .setConfig (config );
130+ StringWriter sw = new StringWriter ();
131+ writer .setWriter (sw );
132+ writer .writeRecord (map );
133+ assertEquals ("field1,field2\n 12345,1234\n " , sw .toString ());
134+ }
135+
136+ public void testWriterWithExplicitRowDelimiterLF () throws Exception {
137+ CSVWriter writer = new CSVWriter ();
138+ CSVConfig config = getConfig ();
139+ config .setRowDelimiter ("\n " );
140+ writer .setConfig (config );
141+ StringWriter sw = new StringWriter ();
142+ writer .setWriter (sw );
143+ writer .writeRecord (map );
144+ assertEquals ("12345,1234\n " , sw .toString ());
145+ }
146+
147+ public void testWriterWithExplicitRowDelimiterCRLF () throws Exception {
148+ CSVWriter writer = new CSVWriter ();
149+ CSVConfig config = getConfig ();
150+ config .setRowDelimiter ("\r \n " );
151+ writer .setConfig (config );
152+ StringWriter sw = new StringWriter ();
153+ writer .setWriter (sw );
154+ writer .writeRecord (map );
155+ assertEquals ("12345,1234\r \n " , sw .toString ());
156+ }
157+
158+ private CSVConfig getConfig () {
159+ CSVConfig config = new CSVConfig ();
160+ config .addField (new CSVField ("field1" , 5 ));
161+ config .addField (new CSVField ("field2" , 4 ));
162+
163+ return config ;
58164 }
59165}
0 commit comments