/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.csv.bugs;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.csv.QuoteMode;
import org.junit.Assert;
import org.junit.Test;

public class JiraCsv167Test {

    @Test
    public void parse() throws IOException {
        final File csvData = new File("src/test/resources/csv-167/sample1.csv");
        final BufferedReader br = new BufferedReader(new FileReader(csvData));
        String s = null;
        int totcomment = 0;
        int totrecs = 0;
        boolean lastWasComment = false;
        while((s=br.readLine()) != null) {
            if (s.startsWith("#")) {
                if (!lastWasComment) { // comments are merged
                    totcomment++;
                }
                lastWasComment = true;
            } else {
                totrecs++;
                lastWasComment = false;
            }
        }
        br.close();
        CSVFormat format = CSVFormat.DEFAULT;
        //
        format = format.withAllowMissingColumnNames(false);
        format = format.withCommentMarker('#');
        format = format.withDelimiter(',');
        format = format.withEscape('\\');
        format = format.withHeader("author", "title", "publishDate");
        format = format.withHeaderComments("headerComment");
        format = format.withNullString("NULL");
        format = format.withIgnoreEmptyLines(true);
        format = format.withIgnoreSurroundingSpaces(true);
        format = format.withQuote('"');
        format = format.withQuoteMode(QuoteMode.ALL);
        format = format.withRecordSeparator('\n');
        format = format.withSkipHeaderRecord(false);
        //
        final CSVParser parser = CSVParser.parse(csvData, Charset.defaultCharset(), format);
        int comments = 0;
        int records = 0;
        for (final CSVRecord csvRecord : parser) {
//            System.out.println(csvRecord.isComment() + "[" + csvRecord.toString() + "]");
            records++;
            if (csvRecord.hasComment()) {
                comments++;
            }
        }
        // Comment lines are concatenated, in this example 4 lines become 2 comments.
        Assert.assertEquals(totcomment, comments);
        Assert.assertEquals(totrecs, records); // records includes the header
    }
}

@@ -880,20 +880,27 @@
 java.io.
-File
+IOException
 ;%0Aimport
@@ -908,26 +908,27 @@
 java.io.
-FileReader
+InputStream
 ;%0Aimport
@@ -933,34 +933,40 @@
 rt java.io.I
-OException
+nputStreamReader
 ;%0Aimport jav
@@ -971,27 +971,17 @@
 ava.
-n
 io.
-charset.Charset
+Reader
 ;%0A%0Ai
@@ -1282,89 +1282,8 @@
 n %7B%0A
-        final File csvData = new File(%22src/test/resources/csv-167/sample1.csv%22);%0A
     
@@ -1335,30 +1335,21 @@
 der(
-new FileReader(csvData
+getTestInput(
 ));%0A
@@ -2618,65 +2618,35 @@
 r = 
-CSVParser.parse(csvData, Charset.defaultCharset(), format
+format.parse(getTestInput()
 );%0A 
@@ -2750,100 +2750,8 @@
 ) %7B%0A
-//            System.out.println(csvRecord.isComment() + %22%5B%22 + csvRecord.toString() + %22%5D%22);%0A
     
@@ -3083,11 +3083,206 @@
 er%0A    %7D
+%0A%0A    private Reader getTestInput() %7B%0A        final InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(%22csv-167/sample1.csv%22);%0A        return new InputStreamReader(is);%0A    %7D
 %0A%7D%0A

Access test data using the classloader instead of defining a file handle