View Javadoc
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 java.io.BufferedReader;
25  import java.io.File;
26  import java.io.FileInputStream;
27  import java.io.IOException;
28  import java.io.InputStreamReader;
29  import java.io.LineNumberReader;
30  import java.util.List;
31  
32  import junit.framework.TestCase;
33  
34  import org.dbunit.dataset.common.handlers.IllegalInputCharacterException;
35  import org.dbunit.dataset.common.handlers.PipelineException;
36  import org.dbunit.testutil.TestUtils;
37  
38  public class CsvParserTest extends TestCase {
39  
40      CsvParser parser;
41  
42  /*
43      public void testNewParserHasNotNullPipeline() {
44          assertNotNull(parser.getPipeline());
45      }
46  
47      public void testAfterEachParsingThePipelineIsEmpty() throws PipelineException, IllegalInputCharacterException {
48  
49          class MockPipeline extends Pipeline {
50              boolean setProductCalled = false;
51  
52              protected void setProducts(List products) {
53                  assertEquals(0, products.size());
54                  super.setProducts(products);
55                  setProductCalled = true;
56              }
57          }
58  
59          MockPipeline mockPipeline = new MockPipeline();
60          parser.setPipeline(mockPipeline);
61          parser.parse("");
62          assertTrue("the set product method should be called to prepare a new list of products",
63                  mockPipeline.setProductCalled);
64      }
65  */
66  
67      
68      public void testCanParseNonQuotedStrings() throws PipelineException, IllegalInputCharacterException {
69          String csv = "Hello, world";
70          List parsed = parser.parse(csv);
71          assertEquals(2, parsed.size());
72          assertEquals(parsed.get(0), "Hello");
73          assertEquals(parsed.get(1), "world");
74      }
75  
76      public void testAFieldCanContainANewLine () throws PipelineException, IllegalInputCharacterException {
77          assertEquals("", 3, parser.parse("Hello, World\nIt's today, the day before tomorrow").size());
78      }
79  
80      public void testDontAcceptIncompleteFields () throws PipelineException, IllegalInputCharacterException {
81          String incompleteFields = "AAAAA,\"BB";
82  
83          try {
84              parser.parse(incompleteFields);
85              fail("should have thrown an exception");
86          } catch (IllegalStateException e) {
87              assertTrue(true);
88          }
89      }
90  
91      public void testAFileCanContainFieldWithNewLine () throws IOException, CsvParserException {
92          final String pathname = "csv/with-newlines.csv";
93          List list = parser.parse(TestUtils.getFile(pathname));
94          assertEquals("wrong number of lines parsed from " + pathname, 2, list.size());
95          List row = (List) list.get(1);
96          assertEquals("AA\nAAA", row.get(0));
97          assertEquals("BB\nBBB", row.get(1));
98      }
99  
100     public void testRaiseACSVParserExceptonWhenParsingAnEmptyFile () throws IOException {
101         failParsing(TestUtils.getFile("csv/empty-file.csv"));
102     }
103 
104     public void testRaiseACSVParserExceptonWhenParsingFileWithDifferentNumberOfColumns () throws IllegalInputCharacterException, IOException, PipelineException {
105         failParsing(TestUtils.getFile("csv/different-column-numbers-last.csv"));
106         failParsing(TestUtils.getFile("csv/different-column-numbers-first.csv"));
107     }
108 
109     private void failParsing(File sample) throws IOException {
110         try {
111             parser.parse(sample);
112             fail("should have thrown a CsvParserException");
113         } catch (CsvParserException e) {
114             assertTrue(true);
115         }
116     }
117 
118     public void testSample() throws Exception {
119 
120         File sample = TestUtils.getFile("csv/sample.csv");
121         BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(sample)));
122         LineNumberReader lineNumberReader = new LineNumberReader(reader);
123         String line;
124         while ((line = lineNumberReader.readLine()) != null) {
125             if (line.startsWith("#") || line.trim().length() == 0)
126                 continue;
127             //System.out.println("line: " + line);
128             List actual = parser.parse(line);
129             assertEquals("wrong tokens on line " + lineNumberReader.getLineNumber() + " " + line,
130                     3, actual.size());
131         }
132     }
133 
134     public void testWhitespacePreservedOnQuotedStrings() throws PipelineException, IllegalInputCharacterException {
135         String csv = "\" Hello, \",world";
136         List parsed = parser.parse(csv);
137         assertEquals(2, parsed.size());
138         assertEquals(" Hello, ", parsed.get(0));
139         assertEquals("world", parsed.get(1));
140         csv = " Hello, world";
141         parsed = parser.parse(csv);
142         assertEquals(2, parsed.size());
143         assertEquals("Hello", parsed.get(0));
144         assertEquals("world", parsed.get(1));
145         csv = "\" Hello, \",\" world \"";
146         parsed = parser.parse(csv);
147         assertEquals(2, parsed.size());
148         assertEquals(" Hello, ", parsed.get(0));
149         assertEquals(" world ", parsed.get(1));
150     }
151 
152     protected void setUp() throws Exception {
153         parser = new CsvParserImpl();
154     }
155 
156 }
157