csv-comparator

Utils for comparison and assertion CSV files in Java

License

License

MIT
Categories

Categories

H2 Data Databases CSV Data Formats
GroupId

GroupId

com.github.ngoanh2n
ArtifactId

ArtifactId

csv-comparator
Last Version

Last Version

1.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

csv-comparator
Utils for comparison and assertion CSV files in Java
Project URL

Project URL

https://github.com/ngoanh2n/csv-comparator
Source Code Management

Source Code Management

http://github.com/ngoanh2n/csv-comparator/tree/master

Download csv-comparator

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.ngoanh2n/csv-comparator/ -->
<dependency>
    <groupId>com.github.ngoanh2n</groupId>
    <artifactId>csv-comparator</artifactId>
    <version>1.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.ngoanh2n/csv-comparator/
implementation 'com.github.ngoanh2n:csv-comparator:1.2.0'
// https://jarcasting.com/artifacts/com.github.ngoanh2n/csv-comparator/
implementation ("com.github.ngoanh2n:csv-comparator:1.2.0")
'com.github.ngoanh2n:csv-comparator:jar:1.2.0'
<dependency org="com.github.ngoanh2n" name="csv-comparator" rev="1.2.0">
  <artifact name="csv-comparator" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.ngoanh2n', module='csv-comparator', version='1.2.0')
)
libraryDependencies += "com.github.ngoanh2n" % "csv-comparator" % "1.2.0"
[com.github.ngoanh2n/csv-comparator "1.2.0"]

Dependencies

runtime (4)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.30
com.google.guava : guava jar 28.2-jre
com.univocity : univocity-parsers jar 2.8.4
com.github.albfernandez : juniversalchardet jar 2.3.2

test (3)

Group / Artifact Type Version
org.slf4j : slf4j-log4j12 jar 1.7.30
org.junit.jupiter : junit-jupiter-api jar 5.7.0
org.junit.jupiter : junit-jupiter-engine jar 5.7.0

Project Modules

There are no modules declared in this project.

GitHub stars GitHub watchers GitHub forks

Maven Central GitHub release javadoc Build Status License: MIT badge-jdk GitHub contributors GitHub issues GitHub issues-closed GitHub pull-requests GitHub pull-requests closed

CSV Comparator

Table of Contents generated with DocToc

How To Use

Gradle Project

Add the csv-comparator dependency to your build.gradle

dependencies {
    testImplementation("com.github.ngoanh2n:csv-comparator:1.2.0")
}

Maven Project

Add the csv-comparator dependency to your pom

<dependencies>
    [...]
    <dependency>
        <groupId>com.github.ngoanh2n</groupId>
        <artifactId>csv-comparator</artifactId>
        <version>1.2.0</version>
        <scope>test</scope>
    </dependency>
    [...]
</dependencies>

How To Apply

Compare 2 CSV files formatted columns:

id,email,firstname,lastname,age,note

Provide CsvComparisonSource

CsvComparisonSource<File> source = CsvComparisonSource.create(expectedCsv, actualCsv);

Build CsvComparisonOptions

CsvComparisonOptions options = CsvComparisonOptions
                .builder()
                .setColumns(1, 2, 3)
                .setIdentityColumn(0) // position starts with 0 in array [1, 2, 3]
                .build();

If you want to use column names:

CsvComparisonOptions options = CsvComparisonOptions
                .builder()
                .setColumns("email", "firstname", "lastname")
                .setIdentityColumn("email")
                .build();

Do CsvComparator

CsvComparisonResult result = new CsvComparator(source, options).compare();

Asssert CsvComparisonResult

CsvComparisonResult.hasDiff()
CsvComparisonResult.hasDeleted()
CsvComparisonResult.hasInserted()
CsvComparisonResult.hasModified()
CsvComparisonResult.rowsKept()
CsvComparisonResult.rowsDeleted()
CsvComparisonResult.rowsInserted()
CsvComparisonResult.rowsModified()

By default, result files which is created after comparing is located at build/comparator/csv/{yyyyMMdd.HHmmss.SSS}/

Use CsvComparisonVisitor To Walk Through CsvComparator

CsvComparisonVisitor.visitStarted(CsvComparisonSource<?> source)
CsvComparisonVisitor.visitEnded(CsvComparisonSource<?> source)
CsvComparisonVisitor.rowKept(String[] row, String[] headers, CsvComparisonOptions options)
CsvComparisonVisitor.rowDeleted(String[] row, String[] headers, CsvComparisonOptions options)
CsvComparisonVisitor.rowInserted(String[] row, String[] headers, CsvComparisonOptions options)
CsvComparisonVisitor.rowModified(String[] row, String[] headers, CsvComparisonOptions options)
CsvComparisonResult result = new CsvComparator(source, options, visitor).compare();

Versions

Version
1.2.0
1.1.0
1.0.0
v1.0.0