Browse Source
Merge branch 'fdai7921' into 'master'
Merge branch 'fdai7921' into 'master'
Fdai7921 See merge request fdai7600/PWN_Alpha_Dogs!3remotes/origin/fdai7780
fdai7921
11 months ago
3 changed files with 148 additions and 0 deletions
-
0courseData.txt
-
38src/main/java/org/example/Course.java
-
110src/test/java/org/example/CourseTest.java
@ -0,0 +1,110 @@ |
|||
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; |
|||
import java.io.*; |
|||
import java.util.List; |
|||
|
|||
|
|||
class CourseTest { |
|||
|
|||
@Test |
|||
void printCourseInfo() { |
|||
// Create a new Course object |
|||
Course course = new Course("Introduction to Programming", "CS101", 3); |
|||
|
|||
// Set up the expected output |
|||
String expectedCourseName = "Course Name: Introduction to Programming"; |
|||
String expectedCourseCode = "Course Code: CS101"; |
|||
String expectedCourseCredits = "Course Credits: 3"; |
|||
|
|||
// Redirect System.out to capture printed output |
|||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|||
PrintStream originalOut = System.out; |
|||
System.setOut(new PrintStream(outputStream)); |
|||
|
|||
// Call the method to be tested |
|||
course.printCourseInfo(); |
|||
|
|||
// Reset System.out |
|||
System.setOut(originalOut); |
|||
|
|||
// Get the printed output |
|||
String printedOutput = outputStream.toString().trim(); // trim to remove trailing newline |
|||
|
|||
// Split the printed output into lines |
|||
String[] outputLines = printedOutput.split("\\r?\\n"); |
|||
|
|||
// Assert that each line matches the expected output |
|||
assertEquals(expectedCourseName, outputLines[0].trim()); |
|||
assertEquals(expectedCourseCode, outputLines[1].trim()); |
|||
assertEquals(expectedCourseCredits, outputLines[2].trim()); |
|||
} |
|||
|
|||
@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); |
|||
|
|||
// Read course data from the temporary file |
|||
List<Course> courses = Course.readFromFile(filename); |
|||
|
|||
// Assert that the read data matches the original course |
|||
assertEquals(1, courses.size()); |
|||
Course readCourse = courses.get(0); |
|||
assertEquals("Math", readCourse.getCourseName()); |
|||
assertEquals(3, readCourse.getCourseCredit()); |
|||
|
|||
// Delete the temporary file |
|||
File file = new File(filename); |
|||
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(); |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue