Skip to content

Commit f61a517

Browse files
committed
test 3
1 parent 90b5a13 commit f61a517

17 files changed

Lines changed: 1329 additions & 0 deletions
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.issues;
18+
19+
import org.apache.commons.csv.format.CSVFormat;
20+
import org.apache.commons.csv.format.QuoteMode;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
25+
public class JiraCsv148Test {
26+
27+
@Test
28+
public void testWithIgnoreSurroundingSpacesEmpty() {
29+
// @formatter:off
30+
final CSVFormat format = CSVFormat.DEFAULT.builder()
31+
.setQuoteMode(QuoteMode.ALL)
32+
.setIgnoreSurroundingSpaces(true)
33+
.build();
34+
// @formatter:on
35+
assertEquals(
36+
"\"\",\" \",\" Single space on the left\",\"Single space on the right \","
37+
+ "\" Single spaces on both sides \",\" Multiple spaces on the left\","
38+
+ "\"Multiple spaces on the right \",\" Multiple spaces on both sides \"",
39+
format.format("", " ", " Single space on the left", "Single space on the right ",
40+
" Single spaces on both sides ", " Multiple spaces on the left", "Multiple spaces on the right ",
41+
" Multiple spaces on both sides "));
42+
}
43+
44+
/**
45+
* The difference between withTrim()and withIgnoreSurroundingSpace(): difference: withTrim() can remove the leading
46+
* and trailing spaces and newlines in quotation marks, while withIgnoreSurroundingSpace() cannot The same point:
47+
* you can remove the leading and trailing spaces,tabs and other symbols.
48+
*/
49+
@Test
50+
public void testWithTrimEmpty() {
51+
// @formatter:off
52+
final CSVFormat format = CSVFormat.DEFAULT.builder()
53+
.setQuoteMode(QuoteMode.ALL)
54+
.setTrim(true)
55+
.build();
56+
// @formatter:on
57+
assertEquals(
58+
"\"\",\"\",\"Single space on the left\",\"Single space on the right\","
59+
+ "\"Single spaces on both sides\",\"Multiple spaces on the left\","
60+
+ "\"Multiple spaces on the right\",\"Multiple spaces on both sides\"",
61+
format.format("", " ", " Single space on the left", "Single space on the right ",
62+
" Single spaces on both sides ", " Multiple spaces on the left", "Multiple spaces on the right ",
63+
" Multiple spaces on both sides "));
64+
}
65+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.issues;
18+
19+
import org.apache.commons.csv.format.CSVFormat;
20+
import org.apache.commons.csv.parser.CSVParser;
21+
import org.apache.commons.csv.parser.ICSVParser;
22+
import org.apache.commons.csv.record.CSVRecord;
23+
import org.junit.jupiter.api.Test;
24+
25+
import java.io.IOException;
26+
import java.io.StringReader;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
30+
public class JiraCsv149Test {
31+
32+
private static final String CR_LF = "\r\n";
33+
34+
@Test
35+
public void testJiraCsv149EndWithEOL() throws IOException {
36+
testJiraCsv149EndWithEolAtEof(true);
37+
}
38+
39+
private void testJiraCsv149EndWithEolAtEof(final boolean eolAtEof) throws IOException {
40+
String source = "A,B,C,D" + CR_LF + "a1,b1,c1,d1" + CR_LF + "a2,b2,c2,d2";
41+
if (eolAtEof) {
42+
source += CR_LF;
43+
}
44+
final StringReader records = new StringReader(source);
45+
// @formatter:off
46+
final CSVFormat format = CSVFormat.RFC4180.builder()
47+
.setHeader()
48+
.setSkipHeaderRecord(true)
49+
.setQuote('"')
50+
.build();
51+
// @formatter:on
52+
int lineCounter = 2;
53+
try (final ICSVParser parser = new CSVParser(records, format)) {
54+
for (final CSVRecord record : parser) {
55+
assertEquals(lineCounter++, parser.getCurrentLineNumber());
56+
}
57+
}
58+
}
59+
60+
@Test
61+
public void testJiraCsv149EndWithoutEOL() throws IOException {
62+
testJiraCsv149EndWithEolAtEof(false);
63+
}
64+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.issues;
18+
19+
import org.apache.commons.csv.format.CSVFormat;
20+
import org.apache.commons.csv.printer.CSVPrinter;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.io.IOException;
24+
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
27+
public class JiraCsv154Test {
28+
29+
@Test
30+
public void testJiraCsv154_withCommentMarker() throws IOException {
31+
final String comment = "This is a header comment";
32+
// @formatter:off
33+
final CSVFormat format = CSVFormat.EXCEL.builder()
34+
.setHeader("H1", "H2")
35+
.setCommentMarker('#')
36+
.setHeaderComments(comment)
37+
.build();
38+
// @formatter:on
39+
final StringBuilder out = new StringBuilder();
40+
try (final CSVPrinter printer = new CSVPrinter(out, format)) {
41+
printer.print("A");
42+
printer.print("B");
43+
}
44+
final String s = out.toString();
45+
assertTrue(s.contains(comment), s);
46+
}
47+
48+
@Test
49+
public void testJiraCsv154_withHeaderComments() throws IOException {
50+
final String comment = "This is a header comment";
51+
// @formatter:off
52+
final CSVFormat format = CSVFormat.EXCEL.builder()
53+
.setHeader("H1", "H2")
54+
.setHeaderComments(comment)
55+
.setCommentMarker('#')
56+
.build();
57+
// @formatter:on
58+
final StringBuilder out = new StringBuilder();
59+
try (final CSVPrinter printer = new CSVPrinter(out, format)) {
60+
printer.print("A");
61+
printer.print("B");
62+
}
63+
final String s = out.toString();
64+
assertTrue(s.contains(comment), s);
65+
}
66+
67+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.issues;
18+
19+
import org.apache.commons.csv.format.CSVFormat;
20+
import org.apache.commons.csv.format.QuoteMode;
21+
import org.apache.commons.csv.parser.CSVParser;
22+
import org.apache.commons.csv.parser.ICSVParser;
23+
import org.apache.commons.csv.record.CSVRecord;
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.io.BufferedReader;
27+
import java.io.IOException;
28+
import java.io.InputStreamReader;
29+
import java.io.Reader;
30+
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
33+
public class JiraCsv167Test {
34+
35+
private Reader getTestReader() {
36+
return new InputStreamReader(
37+
ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/csv-167/sample1.csv"));
38+
}
39+
40+
@Test
41+
public void parse() throws IOException {
42+
int totcomment = 0;
43+
int totrecs = 0;
44+
try (final Reader reader = getTestReader(); final BufferedReader br = new BufferedReader(reader)) {
45+
String s = null;
46+
boolean lastWasComment = false;
47+
while ((s = br.readLine()) != null) {
48+
if (s.startsWith("#")) {
49+
if (!lastWasComment) { // comments are merged
50+
totcomment++;
51+
}
52+
lastWasComment = true;
53+
} else {
54+
totrecs++;
55+
lastWasComment = false;
56+
}
57+
}
58+
}
59+
final CSVFormat format = CSVFormat.DEFAULT.builder()
60+
// @formatter:off
61+
.setAllowMissingColumnNames(false)
62+
.setCommentMarker('#')
63+
.setDelimiter(',')
64+
.setEscape('\\')
65+
.setHeader("author", "title", "publishDate")
66+
.setHeaderComments("headerComment")
67+
.setNullString("NULL")
68+
.setIgnoreEmptyLines(true)
69+
.setIgnoreSurroundingSpaces(true)
70+
.setQuote('"')
71+
.setQuoteMode(QuoteMode.ALL)
72+
.setRecordSeparator('\n')
73+
.setSkipHeaderRecord(false)
74+
.build();
75+
// @formatter:on
76+
int comments = 0;
77+
int records = 0;
78+
try (final Reader reader = getTestReader(); final ICSVParser parser = new CSVParser(reader, format)) {
79+
for (final CSVRecord csvRecord : parser) {
80+
records++;
81+
if (csvRecord.hasComment()) {
82+
comments++;
83+
}
84+
}
85+
}
86+
// Comment lines are concatenated, in this example 4 lines become 2 comments.
87+
assertEquals(totcomment, comments);
88+
assertEquals(totrecs, records); // records includes the header
89+
}
90+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.issues;
18+
19+
import org.apache.commons.csv.format.CSVFormat;
20+
import org.apache.commons.csv.parser.CSVParser;
21+
import org.apache.commons.csv.parser.ICSVParser;
22+
import org.apache.commons.csv.record.CSVRecord;
23+
import org.junit.jupiter.api.Disabled;
24+
import org.junit.jupiter.api.Test;
25+
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
import java.io.InputStreamReader;
29+
import java.io.UnsupportedEncodingException;
30+
import java.nio.charset.StandardCharsets;
31+
32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
33+
34+
public class JiraCsv198Test {
35+
36+
// @formatter:off
37+
private static final CSVFormat CSV_FORMAT = CSVFormat.EXCEL.builder()
38+
.setDelimiter('^')
39+
.setHeader()
40+
.setSkipHeaderRecord(true)
41+
.build();
42+
// @formatter:on
43+
44+
@Test
45+
@Disabled
46+
public void test() throws UnsupportedEncodingException, IOException {
47+
final InputStream pointsOfReference = getClass()
48+
.getResourceAsStream("/org/apache/commons/csv/CSV-198/optd_por_public.csv");
49+
assertNotNull(pointsOfReference);
50+
try (@SuppressWarnings("resource")
51+
ICSVParser parser = new CSVParser(new InputStreamReader(pointsOfReference, StandardCharsets.UTF_8), CSVFormat.DEFAULT)) {
52+
for (final CSVRecord record : parser) {
53+
final String locationType = record.get("location_type");
54+
assertNotNull(locationType);
55+
}
56+
}
57+
}
58+
59+
}

0 commit comments

Comments
 (0)