Ruiqi Dong created CSV-321:
------------------------------

             Summary: CSVFormat.equals()/hashCode() ignores maxRows
                 Key: CSV-321
                 URL: https://issues.apache.org/jira/browse/CSV-321
             Project: Commons CSV
          Issue Type: Bug
    Affects Versions: 1.14.1
            Reporter: Ruiqi Dong


*Summary*
CSVFormat has a maxRows configuration field, but equals() and hashCode() both 
ignore it. Two format objects with different row limits can therefore compare 
equal.
 
*Affected code*
File: src/main/java/org/apache/commons/csv/CSVFormat.java
{code:java}
/** The maximum number of rows to process, excluding the header row. */
private long maxRows;

public Builder setMaxRows(final long maxRows) {
    this.maxRows = maxRows;
    return this;
}

private final long maxRows;

return allowMissingColumnNames == other.allowMissingColumnNames
        && ...
        && trailingData == other.trailingData
        && trailingDelimiter == other.trailingDelimiter
        && trim == other.trim;

@Override
public int hashCode() {
    return prime * result + Objects.hash(
            allowMissingColumnNames, autoFlush, commentMarker, delimiter,
            duplicateHeaderMode, escapeCharacter, ignoreEmptyLines,
            ignoreHeaderCase, ignoreSurroundingSpaces, lenientEof,
            nullString, quoteCharacter, quoteMode, quotedNullString,
            recordSeparator, skipHeaderRecord, trailingData,
            trailingDelimiter, trim);
}{code}
 
*Reproducer*
Add the following test to 
src/test/java/org/apache/commons/csv/CSVFormatTest.java:
{code:java}
@Test
void testEqualsMaxRows() {
    final CSVFormat right = CSVFormat.DEFAULT.builder().setMaxRows(10).get();
    final CSVFormat left = CSVFormat.DEFAULT.builder().setMaxRows(1000).get();
    assertNotEqualsFlip(right, left);
    assertNotEquals(right.hashCode(), left.hashCode());
} {code}
Run:

{code:java}
mvn -q -Dtest=org.apache.commons.csv.CSVFormatTest test {code}
Observed behavior:
{code:java}
CSVFormatTest.testEqualsMaxRows:256
expected: not equal but was: <Delimiter=<,> QuoteChar=<"> 
RecordSeparator=<\r\n> EmptyLines:ignored SkipHeaderRecord:false> {code}
Expected behavior:
Objects with different {{maxRows}} values should not compare equal.
 
{{maxRows}} changes the format's observable behavior by limiting how many 
records are processed. It should therefore participate in equality and hashing.
 
 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to