-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for MyFileDatabase class (#18)
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
LiveSched/src/test/java/dev/coms4156/project/livesched/MyFileDatabaseUnitTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package dev.coms4156.project.livesched; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.io.*; | ||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
class MyFileDatabaseUnitTests { | ||
|
||
private MyFileDatabase database; | ||
private static final String TASK_FILE = "tasks.dat"; | ||
private static final String RESOURCE_FILE = "resources.dat"; | ||
|
||
@TempDir | ||
File tempDir; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
String taskPath = new File(tempDir, TASK_FILE).getAbsolutePath(); | ||
String resourcePath = new File(tempDir, RESOURCE_FILE).getAbsolutePath(); | ||
database = new MyFileDatabase(1, taskPath, resourcePath); | ||
} | ||
|
||
@Test | ||
void testConstructor() { | ||
assertNotNull(database); | ||
assertTrue(database.getAllTasks().isEmpty()); | ||
assertTrue(database.getAllResourceTypes().isEmpty()); | ||
} | ||
|
||
@Test | ||
void testSetAndGetAllTasks() { | ||
List<Task> tasks = new ArrayList<>(); | ||
tasks.add(createDummyTask()); | ||
database.setAllTasks(tasks); | ||
assertEquals(1, database.getAllTasks().size()); | ||
} | ||
|
||
@Test | ||
void testSetAndGetAllResourceTypes() { | ||
List<ResourceType> resources = new ArrayList<>(); | ||
resources.add(createDummyResourceType()); | ||
database.setAllResourceTypes(resources); | ||
assertEquals(1, database.getAllResourceTypes().size()); | ||
} | ||
|
||
@Test | ||
void testInvalidContentType() { | ||
assertThrows(IllegalArgumentException.class, () -> database.saveContentsToFile(3)); | ||
assertThrows(IllegalArgumentException.class, () -> database.deSerializeObjectFromFile(3)); | ||
} | ||
|
||
@Test | ||
void testNullInput() { | ||
database.setAllTasks(null); | ||
assertTrue(database.getAllTasks().isEmpty()); | ||
|
||
database.setAllResourceTypes(null); | ||
assertTrue(database.getAllResourceTypes().isEmpty()); | ||
} | ||
|
||
@Test | ||
void testToString() { | ||
// As the toString method is not implemented, we just check it doesn't throw an exception | ||
assertDoesNotThrow(() -> database.toString()); | ||
} | ||
|
||
private Task createDummyTask() { | ||
Map<ResourceType, Integer> resources = new HashMap<>(); | ||
resources.put(createDummyResourceType(), 1); | ||
return new Task("DummyTask", resources, 1, LocalDateTime.now(), LocalDateTime.now().plusHours(1), 0, 0); | ||
} | ||
|
||
private ResourceType createDummyResourceType() { | ||
return new ResourceType("DummyResource", 5, 0, 0); | ||
} | ||
} |