From 835bdef6b7e80da2227bf3eab760ab879b1d4152 Mon Sep 17 00:00:00 2001 From: Anujrathee7 Date: Wed, 16 Oct 2024 17:42:40 +0300 Subject: [PATCH 1/3] unit test --- pom.xml | 15 +++--- tests/ExcelDiffCheckerTest.java | 92 +++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 tests/ExcelDiffCheckerTest.java diff --git a/pom.xml b/pom.xml index 3295322..2214822 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,7 @@ + org.apache.maven.plugins maven-compiler-plugin 3.6.1 @@ -54,7 +55,6 @@ - org.apache.maven.plugins maven-jar-plugin 3.0.2 @@ -68,14 +68,16 @@ - + - maven-compiler-plugin + org.apache.maven.plugins + maven-surefire-plugin + 2.22.2 - 1.8 - 1.8 + tests + org.apache.maven.plugins maven-assembly-plugin @@ -110,5 +112,4 @@ - - \ No newline at end of file + diff --git a/tests/ExcelDiffCheckerTest.java b/tests/ExcelDiffCheckerTest.java new file mode 100644 index 0000000..cdd6e5f --- /dev/null +++ b/tests/ExcelDiffCheckerTest.java @@ -0,0 +1,92 @@ +package edu.abhi.poi.excel; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import java.io.File; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.Future; + +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +public class ExcelDiffCheckerTest { + + @Mock + private XSSFWorkbook mockWorkbook1; + + @Mock + private XSSFWorkbook mockWorkbook2; + + @Mock + private XSSFSheet mockSheet1; + + @Mock + private XSSFSheet mockSheet2; + + @Before + public void setUp() { + MockitoAnnotations.openMocks(this); + + // Mocking the behavior of the workbooks and sheets + when(mockWorkbook1.getSheet("Sheet1")).thenReturn(mockSheet1); + when(mockWorkbook2.getSheet("Sheet1")).thenReturn(mockSheet2); + when(mockWorkbook1.getSheet("SheetMissing")).thenReturn(null); + when(mockWorkbook2.getSheet("SheetMissing")).thenReturn(null); + } + + @Test + public void testProcessSpecifiedSheets() { + // Mock the specified sheets + ExcelDiffChecker.specifiedSheets = "Sheet1,SheetMissing"; + List tasks = ExcelDiffChecker.processSpecifiedSheets(mockWorkbook1, mockWorkbook2); + + // Verify the task count and behavior + assertEquals(1, tasks.size()); + } + + @Test + public void testProcessAllSheets() { + when(mockWorkbook1.iterator()).thenReturn(Arrays.asList(mockSheet1).iterator()); + + List tasks = ExcelDiffChecker.processAllSheets(mockWorkbook1, mockWorkbook2); + + // There should be one task since both Sheet1 are present + assertEquals(1, tasks.size()); + } + + @Test + public void testExecuteAllTasks() throws Exception { + SheetProcessorTask task = mock(SheetProcessorTask.class); + List tasks = Arrays.asList(task); + + Future future = mock(Future.class); + when(future.get()).thenReturn(new CallableValue(true)); + when(task.call()).thenReturn(new CallableValue(true)); + + List> results = ExcelDiffChecker.executeAllTasks(tasks); + + // Check the result from future + assertEquals(1, results.size()); + } + + @Test + public void testProcessOptionsWithValidArgs() { + String[] args = {"-b", "base.xlsx", "-t", "target.xlsx"}; + ExcelDiffChecker.processOptions(args); + + assertEquals("base.xlsx", ExcelDiffChecker.FILE_NAME1); + assertEquals("target.xlsx", ExcelDiffChecker.FILE_NAME2); + } + + @Test(expected = RuntimeException.class) + public void testProcessOptionsWithMissingRequiredArgs() { + String[] args = {"-b", "base.xlsx"}; + ExcelDiffChecker.processOptions(args); + } +} From 9303cc1cf9fc58f34ae41a97c715579e125c5169 Mon Sep 17 00:00:00 2001 From: Anujrathee7 Date: Wed, 16 Oct 2024 18:02:54 +0300 Subject: [PATCH 2/3] ad unit test --- pom.xml | 15 +++++++-------- .../poi/excel/tests}/ExcelDiffCheckerTest.java | 13 +++++++++++-- 2 files changed, 18 insertions(+), 10 deletions(-) rename {tests => src/main/java/edu/abhi/poi/excel/tests}/ExcelDiffCheckerTest.java (81%) diff --git a/pom.xml b/pom.xml index 2214822..3295322 100644 --- a/pom.xml +++ b/pom.xml @@ -46,7 +46,6 @@ - org.apache.maven.plugins maven-compiler-plugin 3.6.1 @@ -55,6 +54,7 @@ + org.apache.maven.plugins maven-jar-plugin 3.0.2 @@ -68,16 +68,14 @@ - + - org.apache.maven.plugins - maven-surefire-plugin - 2.22.2 + maven-compiler-plugin - tests + 1.8 + 1.8 - org.apache.maven.plugins maven-assembly-plugin @@ -112,4 +110,5 @@ - + + \ No newline at end of file diff --git a/tests/ExcelDiffCheckerTest.java b/src/main/java/edu/abhi/poi/excel/tests/ExcelDiffCheckerTest.java similarity index 81% rename from tests/ExcelDiffCheckerTest.java rename to src/main/java/edu/abhi/poi/excel/tests/ExcelDiffCheckerTest.java index cdd6e5f..f35d242 100644 --- a/tests/ExcelDiffCheckerTest.java +++ b/src/main/java/edu/abhi/poi/excel/tests/ExcelDiffCheckerTest.java @@ -6,6 +6,7 @@ import java.io.File; import java.util.Arrays; import java.util.List; +import java.util.concurrent.Callable; import java.util.concurrent.Future; import org.apache.poi.xssf.usermodel.XSSFSheet; @@ -73,11 +74,12 @@ public void testExecuteAllTasks() throws Exception { // Check the result from future assertEquals(1, results.size()); + assertTrue(results.get(0).get().isSuccessful()); // Assuming CallableValue has a method isSuccessful() } @Test public void testProcessOptionsWithValidArgs() { - String[] args = {"-b", "base.xlsx", "-t", "target.xlsx"}; + String[] args = {"base.xlsx", "target.xlsx"}; // Adjusting to match the required args ExcelDiffChecker.processOptions(args); assertEquals("base.xlsx", ExcelDiffChecker.FILE_NAME1); @@ -86,7 +88,14 @@ public void testProcessOptionsWithValidArgs() { @Test(expected = RuntimeException.class) public void testProcessOptionsWithMissingRequiredArgs() { - String[] args = {"-b", "base.xlsx"}; + String[] args = {"base.xlsx"}; // Missing second required arg ExcelDiffChecker.processOptions(args); } + + @Test + public void testProcessOptionsWithInvalidArgs() { + String[] args = {"-invalid", "base.xlsx", "target.xlsx"}; + ExcelDiffChecker.processOptions(args); // Assuming it should throw or handle an invalid argument + // You may want to add assertions here based on how your method handles invalid args + } } From 9608b25e9313ee0b4a56f38b03b0c98f3818e9a3 Mon Sep 17 00:00:00 2001 From: Anujrathee7 Date: Wed, 16 Oct 2024 20:48:16 +0300 Subject: [PATCH 3/3] written test --- .classpath | 25 ++++ .project | 11 ++ .settings/org.eclipse.jdt.apt.core.prefs | 2 + .settings/org.eclipse.jdt.core.prefs | 1 + Automation_script.py | 116 ++++++++++++++++++ README.txt | 14 +++ .../poi/excel/tests/ExcelDiffCheckerTest.java | 101 --------------- .../ExcelDiffCheckerIntegrationTest.java | 48 ++++++++ 8 files changed, 217 insertions(+), 101 deletions(-) create mode 100644 .settings/org.eclipse.jdt.apt.core.prefs create mode 100644 Automation_script.py create mode 100644 README.txt delete mode 100644 src/main/java/edu/abhi/poi/excel/tests/ExcelDiffCheckerTest.java create mode 100644 src/test/java/edu/abhi/poi/excel/ExcelDiffCheckerIntegrationTest.java diff --git a/.classpath b/.classpath index 740235a..da472b6 100644 --- a/.classpath +++ b/.classpath @@ -16,6 +16,7 @@ + @@ -28,5 +29,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project index 1a0c884..b8e9756 100644 --- a/.project +++ b/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature + + + 1729100864382 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/.settings/org.eclipse.jdt.apt.core.prefs b/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 0000000..d4313d4 --- /dev/null +++ b/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=false diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index cac0df4..fa50df0 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -7,5 +7,6 @@ org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.processAnnotations=disabled org.eclipse.jdt.core.compiler.release=disabled org.eclipse.jdt.core.compiler.source=1.8 diff --git a/Automation_script.py b/Automation_script.py new file mode 100644 index 0000000..0c5dafd --- /dev/null +++ b/Automation_script.py @@ -0,0 +1,116 @@ +import subprocess +import os +import pytest + +# Define base directory and paths to your test files +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) +EXCEL_DIFF_JAR_PATH = os.path.join(BASE_DIR, "excel-diff-checker-1/apps/excel-diff-checker.jar") +BASE_FILE_PATH = os.path.join(BASE_DIR, "Input/file.xlsx") +DIFF_FILE_PATH = os.path.join(BASE_DIR, "Input/file_diff.xlsx") +EMPTY_FILE_PATH = os.path.join(BASE_DIR, "Input/file_empty.xlsx") +IDENTICAL_FILE_PATH = os.path.join(BASE_DIR, "Input/file_identical.xlsx") +LARGE_FILE_PATH = os.path.join(BASE_DIR, "Input/large_file.xlsx") + +# Helper function to construct the output file path +def get_output_file_path(base_file, target_file): + base_name = os.path.splitext(os.path.basename(base_file))[0] + target_name = os.path.splitext(os.path.basename(target_file))[0] + output_file_name = f"{base_name} vs {target_name}.xlsx" + return os.path.join(BASE_DIR, output_file_name) + +# Helper function to run the excel-diff-checker command +def run_diff_checker(base_file, target_file, options=None): + command = f"java -jar {EXCEL_DIFF_JAR_PATH} -b {base_file} -t {target_file}" + if options: + command += " " + options + result = subprocess.run(command, shell=True, capture_output=True, text=True) + return result.stdout, result.stderr + +@pytest.fixture # Reference https://docs.pytest.org/en/stable/explanation/fixtures.html +def clean_up(): + yield + # Clean up generated output files after tests + for base_file in [BASE_FILE_PATH, DIFF_FILE_PATH, EMPTY_FILE_PATH, IDENTICAL_FILE_PATH, LARGE_FILE_PATH]: + for target_file in [DIFF_FILE_PATH, EMPTY_FILE_PATH, IDENTICAL_FILE_PATH, LARGE_FILE_PATH]: + output_file_path = get_output_file_path(base_file, target_file) + if os.path.exists(output_file_path): + os.remove(output_file_path) + +# Test case F-1: Basic Functionality Test +def test_basic_functionality(clean_up): + stdout, stderr = run_diff_checker(BASE_FILE_PATH, DIFF_FILE_PATH) + OUTPUT_FILE_PATH = get_output_file_path(BASE_FILE_PATH, DIFF_FILE_PATH) + assert os.path.exists(OUTPUT_FILE_PATH), "Output file was not generated." + assert "Diff" in stdout, "No differences found in the output." + assert "Diff at Row[3]" in stdout, "Expected specific difference in age not found." + +# Test case F-2: Identical File Diff Check +def test_identical_files(clean_up): + stdout, stderr = run_diff_checker(BASE_FILE_PATH, IDENTICAL_FILE_PATH) + OUTPUT_FILE_PATH = get_output_file_path(BASE_FILE_PATH, IDENTICAL_FILE_PATH) + assert not os.path.exists(OUTPUT_FILE_PATH), "No output file should be generated for identical files." + assert "No diff found!" in stdout, "Unexpected output for identical files." + +# Test case F-3: Diffs in Comments/Notes +def test_diffs_in_comments(clean_up): + stdout, stderr = run_diff_checker(BASE_FILE_PATH, DIFF_FILE_PATH) + assert "Diff at Row[3] of Sheet[Sheet1]" in stdout, "Differences not correctly reported in comments." + +# Test case F-4: Cell Difference Detection +def test_cell_difference_detection(clean_up): + stdout, stderr = run_diff_checker(BASE_FILE_PATH, DIFF_FILE_PATH) + assert "Diff at Row[3]" in stdout, "Cell differences were not detected properly." + +# Test case F-5: Empty File Comparison +def test_empty_file_comparison(clean_up): + stdout, stderr = run_diff_checker(EMPTY_FILE_PATH, EMPTY_FILE_PATH) + OUTPUT_FILE_PATH = get_output_file_path(EMPTY_FILE_PATH, EMPTY_FILE_PATH) + assert not os.path.exists(OUTPUT_FILE_PATH), "No output file should be generated for empty files." + assert "No diff found!" in stdout, "Unexpected output for empty file comparison." + +# Test case F-6: Specific Sheet Comparison (Option -s) +def test_specific_sheet_comparison(clean_up): + options = "-s Sheet1" + stdout, stderr = run_diff_checker(BASE_FILE_PATH, DIFF_FILE_PATH, options) + OUTPUT_FILE_PATH = get_output_file_path(BASE_FILE_PATH, DIFF_FILE_PATH) + assert os.path.exists(OUTPUT_FILE_PATH), "Output file was not generated for specific sheet comparison." + assert "Diff" in stdout, "No differences found in the output for specific sheet comparison." + assert "Diff at Row[3]" in stdout, "Expected specific difference in age not found." + +# Test case F-7: Mixed Data Types +def test_mixed_data_types(clean_up): + stdout, stderr = run_diff_checker(BASE_FILE_PATH, DIFF_FILE_PATH) + OUTPUT_FILE_PATH = get_output_file_path(BASE_FILE_PATH, DIFF_FILE_PATH) + assert os.path.exists(OUTPUT_FILE_PATH), "Output file was not generated for mixed data type comparison." + assert "Diff" in stdout, "No differences found in the output for mixed data types." + assert "Diff at Row[3]" in stdout, "Expected name not found in the differences." + +# Test case F-8: Redundant Row Removal +def test_redundant_row_removal(clean_up): + stdout, stderr = run_diff_checker(BASE_FILE_PATH, DIFF_FILE_PATH) + assert "Diff at Row" in stdout, "Redundant row differences not handled properly." + +# Test case F-9: Large File Comparison +def test_large_file_comparison(clean_up): + stdout, stderr = run_diff_checker(BASE_FILE_PATH, LARGE_FILE_PATH) + OUTPUT_FILE_PATH = get_output_file_path(BASE_FILE_PATH, LARGE_FILE_PATH) + assert os.path.exists(OUTPUT_FILE_PATH), "Output file was not generated for large file comparison." + assert "Diff" in stdout, "No differences found in the output for large file comparison." + +# Test case F-10: Use of 'r' Option +def test_use_of_r_option(clean_up): + options = "-r" + stdout, stderr = run_diff_checker(BASE_FILE_PATH, DIFF_FILE_PATH, options) + assert "Diff at Cell" in stdout, "Differences not reported correctly when using 'r' option." + assert not os.path.exists(get_output_file_path(BASE_FILE_PATH, DIFF_FILE_PATH)), "No output file should be generated with 'r' option." + +# Test case F-11: Non-Existent Sheet Name +def test_non_existent_sheet_name(clean_up): + options = "-s NonExistentSheet" + stdout, stderr = run_diff_checker(BASE_FILE_PATH, DIFF_FILE_PATH, options) + assert "doesn't exist in both workbooks" in stdout, "Non-existent sheet name was not handled properly." + +# Test case F-12: Invalid File Path +def test_file_not_found(clean_up): + stdout, stderr = run_diff_checker("invalid_path.xlsx", DIFF_FILE_PATH) + assert "The system cannot find the file specified" in stdout or "The system cannot find the file specified" in stderr, "No error message for missing file." \ No newline at end of file diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..e0790f8 --- /dev/null +++ b/README.txt @@ -0,0 +1,14 @@ +Excel Diff Checker Tester: +Testing suite for the Excel Diff Checker tool. + +Requirements: +Python 3.x +pytest +Java Runtime Environment (JRE) + +Running tests: +To use the testing suite, make sure that the tool is included in the same folder as this script in a folder named "excel-diff-checker-1". As an example, a version of the tool is included in this repository. +After acquiring this repository and installing requirements, run pytest Automation_script.py. + +Use of AI: +GPT-4o and GPT-4o mini were used for debugging and adding functional comments. \ No newline at end of file diff --git a/src/main/java/edu/abhi/poi/excel/tests/ExcelDiffCheckerTest.java b/src/main/java/edu/abhi/poi/excel/tests/ExcelDiffCheckerTest.java deleted file mode 100644 index f35d242..0000000 --- a/src/main/java/edu/abhi/poi/excel/tests/ExcelDiffCheckerTest.java +++ /dev/null @@ -1,101 +0,0 @@ -package edu.abhi.poi.excel; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - -import java.io.File; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.Future; - -import org.apache.poi.xssf.usermodel.XSSFSheet; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -public class ExcelDiffCheckerTest { - - @Mock - private XSSFWorkbook mockWorkbook1; - - @Mock - private XSSFWorkbook mockWorkbook2; - - @Mock - private XSSFSheet mockSheet1; - - @Mock - private XSSFSheet mockSheet2; - - @Before - public void setUp() { - MockitoAnnotations.openMocks(this); - - // Mocking the behavior of the workbooks and sheets - when(mockWorkbook1.getSheet("Sheet1")).thenReturn(mockSheet1); - when(mockWorkbook2.getSheet("Sheet1")).thenReturn(mockSheet2); - when(mockWorkbook1.getSheet("SheetMissing")).thenReturn(null); - when(mockWorkbook2.getSheet("SheetMissing")).thenReturn(null); - } - - @Test - public void testProcessSpecifiedSheets() { - // Mock the specified sheets - ExcelDiffChecker.specifiedSheets = "Sheet1,SheetMissing"; - List tasks = ExcelDiffChecker.processSpecifiedSheets(mockWorkbook1, mockWorkbook2); - - // Verify the task count and behavior - assertEquals(1, tasks.size()); - } - - @Test - public void testProcessAllSheets() { - when(mockWorkbook1.iterator()).thenReturn(Arrays.asList(mockSheet1).iterator()); - - List tasks = ExcelDiffChecker.processAllSheets(mockWorkbook1, mockWorkbook2); - - // There should be one task since both Sheet1 are present - assertEquals(1, tasks.size()); - } - - @Test - public void testExecuteAllTasks() throws Exception { - SheetProcessorTask task = mock(SheetProcessorTask.class); - List tasks = Arrays.asList(task); - - Future future = mock(Future.class); - when(future.get()).thenReturn(new CallableValue(true)); - when(task.call()).thenReturn(new CallableValue(true)); - - List> results = ExcelDiffChecker.executeAllTasks(tasks); - - // Check the result from future - assertEquals(1, results.size()); - assertTrue(results.get(0).get().isSuccessful()); // Assuming CallableValue has a method isSuccessful() - } - - @Test - public void testProcessOptionsWithValidArgs() { - String[] args = {"base.xlsx", "target.xlsx"}; // Adjusting to match the required args - ExcelDiffChecker.processOptions(args); - - assertEquals("base.xlsx", ExcelDiffChecker.FILE_NAME1); - assertEquals("target.xlsx", ExcelDiffChecker.FILE_NAME2); - } - - @Test(expected = RuntimeException.class) - public void testProcessOptionsWithMissingRequiredArgs() { - String[] args = {"base.xlsx"}; // Missing second required arg - ExcelDiffChecker.processOptions(args); - } - - @Test - public void testProcessOptionsWithInvalidArgs() { - String[] args = {"-invalid", "base.xlsx", "target.xlsx"}; - ExcelDiffChecker.processOptions(args); // Assuming it should throw or handle an invalid argument - // You may want to add assertions here based on how your method handles invalid args - } -} diff --git a/src/test/java/edu/abhi/poi/excel/ExcelDiffCheckerIntegrationTest.java b/src/test/java/edu/abhi/poi/excel/ExcelDiffCheckerIntegrationTest.java new file mode 100644 index 0000000..518799f --- /dev/null +++ b/src/test/java/edu/abhi/poi/excel/ExcelDiffCheckerIntegrationTest.java @@ -0,0 +1,48 @@ +package edu.abhi.poi.excel; + + +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.FileInputStream; +import java.util.concurrent.Callable; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class ExcelDiffCheckerIntegrationTest { + + private XSSFWorkbook workbookOne; + private XSSFWorkbook workbookTwo; + + // Test setup with existing file paths + @BeforeEach + void setUp() throws Exception { + workbookOne = new XSSFWorkbook(new FileInputStream("C:\\Users\\anujr\\Downloads\\excel1.xlsx")); + workbookTwo = new XSSFWorkbook(new FileInputStream("C:\\Users\\anujr\\Downloads\\excel2.xlsx")); + } + + // Test calling two Excel files + @Test + void testSheetProcessorTask() throws Exception { + XSSFSheet sheetOne = workbookOne.getSheetAt(0); + XSSFSheet sheetTwo = workbookTwo.getSheetAt(0); + + // Create a task for processing the sheets + Callable sheetTask = new SheetProcessorTask(sheetOne, sheetTwo, false); + + // Execute the task + CallableValue taskResult = sheetTask.call(); + + // Assert that the result is not null + assertNotNull(taskResult); + + System.out.println("Test passed successfully!"); + + + // Additional assertions can be made on taskResult to check for expected behavior + // For example: + // assertTrue(taskResult.getDiffFlag(), "Differences should be detected"); + } +}