Browse Source
Merge branch 'master' of https://gitlab.cs.hs-fulda.de/fdai7600/PWN_Alpha_Dogs into fdai7887
remotes/origin/fdai7780
Merge branch 'master' of https://gitlab.cs.hs-fulda.de/fdai7600/PWN_Alpha_Dogs into fdai7887
remotes/origin/fdai7780
fdai7887
11 months ago
3 changed files with 183 additions and 0 deletions
-
0courseData.txt
-
49src/main/java/org/example/Course.java
-
134src/test/java/org/example/CourseTest.java
@ -0,0 +1,134 @@ |
|||
package org.example; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
import java.io.ByteArrayOutputStream; |
|||
import java.io.PrintStream; |
|||
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 |
|||
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()); |
|||
} |
|||
|
|||
/** |
|||
* 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 |
|||
Course course = new Course("Math", "C101", 3); |
|||
|
|||
// Write course data to the temporary file |
|||
course.writeToFile(TEMP_FILE); |
|||
|
|||
// Read course data from the temporary file |
|||
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 |
|||
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 |
|||
createTempFile("Math", "C101", "3", "Science", "C201", "4"); |
|||
|
|||
// Read course data from the temporary file |
|||
List<Course> courses = Course.readFromFile(TEMP_FILE); |
|||
|
|||
// 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 |
|||
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) { |
|||
writer.println(line); |
|||
} |
|||
} catch (IOException e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
|
|||
/** |
|||
* Deletes the temporary file used for testing. |
|||
*/ |
|||
private void deleteTempFile() { |
|||
File file = new File(TEMP_FILE); |
|||
if (file.exists()) { |
|||
file.delete(); |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue