From c168d1b4bb802cfd84641f729861e5cf59fab417 Mon Sep 17 00:00:00 2001 From: OlgaPegushina Date: Sun, 10 Nov 2024 15:15:51 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B5=D0=BD=D0=B5=D0=B4=D0=B6=D0=B5?= =?UTF-8?q?=D1=80=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=20-=20=D0=A4=D0=97-5=20?= =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81?= =?UTF-8?q?=D1=82=D1=80=D1=83=D0=BA=D1=82=D1=83=D1=80=D1=8B=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B5=D0=BA=D1=82=D0=B0=20=D0=BF=D0=BE=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=BC,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=B4=D0=BB=D1=8F=20Managers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java-kanban.iml | 2 +- src/Main.java | 4 +- src/service/InMemoryHistoryManager.java | 4 +- src/service/InMemoryTaskManager.java | 12 ++-- src/service/TaskManager.java | 9 ++- test/model/EpicTest.java | 21 ++++++ test/model/SubtaskTest.java | 21 ++++++ test/model/TaskTest.java | 18 +++++ .../service}/InMemoryHistoryManagerTest.java | 13 ++-- test/service/ManagersTest.java | 20 ++++++ .../service/TaskManagerTest.java | 65 ++++++++++++++--- tests/test/TaskTest.java | 70 ------------------- 12 files changed, 154 insertions(+), 105 deletions(-) create mode 100644 test/model/EpicTest.java create mode 100644 test/model/SubtaskTest.java create mode 100644 test/model/TaskTest.java rename {tests/test => test/service}/InMemoryHistoryManagerTest.java (92%) create mode 100644 test/service/ManagersTest.java rename tests/test/EpicAndSubtaskTest.java => test/service/TaskManagerTest.java (76%) delete mode 100644 tests/test/TaskTest.java diff --git a/java-kanban.iml b/java-kanban.iml index 3368c75..074ce77 100644 --- a/java-kanban.iml +++ b/java-kanban.iml @@ -4,7 +4,7 @@ - + diff --git a/src/Main.java b/src/Main.java index 7cb8db9..55dfbf5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,9 +1,7 @@ import model.Epic; -import model.Status; import model.Subtask; import model.Task; -import service.HistoryManager; -import service.InMemoryTaskManager; + import service.Managers; import service.TaskManager; diff --git a/src/service/InMemoryHistoryManager.java b/src/service/InMemoryHistoryManager.java index 6d3e5a5..3fdf116 100644 --- a/src/service/InMemoryHistoryManager.java +++ b/src/service/InMemoryHistoryManager.java @@ -17,12 +17,12 @@ private void removeHistoryFirst() { @Override public List getHistory() { - return history; + return new ArrayList<>(history); } @Override public void addInHistory(Task task) { - if (task != null && SIZE_HISTORY != 0) { + if (task != null) { removeHistoryFirst(); history.add(task); } diff --git a/src/service/InMemoryTaskManager.java b/src/service/InMemoryTaskManager.java index 4c9ebb3..82523a3 100644 --- a/src/service/InMemoryTaskManager.java +++ b/src/service/InMemoryTaskManager.java @@ -18,17 +18,17 @@ public class InMemoryTaskManager implements TaskManager { private final HistoryManager inMemoryHistoryManager = Managers.getDefaultHistory(); @Override - public ArrayList getAllTasks() { + public List getAllTasks() { return new ArrayList<>(tasks.values()); } @Override - public ArrayList getAllSubtasks() { + public List getAllSubtasks() { return new ArrayList<>(subtasks.values()); } @Override - public ArrayList getAllEpics() { + public List getAllEpics() { return new ArrayList<>(epics.values()); } @@ -167,8 +167,8 @@ public void deleteEpic(int epicId) { } @Override - public ArrayList getEpicSubtasks(int epicId) { - ArrayList epicSubtasks = new ArrayList<>(); + public List getEpicSubtasks(int epicId) { + List epicSubtasks = new ArrayList<>(); Epic epic = epics.get(epicId); if (epic != null) { for (Integer subtaskId : epic.getSubtaskIds()) { @@ -180,7 +180,7 @@ public ArrayList getEpicSubtasks(int epicId) { @Override public void updateStatusEpic(int epicId) { - ArrayList epicSubtasks = getEpicSubtasks(epicId); + List epicSubtasks = getEpicSubtasks(epicId); int countNew = 0; int countDone = 0; if (epicSubtasks.isEmpty()) { diff --git a/src/service/TaskManager.java b/src/service/TaskManager.java index e35dce9..67d19ee 100644 --- a/src/service/TaskManager.java +++ b/src/service/TaskManager.java @@ -4,15 +4,14 @@ import model.Subtask; import model.Task; -import java.util.ArrayList; import java.util.List; public interface TaskManager { - ArrayList getAllTasks(); + List getAllTasks(); - ArrayList getAllSubtasks(); + List getAllSubtasks(); - ArrayList getAllEpics(); + List getAllEpics(); List getHistory(); @@ -46,7 +45,7 @@ public interface TaskManager { void deleteEpic(int epicId); - ArrayList getEpicSubtasks(int epicId); + List getEpicSubtasks(int epicId); void updateStatusEpic(int epicId); } diff --git a/test/model/EpicTest.java b/test/model/EpicTest.java new file mode 100644 index 0000000..e2060c4 --- /dev/null +++ b/test/model/EpicTest.java @@ -0,0 +1,21 @@ +package model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class EpicTest { + + @Test + void epicTest() { + Epic epic1 = new Epic("Эпическая задача - 1", + "Описание эпической задачи - 1"); + Epic epic2 = new Epic("Эпическая задача - 2", + "Описание эпической задачи - 2"); + epic2.setId(epic1.getId()); + + assertEquals(epic1.getId(), epic2.getId(), "Id Эпиков не совпадают."); + assertEquals(epic1, epic2, "Эпики не совпадают."); + } +} + diff --git a/test/model/SubtaskTest.java b/test/model/SubtaskTest.java new file mode 100644 index 0000000..d324f44 --- /dev/null +++ b/test/model/SubtaskTest.java @@ -0,0 +1,21 @@ +package model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class SubtaskTest { + + @Test + void epicTest() { + Subtask subtask1 = new Subtask( "Подзадача - 1", + "Описание подзадачи - 1, эпической задачи - 1", 1); + Subtask subtask2 = new Subtask("Подзадача - 2", + "Описание подзадачи - 2, эпической задачи - 1", 1); + subtask2.setId(subtask1.getId()); + + assertEquals(subtask1.getId(), subtask2.getId(), "Id Подзадач не совпадают."); + assertEquals(subtask1, subtask2, "Подзадачи не совпадают."); + } +} + diff --git a/test/model/TaskTest.java b/test/model/TaskTest.java new file mode 100644 index 0000000..85a2fe0 --- /dev/null +++ b/test/model/TaskTest.java @@ -0,0 +1,18 @@ +package model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TaskTest { + + @Test + void taskTest() { + Task task1 = new Task("Просто задача - 1", "Описание простой задачи - 1"); + Task task2 = new Task("Просто задача - 2", "Описание простой задачи - 1"); + task2.setId(task1.getId()); + + assertEquals(task1.getId(), task2.getId(), "Id Задач не совпадают."); + assertEquals(task1, task2, "Задачи не совпадают."); + } +} \ No newline at end of file diff --git a/tests/test/InMemoryHistoryManagerTest.java b/test/service/InMemoryHistoryManagerTest.java similarity index 92% rename from tests/test/InMemoryHistoryManagerTest.java rename to test/service/InMemoryHistoryManagerTest.java index 0fb019a..9851d18 100644 --- a/tests/test/InMemoryHistoryManagerTest.java +++ b/test/service/InMemoryHistoryManagerTest.java @@ -1,15 +1,12 @@ -package test; +package service; import model.Epic; -import model.Status; import model.Subtask; import model.Task; import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach; -import service.Managers; -import service.TaskManager; import static org.junit.jupiter.api.Assertions.*; @@ -27,7 +24,7 @@ void setUp() { } @Test - void addTaskInHistory() { + void addTaskInHistoryTest() { final int taskId = manager.addNewTask(task); manager.getTask(taskId); @@ -39,7 +36,7 @@ void addTaskInHistory() { } @Test - void addEpicInHistory() { + void addEpicInHistoryTest() { final int epicId = manager.addNewEpic(epic); manager.getEpic(epicId); @@ -51,7 +48,7 @@ void addEpicInHistory() { } @Test - void addSubtaskInHistory() { + void addSubtaskInHistoryTest() { final int epicId = manager.addNewEpic(epic); Subtask subtask = new Subtask("Подзадача - 1", "Описание подзадачи - 1, эпической задачи - 1", epicId); @@ -67,7 +64,7 @@ void addSubtaskInHistory() { } @Test - void getHistory() { + void getHistoryTest() { final int epicId = manager.addNewEpic(epic); Subtask subtask = new Subtask("Подзадача - 1", "Описание подзадачи - 1, эпической задачи - 1", epicId); diff --git a/test/service/ManagersTest.java b/test/service/ManagersTest.java new file mode 100644 index 0000000..ad5f52e --- /dev/null +++ b/test/service/ManagersTest.java @@ -0,0 +1,20 @@ +package service; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class ManagersTest { + + @Test + void TaskManagerGetDefaultTest() { + final TaskManager manager = Managers.getDefault(); + assertNotNull(manager, "Объект TaskManager не создан"); + } + + @Test + void HistoryManagerGetDefaultHistory() { + final HistoryManager inMemoryHistoryManager = Managers.getDefaultHistory(); + assertNotNull(inMemoryHistoryManager, "Объект HistoryManager не создан"); + } +} diff --git a/tests/test/EpicAndSubtaskTest.java b/test/service/TaskManagerTest.java similarity index 76% rename from tests/test/EpicAndSubtaskTest.java rename to test/service/TaskManagerTest.java index a2faeb2..2fa8ca1 100644 --- a/tests/test/EpicAndSubtaskTest.java +++ b/test/service/TaskManagerTest.java @@ -1,32 +1,78 @@ -package test; +package service; import model.Epic; import model.Status; import model.Subtask; - -import org.junit.jupiter.api.Test; +import model.Task; import org.junit.jupiter.api.BeforeEach; -import service.Managers; -import service.TaskManager; +import org.junit.jupiter.api.Test; import java.util.List; import static org.junit.jupiter.api.Assertions.*; -import static org.junit.jupiter.api.Assertions.assertTrue; -public class EpicAndSubtaskTest { +public class TaskManagerTest { private TaskManager manager; + private Task task; private Epic epic; @BeforeEach void setUp() { + task = new Task("Просто задача - 1", "Описание простой задачи - 1"); epic = new Epic("Эпическая задача - 1", "Описание эпической задачи - 1"); manager = Managers.getDefault(); } @Test - void addNewEpic() { + void addNewTaskTest() { + final int taskId = manager.addNewTask(task); + final Task savedTask = manager.getTask(taskId); + + assertNotNull(savedTask, "Задача не найдена."); + assertEquals(task, savedTask, "Задачи не совпадают."); + + final List tasks = manager.getAllTasks(); + + assertNotNull(tasks, "Задачи не возвращаются."); + assertEquals(1, tasks.size(), "Неверное количество задач."); + assertEquals(task, tasks.get(0), "Задачи не совпадают."); + } + + @Test + void updateTaskTest() { + final int taskId = manager.addNewTask(task); + final Task savedTask = manager.getTask(taskId); + + savedTask.setStatus(Status.DONE); + manager.updateTask(savedTask); + + assertNotNull(savedTask, "Задача не найдена."); + assertEquals(task, savedTask, "Задачи не совпадают."); + assertEquals(task.getStatus(), savedTask.getStatus(), "Статусы задач не совпадают"); + } + + @Test + void deleteTaskTest() { + manager.addNewTask(task); + manager.deleteTask(task.getId()); + + assertTrue(manager.getAllTasks().isEmpty(), "Задача не удалилась"); + assertEquals(0, manager.getAllTasks().size(), "Задача не удалилась"); + } + + @Test + void deleteAllTasksTest() { + manager.addNewTask(task); + manager.addNewTask(task); + manager.deleteAllTasks(); + + assertTrue(manager.getAllTasks().isEmpty(), "Задачи не удалились"); + assertEquals(0, manager.getAllTasks().size(), "Задачи не удалились"); + } + + @Test + void addNewEpicTest() { final int epicId = manager.addNewEpic(epic); final Epic savedEpic = manager.getEpic(epicId); @@ -41,7 +87,7 @@ void addNewEpic() { } @Test - void addNewSubtask() { + void addNewSubtaskTest() { final int epicId = manager.addNewEpic(epic); Subtask subtask = new Subtask("Подзадача - 1", "Описание подзадачи - 1, эпической задачи - 1", epicId); @@ -164,4 +210,3 @@ void updateStatusEpicTest() { assertEquals(Status.IN_PROGRESS, epic.getStatus(), "Неверный статус IN_PROGRESS"); } } - diff --git a/tests/test/TaskTest.java b/tests/test/TaskTest.java deleted file mode 100644 index c763f83..0000000 --- a/tests/test/TaskTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package test; - -import model.Status; -import model.Task; - -import java.util.List; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.BeforeEach; -import service.Managers; -import service.TaskManager; - -import static org.junit.jupiter.api.Assertions.*; - -public class TaskTest { - private TaskManager manager; - private Task task; - - @BeforeEach - void setUp() { - task = new Task("Просто задача - 1", "Описание простой задачи - 1"); - manager = Managers.getDefault(); - } - - @Test - void addNewTask() { - final int taskId = manager.addNewTask(task); - final Task savedTask = manager.getTask(taskId); - - assertNotNull(savedTask, "Задача не найдена."); - assertEquals(task, savedTask, "Задачи не совпадают."); - - final List tasks = manager.getAllTasks(); - - assertNotNull(tasks, "Задачи не возвращаются."); - assertEquals(1, tasks.size(), "Неверное количество задач."); - assertEquals(task, tasks.get(0), "Задачи не совпадают."); - } - - @Test - void updateTaskTest() { - final int taskId = manager.addNewTask(task); - final Task savedTask = manager.getTask(taskId); - - savedTask.setStatus(Status.DONE); - manager.updateTask(savedTask); - - assertNotNull(savedTask, "Задача не найдена."); - assertEquals(task, savedTask, "Задачи не совпадают."); - assertEquals(task.getStatus(), savedTask.getStatus(), "Статусы задач не совпадают"); - } - - @Test - void deleteTaskTest() { - manager.addNewTask(task); - manager.deleteTask(task.getId()); - - assertTrue(manager.getAllTasks().isEmpty(), "Задача не удалилась"); - assertEquals(0, manager.getAllTasks().size(), "Задача не удалилась"); - } - - @Test - void deleteAllTasksTest() { - manager.addNewTask(task); - manager.addNewTask(task); - manager.deleteAllTasks(); - - assertTrue(manager.getAllTasks().isEmpty(), "Задачи не удалились"); - assertEquals(0, manager.getAllTasks().size(), "Задачи не удалились"); - } -} \ No newline at end of file