1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sourceforge.addam.impexp.csv;
17
18 import junit.framework.TestCase;
19
20 import java.io.StringWriter;
21
22 import net.sourceforge.addam.impexp.csv.CSVPrinter;
23
24 /**
25 * Tests a CSVPrinter.
26 *
27 * @author Tim Dawson
28 * @since Jul 30, 2004
29 */
30 public class CSVPrinterTEST extends TestCase {
31
32 public void testEmpty() throws Exception {
33 StringWriter raw = new StringWriter();
34 CSVPrinter printer = new CSVPrinter(raw);
35 String content[] = {};
36 printer.writeRecord(content);
37 assertEquals("\n", raw.toString());
38 }
39
40 public void testSimple() throws Exception {
41 StringWriter raw = new StringWriter();
42 CSVPrinter printer = new CSVPrinter(raw);
43 String content[] = {"foo", "bar"};
44 printer.writeRecord(content);
45 assertEquals("foo,bar\n", raw.toString());
46 }
47
48 public void testMultipleComma() throws Exception {
49 StringWriter raw = new StringWriter();
50 CSVPrinter printer = new CSVPrinter(raw);
51 String content[] = {"foo", "", "bar"};
52 printer.writeRecord(content);
53 assertEquals("foo,,bar\n", raw.toString());
54 }
55
56 public void testEmbeddedQuotes() throws Exception {
57 StringWriter raw = new StringWriter();
58 CSVPrinter printer = new CSVPrinter(raw);
59 String content[] = {"foo", "\"bar\""};
60 printer.writeRecord(content);
61 assertEquals("foo,\"\"bar\"\"\n", raw.toString());
62 }
63
64 public void testEmbeddedComma() throws Exception {
65 StringWriter raw = new StringWriter();
66 CSVPrinter printer = new CSVPrinter(raw);
67 String content[] = {"foo", "bar,baz"};
68 printer.writeRecord(content);
69 assertEquals("foo,\"bar,baz\"\n", raw.toString());
70 }
71
72 public void testEmbeddedCommaAndQuotes() throws Exception {
73 StringWriter raw = new StringWriter();
74 CSVPrinter printer = new CSVPrinter(raw);
75 String content[] = {"foo", "\"bar,baz\""};
76 printer.writeRecord(content);
77 assertEquals("foo,\"\"\"bar,baz\"\"\"\n", raw.toString());
78 }
79
80 public void testLeadingSpace() throws Exception {
81 StringWriter raw = new StringWriter();
82 CSVPrinter printer = new CSVPrinter(raw);
83 String content[] = {"foo", " bar"};
84 printer.writeRecord(content);
85 assertEquals("foo,\" bar\"\n", raw.toString());
86 }
87
88 public void testTrailingSpace() throws Exception {
89 StringWriter raw = new StringWriter();
90 CSVPrinter printer = new CSVPrinter(raw);
91 String content[] = {"foo", "bar "};
92 printer.writeRecord(content);
93 assertEquals("foo,\"bar \"\n", raw.toString());
94 }
95
96 public void testMultiLine() throws Exception {
97 StringWriter raw = new StringWriter();
98 CSVPrinter printer = new CSVPrinter(raw);
99 String content[] = {"foo", "bar\nbaz\n"};
100 printer.writeRecord(content);
101 assertEquals("foo,\"bar\nbaz\n\"\n", raw.toString());
102 }
103 }