Fakultas Ilmu Komputer UI

Commit f8cbd952 authored by saddamonpc's avatar saddamonpc
Browse files

Add tests to MainTest.java

parent f609a713
Pipeline #81808 passed with stage
in 27 minutes and 15 seconds
...@@ -2,6 +2,7 @@ package softeng.g4; ...@@ -2,6 +2,7 @@ package softeng.g4;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import softeng.g4.cli.ListIO; import softeng.g4.cli.ListIO;
import softeng.g4.course.Course; import softeng.g4.course.Course;
...@@ -11,43 +12,163 @@ import softeng.g4.user.Student; ...@@ -11,43 +12,163 @@ import softeng.g4.user.Student;
import softeng.g4.user.TeachingAssistant; import softeng.g4.user.TeachingAssistant;
import softeng.g4.user.UserList; import softeng.g4.user.UserList;
import java.io.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class MainTest { class MainTest {
private UserList userList; private UserList userList;
private CourseList courseList; private CourseList courseList;
private Admin testAdmin;
private TeachingAssistant testTa; private final InputStream systemIn = System.in;
private Student testStudent; private final OutputStream systemOut = System.out;
private ByteArrayInputStream testIn;
private ByteArrayOutputStream testOut;
@BeforeEach @BeforeEach
void setUpTearDown() { void setUpTearDown() {
userList = new UserList(); testOut = new ByteArrayOutputStream();
courseList = new CourseList(); System.setOut(new PrintStream(testOut));
}
// This will add false users, for checking users in the arraylist. (NECESSARY) @Test
new Student("falseStudent", "False Student", Long.valueOf("123456789"), userList); void mainExit() throws IOException {
new TeachingAssistant("falseTA", "False TA", Long.valueOf("123456789"), userList); userInput("EXIT");
new Admin("falseAdmin", "False Admin", userList); Main.main(new String[0]);
String output = getOutput();
// Remove carriage break from the print, since different environment uses different line separators
String outContentEdit = output.replaceAll("\r", "");
// Split by \n
String[] outputs = outContentEdit.split("\n");
// Check line output
assertEquals(outputs[outputs.length - 1], "Thank you for using our program! Have a good day!");
}
testAdmin = new Admin("testAdmin", "Test Admin", userList); @Test
testTa = new TeachingAssistant("testTa", void mainFailLoginAsStudent() {
"Test Teacher", Long.parseLong("123456789"), userList); userInput("S\nEXIT");
testStudent = new Student("testStudent", Main.main(new String[0]);
"Test Student", Long.valueOf("123456789"), userList);
String output = getOutput();
// Remove carriage break from the print, since different environment uses different line separators
String outContentEdit = output.replaceAll("\r", "");
// Split by \n
String[] outputs = outContentEdit.split("\n");
// Check line output
assertEquals(outputs[outputs.length - 7], "Wrong username!");
}
@Test
void mainFailLoginAsTeachingAssistant() {
userInput("T\nEXIT");
Main.main(new String[0]);
String output = getOutput();
// Remove carriage break from the print, since different environment uses different line separators
String outContentEdit = output.replaceAll("\r", "");
// Split by \n
String[] outputs = outContentEdit.split("\n");
// Check line output
assertEquals(outputs[outputs.length - 7], "Wrong username!");
} }
@Test @Test
void main() { void mainFailLoginAsAdmin() {
userInput("A\nEXIT");
Main.main(new String[0]);
String output = getOutput();
// Remove carriage break from the print, since different environment uses different line separators
String outContentEdit = output.replaceAll("\r", "");
// Split by \n
String[] outputs = outContentEdit.split("\n");
// Check line output
assertEquals(outputs[outputs.length - 7], "Wrong username!");
} }
@Test @Test
void intro() { void mainFailRegister() {
userInput("REGISTER\ntest\nEXIT");
Main.main(new String[0]);
String output = getOutput();
// Remove carriage break from the print, since different environment uses different line separators
String outContentEdit = output.replaceAll("\r", "");
// Split by \n
String[] outputs = outContentEdit.split("\n");
// Check line output
assertEquals(outputs[outputs.length - 7], "Wrong input!");
}
@Test
void mainRegisterStudent() {
userInput("REGISTER\nS\ntestStudent\nTest Student\n1234567891\nEXIT");
Main.main(new String[0]);
String output = getOutput();
// Remove carriage break from the print, since different environment uses different line separators
String outContentEdit = output.replaceAll("\r", "");
// Split by \n
String[] outputs = outContentEdit.split("\n");
// Check line output
assertEquals(outputs[outputs.length - 7], "Successfully registered testStudent as a Student!");
}
@Test
void mainRegisterTeachingAssistant() {
userInput("REGISTER\nT\ntestTA\nTest TA\n1234567891\nEXIT");
Main.main(new String[0]);
String output = getOutput();
// Remove carriage break from the print, since different environment uses different line separators
String outContentEdit = output.replaceAll("\r", "");
// Split by \n
String[] outputs = outContentEdit.split("\n");
// Check line output
assertEquals(outputs[outputs.length - 7], "Successfully registered testTA as a Teaching Assistant!");
}
@Test
void mainRegisterAdmin() {
userInput("REGISTER\nA\ntestAdmin\nTest Admin\nEXIT");
Main.main(new String[0]);
String output = getOutput();
// Remove carriage break from the print, since different environment uses different line separators
String outContentEdit = output.replaceAll("\r", "");
// Split by \n
String[] outputs = outContentEdit.split("\n");
// Check line output
assertEquals(outputs[outputs.length - 7], "Successfully registered testAdmin as an Admin!");
}
private void userInput(String data) {
testIn = new ByteArrayInputStream(data.getBytes());
System.setIn(testIn);
}
private String getOutput() {
return testOut.toString();
} }
@AfterEach @AfterEach
void finalTearDown() { void finalTearDown() {
// Reset System Input and Output
System.setIn(systemIn);
System.setOut((PrintStream) systemOut);
courseList = new CourseList(); courseList = new CourseList();
userList = new UserList(); userList = new UserList();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment