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.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
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.apache.commons.csv.CSVRecord;
30  import org.junit.jupiter.api.Test;
31  
32  class JiraCsv149Test {
33  
34      private static final String CR_LF = "\r\n";
35  
36      @Test
37      void testJiraCsv149EndWithEOL() throws IOException {
38          testJiraCsv149EndWithEolAtEof(true);
39      }
40  
41      private void testJiraCsv149EndWithEolAtEof(final boolean eolAtEof) throws IOException {
42          String source = "A,B,C,D" + CR_LF + "a1,b1,c1,d1" + CR_LF + "a2,b2,c2,d2";
43          if (eolAtEof) {
44              source += CR_LF;
45          }
46          final StringReader reader = new StringReader(source);
47          // @formatter:off
48          final CSVFormat format = CSVFormat.RFC4180.builder()
49              .setHeader()
50              .setSkipHeaderRecord(true)
51              .setQuote('"')
52              .get();
53          // @formatter:on
54          int lineCounter = 2;
55          try (CSVParser parser = CSVParser.builder().setReader(reader).setFormat(format).get()) {
56              for (final CSVRecord record : parser) {
57                  assertNotNull(record);
58                  assertEquals(lineCounter++, parser.getCurrentLineNumber());
59              }
60          }
61      }
62  
63      @Test
64      void testJiraCsv149EndWithoutEOL() throws IOException {
65          testJiraCsv149EndWithEolAtEof(false);
66      }
67  }