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