1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sourceforge.addam.impexp.csv;
17  
18  import junit.framework.TestCase;
19  
20  import java.io.StringReader;
21  import java.util.List;
22  
23  import net.sourceforge.addam.impexp.csv.CSVFormatException;
24  import net.sourceforge.addam.impexp.csv.CSVParser;
25  
26  /**
27   * Tests a CSVParser.
28   *
29   * @author TIM3
30   * @since Jul 30, 2004
31   */
32  public class CSVParserTEST extends TestCase {
33  
34      public void testEmptyField() throws Exception {
35          StringReader raw = new StringReader("");
36          CSVParser parser = new CSVParser(raw);
37          String content = parser.readField();
38          assertEquals(null, content);
39      }
40  
41      public void testFieldGet() throws Exception {
42          StringReader raw = new StringReader("asdf,asdf,");
43          CSVParser parser = new CSVParser(raw);
44          parser.nextLine();
45          assertEquals("asdf", parser.readField());
46          assertEquals("asdf", parser.readField());
47          assertEquals("", parser.readField());
48          assertEquals(null, parser.readField());
49      }
50  
51      public void testEmpty() throws Exception {
52          StringReader raw = new StringReader("\n");
53          CSVParser parser = new CSVParser(raw);
54          List content = parser.readLine();
55          assertTrue(content.size() == 1);
56          content = parser.readLine();
57          assertTrue(content == null);
58      }
59  
60      public void testSingle() throws Exception {
61          StringReader raw = new StringReader("abc");
62          CSVParser parser = new CSVParser(raw);
63          List content = parser.readLine();
64          assertEquals(1, content.size());
65      }
66  
67      public void testSingleWithLF() throws Exception {
68          StringReader raw = new StringReader("abc\n");
69          CSVParser parser = new CSVParser(raw);
70          List content = parser.readLine();
71          assertEquals(1, content.size());
72      }
73  
74      public void testSimple() throws Exception {
75          StringReader raw = new StringReader("foo,bar");
76          CSVParser parser = new CSVParser(raw);
77          List content = parser.readLine();
78          assertEquals("foo", content.get(0));
79          assertEquals("bar", content.get(1));
80      }
81  
82      public void testSimpleWithLF() throws Exception {
83          StringReader raw = new StringReader("foo,bar\n");
84          CSVParser parser = new CSVParser(raw);
85          List content = parser.readLine();
86          assertEquals("foo", content.get(0));
87          assertEquals("bar", content.get(1));
88      }
89  
90      public void testSpaceComma() throws Exception {
91          StringReader raw = new StringReader("foo ,bar");
92          CSVParser parser = new CSVParser(raw);
93          List content = parser.readLine();
94          assertEquals("foo", content.get(0));
95          assertEquals("bar", content.get(1));
96      }
97  
98      public void testCommaSpace() throws Exception {
99          StringReader raw = new StringReader("foo, bar");
100         CSVParser parser = new CSVParser(raw);
101         List content = parser.readLine();
102         assertEquals("foo", content.get(0));
103         assertEquals("bar", content.get(1));
104     }
105 
106     public void testMultipleComma() throws Exception {
107         StringReader raw = new StringReader("foo,,bar");
108         CSVParser parser = new CSVParser(raw);
109         List content = parser.readLine();
110         assertEquals("foo", content.get(0));
111         assertEquals("", content.get(1));
112         assertEquals("bar", content.get(2));
113     }
114 
115     public void testEmbeddedQuotes() throws Exception {
116         StringReader raw = new StringReader("\"\",\"\"");
117         CSVParser parser = new CSVParser(raw);
118         List content = parser.readLine();
119         assertEquals("\"", content.get(0));
120         assertEquals("\"", content.get(1));
121     }
122 
123     public void testEmbeddedQuotesAndText() throws Exception {
124         StringReader raw = new StringReader("foo,\"\"bar\"\"");
125         CSVParser parser = new CSVParser(raw);
126         List content = parser.readLine();
127         assertEquals("foo", content.get(0));
128         assertEquals("\"bar\"", content.get(1));
129     }
130 
131     public void testEmbeddedComma() throws Exception {
132         StringReader raw = new StringReader("foo,\"bar,baz\"");
133         CSVParser parser = new CSVParser(raw);
134         List content = parser.readLine();
135         assertEquals("foo", content.get(0));
136         assertEquals("bar,baz", content.get(1));
137     }
138 
139     public void testEmbeddedCommaAndQuotes() throws Exception {
140         // this is a bug -- three quotes in a row confuses the parser.
141         StringReader raw = new StringReader("foo,\"bar\"\"bar,baz\"\"\"");
142         CSVParser parser = new CSVParser(raw);
143         List content = parser.readLine();
144         assertEquals("foo", content.get(0));
145         assertEquals("bar\"bar,baz\"", content.get(1));
146     }
147 
148     public void testEmbeddedMultipleQuotes() throws Exception {
149         // this is a bug -- three quotes in a row confuses the parser.
150         StringReader raw = new StringReader(" \"\"\"\" ");
151         CSVParser parser = new CSVParser(raw);
152         List content = parser.readLine();
153         assertEquals("\"\"", content.get(0));
154     }
155 
156     public void testEmbeddedQuotesAtBeginningOfValue() throws Exception {
157         // this is a bug -- three quotes in a row confuses the parser.
158         StringReader raw = new StringReader("one,\"\"\" two\"");
159         CSVParser parser = new CSVParser(raw);
160         List content = parser.readLine();
161         assertEquals("one", content.get(0));
162         assertEquals("\" two", content.get(1));
163     }
164 
165     public void testEmbeddedQuotesAtEndOfValue() throws Exception {
166         // this is a bug -- three quotes in a row confuses the parser.
167         StringReader raw = new StringReader("one,\"two \"\"\"");
168         CSVParser parser = new CSVParser(raw);
169         List content = parser.readLine();
170         assertEquals("one", content.get(0));
171         assertEquals("two \"", content.get(1));
172     }
173 
174     public void testQuoteComma() throws Exception {
175         StringReader raw = new StringReader("\"1077,151604\",16967");
176         CSVParser parser = new CSVParser(raw);
177         List content = parser.readLine();
178         assertEquals("1077,151604", content.get(0));
179         assertEquals("16967", content.get(1));
180     }
181 
182     public void testQuoteCommaSpace() throws Exception {
183         StringReader raw = new StringReader("\"1077,151604\", 16967");
184         CSVParser parser = new CSVParser(raw);
185         List content = parser.readLine();
186         assertEquals("1077,151604", content.get(0));
187         assertEquals("16967", content.get(1));
188     }
189 
190     public void testLeadingSpace() throws Exception {
191         StringReader raw = new StringReader(" foo,\" bar\"");
192         CSVParser parser = new CSVParser(raw);
193         List content = parser.readLine();
194         assertEquals("foo", content.get(0));
195         assertEquals(" bar", content.get(1));
196     }
197 
198     public void testTrailingSpace() throws Exception {
199         StringReader raw = new StringReader("foo,\"bar \" ");
200         CSVParser parser = new CSVParser(raw);
201         List content = parser.readLine();
202         assertEquals("foo", content.get(0));
203         assertEquals("bar ", content.get(1));
204     }
205 
206     public void testMultiLine() throws Exception {
207         StringReader raw = new StringReader("foo,\"bar\nbaz\nasd\"");
208         CSVParser parser = new CSVParser(raw);
209         List content = parser.readLine();
210         assertEquals("foo", content.get(0));
211         assertEquals("bar\nbaz\nasd", content.get(1));
212     }
213 
214     public void testSimpleTwoRecords() throws Exception {
215         StringReader raw = new StringReader("foo,bar\nbaz,baw");
216         CSVParser parser = new CSVParser(raw);
217         List content = parser.readLine();
218         assertEquals("foo", content.get(0));
219         assertEquals("bar", content.get(1));
220         content = parser.readLine();
221         assertEquals("baz", content.get(0));
222         assertEquals("baw", content.get(1));
223     }
224 
225     public void testMultiLineException() throws Exception {
226         StringReader raw = new StringReader("\"abc\n");
227         CSVParser parser = new CSVParser(raw);
228         try {
229             parser.readLine();
230             fail("expected CSVFormatException");
231         } catch (CSVFormatException e) {
232             assertTrue(e.getMessage().startsWith("quoted multi-line item doesn't end"));
233             assertEquals(2, e.lineNumber());
234             assertEquals(0, e.charNumber());
235         }
236     }
237 
238     public void testTextBeforeQuotesException() throws Exception {
239         StringReader raw = new StringReader("asd\"abc\"");
240         CSVParser parser = new CSVParser(raw);
241         try {
242             parser.readLine();
243             fail("expected CSVFormatException");
244         } catch (CSVFormatException e) {
245             assertTrue(e.getMessage().startsWith("invalid text before quotes"));
246             assertEquals(1, e.lineNumber());
247             assertEquals(4, e.charNumber());
248         }
249     }
250 
251     public void testTextAfterQuotesException() throws Exception {
252         StringReader raw = new StringReader("\"abc\"asd");
253         CSVParser parser = new CSVParser(raw);
254         try {
255             parser.readLine();
256             fail("expected CSVFormatException");
257         } catch (CSVFormatException e) {
258             assertTrue(e.getMessage().startsWith("invalid text after quotes"));
259             assertEquals(1, e.lineNumber());
260             assertEquals(6, e.charNumber());
261         }
262     }
263 
264     public void testExcelExcerpt() throws Exception {
265         StringReader raw = new StringReader("17492,469 Main Street,MARION,51,US,46952,12,,GRANT,,\"1077,151604\",16967,,,,,,0,IceMan,2002-01-21 17:16:30.0,IceMan,2002-01-21 17:16:30.0,,");
266         CSVParser parser = new CSVParser(raw);
267         List content = parser.readLine();
268         assertEquals("17492", content.get(0));
269         assertEquals("469 Main Street", content.get(1));
270         assertEquals("MARION", content.get(2));
271         assertEquals("51", content.get(3));
272         assertEquals("US", content.get(4));
273         assertEquals("46952", content.get(5));
274         assertEquals("12", content.get(6));
275         assertEquals("", content.get(7));
276         assertEquals("GRANT", content.get(8));
277         assertEquals("", content.get(9));
278         assertEquals("1077,151604", content.get(10));
279         assertEquals("16967", content.get(11));
280     }
281 
282     public void testGetLineNumberAndRow() throws Exception {
283         StringReader raw = new StringReader("A1,B1,C1,D1\nA2,B2,C2,\"D\nE\",A3,B3,C3,D3\n");
284         CSVParser parser = new CSVParser(raw);
285         // row 1 & line 1 are the same
286         parser.readLine();
287         assertEquals(1, parser.getRow());
288         assertEquals(1, parser.getLineNumber());
289         // row 2 spans line 2 and 3
290         parser.readLine();
291         assertEquals(2, parser.getRow());
292         assertEquals(3, parser.getLineNumber());
293         // row 3 starts on line 4
294         parser.readLine();
295         assertEquals(3, parser.getRow());
296         assertEquals(4, parser.getLineNumber());
297     }
298 
299     public void testExcelColumnString() {
300         StringReader raw = new StringReader("ignore");
301         CSVParser parser = new CSVParser(raw);
302         assertEquals("A101", parser.getExcelCell(101, 1));
303         assertEquals("M123", parser.getExcelCell(123, 13));
304         assertEquals("Z445", parser.getExcelCell(445, 26));
305         assertEquals("AA54", parser.getExcelCell(54, 27));
306         assertEquals("AZ10", parser.getExcelCell(10, 52));
307         try {
308             parser.getExcelCell(0, 0);
309             fail("expected IndexOutOfBoundsException");
310         } catch (IndexOutOfBoundsException e) {
311             // expected
312         }
313 
314     }
315 
316 }