KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CSV Regression test.
3  * Copyright (C) 2001-2004 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/CSV.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 CSVTests {
31     public static void main(String JavaDoc[] args){
32         try {
33             StringWriter sw = new StringWriter();
34             CSVPrinter csvOut = new CSVPrinter(sw, '#');
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 StringReader(sw.toString()));
55             shredder.setCommentStart("#;!");
56             shredder.setEscapes("nrtf", "\n\r\t\f");
57             String JavaDoc t;
58             compare(shredder.nextValue(), shredder.lastLineNumber(), "Boston", 4);
59             compare(shredder.nextValue(), shredder.lastLineNumber(), "San Francisco", 4);
60             compare(shredder.nextValue(), shredder.lastLineNumber(), "New York", 4);
61             compare(shredder.nextValue(), shredder.lastLineNumber(), "Chicago", 4);
62             compare(shredder.nextValue(), shredder.lastLineNumber(), "Houston", 4);
63             compare(shredder.nextValue(), shredder.lastLineNumber(), "", 6);
64             compare(shredder.nextValue(), shredder.lastLineNumber(), "Two\nTokens", 7);
65             compare(shredder.nextValue(), shredder.lastLineNumber(), "On the\nSame Line", 7);
66             compare(shredder.nextValue(), shredder.lastLineNumber(), null, 9);
67
68             String JavaDoc normalInput = ",\"a\",\",\t'\\\"\"";
69             String JavaDoc[][] normalOutput = new String JavaDoc[][]{{"", "a", ",\t'\""}};
70             shredder = new CSVParser(new StringReader(normalInput));
71             compare("normal", normalOutput, shredder.getAllValues());
72
73             String JavaDoc tabInput = "\t\"a\"\t\",\t'\\\"\"";
74             shredder = new CSVParser(new StringReader(tabInput));
75             shredder.changeDelimiter('\t');
76             compare("tabs", normalOutput, shredder.getAllValues());
77
78             String JavaDoc aposInput = ",'a',',\t\\'\"'";
79             shredder = new CSVParser(new StringReader(aposInput));
80             shredder.changeQuote('\'');
81             compare("apostrophes", normalOutput, shredder.getAllValues());
82
83             String JavaDoc swappedInput = "\",a,\",\\,\t'\\\",";
84             shredder = new CSVParser(new StringReader(swappedInput));
85             shredder.changeDelimiter('\t');
86             shredder.changeQuote(',');
87             shredder.changeDelimiter('"');
88             compare("commas and quotes swapped", normalOutput, shredder.getAllValues());
89
90             normalInput = "\"test\\\\\",test";
91             normalOutput = new String JavaDoc[][]{{"test\\", "test"}};
92             shredder = new CSVParser(new StringReader(normalInput));
93             compare("backslash at end of quoted", normalOutput, shredder.getAllValues());
94
95             normalInput = "field1,field2 , field3,field4 , field5 ,field6";
96             normalOutput = new String JavaDoc[][]{{"field1", "field2", "field3", "field4", "field5", "field6"}};
97             shredder = new CSVParser(new StringReader(normalInput));
98             compare("white space around fields", normalOutput, shredder.getAllValues());
99
100             normalInput = ",field2,, ,field5,";
101             normalOutput = new String JavaDoc[][]{{"", "field2", "", "", "field5", ""}};
102             shredder = new CSVParser(new StringReader(normalInput));
103             compare("empty fields", normalOutput, shredder.getAllValues());
104
105             normalInput = "1,to,tre,four,five5,sixsix";
106             normalOutput = new String JavaDoc[][]{{"1", "to", "tre", "four", "five5", "sixsix"}};
107             shredder = new CSVParser(new StringReader(normalInput));
108             compare("various lengths", normalOutput, shredder.getAllValues());
109
110             normalInput = "!comment\n !field1\n;comment\n ;field2\n#comment\n #field3";
111             normalOutput = new String JavaDoc[][]{{"!field1"},{";field2"},{"#field3"}};
112             shredder = new CSVParser(new StringReader(normalInput));
113             shredder.setCommentStart("#;!");
114             compare("comment must start at beginning of line", normalOutput, shredder.getAllValues());
115
116         } catch (Exception JavaDoc x){
117             System.err.println(x.getMessage());
118             x.printStackTrace();
119             System.exit(1);
120         }
121         System.exit(0);
122     }
123
124     private static void compare(String JavaDoc value, int line, String JavaDoc expectedValue, int expectedLine) throws Exception JavaDoc {
125         if (line != expectedLine) {
126             throw new Exception JavaDoc("Line numbers do not match");
127         }
128         if (expectedValue == null && value == null){
129             return;
130         }
131         if (!value.equals(expectedValue)){
132             throw new Exception JavaDoc("Value and expected value do not match");
133         }
134     }
135
136     private static void compare(String JavaDoc testName, String JavaDoc[][] a, String JavaDoc[][] b) throws Exception JavaDoc {
137         if (a.length != b.length) {
138             throw new Exception JavaDoc(testName + ": unexpected number of lines " + a.length + " found " + b.length + " expected");
139         }
140         for(int i=0; i<a.length; i++){
141             if (a[i].length != b[i].length) {
142                 throw new Exception JavaDoc(testName + ": unexpected number of values in line: " + b[i].length);
143             }
144             for (int j=0; j<a[i].length; j++){
145                 if (!a[i][j].equals(b[i][j])) {
146                     System.err.println(a[i][j]);
147                     System.err.println(b[i][j]);
148                     throw new Exception JavaDoc(testName + ": values do not match.");
149                 }
150             }
151         }
152     }
153 }
154
Popular Tags