View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.csv.issues;
21  
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.io.IOException;
25  import java.io.StringReader;
26  
27  import org.apache.commons.csv.CSVFormat;
28  import org.apache.commons.csv.CSVParser;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests https://issues.apache.org/jira/browse/CSV-257
33   */
34  class JiraCsv257Test {
35  
36      private static final String INPUT = ",";
37  
38      @Test
39      void testHeaderBuilder() throws IOException {
40          // @formatter:off
41          final CSVFormat format = CSVFormat.RFC4180.builder()
42                  .setDelimiter(INPUT.charAt(0))
43                  .setHeader()
44                  .setSkipHeaderRecord(true)
45                  .setIgnoreSurroundingSpaces(true)
46                  .get();
47          // @formatter:on
48          // Document the current behavior: Throw a IllegalArgumentException is a header name is missing.
49          assertThrows(IllegalArgumentException.class, () -> {
50              try (CSVParser parser = CSVParser.parse(INPUT, format)) {
51                  // empty
52              }
53          });
54      }
55  
56      @Test
57      void testHeaderDepreacted() throws IOException {
58          // @formatter:off
59          final CSVFormat format = CSVFormat.RFC4180
60                  .withDelimiter(INPUT.charAt(0))
61                  .withFirstRecordAsHeader()
62                  .withIgnoreSurroundingSpaces();
63          // @formatter:on
64          // Document the current behavior: Throw a IllegalArgumentException is a header name is missing.
65          assertThrows(IllegalArgumentException.class, () -> {
66              try (CSVParser parser = new CSVParser(new StringReader(INPUT), format)) {
67                  // empty
68              }
69          });
70      }
71  
72      @Test
73      void testNoHeaderBuilder() throws IOException {
74          // @formatter:off
75          final CSVFormat format = CSVFormat.RFC4180.builder()
76                  .setDelimiter(INPUT.charAt(0))
77                  .setIgnoreSurroundingSpaces(true)
78                  .get();
79          // @formatter:on
80          try (CSVParser parser = CSVParser.parse(INPUT, format)) {
81              // empty
82          }
83      }
84  }