Skip to content

Commit

Permalink
added result with possibility to output both different lines from new
Browse files Browse the repository at this point in the history
and old files
  • Loading branch information
mareknovotny committed Nov 30, 2015
1 parent 42e2551 commit dbaa673
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/export1.csv
/export2.csv
/diff.csv
/resultDiffedLines.csv
2 changes: 1 addition & 1 deletion compare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# $2 - new file
#

java -jar target/csvcompare-0.0.1-SNAPSHOT.jar -o $1 -n $2 -d ,
java -jar target/csvcompare-0.0.1-SNAPSHOT.jar -o $1 -n $2 -b -d ,
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>
<groupId>org.jboss.windup.utils</groupId>
<artifactId>csvcompare</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.2-SNAPSHOT</version>
<name>csvcompare</name>
<description>Compare util for CSV files</description>

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/jboss/windup/utils/CsvCompareOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,25 @@ public class CsvCompareOptions
private String oldFile;
private String newFile;
private char delimiter = DEFAULT_CSV_DELIMITER;
private boolean exportedBothDifferences;


/**
* @return the exportedBothDifferences
*/
public boolean isExportedBothDifferences()
{
return exportedBothDifferences;
}

/**
* @param exportedBothDifferences the exportedBothDifferences to set
*/
public void setExportedBothDifferences(boolean exportedBothDifferences)
{
this.exportedBothDifferences = exportedBothDifferences;
}

public void parse (String[] args) throws Exception {
CommandLineParser parser = new DefaultParser();
if (args.length <=1){
Expand Down Expand Up @@ -62,6 +79,12 @@ public void parse (String[] args) throws Exception {
logger.debug("CSV delimiter is " + getDelimiter());
}

if (line.hasOption('b')) {
this.setExportedBothDifferences(true);
logger.debug("Diffed export will have both different lines");
}


//validations

}
Expand Down Expand Up @@ -107,6 +130,7 @@ private static Options getCMdOptions() {
options.addOption( Option.builder("o").required(true).hasArg(true).argName("URL of CSV file").longOpt("old-file").build());
options.addOption( Option.builder("n").required(true).hasArg(true).argName("URL of CSV file").longOpt("new-file").build());
options.addOption( Option.builder("d").hasArg(true).argName("delimiter in CSV file").longOpt("csv-delimiter").build());
options.addOption( Option.builder("b").argName("both differences output in CSV file").longOpt("output-both-diff").build());

return options;
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/jboss/windup/utils/MainClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ public static void main(String[] args)
CsvWindupExportLoader loader1 = new CsvWindupExportLoader(new URL(csvCompareOptions.getOldFile()), csvCompareOptions.getDelimiter());
CsvWindupExportLoader loader2 = new CsvWindupExportLoader(new URL(csvCompareOptions.getNewFile()), csvCompareOptions.getDelimiter());
WindupReportComparison reportCmp = new WindupReportComparison(loader1.parseCSV(), loader2.parseCSV());
List<ReportModel> listDiff = reportCmp.compareNewAndOldReports();
if (listDiff.size()> 0) {
List<ReportModel> listDiff = null;
if ( csvCompareOptions.isExportedBothDifferences() ) {
listDiff =reportCmp.compareNewAndOldReportsWithDiffLines();
} else {
listDiff = reportCmp.compareNewAndOldReports();
}

if (listDiff != null && listDiff.size()> 0) {
logger.debug(listDiff);
(new ExportReportModelToCSV(listDiff)).export(new File("diff.csv"));
System.exit(1);
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/jboss/windup/utils/WindupReportComparison.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@ public List<ReportModel> compareNewAndOldReports() {

return result;
}

public List<ReportModel> compareNewAndOldReportsWithDiffLines() {
List<ReportModel> newReportDiff = new ArrayList<>();
List<ReportModel> oldReportDiff = new ArrayList<>();

if (originalReport == null || newReport == null) {
logger.debug("One of the reports is empty");
return null;
}

Set<ReportModel> intersect = new HashSet<>(originalReport);
intersect.retainAll(newReport);
logger.trace("Intersection has got " + intersect.size());

newReportDiff.addAll(newReport);
logger.trace("Result has got " + newReportDiff.size());
newReportDiff.removeAll(intersect);
logger.trace("Result has got " + newReportDiff.size());

oldReportDiff.addAll(originalReport);
logger.trace("Result has got " + oldReportDiff.size());
oldReportDiff.removeAll(intersect);
logger.trace("Result has got " + oldReportDiff.size());

List<ReportModel> diffedResult = new ArrayList<>();
diffedResult.addAll(oldReportDiff);
diffedResult.addAll(newReportDiff);

return diffedResult;
}

public WindupReportComparison(List<ReportModel> oldList, List<ReportModel> newList) {
this.newReport = newList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CsvCompareOptionsTest
@Test
public void testAllArgsParse()
{
String[] testArgs = new String[] {"-o","file1.txt","-n","file2.txt", "-d",";"};
String[] testArgs = new String[] {"-o","file1.txt","-n","file2.txt", "-d",";", "-b"};
CsvCompareOptions ccOptions = new CsvCompareOptions();
try
{
Expand All @@ -43,6 +43,7 @@ public void testAllArgsParse()
assertEquals("Delimiter is not set correctly", ';', ccOptions.getDelimiter());
assertEquals("new-file is not set correctly", "file2.txt", ccOptions.getNewFile());
assertEquals("old-file is not set correctly", "file1.txt", ccOptions.getOldFile());
assertTrue(ccOptions.isExportedBothDifferences());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,24 @@ public void testCompareNewAndOldReports() throws Exception
List<ReportModel> result = cmp.compareNewAndOldReports();
assertNotNull(result);
assertEquals(1, result.size());
assertEquals(new Integer(0),( (ReportModel) result.get(0)).getStoryPoints());
//System.out.println(result);
//(new ExportReportModelToCSV(result)).export(new File("result.csv"));
}

@Test
public void testCompareNewAndOldReportsWithDiffLines() throws Exception
{
WindupReportComparison cmp = new WindupReportComparison(createList1(), createList2());
List<ReportModel> result = cmp.compareNewAndOldReportsWithDiffLines();
assertNotNull(result);
assertEquals(2, result.size());
assertEquals(new Integer(3),( (ReportModel) result.get(0)).getStoryPoints());
assertEquals(new Integer(0),( (ReportModel) result.get(1)).getStoryPoints());
//System.out.println(result);
(new ExportReportModelToCSV(result)).export(new File("resultDiffedLines.csv"));
}

@Test
public void testLoadAndCompareNewAndOldReports() throws Exception
{
Expand Down

0 comments on commit dbaa673

Please sign in to comment.