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.issues;
19  
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import java.io.IOException;
23  import java.io.StringReader;
24  
25  import org.apache.commons.csv.CSVFormat;
26  import org.apache.commons.csv.CSVParser;
27  import org.apache.commons.csv.DuplicateHeaderMode;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * When {@link CSVFormat#withHeader(String...)} is not null; duplicate headers
32   * with empty strings should not be allowed.
33   *
34   * @see <a href="https://issues.apache.org/jira/browse/CSV-264">Jira Ticker</a>
35   */
36  public class JiraCsv264Test {
37  
38      private static final String CSV_STRING = "\"\",\"B\",\"\"\n" +
39                                               "\"1\",\"2\",\"3\"\n" +
40                                               "\"4\",\"5\",\"6\"";
41  
42      /**
43       * A CSV file with a random gap in the middle.
44       */
45      private static final String CSV_STRING_GAP = "\"A\",\"B\",\"\",\"\",\"E\"\n" +
46                                                   "\"1\",\"2\",\"\",\"\",\"5\"\n" +
47                                                   "\"6\",\"7\",\"\",\"\",\"10\"";
48  
49      @Test
50      public void testJiraCsv264() {
51          final CSVFormat csvFormat = CSVFormat.DEFAULT
52              .builder()
53              .setHeader()
54              .setDuplicateHeaderMode(DuplicateHeaderMode.DISALLOW)
55              .setAllowMissingColumnNames(true)
56              .build();
57  
58          try (StringReader reader = new StringReader(CSV_STRING)) {
59              assertThrows(IllegalArgumentException.class, () -> csvFormat.parse(reader));
60          }
61      }
62  
63      @Test
64      public void testJiraCsv264WithGapAllowEmpty() throws IOException {
65          final CSVFormat csvFormat = CSVFormat.DEFAULT
66              .builder()
67              .setHeader()
68              .setDuplicateHeaderMode(DuplicateHeaderMode.ALLOW_EMPTY)
69              .setAllowMissingColumnNames(true)
70              .build();
71  
72          try (StringReader reader = new StringReader(CSV_STRING_GAP); final CSVParser parser = csvFormat.parse(reader)) {
73              // empty
74          }
75      }
76  
77      @Test
78      public void testJiraCsv264WithGapDisallow() {
79          final CSVFormat csvFormat = CSVFormat.DEFAULT
80              .builder()
81              .setHeader()
82              .setDuplicateHeaderMode(DuplicateHeaderMode.DISALLOW)
83              .setAllowMissingColumnNames(true)
84              .build();
85  
86          try (StringReader reader = new StringReader(CSV_STRING_GAP)) {
87              assertThrows(IllegalArgumentException.class, () -> csvFormat.parse(reader));
88          }
89      }
90  }