|
|
@ -1,7 +1,5 @@ |
|
|
|
package org.example; |
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
import java.io.PrintStream; |
|
|
@ -70,6 +68,43 @@ class CourseTest { |
|
|
|
if (file.exists()) { |
|
|
|
file.delete(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
void readFromFile() { |
|
|
|
// Create a temporary file with sample course data |
|
|
|
String filename = "test_course_data.txt"; |
|
|
|
try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) { |
|
|
|
writer.println("Math"); |
|
|
|
writer.println("C101"); |
|
|
|
writer.println(3); |
|
|
|
writer.println("Science"); |
|
|
|
writer.println("C201"); |
|
|
|
writer.println(4); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
// Read course data from the temporary file |
|
|
|
List<Course> courses = Course.readFromFile(filename); |
|
|
|
|
|
|
|
// Assert that the read data matches the original course data |
|
|
|
assertEquals(2, courses.size()); |
|
|
|
|
|
|
|
Course course1 = courses.get(0); |
|
|
|
assertEquals("Math", course1.getCourseName()); |
|
|
|
assertEquals("C101", course1.getCourseCode()); |
|
|
|
assertEquals(3, course1.getCourseCredit()); |
|
|
|
|
|
|
|
Course course2 = courses.get(1); |
|
|
|
assertEquals("Science", course2.getCourseName()); |
|
|
|
assertEquals("C201", course2.getCourseCode()); |
|
|
|
assertEquals(4, course2.getCourseCredit()); |
|
|
|
|
|
|
|
// Delete the temporary file |
|
|
|
File file = new File(filename); |
|
|
|
if (file.exists()) { |
|
|
|
file.delete(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |