Skip to content

Commit

Permalink
Merge pull request #332 from DextheChik3n/196-junit-test-for-file-man…
Browse files Browse the repository at this point in the history
…ager-v2.1

Add JUnit tests for file manager
  • Loading branch information
NaychiMin authored Nov 12, 2023
2 parents fa189ae + 11dc0cb commit 0962c62
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 48 deletions.
59 changes: 19 additions & 40 deletions src/main/java/seedu/cafectrl/storage/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,8 +55,13 @@ public ArrayList<String> 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();
Expand All @@ -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<String> 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<String> 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());
}
}
}
1 change: 1 addition & 0 deletions src/main/java/seedu/cafectrl/ui/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
8 changes: 0 additions & 8 deletions src/test/java/seedu/cafectrl/UiTest.java

This file was deleted.

64 changes: 64 additions & 0 deletions src/test/java/seedu/cafectrl/storage/FileManagerTest.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> inputTextList = new ArrayList<>();

fileManager.overwriteFile(inputFilePath, inputTextList);
assertEquals(ErrorMessages.MISSING_FILEPATH, actualOutput.get(0));
}
}

0 comments on commit 0962c62

Please sign in to comment.