Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hack file compare #3

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/maven_file_compare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Test For File Compare

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'adopt'
cache: maven

- name: Build & Run tests with Maven
run: mvn clean test -DsuiteXmlFile=testNGsuite/fileCompare.xml

- name: Upload Test Result as Artifact
uses: actions/upload-artifact@v3
with:
name: file-compare-result
path: src/test/resources/results.xlsx
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ target/
hs_err_pid*

test-output/*
src/test/resources/results.xlsx
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@
<artifactId>failsafe</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.17.0</version> <!-- Use the latest version -->
</dependency>

<!-- Apache POI for Excel sheet creation -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
</dependencies>

<build>
Expand Down
99 changes: 99 additions & 0 deletions src/test/java/sample/FileCompare.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package sample;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.Test;

import java.io.*;
import java.util.*;

public class FileCompare {

@Test
public static void compare() throws Exception {
// Input file paths (from resources folder)
String filePath1 = "src/test/resources/file1.txt";
String filePath2 = "src/test/resources/file2.txt";
String outputExcelPath = "src/test/resources/results.xlsx";

// Batch size for processing lines
int batchSize = 100;

// Run the comparison
compareFiles(filePath1, filePath2, outputExcelPath, batchSize);
System.out.println("Comparison completed. Results saved in " + outputExcelPath);
}

public static void compareFiles(String filePath1, String filePath2, String outputExcelPath, int batchSize)
throws Exception {
try (BufferedReader reader1 = new BufferedReader(new FileReader(filePath1));
BufferedReader reader2 = new BufferedReader(new FileReader(filePath2))) {

Map<Integer, String[]> mismatches = new LinkedHashMap<>();

List<String> batch1 = new ArrayList<>();
List<String> batch2 = new ArrayList<>();
int globalLineCount = 0;

String line1, line2;
while ((line1 = reader1.readLine()) != null && (line2 = reader2.readLine()) != null) {
batch1.add(line1);
batch2.add(line2);
globalLineCount++;

// Process the batch if it reaches the specified batch size
if (batch1.size() == batchSize) {
processBatch(batch1, batch2, mismatches, globalLineCount - batchSize + 1);
batch1.clear();
batch2.clear();
}
}

// Process any remaining lines
if (!batch1.isEmpty()) {
processBatch(batch1, batch2, mismatches, globalLineCount - batch1.size() + 1);
}

// Write mismatches to Excel
writeMismatchesToExcel(mismatches, outputExcelPath);
}
}

private static void processBatch(List<String> batch1, List<String> batch2, Map<Integer, String[]> mismatches,
int startLineNumber) {
for (int i = 0; i < batch1.size(); i++) {
if (!batch1.get(i).equals(batch2.get(i))) {
int globalLineNumber = startLineNumber + i;
mismatches.put(globalLineNumber, new String[]{batch1.get(i), batch2.get(i)});
}
}
System.out.println("Processed batch starting at line " + startLineNumber);
}

private static void writeMismatchesToExcel(Map<Integer, String[]> mismatches, String outputExcelPath)
throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Mismatches");

// Header row
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("Line Number");
header.createCell(1).setCellValue("File 1 Content");
header.createCell(2).setCellValue("File 2 Content");

// Populate mismatches
int rowNum = 1;
for (Map.Entry<Integer, String[]> entry : mismatches.entrySet()) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(entry.getKey());
row.createCell(1).setCellValue(entry.getValue()[0]);
row.createCell(2).setCellValue(entry.getValue()[1]);
}

// Save to file
try (FileOutputStream fos = new FileOutputStream(outputExcelPath)) {
workbook.write(fos);
}
workbook.close();
}
}
500 changes: 500 additions & 0 deletions src/test/resources/file1.txt

Large diffs are not rendered by default.

500 changes: 500 additions & 0 deletions src/test/resources/file2.txt

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions testNGsuite/fileCompare.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="API Automation">
<test name="Address Service Regression Test Cases">
<classes>
<class name="sample.FileCompare"/>
</classes>
</test>
</suite>
Loading