View Javadoc
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  
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   * Parse tests using test files
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              // first line starts with csv data file name
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); // get string version of format
85              assertEquals(line, format.toString(), testFile.getName() + " Expected format ");
86  
87              // Now parse the file and compare against the expected results
88              // We use a buffered reader internally so no need to create one here.
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             // first line starts with csv data file name
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); // get string version of format
130             assertEquals(line, format.toString(), testFile.getName() + " Expected format ");
131 
132             // Now parse the file and compare against the expected results
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 }