1   package com.generationjava.io;
2   
3   import java.io.IOException;
4   import java.io.StringWriter;
5   
6   import junit.framework.TestCase;
7   
8   public class CsvWriterTest extends TestCase {
9   
10      public CsvWriterTest(String name) {
11          super(name);
12      }
13  
14      public void testWriteFields() {
15          try {
16              StringWriter sw = new StringWriter();
17              CsvWriter csv = new CsvWriter(sw);
18              csv.setBlockDelimiter('\n');
19              csv.setFieldDelimiter(',');
20              csv.writeField("1");
21              csv.writeField("2");
22              csv.writeField("3");
23              csv.writeField("4");
24              csv.writeField("5");
25              csv.endBlock();
26              csv.writeField("6");
27              csv.writeField("7");
28              csv.writeField("8");
29              csv.writeField("9");
30              csv.writeField("0");
31              csv.endBlock();
32              csv.close();
33              assertEquals("Does not write simple csv file out correctly. ", 
34                           "1,2,3,4,5\n6,7,8,9,0\n",
35                           sw.toString() );
36              assertEquals("Does not return the right Writer", sw, csv.getWriter() );
37          } catch(IOException ioe) {
38              fail("IOException should not have been thrown. ");
39          }
40      }
41  
42      public void testWriteObjectFields() {
43          try {
44              StringWriter sw = new StringWriter();
45              CsvWriter csv = new CsvWriter(sw);
46              csv.setBlockDelimiter('\n');
47              csv.setFieldDelimiter(',');
48              csv.writeField(new Integer(1));
49              csv.writeField(new Integer(2));
50              csv.writeField(new Long(3));
51              csv.writeField(new Long(4));
52              csv.writeField(new Short((short)5));
53              csv.endBlock();
54              csv.writeField(new Short((short)6));
55              csv.writeField(new Byte((byte)7));
56              csv.writeField(new Byte((byte)8));
57              csv.writeField(new Integer(9));
58              csv.writeField(new Integer(0));
59              csv.endBlock();
60              csv.close();
61              assertEquals("Does not write simple csv file from objects out correctly. ", 
62                           "1,2,3,4,5\n6,7,8,9,0\n",
63                           sw.toString() );
64              assertEquals("Does not return the right Writer", sw, csv.getWriter() );
65          } catch(IOException ioe) {
66              fail("IOException should not have been thrown. ");
67          }
68      }
69  
70      public void testWriteLines() {
71          try {
72              StringWriter sw = new StringWriter();
73              CsvWriter csv = new CsvWriter(sw);
74              String[] strs = new String[] { "1", "2", "3", "4", "5" };
75              csv.writeLine(strs);
76              strs = new String[] { "6", "7", "8", "9", "0" };
77              csv.writeLine(strs);
78              csv.close();
79              assertEquals("Does not write simple csv file out correctly. ", 
80                           "1,2,3,4,5\n6,7,8,9,0\n",
81                           sw.toString() );
82          } catch(IOException ioe) {
83              fail("IOException should not have been thrown. ");
84          }
85      }
86  
87      public void testWriteObjectLines() {
88          try {
89              StringWriter sw = new StringWriter();
90              CsvWriter csv = new CsvWriter(sw);
91              Object[] objs = new Object[] { new Long(1), new Integer(2), new Byte((byte)3), "4", new Short((short)5) };
92              csv.writeLine(objs);
93              objs = new String[] { "6", "7", "8", "9", "0" };
94              objs = new Object[] { new Long(6), new Integer(7), new Byte((byte)8), "9", new Short((short)0) };
95              csv.writeLine(objs);
96              csv.close();
97              assertEquals("Does not write lines of objects to csv file correctly. ", 
98                           "1,2,3,4,5\n6,7,8,9,0\n",
99                           sw.toString() );
100         } catch(IOException ioe) {
101             fail("IOException should not have been thrown. ");
102         }
103     }
104 
105     public void testEscapeQuotes() {
106         try {
107             StringWriter sw = new StringWriter();
108             CsvWriter csv = new CsvWriter(sw);
109             String[] strs = new String[] { "That's \"Mr. Monkey\", to you", "Fool" };
110             csv.writeLine(strs);
111             strs = new String[] { "Not \"Monkey\"", "Fool" };
112             csv.writeLine(strs);
113             csv.close();
114             assertEquals("Does not write simple csv file out correctly. ", 
115                          "\"That's \"\"Mr. Monkey\"\", to you\",Fool\n" +
116                          "Not \"Monkey\",Fool\n",
117                          sw.toString() );
118         } catch(IOException ioe) {
119             fail("IOException should not have been thrown. ");
120         }
121     }
122 
123 }