KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > test > DelimParserTest


1 package jimm.datavision.test;
2 import jimm.datavision.source.charsep.DelimParser;
3 import jimm.util.StringUtils;
4 import java.io.*;
5 import java.util.List JavaDoc;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8 import junit.framework.Test;
9
10 /**
11  * Compares CSV file input with "answers" file. The answers file is
12  * tab-delimited and lines that end with a backslash are continued
13  * on the next line.
14  *
15  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
16  */

17 public class DelimParserTest extends TestCase {
18
19 protected static final String JavaDoc TEST_INPUT =
20     AllTests.testDataFile("delim_parser_in.txt");
21 protected static final String JavaDoc TEST_ANSWERS =
22     AllTests.testDataFile("delim_parser_answers.txt");
23
24 public static Test suite() {
25     return new TestSuite(DelimParserTest.class);
26 }
27
28 public DelimParserTest(String JavaDoc name) {
29     super(name);
30 }
31
32 protected void sepTest(List JavaDoc answer, DelimParser parser) {
33     List JavaDoc parsed = null;
34     try {
35     parsed = parser.parse();
36     }
37     catch (IOException e) {
38     fail(e.toString());
39     }
40     assertEquals(answer, parsed);
41 }
42
43 public void testParser() {
44     BufferedReader in = null, answers = null;
45     try {
46     in = new BufferedReader(new FileReader(TEST_INPUT));
47     answers = new BufferedReader(new FileReader(TEST_ANSWERS));
48     DelimParser parser = new DelimParser(in, ',');
49
50     List JavaDoc answer;
51     while ((answer = getNextAnswer(answers)) != null)
52         sepTest(answer, parser);
53     sepTest(null, parser);
54     }
55     catch (Exception JavaDoc e) {
56     fail(e.toString());
57     }
58     finally {
59     try {
60         if (answers != null) answers.close();
61         if (in != null) in.close();
62     }
63     catch (Exception JavaDoc e2) {}
64     }
65 }
66
67 protected List JavaDoc getNextAnswer(BufferedReader in) throws IOException {
68     String JavaDoc line = in.readLine();
69     if (line == null)
70     return null;
71
72     while (line.endsWith("\\")) {
73     line = line.substring(0, line.length() - 1);
74     line += "\n";
75     line += in.readLine();
76     }
77     return StringUtils.split(line, "\t");
78 }
79
80 public static void main(String JavaDoc[] args) {
81     junit.textui.TestRunner.run(suite());
82     System.exit(0);
83 }
84
85 }
86
Popular Tags