]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.util/src/test/java/org/argeo/util/CsvParserWithQuotedSeparatorTest.java
Rename some artifacts
[lgpl/argeo-commons.git] / base / runtime / org.argeo.util / src / test / java / org / argeo / util / CsvParserWithQuotedSeparatorTest.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 org.argeo.util;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.InputStream;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import junit.framework.TestCase;
25
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";
30
31 InputStream in = new ByteArrayInputStream(toParse.getBytes());
32
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));
39 }
40 };
41 // System.out.println(toParse);
42 csvParser.parse(in);
43 in.close();
44
45 }
46
47 public void testParseFile() throws Exception {
48
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");
52
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);
58 }
59 };
60
61 parser.parse(in);
62 in.close();
63
64 Map<String, String> line = lines.get(2);
65 assertEquals(",,,,", line.get("Coma testing"));
66 line = lines.get(3);
67 assertEquals(",, ,,", line.get("Coma testing"));
68 line = lines.get(4);
69 assertEquals("module1, module2", line.get("Coma testing"));
70 line = lines.get(5);
71 assertEquals("module1,module2", line.get("Coma testing"));
72 line = lines.get(6);
73 assertEquals(",module1,module2, \nmodule3, module4",
74 line.get("Coma testing"));
75 assertEquals(5, lines.size());
76
77 }
78 }