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