diff --git a/app/src/test/java/MainTest.java b/app/src/test/java/MainTest.java new file mode 100644 index 0000000..5f9ac18 --- /dev/null +++ b/app/src/test/java/MainTest.java @@ -0,0 +1,75 @@ +import org.junit.jupiter.api.*; +import org.junit.jupiter.api.io.TempDir; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.*; + +class MainTest { + + // File handling variables + @TempDir + static Path tempDir; + static Path tempFile; + + // PrintStream (to capture the console output) + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + + // Save the old PrintStreams + private final PrintStream oldOut = System.out; + private final PrintStream oldErr = System.err; + + @BeforeAll + static void start() throws IOException { + tempFile = Files.createFile(tempDir.resolve("testing.txt")); + } + + @BeforeEach + void setUp() { + // Create new PrintStream to capture console outputs + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + } + + @AfterEach + void tearDown() { + // Reset PrintStream to original + System.setOut(oldOut); + System.setErr(oldErr); + } + + @Test + @DisplayName("No arguments provided") + void noArguments() { + String[] arguments = new String[0]; + + Main.main(arguments); + + // 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("Too many arguments provided") + void tooManyArguments() { + String[] arguments = new String[2]; + // Simulating a potential typo of the path with a space + arguments[0] = "/path/to/"; + arguments[1] = "file.txt"; + + Main.main(arguments); + + // 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("Normal test run") + void normalRun() { + } +} \ No newline at end of file