1 18 package com.Ostermiller.util; 19 20 import java.io.*; 21 22 30 class CSVTests { 31 public static void main(String [] 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 [] { 40 "Boston", 41 "San Francisco", 42 "New York", 43 "Chicago", 44 "Houston", 45 }); 46 csvOut.println(); 47 csvOut.println(""); csvOut.println(new String [] { 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 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 normalInput = ",\"a\",\",\t'\\\"\""; 69 String [][] normalOutput = new String [][]{{"", "a", ",\t'\""}}; 70 shredder = new CSVParser(new StringReader(normalInput)); 71 compare("normal", normalOutput, shredder.getAllValues()); 72 73 String 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 aposInput = ",'a',',\t\\'\"'"; 79 shredder = new CSVParser(new StringReader(aposInput)); 80 shredder.changeQuote('\''); 81 compare("apostrophes", normalOutput, shredder.getAllValues()); 82 83 String 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 [][]{{"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 [][]{{"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 [][]{{"", "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 [][]{{"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 [][]{{"!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 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 value, int line, String expectedValue, int expectedLine) throws Exception { 125 if (line != expectedLine) { 126 throw new Exception ("Line numbers do not match"); 127 } 128 if (expectedValue == null && value == null){ 129 return; 130 } 131 if (!value.equals(expectedValue)){ 132 throw new Exception ("Value and expected value do not match"); 133 } 134 } 135 136 private static void compare(String testName, String [][] a, String [][] b) throws Exception { 137 if (a.length != b.length) { 138 throw new Exception (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 (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 (testName + ": values do not match."); 149 } 150 } 151 } 152 } 153 } 154 | Popular Tags |