001: /*
002: * CSV Regression test.
003: * Copyright (C) 2001-2004 Stephen Ostermiller
004: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * See COPYING.TXT for details.
017: */
018: package com.Ostermiller.util;
019:
020: import java.io.*;
021:
022: /**
023: * Regression test for CSV.
024: * More information about this class is available from <a target="_top" href=
025: * "http://ostermiller.org/utils/CSV.html">ostermiller.org</a>.
026: *
027: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
028: * @since ostermillerutils 1.00.00
029: */
030: class CSVTests {
031:
032: /**
033: * Main method for running tests
034: * @param args Command line arguments (ignored)
035: */
036: public static void main(String[] args) {
037: try {
038: StringWriter sw = new StringWriter();
039: CSVPrinter csvOut = new CSVPrinter(sw, '#');
040: csvOut.setLineEnding("\r");
041:
042: csvOut.printlnComment("Comma Separated Value Test");
043: csvOut.println();
044: csvOut.printlnComment("Five Cities");
045: csvOut.println(new String[] { "Boston", "San Francisco",
046: "New York", "Chicago", "Houston", });
047: csvOut.println();
048: csvOut.println(""); // an empty value on a line by itself.
049: csvOut.println(new String[] { "Two\nTokens",
050: "On the\nSame Line" });
051: csvOut
052: .printlnComment("A two line comment\n just to see that it works");
053:
054: CSVParser shredder = new CSVParser(new StringReader(sw
055: .toString()));
056: shredder.setCommentStart("#;!");
057: shredder.setEscapes("nrtf", "\n\r\t\f");
058: compare(shredder.nextValue(), shredder.lastLineNumber(),
059: "Boston", 4);
060: compare(shredder.nextValue(), shredder.lastLineNumber(),
061: "San Francisco", 4);
062: compare(shredder.nextValue(), shredder.lastLineNumber(),
063: "New York", 4);
064: compare(shredder.nextValue(), shredder.lastLineNumber(),
065: "Chicago", 4);
066: compare(shredder.nextValue(), shredder.lastLineNumber(),
067: "Houston", 4);
068: compare(shredder.nextValue(), shredder.lastLineNumber(),
069: "", 6);
070: compare(shredder.nextValue(), shredder.lastLineNumber(),
071: "Two\nTokens", 7);
072: compare(shredder.nextValue(), shredder.lastLineNumber(),
073: "On the\nSame Line", 7);
074: compare(shredder.nextValue(), shredder.lastLineNumber(),
075: null, 9);
076:
077: String normalInput = ",\"a\",\",\t'\\\"\"";
078: String[][] normalOutput = new String[][] { { "", "a",
079: ",\t'\"" } };
080: shredder = new CSVParser(new StringReader(normalInput));
081: compare("normal", normalOutput, shredder.getAllValues());
082:
083: String tabInput = "\t\"a\"\t\",\t'\\\"\"";
084: shredder = new CSVParser(new StringReader(tabInput));
085: shredder.changeDelimiter('\t');
086: compare("tabs", normalOutput, shredder.getAllValues());
087:
088: String aposInput = ",'a',',\t\\'\"'";
089: shredder = new CSVParser(new StringReader(aposInput));
090: shredder.changeQuote('\'');
091: compare("apostrophes", normalOutput, shredder
092: .getAllValues());
093:
094: String swappedInput = "\",a,\",\\,\t'\\\",";
095: shredder = new CSVParser(new StringReader(swappedInput));
096: shredder.changeDelimiter('\t');
097: shredder.changeQuote(',');
098: shredder.changeDelimiter('"');
099: compare("commas and quotes swapped", normalOutput, shredder
100: .getAllValues());
101:
102: normalInput = "\"test\\\\\",test";
103: normalOutput = new String[][] { { "test\\", "test" } };
104: shredder = new CSVParser(new StringReader(normalInput));
105: compare("backslash at end of quoted", normalOutput,
106: shredder.getAllValues());
107:
108: normalInput = "field1,field2 , field3,field4 , field5 ,field6";
109: normalOutput = new String[][] { { "field1", "field2",
110: "field3", "field4", "field5", "field6" } };
111: shredder = new CSVParser(new StringReader(normalInput));
112: compare("white space around fields", normalOutput, shredder
113: .getAllValues());
114:
115: normalInput = ",field2,, ,field5,";
116: normalOutput = new String[][] { { "", "field2", "", "",
117: "field5", "" } };
118: shredder = new CSVParser(new StringReader(normalInput));
119: compare("empty fields", normalOutput, shredder
120: .getAllValues());
121:
122: normalInput = "1,to,tre,four,five5,sixsix";
123: normalOutput = new String[][] { { "1", "to", "tre", "four",
124: "five5", "sixsix" } };
125: shredder = new CSVParser(new StringReader(normalInput));
126: compare("various lengths", normalOutput, shredder
127: .getAllValues());
128:
129: normalInput = "!comment\n !field1\n;comment\n ;field2\n#comment\n #field3";
130: normalOutput = new String[][] { { "!field1" },
131: { ";field2" }, { "#field3" } };
132: shredder = new CSVParser(new StringReader(normalInput));
133: shredder.setCommentStart("#;!");
134: compare("comment must start at beginning of line",
135: normalOutput, shredder.getAllValues());
136:
137: } catch (Exception x) {
138: System.err.println(x.getMessage());
139: x.printStackTrace();
140: System.exit(1);
141: }
142: System.exit(0);
143: }
144:
145: private static void compare(String value, int line,
146: String expectedValue, int expectedLine) throws Exception {
147: if (line != expectedLine) {
148: throw new Exception("Line numbers do not match");
149: }
150: if (expectedValue == null && value == null) {
151: return;
152: }
153: if (!value.equals(expectedValue)) {
154: throw new Exception("Value and expected value do not match");
155: }
156: }
157:
158: private static void compare(String testName, String[][] a,
159: String[][] b) throws Exception {
160: if (a.length != b.length) {
161: throw new Exception(testName
162: + ": unexpected number of lines " + a.length
163: + " found " + b.length + " expected");
164: }
165: for (int i = 0; i < a.length; i++) {
166: if (a[i].length != b[i].length) {
167: throw new Exception(testName
168: + ": unexpected number of values in line: "
169: + b[i].length);
170: }
171: for (int j = 0; j < a[i].length; j++) {
172: if (!a[i][j].equals(b[i][j])) {
173: System.err.println(a[i][j]);
174: System.err.println(b[i][j]);
175: throw new Exception(testName
176: + ": values do not match.");
177: }
178: }
179: }
180: }
181: }
|