From 3be78c0e9ebdac3505223051a3b9e212932efa09 Mon Sep 17 00:00:00 2001 From: Anthony Berg Date: Thu, 17 Oct 2024 20:02:54 +0200 Subject: [PATCH] feat(test): add testing with files in Main --- app/src/test/java/MainTest.java | 59 ++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/app/src/test/java/MainTest.java b/app/src/test/java/MainTest.java index d7cc6cd..37979e9 100644 --- a/app/src/test/java/MainTest.java +++ b/app/src/test/java/MainTest.java @@ -6,6 +6,8 @@ import java.io.IOException; import java.io.PrintStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; import static org.junit.jupiter.api.Assertions.*; @@ -73,9 +75,64 @@ class MainTest { // Check that the program quit at the expected point, with expected output assertEquals("Invalid arguments, you need to define the text file to read from!\n", outContent.toString()); } + + @Test + @DisplayName("Invalid/Nonexistent file") + void invalidFile() { + arguments = new String[1]; + + arguments[0] = "doesNotExist.txt"; + + assertThrowsExactly(RuntimeException.class, () -> Main.main(arguments)); + } @Test @DisplayName("Normal test run") - void normalRun() { + void normalRun() throws IOException { + // List of emails to add to the file + List emails = new ArrayList<>() {{ + add("johndoe@gmail.com"); + add("janedoe@gmail.com"); + add("person@outlook.com"); + add("totallylegitpresident@gmail.com"); + add("google@yahoo.com"); + add("person1st@outlook.com"); + add("google+scam@gmail.com"); + }}; + + Files.write(tempFile, emails); + + // The expected output - \n has been added on the end due to the program quitting at the end + String expectedOutput = "gmail.com 4\noutlook.com 2\nyahoo.com 1\n"; + + // Add argument for filepath + arguments[0] = tempFile.toString(); + + Main.main(arguments); + + assertEquals(expectedOutput, outContent.toString()); + } + + @Test + @DisplayName("File with invalid emails") + void brokenData() throws IOException { + // List of emails, with some of the emails being invalid + List emails = new ArrayList<>() {{ + add("gmail.com"); + add("janedoe@gmail.com"); + add("person@outlook.com"); + add("totallylegit@president@gmail.com"); + add("google@yahoo"); + add("person1st@outlook,com"); + add("google+scam@gmail.com"); + }}; + + Files.write(tempFile, emails); + + // Add argument for filepath + arguments[0] = tempFile.toString(); + + // Checks for the fail from the email validation + assertThrowsExactly(IllegalArgumentException.class, () -> Main.main(arguments)); } } \ No newline at end of file