Skip to content

Commit

Permalink
Add unit test for MyFileDatabase class (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyshin04 authored Oct 18, 2024
1 parent 70ee677 commit 5d0b5ff
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public Location getLocation() {
return location;
}

public String getTypeName() {
return typeName;
}

/**
* Counts the number of available resources within this resource type at the specified time.
*
Expand Down
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);
}
}

0 comments on commit 5d0b5ff

Please sign in to comment.