KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > CSVTest


1 /*
2  * CSV Regression test.
3  * Copyright (C) 2001,2002 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18 package com.Ostermiller.util;
19
20 import java.io.*;
21
22 /**
23  * Regression test for CSV.
24  * More information about this class is available from <a target="_top" HREF=
25  * "http://ostermiller.org/utils/CSVLexer.html">ostermiller.org</a>.
26  *
27  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
28  * @since ostermillerutils 1.00.00
29  */

30 class CSVTest {
31     public static void main (String JavaDoc[] args) throws IOException{
32         File f = new File("CSVTest.txt");
33         FileOutputStream out = new FileOutputStream(f);
34         CSVPrinter csvOut = new CSVPrinter(out, '#');
35
36         csvOut.printlnComment("Comma Separated Value Test");
37         csvOut.println();
38         csvOut.printlnComment("Five Cities");
39         csvOut.println(new String JavaDoc[] {
40             "Boston",
41             "San Francisco",
42             "New York",
43             "Chicago",
44             "Houston",
45         });
46         csvOut.println();
47         csvOut.println(""); // an empty value on a line by itself.
48
csvOut.println(new String JavaDoc[] {
49             "Two\nTokens",
50             "On the\nSame Line"
51         });
52         csvOut.printlnComment("A two line comment\njust to see that it works");
53
54         CSVParser shredder = new CSVParser(new StraightStreamReader(new FileInputStream(f)));
55         shredder.setCommentStart("#;!");
56         shredder.setEscapes("nrtf", "\n\r\t\f");
57         String JavaDoc t;
58         while ((t = shredder.nextValue()) != null) {
59             if (t.length() == 1){
60                 System.out.println("" + shredder.lastLineNumber() + " " + (int)(t.charAt(0)));
61             } else {
62                 System.out.println("" + shredder.lastLineNumber() + " " + t);
63             }
64         }
65
66         try {
67             String JavaDoc normalInput = ",\"a\",\",\t'\\\"\"";
68             String JavaDoc[][] normalOutput = new String JavaDoc[][]{{"", "a", ",\t'\""}};
69             shredder = new CSVParser(new StringReader(normalInput));
70             compare("normal", normalOutput, shredder.getAllValues());
71
72             String JavaDoc tabInput = "\t\"a\"\t\",\t'\\\"\"";
73             shredder = new CSVParser(new StringReader(tabInput));
74             shredder.changeDelimiter('\t');
75             compare("tabs", normalOutput, shredder.getAllValues());
76
77             String JavaDoc aposInput = ",'a',',\t\\'\"'";
78             shredder = new CSVParser(new StringReader(aposInput));
79             shredder.changeQuote('\'');
80             compare("apostrophes", normalOutput, shredder.getAllValues());
81
82             String JavaDoc swappedInput = "\",a,\",\\,\t'\\\",";
83             shredder = new CSVParser(new StringReader(swappedInput));
84             shredder.changeDelimiter('\t');
85             shredder.changeQuote(',');
86             shredder.changeDelimiter('"');
87             compare("commas and quotes swapped", normalOutput, shredder.getAllValues());
88
89             normalInput = "\"test\\\\\",test";
90             normalOutput = new String JavaDoc[][]{{"test\\", "test"}};
91             shredder = new CSVParser(new StringReader(normalInput));
92             compare("backslash at end of quoted", normalOutput, shredder.getAllValues());
93         } catch (Exception JavaDoc x){
94             System.err.println(x.getMessage());
95             System.exit(1);
96         }
97
98     }
99
100     private static void compare(String JavaDoc testName, String JavaDoc[][] a, String JavaDoc[][] b){
101         if (a.length != b.length) {
102             System.err.println(testName + ": unexpected number of lines");
103             System.exit(1);
104         }
105         for(int i=0; i<a.length; i++){
106             if (a[i].length != b[i].length) {
107                 System.err.println(testName + ": unexpected number of values in line: " + b[i].length);
108                 System.exit(1);
109             }
110             for (int j=0; j<a[i].length; j++){
111                 if (!a[i][j].equals(b[i][j])) {
112                     System.err.println(a[i][j]);
113                     System.err.println(b[i][j]);
114                     System.err.println(testName + ": values do not match.");
115                     System.exit(1);
116                 }
117             }
118         }
119
120     }
121 }
122
Popular Tags