|
1 | | -/* |
2 | | - * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | - * contributor license agreements. See the NOTICE file distributed with |
4 | | - * this work for additional information regarding copyright ownership. |
5 | | - * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | - * (the "License"); you may not use this file except in compliance with |
7 | | - * the License. You may obtain a copy of the License at |
8 | | - * |
9 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | - * |
11 | | - * Unless required by applicable law or agreed to in writing, software |
12 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | - * See the License for the specific language governing permissions and |
15 | | - * limitations under the License. |
16 | | - */ |
17 | | -package org.apache.commons.csv; |
18 | | - |
19 | | -import java.io.BufferedReader; |
20 | | -import java.io.File; |
21 | | -import java.io.FileReader; |
22 | | -import java.io.IOException; |
23 | | -import java.nio.charset.Charset; |
24 | | - |
25 | | -import org.junit.Assert; |
26 | | -import org.junit.Test; |
27 | | - |
28 | | -public class JiraCsv167Test { |
29 | | - |
30 | | - @Test |
31 | | - public void parse() throws IOException { |
32 | | - final File csvData = new File("src/test/resources/csv-167/sample1.csv"); |
33 | | - final BufferedReader br = new BufferedReader(new FileReader(csvData)); |
34 | | - String s = null; |
35 | | - int totcomment = 0; |
36 | | - int totrecs = 0; |
37 | | - boolean lastWasComment = false; |
38 | | - while((s=br.readLine()) != null) { |
39 | | - if (s.startsWith("#")) { |
40 | | - if (!lastWasComment) { // comments are merged |
41 | | - totcomment++; |
42 | | - } |
43 | | - lastWasComment = true; |
44 | | - } else { |
45 | | - totrecs++; |
46 | | - lastWasComment = false; |
47 | | - } |
48 | | - } |
49 | | - br.close(); |
50 | | - CSVFormat format = CSVFormat.DEFAULT; |
51 | | - // |
52 | | - format = format.withAllowMissingColumnNames(false); |
53 | | - format = format.withCommentMarker('#'); |
54 | | - format = format.withDelimiter(','); |
55 | | - format = format.withEscape('\\'); |
56 | | - format = format.withHeader("author", "title", "publishDate"); |
57 | | - format = format.withHeaderComments("headerComment"); |
58 | | - format = format.withNullString("NULL"); |
59 | | - format = format.withIgnoreEmptyLines(true); |
60 | | - format = format.withIgnoreSurroundingSpaces(true); |
61 | | - format = format.withQuote('"'); |
62 | | - format = format.withQuoteMode(QuoteMode.ALL); |
63 | | - format = format.withRecordSeparator('\n'); |
64 | | - format = format.withSkipHeaderRecord(false); |
65 | | - // |
66 | | - final CSVParser parser = CSVParser.parse(csvData, Charset.defaultCharset(), format); |
67 | | - int comments = 0; |
68 | | - int records = 0; |
69 | | - for (final CSVRecord csvRecord : parser) { |
70 | | -// System.out.println(csvRecord.isComment() + "[" + csvRecord.toString() + "]"); |
71 | | - records++; |
72 | | - if (csvRecord.hasComment()) { |
73 | | - comments++; |
74 | | - } |
75 | | - } |
76 | | - // Comment lines are concatenated, in this example 4 lines become 2 comments. |
77 | | - Assert.assertEquals(totcomment, comments); |
78 | | - Assert.assertEquals(totrecs, records); // records includes the header |
79 | | - } |
80 | | -} |
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.commons.csv.bugs; |
| 18 | + |
| 19 | +import java.io.BufferedReader; |
| 20 | +import java.io.File; |
| 21 | +import java.io.FileReader; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.charset.Charset; |
| 24 | + |
| 25 | +import org.apache.commons.csv.CSVFormat; |
| 26 | +import org.apache.commons.csv.CSVParser; |
| 27 | +import org.apache.commons.csv.CSVRecord; |
| 28 | +import org.apache.commons.csv.QuoteMode; |
| 29 | +import org.junit.Assert; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +public class JiraCsv167Test { |
| 33 | + |
| 34 | + @Test |
| 35 | + public void parse() throws IOException { |
| 36 | + final File csvData = new File("src/test/resources/csv-167/sample1.csv"); |
| 37 | + final BufferedReader br = new BufferedReader(new FileReader(csvData)); |
| 38 | + String s = null; |
| 39 | + int totcomment = 0; |
| 40 | + int totrecs = 0; |
| 41 | + boolean lastWasComment = false; |
| 42 | + while((s=br.readLine()) != null) { |
| 43 | + if (s.startsWith("#")) { |
| 44 | + if (!lastWasComment) { // comments are merged |
| 45 | + totcomment++; |
| 46 | + } |
| 47 | + lastWasComment = true; |
| 48 | + } else { |
| 49 | + totrecs++; |
| 50 | + lastWasComment = false; |
| 51 | + } |
| 52 | + } |
| 53 | + br.close(); |
| 54 | + CSVFormat format = CSVFormat.DEFAULT; |
| 55 | + // |
| 56 | + format = format.withAllowMissingColumnNames(false); |
| 57 | + format = format.withCommentMarker('#'); |
| 58 | + format = format.withDelimiter(','); |
| 59 | + format = format.withEscape('\\'); |
| 60 | + format = format.withHeader("author", "title", "publishDate"); |
| 61 | + format = format.withHeaderComments("headerComment"); |
| 62 | + format = format.withNullString("NULL"); |
| 63 | + format = format.withIgnoreEmptyLines(true); |
| 64 | + format = format.withIgnoreSurroundingSpaces(true); |
| 65 | + format = format.withQuote('"'); |
| 66 | + format = format.withQuoteMode(QuoteMode.ALL); |
| 67 | + format = format.withRecordSeparator('\n'); |
| 68 | + format = format.withSkipHeaderRecord(false); |
| 69 | + // |
| 70 | + final CSVParser parser = CSVParser.parse(csvData, Charset.defaultCharset(), format); |
| 71 | + int comments = 0; |
| 72 | + int records = 0; |
| 73 | + for (final CSVRecord csvRecord : parser) { |
| 74 | +// System.out.println(csvRecord.isComment() + "[" + csvRecord.toString() + "]"); |
| 75 | + records++; |
| 76 | + if (csvRecord.hasComment()) { |
| 77 | + comments++; |
| 78 | + } |
| 79 | + } |
| 80 | + // Comment lines are concatenated, in this example 4 lines become 2 comments. |
| 81 | + Assert.assertEquals(totcomment, comments); |
| 82 | + Assert.assertEquals(totrecs, records); // records includes the header |
| 83 | + } |
| 84 | +} |
0 commit comments