Browse Source

Merge branch 'master' of https://gitlab.cs.hs-fulda.de/fdai7600/PWN_Alpha_Dogs into fdai7887

remotes/origin/fdai7780
fdai7887 11 months ago
parent
commit
f50e096cfd
  1. 0
      courseData.txt
  2. 49
      src/main/java/org/example/Course.java
  3. 134
      src/test/java/org/example/CourseTest.java

0
courseData.txt

49
src/main/java/org/example/Course.java

@ -1,4 +1,9 @@
package org.example;
import java.io.*;
import java.util.List;
import java.util.ArrayList;
/**
* The Course class represents a course in a curriculum.
@ -96,4 +101,48 @@ public class Course {
System.out.println("Course Code: " + courseCode);
System.out.println("Course Credits: " + courseCredit);
}
/**
* Writes the information of the course to a file.
*
* @param filename The name of the file to write to.
*/
public void writeToFile(String filename) {
try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) {
writer.println(courseName);
writer.println(courseCode);
writer.println(courseCredit);
// Add any other fields you want to save
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Reads course information from a file and returns a list of courses.
*
* @param filename The name of the file to read from.
* @return A list of Course objects read from the file.
*/
public static List<Course> readFromFile(String filename) {
List<Course> courses = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String courseName = line;
String courseCode = reader.readLine();
int credits = Integer.parseInt(reader.readLine());
// Read any other fields you saved
Course course = new Course(courseName, courseCode, credits);
courses.add(course);
}
} catch (IOException e) {
e.printStackTrace();
}
return courses;
}
}

134
src/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();
}
}
}
Loading…
Cancel
Save