|
|
@ -7,10 +7,18 @@ import static org.junit.Assert.assertEquals; |
|
|
|
import java.io.*; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* The CourseTest class contains unit tests for the Course class. |
|
|
|
* It tests methods for printing course information, writing course data to a file, |
|
|
|
* and reading course data from a file. |
|
|
|
*/ |
|
|
|
class CourseTest { |
|
|
|
private static final String TEMP_FILE = "test_course_data.txt"; |
|
|
|
|
|
|
|
/** |
|
|
|
* Tests the printCourseInfo method of the Course class. |
|
|
|
* Verifies that the method correctly prints course information to the console. |
|
|
|
*/ |
|
|
|
@Test |
|
|
|
void printCourseInfo() { |
|
|
|
// Create a new Course object |
|
|
@ -44,6 +52,10 @@ class CourseTest { |
|
|
|
assertEquals(expectedCourseCredits, outputLines[2].trim()); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Tests the writeToFile method of the Course class. |
|
|
|
* Verifies that course data is correctly written to a file and can be read back. |
|
|
|
*/ |
|
|
|
@Test |
|
|
|
void writeToFile() { |
|
|
|
// Create a new course |
|
|
@ -66,6 +78,10 @@ class CourseTest { |
|
|
|
deleteTempFile(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Tests the readFromFile method of the Course class. |
|
|
|
* Verifies that course data can be correctly read from a file. |
|
|
|
*/ |
|
|
|
@Test |
|
|
|
void readFromFile() { |
|
|
|
// Create a temporary file with sample course data |
|
|
@ -91,6 +107,11 @@ class CourseTest { |
|
|
|
deleteTempFile(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Creates a temporary file with the provided data. |
|
|
|
* |
|
|
|
* @param data The data to write to the file. |
|
|
|
*/ |
|
|
|
private void createTempFile(String... data) { |
|
|
|
try (PrintWriter writer = new PrintWriter(new FileWriter(TEMP_FILE))) { |
|
|
|
for (String line : data) { |
|
|
@ -101,6 +122,9 @@ class CourseTest { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Deletes the temporary file used for testing. |
|
|
|
*/ |
|
|
|
private void deleteTempFile() { |
|
|
|
File file = new File(TEMP_FILE); |
|
|
|
if (file.exists()) { |
|
|
|