|
|
@ -1,6 +1,6 @@ |
|
|
|
package org.example; |
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
import java.io.PrintStream; |
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
@ -9,6 +9,7 @@ import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
class CourseTest { |
|
|
|
private static final String TEMP_FILE = "test_course_data.txt"; |
|
|
|
|
|
|
|
@Test |
|
|
|
void printCourseInfo() { |
|
|
@ -45,48 +46,33 @@ class CourseTest { |
|
|
|
|
|
|
|
@Test |
|
|
|
void writeToFile() { |
|
|
|
// Create a temporary file for testing |
|
|
|
String filename = "test_course_data.txt"; |
|
|
|
|
|
|
|
// Create a new course |
|
|
|
Course course = new Course("Math", "C101", 3); |
|
|
|
|
|
|
|
// Write course data to the temporary file |
|
|
|
course.writeToFile(filename); |
|
|
|
course.writeToFile(TEMP_FILE); |
|
|
|
|
|
|
|
// Read course data from the temporary file |
|
|
|
List<Course> courses = Course.readFromFile(filename); |
|
|
|
List<Course> courses = Course.readFromFile(TEMP_FILE); |
|
|
|
|
|
|
|
// Assert that the read data matches the original course |
|
|
|
assertEquals(1, courses.size()); |
|
|
|
Course readCourse = courses.get(0); |
|
|
|
assertEquals("Math", readCourse.getCourseName()); |
|
|
|
assertEquals("C101", readCourse.getCourseCode()); |
|
|
|
assertEquals(3, readCourse.getCourseCredit()); |
|
|
|
|
|
|
|
// Delete the temporary file |
|
|
|
File file = new File(filename); |
|
|
|
if (file.exists()) { |
|
|
|
file.delete(); |
|
|
|
} |
|
|
|
deleteTempFile(); |
|
|
|
} |
|
|
|
|
|
|
|
@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(); |
|
|
|
} |
|
|
|
createTempFile("Math", "C101", "3", "Science", "C201", "4"); |
|
|
|
|
|
|
|
// Read course data from the temporary file |
|
|
|
List<Course> courses = Course.readFromFile(filename); |
|
|
|
List<Course> courses = Course.readFromFile(TEMP_FILE); |
|
|
|
|
|
|
|
// Assert that the read data matches the original course data |
|
|
|
assertEquals(2, courses.size()); |
|
|
@ -102,7 +88,21 @@ class CourseTest { |
|
|
|
assertEquals(4, course2.getCourseCredit()); |
|
|
|
|
|
|
|
// Delete the temporary file |
|
|
|
File file = new File(filename); |
|
|
|
deleteTempFile(); |
|
|
|
} |
|
|
|
|
|
|
|
private void createTempFile(String... data) { |
|
|
|
try (PrintWriter writer = new PrintWriter(new FileWriter(TEMP_FILE))) { |
|
|
|
for (String line : data) { |
|
|
|
writer.println(line); |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void deleteTempFile() { |
|
|
|
File file = new File(TEMP_FILE); |
|
|
|
if (file.exists()) { |
|
|
|
file.delete(); |
|
|
|
} |
|
|
|