1 package au.com.bytecode.opencsv;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import static org.junit.Assert.*;
20 import org.junit.Test;
21
22 import java.io.*;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 public class CSVWriterTest {
27
28
29
30
31
32
33
34
35
36
37
38 private String invokeWriter(String[] args) throws IOException {
39 StringWriter sw = new StringWriter();
40 CSVWriter csvw = new CSVWriter(sw,',','\'');
41 csvw.writeNext(args);
42 return sw.toString();
43 }
44
45 private String invokeNoEscapeWriter(String[] args) throws IOException {
46 StringWriter sw = new StringWriter();
47 CSVWriter csvw = new CSVWriter(sw,',','\'', CSVWriter.NO_ESCAPE_CHARACTER);
48 csvw.writeNext(args);
49 return sw.toString();
50 }
51
52 @Test
53 public void correctlyParseNullString(){
54 StringWriter sw = new StringWriter();
55 CSVWriter csvw = new CSVWriter(sw,',','\'');
56 csvw.writeNext(null);
57 assertEquals(0, sw.toString().length());
58 }
59
60
61
62
63
64
65
66 @Test
67 public void testParseLine() throws IOException {
68
69
70 String[] normal = { "a", "b", "c" };
71 String output = invokeWriter(normal);
72 assertEquals("'a','b','c'\n", output);
73
74
75 String[] quoted = { "a", "b,b,b", "c" };
76 output = invokeWriter(quoted);
77 assertEquals("'a','b,b,b','c'\n", output);
78
79
80 String[] empty = { , };
81 output = invokeWriter(empty);
82 assertEquals("\n", output);
83
84
85 String[] multiline = { "This is a \n multiline entry", "so is \n this" };
86 output = invokeWriter(multiline);
87 assertEquals("'This is a \n multiline entry','so is \n this'\n", output);
88
89 }
90
91
92
93
94
95
96
97 @Test
98 public void testParseLineWithNoEscapeChar() throws IOException {
99
100
101 String[] normal = { "a", "b", "c" };
102 String output = invokeNoEscapeWriter(normal);
103 assertEquals("'a','b','c'\n", output);
104
105
106 String[] quoted = { "a", "b,b,b", "c" };
107 output = invokeNoEscapeWriter(quoted);
108 assertEquals("'a','b,b,b','c'\n", output);
109
110
111 String[] empty = { , };
112 output = invokeNoEscapeWriter(empty);
113 assertEquals("\n", output);
114
115
116 String[] multiline = { "This is a \n multiline entry", "so is \n this" };
117 output = invokeNoEscapeWriter(multiline);
118 assertEquals("'This is a \n multiline entry','so is \n this'\n", output);
119
120
121 String[] quoteLine = { "This is a \" multiline entry", "so is \n this" };
122 output = invokeNoEscapeWriter(quoteLine);
123 assertEquals("'This is a \" multiline entry','so is \n this'\n", output);
124
125 }
126
127
128
129
130
131
132
133 @Test
134 public void testParseAll() throws IOException {
135
136 List<String[]> allElements = new ArrayList<String[]>();
137 String[] line1 = "Name#Phone#Email".split("#");
138 String[] line2 = "Glen#1234#glen@abcd.com".split("#");
139 String[] line3 = "John#5678#john@efgh.com".split("#");
140 allElements.add(line1);
141 allElements.add(line2);
142 allElements.add(line3);
143
144 StringWriter sw = new StringWriter();
145 CSVWriter csvw = new CSVWriter(sw);
146 csvw.writeAll(allElements);
147
148 String result = sw.toString();
149 String[] lines = result.split("\n");
150
151 assertEquals(3, lines.length);
152
153 }
154
155
156
157
158
159
160 @Test
161 public void testNoQuoteChars() throws IOException {
162
163 String[] line = {"Foo","Bar","Baz"};
164 StringWriter sw = new StringWriter();
165 CSVWriter csvw = new CSVWriter(sw, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER);
166 csvw.writeNext(line);
167 String result = sw.toString();
168
169 assertEquals("Foo,Bar,Baz\n",result);
170 }
171
172
173
174
175
176
177 @Test
178 public void testNoQuoteCharsAndNoEscapeChars() throws IOException {
179
180 String[] line = {"Foo","Bar","Baz"};
181 StringWriter sw = new StringWriter();
182 CSVWriter csvw = new CSVWriter(sw, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER);
183 csvw.writeNext(line);
184 String result = sw.toString();
185
186 assertEquals("Foo,Bar,Baz\n",result);
187 }
188
189
190
191
192
193
194
195 @Test
196 public void testNullValues() throws IOException {
197
198 String[] line = {"Foo",null,"Bar","baz"};
199 StringWriter sw = new StringWriter();
200 CSVWriter csvw = new CSVWriter(sw);
201 csvw.writeNext(line);
202 String result = sw.toString();
203
204 assertEquals("\"Foo\",,\"Bar\",\"baz\"\n",result);
205
206 }
207
208 @Test
209 public void testStreamFlushing() throws IOException {
210
211 String WRITE_FILE = "myfile.csv";
212
213 String[] nextLine = new String[]{"aaaa", "bbbb","cccc","dddd"};
214
215 FileWriter fileWriter = new FileWriter(WRITE_FILE);
216 CSVWriter writer = new CSVWriter(fileWriter);
217
218 writer.writeNext(nextLine);
219
220
221 writer.close();
222
223 }
224
225 @Test
226 public void testAlternateEscapeChar() {
227 String[] line = {"Foo","bar's"};
228 StringWriter sw = new StringWriter();
229 CSVWriter csvw = new CSVWriter(sw,CSVWriter.DEFAULT_SEPARATOR,CSVWriter.DEFAULT_QUOTE_CHARACTER,'\'');
230 csvw.writeNext(line);
231 assertEquals("\"Foo\",\"bar''s\"\n",sw.toString());
232 }
233
234 @Test
235 public void testNoQuotingNoEscaping() {
236 String[] line = {"\"Foo\",\"Bar\""};
237 StringWriter sw = new StringWriter();
238 CSVWriter csvw = new CSVWriter(sw,CSVWriter.DEFAULT_SEPARATOR,CSVWriter.NO_QUOTE_CHARACTER,CSVWriter.NO_ESCAPE_CHARACTER);
239 csvw.writeNext(line);
240 assertEquals("\"Foo\",\"Bar\"\n",sw.toString());
241 }
242
243 @Test
244 public void testNestedQuotes(){
245 String[] data = new String[]{"\"\"", "test"};
246 String oracle = new String("\"\"\"\"\"\",\"test\"\n");
247
248 CSVWriter writer=null;
249 File tempFile=null;
250 FileWriter fwriter=null;
251
252 try{
253 tempFile = File.createTempFile("csvWriterTest", ".csv");
254 tempFile.deleteOnExit();
255 fwriter = new FileWriter(tempFile);
256 writer = new CSVWriter(fwriter);
257 }catch(IOException e){
258 fail();
259 }
260
261
262 writer.writeNext(data);
263
264 try{
265 writer.close();
266 }catch(IOException e){
267 fail();
268 }
269
270 try{
271
272 fwriter.flush();
273 fail();
274 }catch(IOException e){
275
276 }
277
278
279 FileReader in=null;
280 try{
281 in = new FileReader(tempFile);
282 }catch(FileNotFoundException e){
283 fail();
284 }
285
286 StringBuilder fileContents = new StringBuilder(CSVWriter.INITIAL_STRING_SIZE);
287 try{
288 int ch;
289 while((ch = in.read()) != -1){
290 fileContents.append((char)ch);
291 }
292 in.close();
293 }catch(IOException e){
294 fail();
295 }
296
297 assertTrue(oracle.equals(fileContents.toString()));
298 }
299
300 @Test
301 public void testAlternateLineFeeds() {
302 String[] line = {"Foo","Bar","baz"};
303 StringWriter sw = new StringWriter();
304 CSVWriter csvw = new CSVWriter(sw, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.DEFAULT_QUOTE_CHARACTER, "\r");
305 csvw.writeNext(line);
306 String result = sw.toString();
307
308 assertTrue(result.endsWith("\r"));
309
310 }
311
312 }