From 1a8d65b15ba148c8c65678658212a82207058e2c Mon Sep 17 00:00:00 2001 From: Harry Linrui XU <xulr0820@hotmail.com> Date: Tue, 25 Apr 2023 14:00:05 +0200 Subject: [PATCH 1/2] Created new tests for in FileHandlingBudgetTest --- .../demo/controller/AddBudgetController.java | 1 - .../Budget/FileHandlingBudgetArchive.java | 2 + .../testFiles/budget/emptyFile.budget | 0 .../testFiles/budget/newBudget.budget | 1 + .../data/Budget/FileHandlingBudgetTest.java | 54 +++++++++++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/testFiles/budget/emptyFile.budget create mode 100644 src/main/resources/testFiles/budget/newBudget.budget diff --git a/src/main/java/no/ntnu/idatt1002/demo/controller/AddBudgetController.java b/src/main/java/no/ntnu/idatt1002/demo/controller/AddBudgetController.java index e7eb2aae..0d394481 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/controller/AddBudgetController.java +++ b/src/main/java/no/ntnu/idatt1002/demo/controller/AddBudgetController.java @@ -9,7 +9,6 @@ import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.TextField; - import javafx.scene.text.Text; import javafx.stage.Stage; import no.ntnu.idatt1002.demo.data.Budget.BudgetItem; diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetArchive.java b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetArchive.java index 1181ec1c..a7600665 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetArchive.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetArchive.java @@ -8,6 +8,8 @@ import java.io.FileWriter; import java.io.IOException; //Set titler på hvert vindu - kan gjøres i switch scene +//Dynamic days left +//teste klasser med lav coverage //Close program properly //close button i first menu //remove printstackstraces diff --git a/src/main/resources/testFiles/budget/emptyFile.budget b/src/main/resources/testFiles/budget/emptyFile.budget new file mode 100644 index 00000000..e69de29b diff --git a/src/main/resources/testFiles/budget/newBudget.budget b/src/main/resources/testFiles/budget/newBudget.budget new file mode 100644 index 00000000..4612d32e --- /dev/null +++ b/src/main/resources/testFiles/budget/newBudget.budget @@ -0,0 +1 @@ +maxAmount=11111.0 \ No newline at end of file diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetTest.java index f2e9189a..c62187e2 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetTest.java @@ -71,4 +71,58 @@ class FileHandlingBudgetTest { assertEquals(generalBudget,FileHandlingBudget.readGeneralBudgetFromFile(fileTitle)); } } + + @Nested + @DisplayName("Test isEmpty") + class IsEmpty { + + String emptyFileFilePath = "testFiles/budget/emptyFile"; + String nonEmptyFileFilePath = "testFiles/budget/multipleBudgetItem"; + + @Test + @DisplayName("Test isEmpty with empty file") + void testIsEmptyWithEmptyFile() throws IOException { + assertTrue(FileHandlingBudget.isEmpty(emptyFileFilePath)); + } + + @Test + @DisplayName("Test isEmpty with non-empty file") + void testIsEmptyWithNonEmptyFile() throws IOException { + assertFalse(FileHandlingBudget.isEmpty(nonEmptyFileFilePath)); + } + } + + @Nested + @DisplayName("Test isNewBudget") + class IsNewBudget { + + String newBudgetFilePath = "testFiles/budget/newBudget"; + + String oldBudgetFilePath = "testFiles/budget/oneBudgetItemTest"; + + + @Test + @DisplayName("Test isNewBudget with new budget") + void testIsNewBudgetWithNewBudget() throws IOException { + assertTrue(FileHandlingBudget.isNewBudget(newBudgetFilePath)); + } + + @Test + @DisplayName("Test isNewBudget with old budget") + void testIsNewBudgetWithOldBudget() throws IOException { + assertTrue(FileHandlingBudget.isNewBudget(oldBudgetFilePath)); + } + } + + @Test + @DisplayName("Test writing max budget amount to file") + void testWriteMaxMountToFile() throws IOException { + String fileDestination = "testFiles/budget/writeNewBudget"; + String maxAmount = "100"; + FileHandlingBudget.writeMaxAmountToFile(fileDestination, maxAmount); + + GeneralBudget generalBudget = FileHandlingBudget.readGeneralBudgetFromFile(fileDestination); + + assertEquals(Double.parseDouble(maxAmount), generalBudget.getMaxAmount()); + } } -- GitLab From dbfbcc6d7d539c6b0f1c142afbfdda141fb43316 Mon Sep 17 00:00:00 2001 From: Harry Linrui XU <xulr0820@hotmail.com> Date: Tue, 25 Apr 2023 14:13:41 +0200 Subject: [PATCH 2/2] Added new tests for GeneralBudget + fixed assertion bug in FileHandlingBudgetTest --- .../demo/data/Budget/GeneralBudget.java | 3 -- .../data/Budget/FileHandlingBudgetTest.java | 2 +- .../demo/data/Budget/GeneralBudgetTest.java | 41 ++++++++++++++++++- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudget.java b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudget.java index d7f1577a..1db492f2 100644 --- a/src/main/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudget.java +++ b/src/main/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudget.java @@ -183,10 +183,8 @@ public class GeneralBudget { throw new IllegalArgumentException("Amount to be added goes over the max value of the budget"); } if (!hasBudgetCategory(category)) { - System.out.println(category.label); budgetItems.add(new BudgetItem(budgetAmount, description, category)); } else { - System.out.println("Failing: " + category.label); //TODO: When adding budgets, this fails at 'other' even if an 'other' budget was not set. throw new IllegalArgumentException("There is already a budget with this category"); } } @@ -212,7 +210,6 @@ public class GeneralBudget { stream().filter(this::hasBudgetCategory).toList(); } - /** * This method checks if there are unused categories in the budget. diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetTest.java index c62187e2..41586aee 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Budget/FileHandlingBudgetTest.java @@ -110,7 +110,7 @@ class FileHandlingBudgetTest { @Test @DisplayName("Test isNewBudget with old budget") void testIsNewBudgetWithOldBudget() throws IOException { - assertTrue(FileHandlingBudget.isNewBudget(oldBudgetFilePath)); + assertFalse(FileHandlingBudget.isNewBudget(oldBudgetFilePath)); } } diff --git a/src/test/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudgetTest.java b/src/test/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudgetTest.java index d433d051..f41fb076 100644 --- a/src/test/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudgetTest.java +++ b/src/test/java/no/ntnu/idatt1002/demo/data/Budget/GeneralBudgetTest.java @@ -1,5 +1,6 @@ package no.ntnu.idatt1002.demo.data.Budget; +import jdk.jfr.Category; import no.ntnu.idatt1002.demo.data.Economics.ExpenseCategory; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -19,6 +20,15 @@ class GeneralBudgetTest { assertThrows(IllegalArgumentException.class, () -> new GeneralBudget(list, -12)); } + @Test + @DisplayName("Constructor with savings throws exception when maxAmount is under zero") + void constructor_with_savings_throws_exception_when_maxAmount_under_zero(){ + List<BudgetItem> list = new ArrayList<>(); + Savings savings = new Savings(1000); + + assertThrows(IllegalArgumentException.class, () -> new GeneralBudget(list, -12, savings)); + } + @Test @DisplayName("AddToBudget throws when totalSum is higher than maxAmount ") void add_to_budget_throws_when_totalSum_is_over_maxAmount(){ @@ -99,6 +109,15 @@ class GeneralBudgetTest { assertTrue(list.isEmpty()); } + @Test + @DisplayName("Test getBudgetItem when requesting a non-added budget item") + void get_budget_item_for_non_added_budget() { + List<BudgetItem> list = new ArrayList<>(); + GeneralBudget budget1 = new GeneralBudget(list, 1200); + + assertThrows(IllegalArgumentException.class, () -> budget1.getBudgetItem(ExpenseCategory.FOOD)); + } + @Nested class UnusedCategories { GeneralBudget budget; @@ -113,7 +132,7 @@ class GeneralBudgetTest { @Test @DisplayName("Test addUnusedCategories adds unused categories") - void hasUnusedCategories_with_full_budget() { + void has_Unused_Categories_with_full_budget() { assertEquals(1, budget.getBudgetItems().size()); budget.addUnusedCategories(); @@ -123,11 +142,29 @@ class GeneralBudgetTest { } @Test - void addToSavingsThrowsExceptionWhenItShould() { + @DisplayName("Add to savings throws illegal argument exception when it should") + void add_To_Savings_Throws_Exception_When_It_Should() { GeneralBudget generalBudget = new GeneralBudget(1000); assertThrows(IllegalArgumentException.class, () -> generalBudget.addToSavings(1500)); } + @Test + @DisplayName("Test getting chosen categories") + void test_getting_chosen_categories() { + List<ExpenseCategory> categories = new ArrayList<>(); + categories.add(ExpenseCategory.FOOD); + categories.add(ExpenseCategory.CLOTHES); + categories.add(ExpenseCategory.OTHER); + + GeneralBudget generalBudget = new GeneralBudget(1000); + + for (ExpenseCategory category : categories) { + generalBudget.addToBudget(200, "", category); + } + + assertEquals(categories, generalBudget.getChosenBudgetCategories()); + } + /* @Test @DisplayName("Gets the number of days left in the month. 17 has to be changed to the actual number of days left in the month.") -- GitLab