01: package jimm.datavision.test;
02:
03: import jimm.datavision.source.charsep.DelimParser;
04: import jimm.util.StringUtils;
05: import java.io.*;
06: import java.util.List;
07: import junit.framework.TestCase;
08: import junit.framework.TestSuite;
09: import junit.framework.Test;
10:
11: /**
12: * Compares CSV file input with "answers" file. The answers file is
13: * tab-delimited and lines that end with a backslash are continued
14: * on the next line.
15: *
16: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
17: */
18: public class DelimParserTest extends TestCase {
19:
20: protected static final String TEST_INPUT = AllTests
21: .testDataFile("delim_parser_in.txt");
22: protected static final String TEST_ANSWERS = AllTests
23: .testDataFile("delim_parser_answers.txt");
24:
25: public static Test suite() {
26: return new TestSuite(DelimParserTest.class);
27: }
28:
29: public DelimParserTest(String name) {
30: super (name);
31: }
32:
33: protected void sepTest(List answer, DelimParser parser) {
34: List parsed = null;
35: try {
36: parsed = parser.parse();
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 answer;
51: while ((answer = getNextAnswer(answers)) != null)
52: sepTest(answer, parser);
53: sepTest(null, parser);
54: } catch (Exception e) {
55: fail(e.toString());
56: } finally {
57: try {
58: if (answers != null)
59: answers.close();
60: if (in != null)
61: in.close();
62: } catch (Exception e2) {
63: }
64: }
65: }
66:
67: protected List getNextAnswer(BufferedReader in) throws IOException {
68: String 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[] args) {
81: junit.textui.TestRunner.run(suite());
82: System.exit(0);
83: }
84:
85: }
|