Skip to content

Commit

Permalink
Refactor: Rename the method names for asserting a file exists or not
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurgul Amat committed Dec 5, 2023
1 parent 6bd3c90 commit 972fe6e
Showing 1 changed file with 35 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,37 @@ public void setup() throws IOException {
deleteFileCallableInstance = new DeleteArtifactsMatchingPatternsAction.DeleteFileCallable(archiveRootDirectory);
}

private void assertTempFolderContains(final int expectedNumFiles) {
private void assertTempFolderContainsFileCount(final int expectedNumFiles) {
final int actualNumFiles = countFiles(tempFolder.getRoot());
final String assertMessage = String.format("Expected %d files, actual found %d files", expectedNumFiles,
actualNumFiles);
Assert.assertEquals(assertMessage, expectedNumFiles, actualNumFiles);
}

// Custom assertFileExists method
public void assertFileExists(final String filePath, final String message) {
// Custom assertFileExistsInTempFolder method
public void assertFileExistsInTempFolder(final String filePath, final String message) {
final File file = new File(tempFolder.getRoot(), filePath);
Assert.assertTrue(message, file.exists());
}

public void assertFileExists(final String filePath) {
assertFileExists(filePath, null);
public void assertFileExistsInTempFolder(final String filePath) {
assertFileExistsInTempFolder(filePath, null);
}

// Custom assertFileNotExists method
public void assertFileNotExists(final String filePath, final String message) {
// Custom assertFileNotExistsInTempFolder method
public void assertFileNotExistsInTempFolder(final String filePath, final String message) {
final File file = new File(tempFolder.getRoot(), filePath);
Assert.assertFalse(message, file.exists());
}

public void assertFileNotExists(final String filePath) {
assertFileNotExists(filePath, null);
public void assertFileNotExistsInTempFolder(final String filePath) {
assertFileNotExistsInTempFolder(filePath, null);
}

// Count the number of files in the directory
public static int countFiles(final File directory) {
int fileCount = 0;
File[] files = directory.listFiles();
assert files != null;
for (File file : files) {
if (file.isFile() && file.exists()) {
fileCount++;
Expand All @@ -117,8 +116,8 @@ public void noneProvidedNothingDeleted() throws IOException, InterruptedExceptio

action.perform(mockRun);

assertTempFolderContains(6);
assertFileExists("testFolder/test1.xml");
assertTempFolderContainsFileCount(6);
assertFileExistsInTempFolder("testFolder/test1.xml");
}

@Test // test 2, inc="", exc="**", nothing deleted
Expand All @@ -128,7 +127,7 @@ public void excludesOnlyNothingDeleted() throws IOException, InterruptedExceptio

action.perform(mockRun);

assertTempFolderContains(6);
assertTempFolderContainsFileCount(6);
}

@Test // test 3, inc="**", exc="**", nothing deleted
Expand All @@ -138,7 +137,7 @@ public void bothProvidedNothingDeleted() throws IOException, InterruptedExceptio

action.perform(mockRun);

assertTempFolderContains(6);
assertTempFolderContainsFileCount(6);
}

@Test // test 4, inc="**", exc="", everything deleted
Expand All @@ -160,9 +159,9 @@ public void everythingDeletedButExcludePatterns() throws IOException, Interrupte

action.perform(mockRun);

assertTempFolderContains(2);
assertFileExists("testLog.log");
assertFileExists("testFolder/testLog1.log", "Log files should have been excluded.");
assertTempFolderContainsFileCount(2);
assertFileExistsInTempFolder("testLog.log");
assertFileExistsInTempFolder("testFolder/testLog1.log", "Log files should have been excluded.");
}

@Test // test 5.1 inc="**", exc="**/*.log, **/*.txt" everything except logs and txt
Expand All @@ -173,10 +172,10 @@ public void everythingDeletedButMultipleExcludePatterns() throws IOException, In

action.perform(mockRun);

assertTempFolderContains(4);
assertFileExists("testLog.log");
assertFileNotExists("test.xml", "XML files should have been deleted.");
assertFileNotExists("testFolder/test1.xml");
assertTempFolderContainsFileCount(4);
assertFileExistsInTempFolder("testLog.log");
assertFileNotExistsInTempFolder("test.xml", "XML files should have been deleted.");
assertFileNotExistsInTempFolder("testFolder/test1.xml");
}

@Test // test 6 inc="*.txt", exc="**/*.log" only text files in the root are deleted
Expand All @@ -186,8 +185,8 @@ public void selectiveFileDeletionRootOnly() throws IOException, InterruptedExcep

action.perform(mockRun);

assertTempFolderContains(5);
assertFileNotExists("testTxt.txt");
assertTempFolderContainsFileCount(5);
assertFileNotExistsInTempFolder("testTxt.txt");
}

@Test // test 6.1 inc="*.txt, *.xml", exc="**/*.log" only txt files and xml files in
Expand All @@ -198,9 +197,9 @@ public void multipleIncludePatternFilesDeletionRootOnly() throws IOException, In

action.perform(mockRun);

assertTempFolderContains(4);
assertFileNotExists("testTxt.txt");
assertFileNotExists("test.xml");
assertTempFolderContainsFileCount(4);
assertFileNotExistsInTempFolder("testTxt.txt");
assertFileNotExistsInTempFolder("test.xml");
}

@Test // test 6.2 inc="*.txt, **/*.xml", exc="**/*.log" only txt files and xml files
Expand All @@ -211,9 +210,9 @@ public void multipleIncludePatternsFilesDeletion() throws IOException, Interrupt

action.perform(mockRun);

assertTempFolderContains(3);
assertFileExists("testLog.log");
assertFileNotExists("test.xml");
assertTempFolderContainsFileCount(3);
assertFileExistsInTempFolder("testLog.log");
assertFileNotExistsInTempFolder("test.xml");
}

@Test // test 7 inc="**/*.txt", exc="**/*.log", all text files are deleted
Expand All @@ -223,9 +222,9 @@ public void deleteIncludeMatchingFilesButExcludePatterns() throws IOException, I

action.perform(mockRun);

assertTempFolderContains(4);
assertFileExists("testLog.log");
assertFileNotExists("testTxt.txt");
assertTempFolderContainsFileCount(4);
assertFileExistsInTempFolder("testLog.log");
assertFileNotExistsInTempFolder("testTxt.txt");
}

@Test // test 8 inc="**/*.txt", exc="*.txt", all text files but those in the root are
Expand All @@ -236,9 +235,9 @@ public void selectiveFileDeletionExcludeRoot() throws IOException, InterruptedEx

action.perform(mockRun);

assertTempFolderContains(5);
assertFileExists("testTxt.txt");
assertFileNotExists("testFolder/testTxt1.txt");
assertTempFolderContainsFileCount(5);
assertFileExistsInTempFolder("testTxt.txt");
assertFileNotExistsInTempFolder("testFolder/testTxt1.txt");
}

@Test // test 9, inc="../**", exc=""
Expand Down Expand Up @@ -266,7 +265,7 @@ public void nothingDeletedWhenPointingAboveStartingPoint() throws IOException, I
Assert.assertTrue(folder.exists());
Assert.assertTrue(archiveFolder.exists());
Assert.assertTrue(buildXML.exists());
assertFileExists("archive/test1.xml");
assertFileExistsInTempFolder("archive/test1.xml");
}

@Test // testing getIncludePatterns method for code coverage
Expand Down

0 comments on commit 972fe6e

Please sign in to comment.