diff --git a/pom.xml b/pom.xml index 9b57003f..3fe5469b 100644 --- a/pom.xml +++ b/pom.xml @@ -153,8 +153,9 @@ CSV files of various types. 3.0.0 ${basedir}/LICENSE-header.txt - LICENSE.txt, NOTICE.txt + LICENSE.txt, NOTICE.txt, **/maven-archiver/pom.properties + 3.12.0 false true @@ -200,6 +201,32 @@ CSV files of various types. ${basedir}/checkstyle.xml false + ${basedir}/src/main/resources/checkstyle/checkstyle-suppressions.xml + + + + + org.codehaus.mojo + findbugs-maven-plugin + ${commons.findbugs.version} + + Normal + Default + ${basedir}/src/main/resources/findbugs/findbugs-exclude-filter.xml + + + + + org.apache.maven.plugins + maven-pmd-plugin + ${pmd.version} + + ${maven.compiler.target} + false + true + + ${basedir}/src/main/resources/pmd/pmd-ruleset.xml + @@ -244,6 +271,7 @@ CSV files of various types. ${basedir}/checkstyle.xml false + ${basedir}/src/main/resources/checkstyle/checkstyle-suppressions.xml @@ -257,15 +285,25 @@ CSV files of various types. org.apache.maven.plugins maven-pmd-plugin - 3.12.0 + ${pmd.version} ${maven.compiler.target} + false + true + + ${basedir}/src/main/resources/pmd/pmd-ruleset.xml + org.codehaus.mojo findbugs-maven-plugin ${commons.findbugs.version} + + Normal + Default + ${basedir}/src/main/resources/findbugs/findbugs-exclude-filter.xml + org.codehaus.mojo diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index 1111762a..263943f2 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -892,7 +892,7 @@ public final class CSVFormat implements Serializable { */ public String format(final Object... values) { final StringWriter out = new StringWriter(); - try (final CSVPrinter csvPrinter = new CSVPrinter(out, this)) { + try (CSVPrinter csvPrinter = new CSVPrinter(out, this)) { csvPrinter.printRecord(values); return out.toString().trim(); } catch (final IOException e) { @@ -1713,7 +1713,7 @@ public final class CSVFormat implements Serializable { * @since 1.7 */ public CSVFormat withAllowDuplicateHeaderNames() { - return withAllowDuplicateHeaderNames(true); + return withAllowDuplicateHeaderNames(true); } /** @@ -1724,7 +1724,7 @@ public final class CSVFormat implements Serializable { * @since 1.7 */ public CSVFormat withAllowDuplicateHeaderNames(final boolean allowDuplicateHeaderNames) { - return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, + return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header, skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush, allowDuplicateHeaderNames); diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index a8b16c2c..3b3b3e7c 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -495,7 +495,7 @@ public final class CSVParser implements Iterable, Closeable { if (headerRecord != null) { for (int i = 0; i < headerRecord.length; i++) { final String header = headerRecord[i]; - final boolean containsHeader = header == null ? false : hdrMap.containsKey(header); + final boolean containsHeader = header != null && hdrMap.containsKey(header); final boolean emptyHeader = header == null || header.trim().isEmpty(); if (containsHeader) { if (!emptyHeader && !this.format.getAllowDuplicateHeaderNames()) { @@ -520,9 +520,9 @@ public final class CSVParser implements Iterable, Closeable { } } if (headerNames == null) { - headerNames = Collections.emptyList(); //immutable + headerNames = Collections.emptyList(); //immutable } else { - headerNames = Collections.unmodifiableList(headerNames); + headerNames = Collections.unmodifiableList(headerNames); } return new Headers(hdrMap, headerNames); } diff --git a/src/main/resources/checkstyle/checkstyle-suppressions.xml b/src/main/resources/checkstyle/checkstyle-suppressions.xml new file mode 100644 index 00000000..edc67836 --- /dev/null +++ b/src/main/resources/checkstyle/checkstyle-suppressions.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/src/main/resources/findbugs/findbugs-exclude-filter.xml b/src/main/resources/findbugs/findbugs-exclude-filter.xml new file mode 100644 index 00000000..da2896a2 --- /dev/null +++ b/src/main/resources/findbugs/findbugs-exclude-filter.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + diff --git a/src/main/resources/pmd/pmd-ruleset.xml b/src/main/resources/pmd/pmd-ruleset.xml new file mode 100644 index 00000000..8d52e45a --- /dev/null +++ b/src/main/resources/pmd/pmd-ruleset.xml @@ -0,0 +1,89 @@ + + + + + This ruleset checks the code for discouraged programming constructs. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index 913b5ead..75253f21 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -1471,4 +1471,45 @@ public class CSVPrinterTest { return CSVParser.parse(expected, format).getRecords().get(0).values(); } + /** + * Test to target the use of {@link IOUtils#copyLarge(java.io.Reader, Writer)} which directly + * buffers the value from the Reader to the Writer. + * + *

Requires the format to have no quote or escape character, value to be a + * {@link java.io.Reader Reader} and the output MUST be a + * {@link java.io.Writer Writer}.

+ * + * @throws IOException Not expected to happen + */ + @Test + public void testPrintReaderWithoutQuoteToWriter() throws IOException { + final StringWriter sw = new StringWriter(); + final String content = "testValue"; + try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(null))) { + final StringReader value = new StringReader(content); + printer.print(value); + } + assertEquals(content, sw.toString()); + } + + /** + * Test to target the use of {@link IOUtils#copy(java.io.Reader, Appendable)} which directly + * buffers the value from the Reader to the Appendable. + * + *

Requires the format to have no quote or escape character, value to be a + * {@link java.io.Reader Reader} and the output MUST NOT be a + * {@link java.io.Writer Writer} but some other Appendable.

+ * + * @throws IOException Not expected to happen + */ + @Test + public void testPrintReaderWithoutQuoteToAppendable() throws IOException { + final StringBuilder sb = new StringBuilder(); + final String content = "testValue"; + try (final CSVPrinter printer = new CSVPrinter(sb, CSVFormat.DEFAULT.withQuote(null))) { + final StringReader value = new StringReader(content); + printer.print(value); + } + assertEquals(content, sb.toString()); + } }