1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.csv;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23 import static org.junit.jupiter.api.Assertions.fail;
24
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.net.URL;
30 import java.nio.charset.Charset;
31 import java.nio.charset.StandardCharsets;
32 import java.util.Arrays;
33 import java.util.stream.Stream;
34
35 import org.junit.jupiter.params.ParameterizedTest;
36 import org.junit.jupiter.params.provider.MethodSource;
37
38
39
40
41 public class CSVFileParserTest {
42
43 private static final File BASE_DIR = new File("src/test/resources/org/apache/commons/csv/CSVFileParser");
44
45 public static Stream<File> generateData() {
46 final File[] files = BASE_DIR.listFiles((dir, name) -> name.startsWith("test") && name.endsWith(".txt"));
47 return files != null ? Stream.of(files) : Stream.empty();
48 }
49
50 private String readTestData(final BufferedReader reader) throws IOException {
51 String line;
52 do {
53 line = reader.readLine();
54 } while (line != null && line.startsWith("#"));
55 return line;
56 }
57
58 @ParameterizedTest
59 @MethodSource("generateData")
60 public void testCSVFile(final File testFile) throws Exception {
61 try (FileReader fr = new FileReader(testFile); BufferedReader testData = new BufferedReader(fr)) {
62 String line = readTestData(testData);
63 assertNotNull("file must contain config line", line);
64 final String[] split = line.split(" ");
65 assertTrue(split.length >= 1, testFile.getName() + " require 1 param");
66
67 CSVFormat format = CSVFormat.newFormat(',').withQuote('"');
68 boolean checkComments = false;
69 for (int i = 1; i < split.length; i++) {
70 final String option = split[i];
71 final String[] option_parts = option.split("=", 2);
72 if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])) {
73 format = format.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1]));
74 } else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
75 format = format.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1]));
76 } else if ("CommentStart".equalsIgnoreCase(option_parts[0])) {
77 format = format.withCommentMarker(option_parts[1].charAt(0));
78 } else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
79 checkComments = true;
80 } else {
81 fail(testFile.getName() + " unexpected option: " + option);
82 }
83 }
84 line = readTestData(testData);
85 assertEquals(line, format.toString(), testFile.getName() + " Expected format ");
86
87
88
89 try (final CSVParser parser = CSVParser.parse(new File(BASE_DIR, split[0]), Charset.defaultCharset(), format)) {
90 for (final CSVRecord record : parser) {
91 String parsed = Arrays.toString(record.values());
92 final String comment = record.getComment();
93 if (checkComments && comment != null) {
94 parsed += "#" + comment.replace("\n", "\\n");
95 }
96 final int count = record.size();
97 assertEquals(readTestData(testData), count + ":" + parsed, testFile.getName());
98 }
99 }
100 }
101 }
102
103 @ParameterizedTest
104 @MethodSource("generateData")
105 public void testCSVUrl(final File testFile) throws Exception {
106 try (FileReader fr = new FileReader(testFile); BufferedReader testData = new BufferedReader(fr)) {
107 String line = readTestData(testData);
108 assertNotNull("file must contain config line", line);
109 final String[] split = line.split(" ");
110 assertTrue(split.length >= 1, testFile.getName() + " require 1 param");
111
112 CSVFormat format = CSVFormat.newFormat(',').withQuote('"');
113 boolean checkComments = false;
114 for (int i = 1; i < split.length; i++) {
115 final String option = split[i];
116 final String[] option_parts = option.split("=", 2);
117 if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])) {
118 format = format.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1]));
119 } else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
120 format = format.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1]));
121 } else if ("CommentStart".equalsIgnoreCase(option_parts[0])) {
122 format = format.withCommentMarker(option_parts[1].charAt(0));
123 } else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
124 checkComments = true;
125 } else {
126 fail(testFile.getName() + " unexpected option: " + option);
127 }
128 }
129 line = readTestData(testData);
130 assertEquals(line, format.toString(), testFile.getName() + " Expected format ");
131
132
133 final URL resource = ClassLoader.getSystemResource("org/apache/commons/csv/CSVFileParser/" + split[0]);
134 try (final CSVParser parser = CSVParser.parse(resource, StandardCharsets.UTF_8, format)) {
135 for (final CSVRecord record : parser) {
136 String parsed = Arrays.toString(record.values());
137 final String comment = record.getComment();
138 if (checkComments && comment != null) {
139 parsed += "#" + comment.replace("\n", "\\n");
140 }
141 final int count = record.size();
142 assertEquals(readTestData(testData), count + ":" + parsed, testFile.getName());
143 }
144 }
145 }
146 }
147 }