Skip to content

Commit

Permalink
finish testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan4506 committed Sep 12, 2024
1 parent 5c08911 commit b8027ee
Show file tree
Hide file tree
Showing 9 changed files with 562 additions and 21 deletions.
Binary file modified IndividualProject/data.txt
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ public void reassignTime(String newTime) {
* @throws IllegalArgumentException if the count is invalid.
*/
public void setEnrolledStudentCount(int count) {
if (count >= 0 && count <= this.enrollmentCapacity) {
this.enrolledStudentCount = count;
} else {
throw new IllegalArgumentException("Invalid student count.");
}
this.enrolledStudentCount = count;
}

public boolean isCourseFull() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,96 @@
package dev.coms4156.project.individualproject;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

/**
* Unit tests for the Course class.
* This class contains test cases for verifying the behavior of the Course class methods,
* specifically the toString() method.
*/
/** Unit tests for the Course class. */
@SpringBootTest
@ContextConfiguration
public class CourseUnitTests {

public static Course testCourse;

@BeforeAll
public static void setupCourseForTesting() {
testCourse = new Course("Griffin Newbold", "417 IAB", "11:40-12:55", 250);
testCourse = new Course("Alan Chen", "417 IAB", "11:40-12:55", 240);
}

/**
* Sets up a Course instance for testing.
* This method is executed once before all test methods in this class.
*/
/** Tests toString(). */
@Test
public void toStringTest() {
String expectedResult = "\nInstructor: Griffin Newbold; Location: 417 IAB; Time: 11:40-12:55";
assertEquals(expectedResult, testCourse.toString());
Course testCourse1 = new Course("A B", "120S", "11:20-12:00", 240);
String expectedResult = "\nInstructor: A B; Location: 120S; Time: 11:20-12:00";
assertEquals(expectedResult, testCourse1.toString());
}

/** The test course instance used for testing. */
public static Course testCourse;
/** Tests enrollStudent() (valid case). */
@Test
public void enrollStudentTest() {
testCourse.setEnrolledStudentCount(239);
assertTrue(testCourse.enrollStudent());
}

/** Tests enrollStudent() (full case). */
@Test
public void enrollStudentWhenFullTest() {
testCourse.setEnrolledStudentCount(240);
assertFalse(testCourse.enrollStudent());
}

/** Tests dropStudent() (valid case). */
@Test
public void dropStudentTest() {
testCourse.setEnrolledStudentCount(240);
assertTrue(testCourse.dropStudent());
}

/** Tests dropStudent() (invalid case). */
@Test
public void dropStudentWhenEmptyTest() {
testCourse.setEnrolledStudentCount(0);
assertFalse(testCourse.dropStudent());
}

/** Tests setEnrolledStudentCount(). */
@Test
public void setEnrolledStudentCountValidTest() {
testCourse.setEnrolledStudentCount(100);
}

/** Tests isCourseFull(). */
@Test
public void isCourseFullTest() {
testCourse.setEnrolledStudentCount(240);
assertTrue(testCourse.isCourseFull());

testCourse.setEnrolledStudentCount(239);
assertFalse(testCourse.isCourseFull());
}

/** Tests reassignInstructor(). */
@Test
public void reassignInstructorTest() {
testCourse.reassignInstructor("instructor1");
assertEquals("instructor1", testCourse.getInstructorName());
}

/** Tests reassignLocation(). */
@Test
public void reassignLocationTest() {
testCourse.reassignLocation("location1");
assertEquals("location1", testCourse.getCourseLocation());
}

/** Tests reassignTime(). */
@Test
public void reassignTimeTest() {
testCourse.reassignTime("10:00-11:00");
assertEquals("10:00-11:00", testCourse.getCourseTimeSlot());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package dev.coms4156.project.individualproject;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

/** Unit tests for the Department class. */
@SpringBootTest
@ContextConfiguration
public class DepartmentUnitTests {
private static Department testDepartment;

/** Setup method for creating initial test data. */
@BeforeAll
public static void setupDepartmentForTesting() {
Course course1 = new Course("Danny", "K st", "9:00-10:00", 30);
Course course2 = new Course("Jenny", "L Ave", "10:00-11:00", 50);
Map<String, Course> courses = new HashMap<>();
courses.put("C101", course1);
courses.put("C102", course2);
testDepartment = new Department("CS", courses, "Bill", 200);
}

/** Test getNumberOfMajors(). */
@Test
public void getNumberOfMajorsTest() {
assertEquals(200, testDepartment.getNumberOfMajors());
}

/** Test getDepartmentChair(). */
@Test
public void getDepartmentChairTest() {
assertEquals("Bill", testDepartment.getDepartmentChair());
}

/** Test getCourseSelection(). */
@Test
public void getCourseSelectionTest() {
Map<String, Course> courses = testDepartment.getCourseSelection();
assertEquals(4, courses.size());
assertTrue(courses.containsKey("C101"));
assertTrue(courses.containsKey("C102"));
}

/** Test addPersonToMajor(). */
@Test
public void addPersonToMajorTest() {
int initialMajors = testDepartment.getNumberOfMajors();
testDepartment.addPersonToMajor();
assertEquals(initialMajors + 1, testDepartment.getNumberOfMajors());
}

/** Test dropping a person from the major. */
@Test
public void dropPersonFromMajorTest() {
int initialMajors = testDepartment.getNumberOfMajors();
testDepartment.dropPersonFromMajor();
assertEquals(initialMajors - 1, testDepartment.getNumberOfMajors());
}

/** Test that dropping a person does not decrease the count below 0. */
@Test
public void dropPersonFromMajorNonNegativeTest() {
Department smallDept = new Department("EE", new HashMap<>(), "Dr. Brown", 0);
smallDept.dropPersonFromMajor();
assertEquals(0, smallDept.getNumberOfMajors());
}

/** Test addCourse(). */
@Test
public void addCourseTest() {
Course newCourse = new Course("Klain", "1st Ave", "9:00-16:00", 60);
testDepartment.addCourse("4018", newCourse);
assertTrue(testDepartment.getCourseSelection().containsKey("4018"));
assertEquals(newCourse, testDepartment.getCourseSelection().get("4018"));
}

/** Test createCourse(). */
@Test
public void createCourseTest() {
testDepartment.createCourse("5416", "Bob", "2nd St", "12:00-1:00", 40);
assertTrue(testDepartment.getCourseSelection().containsKey("5416"));
Course createdCourse = testDepartment.getCourseSelection().get("5416");
assertEquals("Bob", createdCourse.getInstructorName());
assertEquals("2nd St", createdCourse.getCourseLocation());
assertEquals("12:00-1:00", createdCourse.getCourseTimeSlot());
}

/** Test toString(). */
@Test
public void toStringTest() {
String expectedOutput =
"CS C101: \n"
+ "Instructor: Danny; Location: K st; Time: 9:00-10:00\n"
+ "CS 4018: \n"
+ "Instructor: Klain; Location: 1st Ave; Time: 9:00-16:00\n"
+ "CS 5416: \n"
+ "Instructor: Bob; Location: 2nd St; Time: 12:00-1:00\n"
+ "CS C102: \n"
+ "Instructor: Jenny; Location: L Ave; Time: 10:00-11:00\n";
assertEquals(expectedOutput, testDepartment.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package dev.coms4156.project.individualproject;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

/** Tests the IndividualProjectApplication class. */
@SpringBootTest
@ContextConfiguration
public class IndividualProjectApplicationTests {

private IndividualProjectApplication app;

/** Sets up the application and mocks the database before each test. */
@BeforeEach
public void setup() {
app = new IndividualProjectApplication();
MyFileDatabase mockDatabase = Mockito.mock(MyFileDatabase.class);
IndividualProjectApplication.overrideDatabase(mockDatabase);
}

/** Tests main(). */
@Test
public void testMainMethod() {
IndividualProjectApplication.main(new String[] {});
}

/** Tests onTermination(). */
@Test
public void testOnTerminationSavesData() {
app.onTermination();
}

/** Tests run() with the "setup" argument. */
@Test
public void testRunWithSetup() {
app.run(new String[] {"setup"});
assertNotNull(IndividualProjectApplication.myFileDatabase);
}

/** Tests run() without the "setup" argument. */
@Test
public void testRunWithoutSetup() {
app.run(new String[] {});
assertNotNull(IndividualProjectApplication.myFileDatabase);
}

/** Tests resetDataFile(). */
@Test
public void testResetDataFile() {
app.resetDataFile();
assertNotNull(IndividualProjectApplication.myFileDatabase);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package dev.coms4156.project.individualproject;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/** Unit tests for the MyFileDatabase class. */
public class MyFileDatabaseUnitTests {

private static final String TEST_FILE_PATH = "./test_data.txt";

private MyFileDatabase myFileDatabase;

/** Sets up the MyFileDatabase instance before each test. */
@BeforeEach
public void setUp() {
myFileDatabase = new MyFileDatabase(1, TEST_FILE_PATH);
}

/** Tests setMapping(). */
@Test
public void testSetMapping() {
Map<String, Department> testMapping = new HashMap<>();
testMapping.put("FIN", new Department("FIN", new HashMap<>(), "chair1", 120));
myFileDatabase.setMapping(testMapping);
assertEquals(testMapping, myFileDatabase.getDepartmentMapping());
}

/** Tests getDepartmentMapping(). */
@Test
public void testGetDepartmentMapping() {
assertNull(myFileDatabase.getDepartmentMapping());
}

/** Tests toString(). */
@Test
public void testToString() {
Map<String, Department> testMapping = new HashMap<>();
Department department = new Department("FIN", new HashMap<>(), "chair1", 120);
testMapping.put("FIN", department);
myFileDatabase.setMapping(testMapping);
String expected = "For the FIN department: \n" + department;
assertEquals(expected, myFileDatabase.toString());
}

/** Tests saveContentsToFile(). */
@Test
public void testSaveContentsToFile() {
Map<String, Department> testMapping = new HashMap<>();
Department department = new Department("FIN", new HashMap<>(), "chair1", 120);
testMapping.put("FIN", department);
myFileDatabase.setMapping(testMapping);
myFileDatabase.saveContentsToFile();
File file = new File(TEST_FILE_PATH);
assertTrue(file.exists());
}

/** Tests deSerializeObjectFromFile(). */
@Test
public void testDeSerializeObjectFromFile() {
myFileDatabase = new MyFileDatabase(1, "./data.txt");
Map<String, Department> deserializedMapping = myFileDatabase.deSerializeObjectFromFile();
assertFalse(deserializedMapping.isEmpty());
}
}
Loading

0 comments on commit b8027ee

Please sign in to comment.