1 21 22 package com.Ostermiller.util; 23 24 import java.io.*; 25 26 33 class LabeledCSVParserTests { 34 35 public static void main(String [] args){ 36 try { 37 test01(); 38 test02(); 39 test03(); 40 } catch (Exception x){ 41 x.printStackTrace(); 42 System.exit(1); 43 } 44 } 45 46 private static void test01() throws Exception { 47 LabeledCSVParser parse = new LabeledCSVParser( 49 new CSVParser( 50 new StringReader( 51 "" 52 ) 53 ) 54 ); 55 56 if (parse.getLine() != null){ 57 throw new Exception ("Expected null from getLine()"); 58 } 59 if (parse.getLabels() != null){ 60 throw new Exception ("Expected null from getLabels()"); 61 } 62 63 if (parse.getLastLineNumber() != -1){ 64 throw new Exception ("Expected -1 from getLastLineNumber()"); 65 } 66 parse.close(); 67 } 68 69 private static void test02() throws Exception { 70 LabeledCSVParser parse = new LabeledCSVParser( 72 new CSVParser( 73 new StringReader( 74 "FIELD01,FIELD02,FIELD03" 75 ) 76 ) 77 ); 78 if (parse.getLine() != null){ 79 throw new Exception ("Expected null from getLine()"); 80 } 81 String [] labels = parse.getLabels(); 82 if (labels.length != 3 || !"FIELD01".equals(labels[0]) || !"FIELD02".equals(labels[1]) || !"FIELD03".equals(labels[2])){ 83 throw new Exception ("Didn't get expected labels."); 84 } 85 if (parse.getLastLineNumber() != -1){ 86 throw new Exception ("Expected -1 from getLastLineNumber()"); 87 } 88 parse.close(); 89 } 90 91 private static void test03() throws Exception { 92 LabeledCSVParser parse = new LabeledCSVParser( 94 new CSVParser( 95 new StringReader( 96 "FIELD01,FIELD02,FIELD03\n" + 97 "9.23,\"FOO\",\"BAR\"\n" + 98 "10.5,\"BAZ\",\"FOO2\"\n" 99 ) 100 ) 101 ); 102 String [] labels = parse.getLabels(); 103 if (labels.length != 3 || !"FIELD01".equals(labels[0]) || !"FIELD02".equals(labels[1]) || !"FIELD03".equals(labels[2])){ 104 throw new Exception ("Didn't get expected labels."); 105 } 106 if (parse.getLabelIdx("FIELD01") != 0){ 107 throw new Exception ("FIELD01 expected index of zero"); 108 } 109 if (parse.getLabelIdx("FIELD02") != 1){ 110 throw new Exception ("FIELD02 expected index of one"); 111 } 112 if (parse.getLabelIdx("FIELD03") != 2){ 113 throw new Exception ("FIELD03 expected index of two"); 114 } 115 if (parse.getLabelIdx("FIELD04") != -1){ 116 throw new Exception ("FIELD04 expected not to be present"); 117 } 118 if (parse.getValueByLabel("FIELD01") != null){ 119 throw new Exception ("Line not yet read, expected value of FIELD01 to be null."); 120 } 121 122 String [] line = parse.getLine(); 123 if (line == null){ 124 throw new Exception ("getLine() null on line 1"); 125 } 126 if (parse.getLastLineNumber() != 1){ 127 throw new Exception ("Expected 1 from getLastLineNumber()"); 128 } 129 if (line.length != 3 || !"9.23".equals(line[0]) || !"FOO".equals(line[1]) || !"BAR".equals(line[2])){ 130 throw new Exception ("Didn't get expected line."); 131 } 132 if (!"9.23".equals(parse.getValueByLabel("FIELD01"))){ 133 throw new Exception ("FIELD01 expected to have value 9.23."); 134 } 135 136 String value = parse.nextValue(); 137 if (!"10.5".equals(value)){ 138 throw new Exception ("Expected nextValue to return 10.5."); 139 } 140 try { 141 parse.getValueByLabel("FIELD01"); 142 throw new Exception ("IllegalStateException expected"); 143 } catch (IllegalStateException iex){ 144 } 145 if (parse.getLastLineNumber() != 2){ 146 throw new Exception ("Expected 2 from getLastLineNumber()"); 147 } 148 line = parse.getLine(); 149 if (line == null){ 150 throw new Exception ("getLine() null on line 2"); 151 } 152 if (parse.getLastLineNumber() != 2){ 153 throw new Exception ("Expected 2 from getLastLineNumber()"); 154 } 155 if (line.length != 2 || !"BAZ".equals(line[0]) || !"FOO2".equals(line[1])){ 156 throw new Exception ("Didn't get expected line."); 157 } 158 try { 159 parse.getValueByLabel("FIELD01"); 160 throw new Exception ("IllegalStateException expected"); 161 } catch (IllegalStateException iex){ 162 } 163 164 if (parse.getLine() != null){ 165 throw new Exception ("Expected null from getLine()"); 166 } 167 parse.close(); 168 } 169 } 170 | Popular Tags |