KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > csv > CsvParserTest


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

21
22 package org.dbunit.dataset.csv;
23
24 import junit.framework.TestCase;
25 import org.dbunit.dataset.csv.handlers.PipelineException;
26
27 import java.io.*;
28 import java.util.List JavaDoc;
29
30
31 public class CsvParserTest extends TestCase {
32
33     CsvParser parser;
34
35 /*
36     public void testNewParserHasNotNullPipeline() {
37         assertNotNull(parser.getPipeline());
38     }
39
40     public void testAfterEachParsingThePipelineIsEmpty() throws PipelineException, IllegalInputCharacterException {
41
42         class MockPipeline extends Pipeline {
43             boolean setProductCalled = false;
44
45             protected void setProducts(List products) {
46                 assertEquals(0, products.size());
47                 super.setProducts(products);
48                 setProductCalled = true;
49             }
50         }
51
52         MockPipeline mockPipeline = new MockPipeline();
53         parser.setPipeline(mockPipeline);
54         parser.parse("");
55         assertTrue("the set product method should be called to prepare a new list of products",
56                 mockPipeline.setProductCalled);
57     }
58 */

59
60     public void testCanParseNonQuotedStrings() throws PipelineException, IllegalInputCharacterException {
61         String JavaDoc csv = "Hello, world";
62         List JavaDoc parsed = parser.parse(csv);
63         assertEquals(2, parsed.size());
64         assertEquals(parsed.get(0), "Hello");
65         assertEquals(parsed.get(1), "world");
66     }
67
68     public void testAFieldCanContainANewLine () throws PipelineException, IllegalInputCharacterException {
69         assertEquals("", 3, parser.parse("Hello, World\nIt's today, the day before tomorrow").size());
70     }
71
72     public void testDontAcceptIncompleteFields () throws PipelineException, IllegalInputCharacterException {
73         String JavaDoc incompleteFields = "AAAAA,\"BB";
74
75         try {
76             parser.parse(incompleteFields);
77             fail("should have thrown an exception");
78         } catch (IllegalStateException JavaDoc e) {
79             assertTrue(true);
80         }
81     }
82
83     public void testAFileCanContainFieldWithNewLine () throws IOException, CsvParserException {
84         final String JavaDoc pathname = "src/csv/with-newlines.csv";
85         List JavaDoc list = parser.parse(new File(pathname));
86         assertEquals("wrong number of lines parsed from " + pathname, 2, list.size());
87         List JavaDoc row = (List JavaDoc) list.get(1);
88         assertEquals("AA\nAAA", row.get(0));
89         assertEquals("BB\nBBB", row.get(1));
90     }
91
92     public void testRaiseACSVParserExceptonWhenParsingAnEmptyFile () throws IOException {
93         failParsing(new File("src/csv/empty-file.csv"));
94     }
95
96     public void testRaiseACSVParserExceptonWhenParsingFileWithDifferentNumberOfColumns () throws IllegalInputCharacterException, IOException, PipelineException {
97         failParsing(new File("src/csv/different-column-numbers-last.csv"));
98         failParsing(new File("src/csv/different-column-numbers-first.csv"));
99     }
100
101     private void failParsing(File sample) throws IOException {
102         try {
103             parser.parse(sample);
104             fail("should have thrown a CsvParserException");
105         } catch (CsvParserException e) {
106             assertTrue(true);
107         }
108     }
109
110     public void testSample() throws Exception JavaDoc {
111
112         File sample = new File("src/csv/sample.csv");
113         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(sample)));
114         LineNumberReader lineNumberReader = new LineNumberReader(reader);
115         String JavaDoc line;
116         while ((line = lineNumberReader.readLine()) != null) {
117             if (line.startsWith("#") || line.trim().length() == 0)
118                 continue;
119             //System.out.println("line: " + line);
120
List JavaDoc actual = parser.parse(line);
121             assertEquals("wrong tokens on line " + lineNumberReader.getLineNumber() + " " + line,
122                     3, actual.size());
123         }
124     }
125
126     protected void setUp() throws Exception JavaDoc {
127         parser = new CsvParserImpl();
128     }
129
130 }
131
132
Popular Tags