Skip to content
Snippets Groups Projects
Commit d6f12c7e authored by Harry Linrui XU's avatar Harry Linrui XU
Browse files

Created tests for new relatively new file handling classes.

parent c4a1fe14
No related branches found
No related tags found
1 merge request!52Added tests for new classes
......@@ -101,6 +101,7 @@ public class FileHandlingSelectedBudget {
* @return True, if a directory is successfully created. Else, returns false.
*/
public static boolean createBudgetDirectory(String budgetId) {
System.out.println("Creating directory: " + filePath + budgetId);
File f = new File(filePath + budgetId);
return f.mkdir();
}
......@@ -138,6 +139,7 @@ public class FileHandlingSelectedBudget {
* @throws IOException if an input or output exception occurred.
*/
public static boolean createNewIncomeFile(String budgetId, String incomeFileTitle) throws IOException {
System.out.println("Income filePath: " + filePath + budgetId + "/" + incomeFileTitle + registerFileType);
File incomeFile = new File(filePath + budgetId + "/" + incomeFileTitle + registerFileType);
return incomeFile.createNewFile();
}
......
APPPAPAP
\ No newline at end of file
package no.ntnu.idatt1002.demo.data.Budget;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class FileHandlingBudgetArchiveTest {
BudgetRegister budgetRegister;
@Nested
@DisplayName("Test read/writeBudgetRegisterToArchive")
class ReadAndWriteToFile {
BudgetRegister newBudgetRegister;
String name1, name2, name3, name4;
String filePath;
String toString;
@BeforeEach
void setUp() {
budgetRegister = new BudgetRegister();
filePath = "testFiles/budget/Archive";
name1 = "APRIL1";
name2 = "APRIL2";
name3 = "APRIL3";
name4 = "APRIL5";
newBudgetRegister = new BudgetRegister();
newBudgetRegister.addBudgetName(name1);
newBudgetRegister.addBudgetName(name2);
newBudgetRegister.addBudgetName(name3);
newBudgetRegister.addBudgetName(name4);
toString = "APRIL1\nAPRIL2\nAPRIL3\nAPRIL5\n";
}
@Test
@DisplayName("Test read and write to file")
void testReadAndWrite() throws IOException {
budgetRegister = FileHandlingBudgetArchive.readBudgetArchive(filePath);
assertEquals(toString, budgetRegister.toString());
FileHandlingBudgetArchive.writeBudgetRegisterToArchive(newBudgetRegister, filePath);
newBudgetRegister = FileHandlingBudgetArchive.readBudgetArchive(filePath);
assertEquals(budgetRegister.toString(), newBudgetRegister.toString());
}
@Test
@DisplayName("Test read throws IOException for non-existent file")
void testReadWithNonExistentFile() {
assertThrows(IOException.class,
() -> FileHandlingBudgetArchive.readBudgetArchive("sheesh"));
}
}
@Nested
@DisplayName("Test isBudgetRegisterEmpty")
class IsBudgetRegisterEmpty {
String filePath;
String filePath, filePath1;
@BeforeEach
void setUp() {
budgetRegister = new BudgetRegister();
filePath = null;
filePath = "testFiles/budget/emptyFile";
filePath1 = "testFiles/budget/notEmptyFile";
}
@Test
@DisplayName("Test IsBudgetRegisterEmpty with empty file")
void testEmptyFileWithEmptyFile() throws IOException {
assertTrue(FileHandlingBudgetArchive.isBudgetRegisterEmpty(filePath));
}
@Test
@DisplayName("Test IsBudgetRegisterEmpty with non-empty file")
void testEmptyFileWithNonEmptyFile() throws IOException {
assertFalse(FileHandlingBudgetArchive.isBudgetRegisterEmpty(filePath1));
}
@Test
@DisplayName("Test IsBudgetRegisterEmpty with non-existent file throws IOException")
void testEmptyFileWithNonExistentFile() {
assertThrows(IOException.class, () ->
FileHandlingBudgetArchive.isBudgetRegisterEmpty("sheesh"));
}
}
}
package no.ntnu.idatt1002.demo.data.Budget;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
......@@ -9,6 +14,168 @@ import static org.junit.jupiter.api.Assertions.*;
public class FileHandlingSelectedBudgetTest {
@Nested
@DisplayName("Test read/writeSelectedBudget")
class ReadAndWriteSelectedBudget {
String filePath, emptyFilePath, toString, budgetName1, budgetName2;
@BeforeEach
void setUp() {
filePath = "testFiles/budget/SelectedBudget";
emptyFilePath = "testFiles/budget/emptyFile";
toString = "APRIL1";
budgetName1 = "";
budgetName2 = "APRIL1";
}
@Test
@DisplayName("Test read and write to file")
void testReadAndWriteToFile() throws IOException {
budgetName1 = FileHandlingSelectedBudget.readSelectedBudget(filePath);
assertEquals(toString, budgetName1);
FileHandlingSelectedBudget.updateSelectedBudget(budgetName2, filePath);
budgetName2 = FileHandlingSelectedBudget.readSelectedBudget(filePath);
assertEquals(budgetName1, budgetName2);
}
@Test
@DisplayName("Test readSelectedBudget returns null for empty file")
void testReadSelectedBudgetWithEmptyFile() throws IOException {
assertNull(null, FileHandlingSelectedBudget.readSelectedBudget(emptyFilePath));
}
@Test
@DisplayName("Test readSelectedBudget for non-existent file throws IOException")
void testReadSelectedBudgetForNonExistentFile() {
assertThrows(IOException.class,
() -> FileHandlingSelectedBudget.readSelectedBudget("sheesh"));
}
}
@Test
@DisplayName("Test clearSelectedBudget")
void testClearSelectedBudget() throws IOException {
String filePath = "testFiles/budget/fileForClearing";
FileHandlingSelectedBudget.clearSelectedBudget(filePath);
assertNull(FileHandlingSelectedBudget.readSelectedBudget(filePath));
}
@Nested
@DisplayName("Test isSelectedBudgetEmpty")
class testIsEmpty {
String filePath;
@BeforeEach
void setUp() {
filePath = "testFiles/budget/emptyFile";
}
@Test
@DisplayName("Test isSelectedBudget with non-empty file")
void testIsSelectedBudgetWithNonEmptyFile() throws IOException {
FileHandlingSelectedBudget.updateSelectedBudget("notempty", filePath);
assertFalse(FileHandlingSelectedBudget.isSelectedBudgetEmpty(filePath));
FileHandlingSelectedBudget.clearSelectedBudget(filePath);
}
@Test
@DisplayName("Test isSelectedBudget with empty file")
void testIsSelectedBudgetWithEmptyFile() throws IOException {
assertTrue(FileHandlingSelectedBudget.isSelectedBudgetEmpty(filePath));
}
}
@Nested
@DisplayName("Test create budget directory")
class CreateBudgetDirectory {
String filePath;
private static File directory;
@BeforeAll
public static void setup() {
directory = new File("src/main/resources/testFiles/budget/testDirectory");
if(!directory.exists()){
directory.mkdir();
}
}
@AfterAll
public static void teardown() throws IOException {
if(directory.exists()){
FileUtils.deleteDirectory(directory);
}
}
@BeforeEach
void setUp() {
filePath = "testFiles/budget/testDirectory";
}
@Test
@DisplayName("Test create existing then new budget directory")
void testCreateExistingAndNewBudgetDirectory() {
assertFalse(FileHandlingSelectedBudget.createBudgetDirectory(filePath));
FileHandlingSelectedBudget.deleteBudgetDirectory(filePath);
assertTrue(FileHandlingSelectedBudget.createBudgetDirectory(filePath));
}
@Test
@DisplayName("Test create new and existing files")
void testCreateNewAndExistingFiles() throws IOException {
assertTrue(FileHandlingSelectedBudget.createNewBudgetFile(filePath, "Budget"));
assertTrue(FileHandlingSelectedBudget.createNewIncomeFile(filePath, "Income"));
assertTrue(FileHandlingSelectedBudget.createNewExpenseFile(filePath, "Expense"));
assertFalse(FileHandlingSelectedBudget.createNewBudgetFile(filePath, "Budget"));
assertFalse(FileHandlingSelectedBudget.createNewIncomeFile(filePath, "Income"));
assertFalse(FileHandlingSelectedBudget.createNewExpenseFile(filePath, "Expense"));
}
}
@Nested
@DisplayName("Test delete budget directory")
class DeleteBudgetDirectory {
String filePath;
private static File directory;
@BeforeAll
public static void setup() {
directory = new File("src/main/resources/testFiles/budget/testDirectory");
if (!directory.exists()) {
directory.mkdir();
}
}
@AfterAll
public static void teardown() throws IOException {
if (directory.exists()) {
FileUtils.deleteDirectory(directory);
}
}
@BeforeEach
void setUp() {
filePath = "testFiles/budget/testDirectory";
}
@Test
@DisplayName("Test remove existing budget directory")
void testCreateExistingBudgetDirectory() {
assertTrue(FileHandlingSelectedBudget.deleteBudgetDirectory(filePath));
FileHandlingSelectedBudget.createBudgetDirectory(filePath);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment