001: /*
002: * Tests reading files in comma separated value format with a fist line of labels.
003: *
004: * Copyright (C) 2004 Campbell, Allen T. <allenc28@yahoo.com>
005: *
006: * Copyright (C) 2004 Stephen Ostermiller
007: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
008: *
009: * This program is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * See COPYING.TXT for details.
020: */
021:
022: package com.Ostermiller.util;
023:
024: import java.io.*;
025:
026: /**
027: * Tests reading files in comma separated value format with a fist line of labels.
028: *
029: * @author Campbell, Allen T. <allenc28@yahoo.com>
030: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
031: * @since ostermillerutils 1.03.00
032: */
033: class LabeledCSVParserTests {
034:
035: /**
036: * Main method for running test
037: * @param args command line arguments (ignored)
038: */
039: public static void main(String[] args) {
040: try {
041: test01();
042: test02();
043: test03();
044: } catch (Exception x) {
045: x.printStackTrace();
046: System.exit(1);
047: }
048: }
049:
050: private static void test01() throws Exception {
051: // test01: empty input file
052: LabeledCSVParser parse = new LabeledCSVParser(new CSVParser(
053: new StringReader("")));
054:
055: if (parse.getLine() != null) {
056: throw new Exception("Expected null from getLine()");
057: }
058: if (parse.getLabels() != null) {
059: throw new Exception("Expected null from getLabels()");
060: }
061:
062: if (parse.getLastLineNumber() != -1) {
063: throw new Exception("Expected -1 from getLastLineNumber()");
064: }
065: parse.close();
066: }
067:
068: private static void test02() throws Exception {
069: // test02: input has only labels
070: LabeledCSVParser parse = new LabeledCSVParser(new CSVParser(
071: new StringReader("FIELD01,FIELD02,FIELD03")));
072: if (parse.getLine() != null) {
073: throw new Exception("Expected null from getLine()");
074: }
075: String[] labels = parse.getLabels();
076: if (labels.length != 3 || !"FIELD01".equals(labels[0])
077: || !"FIELD02".equals(labels[1])
078: || !"FIELD03".equals(labels[2])) {
079: throw new Exception("Didn't get expected labels.");
080: }
081: if (parse.getLastLineNumber() != -1) {
082: throw new Exception("Expected -1 from getLastLineNumber()");
083: }
084: parse.close();
085: }
086:
087: private static void test03() throws Exception {
088: // test03: labels + data
089: LabeledCSVParser parse = new LabeledCSVParser(new CSVParser(
090: new StringReader("FIELD01,FIELD02,FIELD03\n"
091: + "9.23,\"FOO\",\"BAR\"\n"
092: + "10.5,\"BAZ\",\"FOO2\"\n")));
093: String[] labels = parse.getLabels();
094: if (labels.length != 3 || !"FIELD01".equals(labels[0])
095: || !"FIELD02".equals(labels[1])
096: || !"FIELD03".equals(labels[2])) {
097: throw new Exception("Didn't get expected labels.");
098: }
099: if (parse.getLabelIdx("FIELD01") != 0) {
100: throw new Exception("FIELD01 expected index of zero");
101: }
102: if (parse.getLabelIdx("FIELD02") != 1) {
103: throw new Exception("FIELD02 expected index of one");
104: }
105: if (parse.getLabelIdx("FIELD03") != 2) {
106: throw new Exception("FIELD03 expected index of two");
107: }
108: if (parse.getLabelIdx("FIELD04") != -1) {
109: throw new Exception("FIELD04 expected not to be present");
110: }
111: if (parse.getValueByLabel("FIELD01") != null) {
112: throw new Exception(
113: "Line not yet read, expected value of FIELD01 to be null.");
114: }
115:
116: String[] line = parse.getLine();
117: if (line == null) {
118: throw new Exception("getLine() null on line 1");
119: }
120: if (parse.getLastLineNumber() != 1) {
121: throw new Exception("Expected 1 from getLastLineNumber()");
122: }
123: if (line.length != 3 || !"9.23".equals(line[0])
124: || !"FOO".equals(line[1]) || !"BAR".equals(line[2])) {
125: throw new Exception("Didn't get expected line.");
126: }
127: if (!"9.23".equals(parse.getValueByLabel("FIELD01"))) {
128: throw new Exception("FIELD01 expected to have value 9.23.");
129: }
130:
131: String value = parse.nextValue();
132: if (!"10.5".equals(value)) {
133: throw new Exception("Expected nextValue to return 10.5.");
134: }
135: try {
136: parse.getValueByLabel("FIELD01");
137: throw new Exception("IllegalStateException expected");
138: } catch (IllegalStateException iex) {
139: // expected
140: }
141: if (parse.getLastLineNumber() != 2) {
142: throw new Exception("Expected 2 from getLastLineNumber()");
143: }
144: line = parse.getLine();
145: if (line == null) {
146: throw new Exception("getLine() null on line 2");
147: }
148: if (parse.getLastLineNumber() != 2) {
149: throw new Exception("Expected 2 from getLastLineNumber()");
150: }
151: if (line.length != 2 || !"BAZ".equals(line[0])
152: || !"FOO2".equals(line[1])) {
153: throw new Exception("Didn't get expected line.");
154: }
155: try {
156: parse.getValueByLabel("FIELD01");
157: throw new Exception("IllegalStateException expected");
158: } catch (IllegalStateException iex) {
159: // expected
160: }
161:
162: if (parse.getLine() != null) {
163: throw new Exception("Expected null from getLine()");
164: }
165: parse.close();
166: }
167: }
|