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.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  import java.io.Reader;
28  import java.io.StringReader;
29  import java.util.Arrays;
30  import java.util.Iterator;
31  
32  import org.apache.commons.csv.CSVFormat;
33  import org.apache.commons.csv.CSVParser;
34  import org.apache.commons.csv.CSVRecord;
35  import org.junit.jupiter.api.Test;
36  
37  class JiraCsv247Test {
38  
39      @Test
40      void testHeadersMissingOneColumnWhenAllowingMissingColumnNames() throws Exception {
41          final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().setAllowMissingColumnNames(true).get();
42  
43          assertTrue(format.getAllowMissingColumnNames(), "We should allow missing column names");
44  
45          final Reader in = new StringReader("a,,c,d,e\n1,2,3,4,5\nv,w,x,y,z");
46          try (CSVParser parser = format.parse(in)) {
47              assertEquals(Arrays.asList("a", "", "c", "d", "e"), parser.getHeaderNames());
48              final Iterator<CSVRecord> iterator = parser.iterator();
49              CSVRecord record = iterator.next();
50              assertEquals("1", record.get(0));
51              assertEquals("2", record.get(1));
52              assertEquals("3", record.get(2));
53              assertEquals("4", record.get(3));
54              assertEquals("5", record.get(4));
55              record = iterator.next();
56              assertEquals("v", record.get(0));
57              assertEquals("w", record.get(1));
58              assertEquals("x", record.get(2));
59              assertEquals("y", record.get(3));
60              assertEquals("z", record.get(4));
61              assertFalse(iterator.hasNext());
62          }
63      }
64  
65      @Test
66      void testHeadersMissingThrowsWhenNotAllowingMissingColumnNames() {
67          final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().get();
68  
69          assertFalse(format.getAllowMissingColumnNames(), "By default we should not allow missing column names");
70  
71          assertThrows(IllegalArgumentException.class, () -> {
72              try (Reader reader = new StringReader("a,,c,d,e\n1,2,3,4,5\nv,w,x,y,z");
73                  CSVParser parser = format.parse(reader);) {
74                  // should fail
75              }
76          }, "1 missing column header is not allowed");
77  
78          assertThrows(IllegalArgumentException.class, () -> {
79              try (Reader reader = new StringReader("a,,c,d,\n1,2,3,4,5\nv,w,x,y,z");
80                  CSVParser parser = format.parse(reader);) {
81                  // should fail
82              }
83          }, "2+ missing column headers is not allowed!");
84      }
85  }