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  package org.apache.commons.csv.issues;
20  
21  import static org.apache.commons.csv.CsvAssertions.assertValuesEquals;
22  
23  import java.io.IOException;
24  import java.io.StringReader;
25  import java.util.Iterator;
26  
27  import org.apache.commons.csv.CSVFormat;
28  import org.apache.commons.csv.CSVParser;
29  import org.apache.commons.csv.CSVRecord;
30  import org.apache.commons.csv.QuoteMode;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Setting QuoteMode:ALL_NON_NULL or NON_NUMERIC can distinguish between empty string columns and absent value columns.
35   */
36  class JiraCsv253Test {
37  
38      @Test
39      void testHandleAbsentValues() throws IOException {
40          // @formatter:off
41          final String source =
42                  "\"John\",,\"Doe\"\n" +
43                  ",\"AA\",123\n" +
44                  "\"John\",90,\n" +
45                  "\"\",,90";
46          // @formatter:on
47          final CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setQuoteMode(QuoteMode.NON_NUMERIC).get();
48          try (CSVParser parser = csvFormat.parse(new StringReader(source))) {
49              final Iterator<CSVRecord> csvRecords = parser.iterator();
50              assertValuesEquals(new String[] {"John", null, "Doe"}, csvRecords.next());
51              assertValuesEquals(new String[] {null, "AA", "123"}, csvRecords.next());
52              assertValuesEquals(new String[] {"John", "90", null}, csvRecords.next());
53              assertValuesEquals(new String[] {"", null, "90"}, csvRecords.next());
54          }
55      }
56  }