diff --git a/src/main/java/seedu/cafectrl/storage/FileManager.java b/src/main/java/seedu/cafectrl/storage/FileManager.java index 11062acf98..7e5f271f33 100644 --- a/src/main/java/seedu/cafectrl/storage/FileManager.java +++ b/src/main/java/seedu/cafectrl/storage/FileManager.java @@ -7,13 +7,13 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; -import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Scanner; import java.util.logging.Logger; + //@@author DextheChik3n /** * Manage everything related to file such as writing, reading, opening and creating file @@ -55,8 +55,13 @@ public ArrayList readTextFile(String filePath) throws FileNotFoundExcept * Checks if the text file and folder exists in the user's system and creates them (if needed) * @param filePath the specified path location of the file */ - public void checkFileExists(String filePath) throws IOException { + public void checkFileExists(String filePath) throws Exception { logger.info("Checking if " + filePath + " exists..."); + + if (filePath.isEmpty()) { + throw new Exception(ErrorMessages.MISSING_FILEPATH); + } + String userWorkingDirectory = System.getProperty("user.dir"); Path dataFilePath = Paths.get(userWorkingDirectory, filePath); Path dataFolderPath = dataFilePath.getParent(); @@ -83,45 +88,19 @@ public void checkFileExists(String filePath) throws IOException { * * @param filePath file path of the text file. * @param listOfTextToAdd text to be written to the text file. - * @throws IOException If I/O operations are interrupted. - */ - public void overwriteFile(String filePath, ArrayList listOfTextToAdd) throws IOException { - checkFileExists(filePath); - FileWriter fw = new FileWriter(filePath); - for (String line : listOfTextToAdd) { - logger.info("Overwriting " + filePath + " with " + line + "..."); - fw.write(line); - } - fw.close(); - } - - /** - * Writes text to the text file at the specified file path. - * Will overwrite all text in text file. - * - * @param filePath file path of the text file. - * @param textToAdd text to be written to the text file. - * @throws IOException If I/O operations are interrupted. */ - public void overwriteFile(String filePath, String textToAdd) throws IOException { - checkFileExists(filePath); - FileWriter fw = new FileWriter(filePath); - fw.write(textToAdd); - fw.close(); - } + public void overwriteFile(String filePath, ArrayList listOfTextToAdd) { + try { + checkFileExists(filePath); + FileWriter fw = new FileWriter(filePath); + for (String line : listOfTextToAdd) { + logger.info("Overwriting " + filePath + " with " + line + "..."); + fw.write(line); + } - /** - * Appends text to the text file at the specified file path. - * Will add text to text file. - * - * @param filePath file path of the text file. - * @param textToAdd text to be added to the text file. - * @throws IOException If I/O operations are interrupted. - */ - public void appendToFile(String filePath, String textToAdd) throws IOException { - checkFileExists(filePath); - FileWriter fw = new FileWriter(filePath, true); - fw.write(textToAdd); - fw.close(); + fw.close(); + } catch (Exception e) { + ui.showToUser(e.getMessage()); + } } } diff --git a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java index a4e29d13c7..82951c9474 100644 --- a/src/main/java/seedu/cafectrl/ui/ErrorMessages.java +++ b/src/main/java/seedu/cafectrl/ui/ErrorMessages.java @@ -104,4 +104,5 @@ public class ErrorMessages { + "is inaccurate and will hence be updated from %.2f to %.2f instead."; public static final String WRONG_HELP_FORMAT = "Invalid help command format!\n" + HelpCommand.MESSAGE_USAGE; + public static final String MISSING_FILEPATH = "Error in FileManager: No File Path entered"; } diff --git a/src/test/java/seedu/cafectrl/UiTest.java b/src/test/java/seedu/cafectrl/UiTest.java deleted file mode 100644 index ddf62b2fff..0000000000 --- a/src/test/java/seedu/cafectrl/UiTest.java +++ /dev/null @@ -1,8 +0,0 @@ -package seedu.cafectrl; - - -/** - * Junit test for Ui.java - */ -public class UiTest { -} diff --git a/src/test/java/seedu/cafectrl/storage/FileManagerTest.java b/src/test/java/seedu/cafectrl/storage/FileManagerTest.java new file mode 100644 index 0000000000..09cd0b15ad --- /dev/null +++ b/src/test/java/seedu/cafectrl/storage/FileManagerTest.java @@ -0,0 +1,64 @@ +package seedu.cafectrl.storage; + +import org.junit.jupiter.api.Test; +import seedu.cafectrl.ui.ErrorMessages; +import seedu.cafectrl.ui.Ui; + +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Junit test for FileManager.java + */ +public class FileManagerTest { + @Test + public void readTextFile_emptyFilePath_fileNotFoundExceptionThrown() { + String inputFilePath = ""; + + FileManager fileManager = new FileManager(new Ui()); + assertThrows(FileNotFoundException.class, () -> fileManager.readTextFile(inputFilePath)); + } + + @Test + public void readTextFile_nullFilePath_nullPointerExceptionThrown() { + + FileManager fileManager = new FileManager(new Ui()); + assertThrows(NullPointerException.class, () -> fileManager.readTextFile(null)); + } + + @Test + public void checkFileExists_emptyFilePath_fileNotFoundExceptionThrown() { + String inputFilePath = ""; + + FileManager fileManager = new FileManager(new Ui()); + assertThrows(Exception.class, () -> fileManager.checkFileExists(inputFilePath)); + } + + @Test + public void checkFileExists_nullFilePath_nullPointerExceptionThrown() { + FileManager fileManager = new FileManager(new Ui()); + assertThrows(NullPointerException.class, () -> fileManager.checkFileExists(null)); + } + + @Test + public void overwriteFile_emptyFilePath_emptyFileInputMessage() { + ArrayList actualOutput = new ArrayList<>(); + Ui ui = new Ui() { + @Override + public void showToUser(String... message) { + actualOutput.addAll(Arrays.asList(message)); + } + }; + FileManager fileManager = new FileManager(ui); + + String inputFilePath = ""; + ArrayList inputTextList = new ArrayList<>(); + + fileManager.overwriteFile(inputFilePath, inputTextList); + assertEquals(ErrorMessages.MISSING_FILEPATH, actualOutput.get(0)); + } +}