]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/ext/test/org/argeo/util/CsvParserWithQuotedSeparatorTest.java
Rename container scripts.
[lgpl/argeo-commons.git] / org.argeo.util / ext / test / org / argeo / util / CsvParserWithQuotedSeparatorTest.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 /** Test that {@link CsvParser} deals properly with "" quotes. */
25 public class CsvParserWithQuotedSeparatorTest {
26 public void testSimpleParse() throws Exception {
27 String toParse = "Header1,\"Header2\",Header3,\"Header4\"\n"
28 + "\"Col1, Col2\",\"Col\n2\",Col3,\"\"\"Col4\"\"\"\n";
29
30 InputStream in = new ByteArrayInputStream(toParse.getBytes());
31
32 CsvParser csvParser = new CsvParser() {
33 protected void processLine(Integer lineNumber, List<String> header, List<String> tokens) {
34 assert header.size() == tokens.size();
35 assert 4 == tokens.size();
36 assert "Col1, Col2".equals(tokens.get(0));
37 }
38 };
39 // System.out.println(toParse);
40 csvParser.parse(in);
41 in.close();
42
43 }
44
45 public void testParseFile() throws Exception {
46
47 final Map<Integer, Map<String, String>> lines = new HashMap<Integer, Map<String, String>>();
48 InputStream in = getClass().getResourceAsStream("/org/argeo/util/ReferenceFile.csv");
49
50 CsvParserWithLinesAsMap parser = new CsvParserWithLinesAsMap() {
51 protected void processLine(Integer lineNumber, Map<String, String> line) {
52 // System.out.println("processing line #" + lineNumber);
53 lines.put(lineNumber, line);
54 }
55 };
56
57 parser.parse(in);
58 in.close();
59
60 Map<String, String> line = lines.get(2);
61 assert ",,,,".equals(line.get("Coma testing"));
62 line = lines.get(3);
63 assert ",, ,,".equals(line.get("Coma testing"));
64 line = lines.get(4);
65 assert "module1, module2".equals(line.get("Coma testing"));
66 line = lines.get(5);
67 assert "module1,module2".equals(line.get("Coma testing"));
68 line = lines.get(6);
69 assert ",module1,module2, \nmodule3, module4".equals(line.get("Coma testing"));
70 assert 5 == lines.size();
71
72 }
73 }