From 9ab47dc56b43066be740488948d38032df636502 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Tue, 3 May 2016 18:28:54 +0000 Subject: [PATCH] Move tests for specific issues to own package git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1742173 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/csv/CSVFormatTest.java | 26 --- .../commons/csv/bugs/JiraCsv164Test.java | 39 +++++ .../csv/{ => bugs}/JiraCsv167Test.java | 164 +++++++++--------- 3 files changed, 123 insertions(+), 106 deletions(-) create mode 100644 src/test/java/org/apache/commons/csv/bugs/JiraCsv164Test.java rename src/test/java/org/apache/commons/csv/{ => bugs}/JiraCsv167Test.java (93%) diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java index 2bf37534..0196c436 100644 --- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -383,32 +383,6 @@ public class CSVFormatTest { Assert.assertTrue(formatWithHeader.getHeader().length == 0); } - @Test - public void testJiraCsv154_withCommentMarker() throws IOException { - final String comment = "This is a header comment"; - final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withCommentMarker('#').withHeaderComments(comment); - final StringBuilder out = new StringBuilder(); - final CSVPrinter printer = format.print(out); - printer.print("A"); - printer.print("B"); - printer.close(); - final String s = out.toString(); - Assert.assertTrue(s, s.contains(comment)); - } - - @Test - public void testJiraCsv154_withHeaderComments() throws IOException { - final String comment = "This is a header comment"; - final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withHeaderComments(comment).withCommentMarker('#'); - final StringBuilder out = new StringBuilder(); - final CSVPrinter printer = format.print(out); - printer.print("A"); - printer.print("B"); - printer.close(); - final String s = out.toString(); - Assert.assertTrue(s, s.contains(comment)); - } - @Test public void testWithIgnoreEmptyLines() throws Exception { assertFalse(CSVFormat.DEFAULT.withIgnoreEmptyLines(false).getIgnoreEmptyLines()); diff --git a/src/test/java/org/apache/commons/csv/bugs/JiraCsv164Test.java b/src/test/java/org/apache/commons/csv/bugs/JiraCsv164Test.java new file mode 100644 index 00000000..8309079e --- /dev/null +++ b/src/test/java/org/apache/commons/csv/bugs/JiraCsv164Test.java @@ -0,0 +1,39 @@ +package org.apache.commons.csv.bugs; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; +import org.junit.Test; + +public class JiraCsv164Test { + + @Test + public void testJiraCsv154_withCommentMarker() throws IOException { + final String comment = "This is a header comment"; + final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withCommentMarker('#').withHeaderComments(comment); + final StringBuilder out = new StringBuilder(); + final CSVPrinter printer = format.print(out); + printer.print("A"); + printer.print("B"); + printer.close(); + final String s = out.toString(); + assertTrue(s, s.contains(comment)); + } + + @Test + public void testJiraCsv154_withHeaderComments() throws IOException { + final String comment = "This is a header comment"; + final CSVFormat format = CSVFormat.EXCEL.withHeader("H1", "H2").withHeaderComments(comment).withCommentMarker('#'); + final StringBuilder out = new StringBuilder(); + final CSVPrinter printer = format.print(out); + printer.print("A"); + printer.print("B"); + printer.close(); + final String s = out.toString(); + assertTrue(s, s.contains(comment)); + } + +} diff --git a/src/test/java/org/apache/commons/csv/JiraCsv167Test.java b/src/test/java/org/apache/commons/csv/bugs/JiraCsv167Test.java similarity index 93% rename from src/test/java/org/apache/commons/csv/JiraCsv167Test.java rename to src/test/java/org/apache/commons/csv/bugs/JiraCsv167Test.java index ac727ef1..19b3d86b 100644 --- a/src/test/java/org/apache/commons/csv/JiraCsv167Test.java +++ b/src/test/java/org/apache/commons/csv/bugs/JiraCsv167Test.java @@ -1,80 +1,84 @@ -/* - * 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; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.nio.charset.Charset; - -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 - } -} +/* + * 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 + } +}