KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Tests reading files in comma separated value format with a fist line of labels.
3  *
4  * Copyright (C) 2004 Campbell, Allen T. <allenc28@yahoo.com>
5  *
6  * Copyright (C) 2004 Stephen Ostermiller
7  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * See COPYING.TXT for details.
20  */

21
22 package com.Ostermiller.util;
23
24 import java.io.*;
25
26 /**
27  * Tests reading files in comma separated value format with a fist line of labels.
28  *
29  * @author Campbell, Allen T. <allenc28@yahoo.com>
30  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
31  * @since ostermillerutils 1.03.00
32  */

33 class LabeledCSVParserTests {
34
35     public static void main(String JavaDoc[] args){
36         try {
37             test01();
38             test02();
39             test03();
40         } catch (Exception JavaDoc x){
41             x.printStackTrace();
42             System.exit(1);
43         }
44     }
45
46     private static void test01() throws Exception JavaDoc {
47         // test01: empty input file
48
LabeledCSVParser parse = new LabeledCSVParser(
49             new CSVParser(
50                 new StringReader(
51                     ""
52                 )
53             )
54         );
55
56         if (parse.getLine() != null){
57             throw new Exception JavaDoc("Expected null from getLine()");
58         }
59         if (parse.getLabels() != null){
60             throw new Exception JavaDoc("Expected null from getLabels()");
61         }
62
63         if (parse.getLastLineNumber() != -1){
64             throw new Exception JavaDoc("Expected -1 from getLastLineNumber()");
65         }
66         parse.close();
67     }
68
69     private static void test02() throws Exception JavaDoc {
70         // test02: input has only labels
71
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 JavaDoc("Expected null from getLine()");
80         }
81         String JavaDoc[] labels = parse.getLabels();
82         if (labels.length != 3 || !"FIELD01".equals(labels[0]) || !"FIELD02".equals(labels[1]) || !"FIELD03".equals(labels[2])){
83             throw new Exception JavaDoc("Didn't get expected labels.");
84         }
85         if (parse.getLastLineNumber() != -1){
86             throw new Exception JavaDoc("Expected -1 from getLastLineNumber()");
87         }
88         parse.close();
89     }
90
91     private static void test03() throws Exception JavaDoc {
92         // test03: labels + data
93
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 JavaDoc[] labels = parse.getLabels();
103         if (labels.length != 3 || !"FIELD01".equals(labels[0]) || !"FIELD02".equals(labels[1]) || !"FIELD03".equals(labels[2])){
104             throw new Exception JavaDoc("Didn't get expected labels.");
105         }
106         if (parse.getLabelIdx("FIELD01") != 0){
107             throw new Exception JavaDoc("FIELD01 expected index of zero");
108         }
109         if (parse.getLabelIdx("FIELD02") != 1){
110             throw new Exception JavaDoc("FIELD02 expected index of one");
111         }
112         if (parse.getLabelIdx("FIELD03") != 2){
113             throw new Exception JavaDoc("FIELD03 expected index of two");
114         }
115         if (parse.getLabelIdx("FIELD04") != -1){
116             throw new Exception JavaDoc("FIELD04 expected not to be present");
117         }
118         if (parse.getValueByLabel("FIELD01") != null){
119             throw new Exception JavaDoc("Line not yet read, expected value of FIELD01 to be null.");
120         }
121
122         String JavaDoc[] line = parse.getLine();
123         if (line == null){
124             throw new Exception JavaDoc("getLine() null on line 1");
125         }
126         if (parse.getLastLineNumber() != 1){
127             throw new Exception JavaDoc("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 JavaDoc("Didn't get expected line.");
131         }
132         if (!"9.23".equals(parse.getValueByLabel("FIELD01"))){
133             throw new Exception JavaDoc("FIELD01 expected to have value 9.23.");
134         }
135
136         String JavaDoc value = parse.nextValue();
137         if (!"10.5".equals(value)){
138             throw new Exception JavaDoc("Expected nextValue to return 10.5.");
139         }
140         try {
141             parse.getValueByLabel("FIELD01");
142             throw new Exception JavaDoc("IllegalStateException expected");
143         } catch (IllegalStateException JavaDoc iex){
144         }
145         if (parse.getLastLineNumber() != 2){
146             throw new Exception JavaDoc("Expected 2 from getLastLineNumber()");
147         }
148         line = parse.getLine();
149         if (line == null){
150             throw new Exception JavaDoc("getLine() null on line 2");
151         }
152         if (parse.getLastLineNumber() != 2){
153             throw new Exception JavaDoc("Expected 2 from getLastLineNumber()");
154         }
155         if (line.length != 2 || !"BAZ".equals(line[0]) || !"FOO2".equals(line[1])){
156             throw new Exception JavaDoc("Didn't get expected line.");
157         }
158         try {
159             parse.getValueByLabel("FIELD01");
160             throw new Exception JavaDoc("IllegalStateException expected");
161         } catch (IllegalStateException JavaDoc iex){
162         }
163
164         if (parse.getLine() != null){
165             throw new Exception JavaDoc("Expected null from getLine()");
166         }
167         parse.close();
168     }
169 }
170
Popular Tags