2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.util
;
18 import java
.io
.ByteArrayInputStream
;
19 import java
.io
.InputStream
;
20 import java
.util
.HashMap
;
21 import java
.util
.List
;
24 import junit
.framework
.TestCase
;
26 public class CsvParserWithQuotedSeparatorTest
extends TestCase
{
27 public void testSimpleParse() throws Exception
{
28 String toParse
= "Header1,\"Header2\",Header3,\"Header4\"\n"
29 + "\"Col1, Col2\",\"Col\n2\",Col3,\"\"\"Col4\"\"\"\n";
31 InputStream in
= new ByteArrayInputStream(toParse
.getBytes());
33 CsvParser csvParser
= new CsvParser() {
34 protected void processLine(Integer lineNumber
, List
<String
> header
,
35 List
<String
> tokens
) {
36 assertEquals(header
.size(), tokens
.size());
37 assertEquals(4, tokens
.size());
38 assertEquals("Col1, Col2", tokens
.get(0));
41 // System.out.println(toParse);
47 public void testParseFile() throws Exception
{
49 final Map
<Integer
, Map
<String
, String
>> lines
= new HashMap
<Integer
, Map
<String
, String
>>();
50 InputStream in
= getClass().getResourceAsStream(
51 "/org/argeo/util/ReferenceFile.csv");
53 CsvParserWithLinesAsMap parser
= new CsvParserWithLinesAsMap() {
54 protected void processLine(Integer lineNumber
,
55 Map
<String
, String
> line
) {
56 // System.out.println("processing line #" + lineNumber);
57 lines
.put(lineNumber
, line
);
64 Map
<String
, String
> line
= lines
.get(2);
65 assertEquals(",,,,", line
.get("Coma testing"));
67 assertEquals(",, ,,", line
.get("Coma testing"));
69 assertEquals("module1, module2", line
.get("Coma testing"));
71 assertEquals("module1,module2", line
.get("Coma testing"));
73 assertEquals(",module1,module2, \nmodule3, module4",
74 line
.get("Coma testing"));
75 assertEquals(5, lines
.size());