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.Map;
22  
23  import net.sourceforge.addam.impexp.csv.MapCSVParser;
24  
25  /**
26   * Tests a MapCSVParser.
27   *
28   * @author TIM3
29   * @since Jul 30, 2004
30   */
31  public class MapCSVParserTEST extends TestCase {
32  
33      public void testEmpty() throws Exception {
34          StringReader raw = new StringReader("");
35          MapCSVParser parser = new MapCSVParser(raw);
36          Map content = parser.readMap();
37          assertEquals(0, content.values().size());
38      }
39  
40      public void testOneFieldNoRows() throws Exception {
41          StringReader raw = new StringReader("field1");
42          MapCSVParser parser = new MapCSVParser(raw);
43          Map content = parser.readMap();
44          assertEquals(0, content.values().size());
45      }
46  
47      public void testOneFieldOneRow() throws Exception {
48          StringReader raw = new StringReader("field1\nvalue1");
49          MapCSVParser parser = new MapCSVParser(raw);
50          Map content = parser.readMap();
51          assertEquals(1, content.values().size());
52          assertEquals("value1", content.get("field1"));
53      }
54  
55      public void testTwoFieldsOneRow() throws Exception {
56          StringReader raw = new StringReader("field1,field2\nvalue1,value2");
57          MapCSVParser parser = new MapCSVParser(raw);
58          Map content = parser.readMap();
59          assertEquals(2, content.values().size());
60          assertEquals("value1", content.get("field1"));
61          assertEquals("value2", content.get("field2"));
62      }
63  
64      public void testTwoFieldsTwoRows() throws Exception {
65          StringReader raw = new StringReader("field1,field2\nvalue1,value2\nvalue3,value4");
66          MapCSVParser parser = new MapCSVParser(raw);
67          Map content = parser.readMap();
68          assertEquals(2, content.values().size());
69          assertEquals("value1", content.get("field1"));
70          assertEquals("value2", content.get("field2"));
71          content = parser.readMap();
72          assertEquals("value3", content.get("field1"));
73          assertEquals("value4", content.get("field2"));
74      }
75  
76      public void testTwoFieldsWithQuotes() throws Exception {
77          StringReader raw = new StringReader("\"field1,1\",field2\nvalue1,value2\nvalue3,value4");
78          MapCSVParser parser = new MapCSVParser(raw);
79          Map content = parser.readMap();
80          assertEquals(2, content.values().size());
81          assertEquals("value1", content.get("field1,1"));
82          assertEquals("value2", content.get("field2"));
83      }
84  
85      public void testShortRow() throws Exception {
86          StringReader raw = new StringReader("field1,field2\nvalue1");
87          MapCSVParser parser = new MapCSVParser(raw);
88          try {
89              Map content = parser.readMap();
90              fail("expected IndexOutOfBoundsException");
91          } catch (IndexOutOfBoundsException e) {
92              // expected
93          }
94      }
95  
96      public void testLongRow() throws Exception {
97          StringReader raw = new StringReader("field1,field2\nvalue1");
98          MapCSVParser parser = new MapCSVParser(raw);
99          try {
100             Map content = parser.readMap();
101             fail("expected IndexOutOfBoundsException");
102         } catch (IndexOutOfBoundsException e) {
103             // expected
104         }
105     }
106 }