From 6ff78829acc70a51fdcd867528801034c81f4e31 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 22:48:18 +0100 Subject: [PATCH 1/9] Creating empty CourseTest file --- src/test/java/org/example/CourseTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/test/java/org/example/CourseTest.java diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java new file mode 100644 index 0000000..1c404db --- /dev/null +++ b/src/test/java/org/example/CourseTest.java @@ -0,0 +1,9 @@ +package org.example; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CourseTest { + +} \ No newline at end of file From 96c6a8da43429c9e864f1acb59a1b841a37fa88c Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 22:58:10 +0100 Subject: [PATCH 2/9] test: printCourseInfo method in Course class --- src/test/java/org/example/CourseTest.java | 37 ++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 1c404db..5445a3d 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -3,7 +3,42 @@ 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; 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()); + } } \ No newline at end of file From 2e1ade76ad3bf75d8841dc8f5d207c3f1f4fdab0 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 22:59:45 +0100 Subject: [PATCH 3/9] Creating empty courseData.txt file --- courseData.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 courseData.txt diff --git a/courseData.txt b/courseData.txt new file mode 100644 index 0000000..e69de29 From 46c2dc1b9eab4cc0e3c87004aa49c46ee606a247 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 23:02:21 +0100 Subject: [PATCH 4/9] Adding writeToFile method in Course class --- src/main/java/org/example/Course.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index e32b098..b294177 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -1,4 +1,7 @@ package org.example; +import java.io.*; + + /** * The Course class represents a course in a curriculum. @@ -96,4 +99,16 @@ public class Course { System.out.println("Course Code: " + courseCode); System.out.println("Course Credits: " + courseCredit); } + + 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(); + } + } + } From 4932036ebd853b9b075f974315bc4fecc028872b Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 23:10:47 +0100 Subject: [PATCH 5/9] Adding readFromFile method in Course class --- src/main/java/org/example/Course.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index b294177..74a5f38 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -1,5 +1,7 @@ package org.example; import java.io.*; +import java.util.List; +import java.util.ArrayList; @@ -111,4 +113,25 @@ public class Course { } } + public static List readFromFile(String filename) { + List 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; + } + + } From 9a9e35153ba435d8d72668333c62122046462aac Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 23:14:25 +0100 Subject: [PATCH 6/9] test: writeToFile method in Course class --- src/test/java/org/example/CourseTest.java | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 5445a3d..0a7d241 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -6,6 +6,9 @@ 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 { @@ -41,4 +44,32 @@ class CourseTest { 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 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(); + } + + } } \ No newline at end of file From 2a2caa891e856ac4a1cf2372908ac38e6d6889c1 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 23:19:26 +0100 Subject: [PATCH 7/9] test: readFromFile method in Course class --- src/test/java/org/example/CourseTest.java | 39 +++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 0a7d241..4c8a05d 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -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 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(); + } } } \ No newline at end of file From 54eb837dbb5df352c84c797fd4440e670cfe118a Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 23:33:53 +0100 Subject: [PATCH 8/9] refactoring: extract temporary file handling into methods in CourseTest file --- src/test/java/org/example/CourseTest.java | 46 +++++++++++------------ 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 4c8a05d..0050875 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -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 courses = Course.readFromFile(filename); + List 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 courses = Course.readFromFile(filename); + List 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(); } From b2a434e5dd0f048ed7279a16e00577e2b489cb77 Mon Sep 17 00:00:00 2001 From: fdai7921 Date: Tue, 6 Feb 2024 23:39:22 +0100 Subject: [PATCH 9/9] Finalizing documentation in Course class and Test file --- src/main/java/org/example/Course.java | 11 ++++++++++ src/test/java/org/example/CourseTest.java | 26 ++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/example/Course.java b/src/main/java/org/example/Course.java index 74a5f38..7715ca3 100644 --- a/src/main/java/org/example/Course.java +++ b/src/main/java/org/example/Course.java @@ -102,6 +102,11 @@ public class Course { 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); @@ -113,6 +118,12 @@ public class Course { } } + /** + * 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 readFromFile(String filename) { List courses = new ArrayList<>(); diff --git a/src/test/java/org/example/CourseTest.java b/src/test/java/org/example/CourseTest.java index 0050875..b7c7620 100644 --- a/src/test/java/org/example/CourseTest.java +++ b/src/test/java/org/example/CourseTest.java @@ -7,10 +7,18 @@ 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 @@ -44,6 +52,10 @@ class CourseTest { 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 @@ -66,6 +78,10 @@ class CourseTest { 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 @@ -91,6 +107,11 @@ class CourseTest { 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) { @@ -101,6 +122,9 @@ class CourseTest { } } + /** + * Deletes the temporary file used for testing. + */ private void deleteTempFile() { File file = new File(TEMP_FILE); if (file.exists()) {