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();