Skip to content

ngoanh2n/csv-comparator

Repository files navigation

Java Maven GitHub Actions CircleCI

Table of Contents

Declaration

Gradle

Add dependency to build.gradle.

implementation("com.github.ngoanh2n:csv-comparator:1.10.0")

Maven

Add dependency to pom.xml.

<dependency>
  <groupId>com.github.ngoanh2n</groupId>
  <artifactId>csv-comparator</artifactId>
  <version>1.10.0</version>
</dependency>

Usage

Comparison

Example: CSV is formatted columns [id,email,firstname,lastname,age,note].

  1. Compare 2 CSV files
    File expectedCsvFile = new File("data/expected/file.csv");
    File actualCsvFile = new File("data/actual/file.csv");
    
    CsvComparisonOptions options = CsvComparisonOptions
            .builder()
            .selectColumns("email", "firstname", "lastname")
            .selectColumnId("email")
            //.selectColumns(1, 2, 3)
            //.selectColumnId(1)
            .build();
    CsvComparisonResult result = CsvComparator.compare(expectedCsvFile, actualCsvFile, options);
  2. Compare 2 CSV directories
    Path expectedCsvDir = Paths.get("data/expected");
    Path actualCsvDir = Paths.get("data/actual");
    
    CsvComparisonOptions options = CsvComparisonOptions
            .builder()
            .selectColumns("email", "firstname", "lastname")
            .selectColumnId("email")
            //.selectColumns(1, 2, 3)
            //.selectColumnId(1)
            .build();
    CsvBulkComparisonResult result = CsvComparator.compare(expectedCsvDir, actualCsvDir, options);

Result

CsvComparisonResult is the result of CsvComparator.compare(expectedCsvFile, actualCsvFile, options).

boolean hasDiff = CsvComparisonResult.hasDiff();
boolean hasDeletion = CsvComparisonResult.hasDeletion();
boolean hasInsertion = CsvComparisonResult.hasInsertion();
boolean hasModification = CsvComparisonResult.hasModification();
List<String[]> keptRows = CsvComparisonResult.getKeptRows();
List<String[]> deletedRows = CsvComparisonResult.getDeletedRows();
List<String[]> insertedRows = CsvComparisonResult.getInsertedRows();
List<String[]> modifiedRows = CsvComparisonResult.getModifiedRows();

CsvBulkComparisonResult is the result of CsvComparator.compare(expectedCsvDir, actualCsvDir, options).

boolean hasDiff = CsvBulkComparisonResult.hasDiff();
int diffTotal = CsvBulkComparisonResult.getDiffTotal();
List<CsvComparisonResult> diffResults = CsvBulkComparisonResult.getDiffResults();

Visitor

CsvComparisonVisitor for walking through CsvComparator.

  • CsvComparisonVisitor#comparisonStarted(CsvComparisonOptions, File, File)
  • CsvComparisonVisitor#rowKept(CsvComparisonOptions, String[], String[])
  • CsvComparisonVisitor#rowDeleted(CsvComparisonOptions, String[], String[])
  • CsvComparisonVisitor#rowInserted(CsvComparisonOptions, String[], String[])
  • CsvComparisonVisitor#rowModified(CsvComparisonOptions, String[], String[], List)
  • CsvComparisonVisitor#comparisonFinished(CsvComparisonOptions, File, File, CsvComparisonResult)

Output

CsvComparisonOutput for writing comparison output files to specified location.
An implementation of CsvComparisonVisitor.

  • The output is always created at build/ngoanh2n/csv/{yyyyMMdd.HHmmss.SSS} by default
  • Use CsvComparisonResultOptions to adjust the output behaviors. And set to CsvComparisonOptions
    CsvComparisonResultOptions resultOptions = CsvComparisonResultOptions
           .builder()
           .writeOutputs(false)                       // Default to true
           //.location(Paths.get("build/custom"))     // Default to build/ngoanh2n/csv
           .build();
    CsvComparisonOptions options = CsvComparisonOptions
            .builder()
            .resultOptions(resultOptions)             // Default to CsvComparisonResultOptions.defaults()
            .build();

Allure Report

When using Allure as a report framework, should use com.github.ngoanh2n:csv-comparator-allure.
csv-comparator-allure README.