|
@ -0,0 +1,77 @@ |
|
|
|
|
|
package org.example; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.*; |
|
|
|
|
|
import java.nio.file.Files; |
|
|
|
|
|
import java.nio.file.Path; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
|
|
|
class StudentTest { |
|
|
|
|
|
|
|
|
|
|
|
@org.junit.jupiter.api.Test |
|
|
|
|
|
void writeToFile() throws IOException { |
|
|
|
|
|
Student student = new Student("Sebastian", "S1001", "Student") ; |
|
|
|
|
|
|
|
|
|
|
|
String filename = "testStudentData.txt"; |
|
|
|
|
|
|
|
|
|
|
|
student.writeToFile(student, filename); |
|
|
|
|
|
|
|
|
|
|
|
Path path = Path.of(filename); |
|
|
|
|
|
String content = Files.readString(path); |
|
|
|
|
|
assertEquals("Sebastian\nS1001\nStudent", content); |
|
|
|
|
|
|
|
|
|
|
|
Files.delete(path); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void readFromFile() throws IOException { |
|
|
|
|
|
|
|
|
|
|
|
try (PrintWriter writer = new PrintWriter(new FileWriter("testStudentData.txt"))){ |
|
|
|
|
|
writer.println("Samuel"); |
|
|
|
|
|
writer.println("S1001"); |
|
|
|
|
|
writer.print("Student"); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
String filename = "testStudentData.txt"; |
|
|
|
|
|
List<Student> testStudents = Student.readFromFile(filename); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(1, testStudents.size()); |
|
|
|
|
|
Student admin = testStudents.get(0); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals("Samuel", admin.getName()); |
|
|
|
|
|
assertEquals("S1001", admin.getId()); |
|
|
|
|
|
assertEquals("Student", admin.getRole()); |
|
|
|
|
|
|
|
|
|
|
|
Files.delete(Path.of(filename)); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void printStudentInfo() { |
|
|
|
|
|
Student student = new Student("Sebastian", "S1001", "Student"); |
|
|
|
|
|
|
|
|
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
|
|
|
|
System.setOut(new PrintStream(outputStream)); |
|
|
|
|
|
|
|
|
|
|
|
student.printStudentInfo(); |
|
|
|
|
|
|
|
|
|
|
|
String printedOutput = outputStream.toString().trim(); |
|
|
|
|
|
|
|
|
|
|
|
assertTrue(printedOutput.contains("Sebastian")); |
|
|
|
|
|
assertTrue(printedOutput.contains("S1001")); |
|
|
|
|
|
assertTrue(printedOutput.contains("Student")); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |