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;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import softeng.g4.cli.ListIO;
import softeng.g4.course.Course;
......@@ -11,43 +12,163 @@ import softeng.g4.user.Student;
import softeng.g4.user.TeachingAssistant;
import softeng.g4.user.UserList;
import java.io.*;
import static org.junit.jupiter.api.Assertions.*;
class MainTest {
private UserList userList;
private CourseList courseList;
private Admin testAdmin;
private TeachingAssistant testTa;
private Student testStudent;
private final InputStream systemIn = System.in;
private final OutputStream systemOut = System.out;
private ByteArrayInputStream testIn;
private ByteArrayOutputStream testOut;
@BeforeEach
void setUpTearDown() {
userList = new UserList();
courseList = new CourseList();
testOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(testOut));
}
// This will add false users, for checking users in the arraylist. (NECESSARY)
new Student("falseStudent", "False Student", Long.valueOf("123456789"), userList);
new TeachingAssistant("falseTA", "False TA", Long.valueOf("123456789"), userList);
new Admin("falseAdmin", "False Admin", userList);
@Test
void mainExit() throws IOException {
userInput("EXIT");
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);
testTa = new TeachingAssistant("testTa",
"Test Teacher", Long.parseLong("123456789"), userList);
testStudent = new Student("testStudent",
"Test Student", Long.valueOf("123456789"), userList);
@Test
void mainFailLoginAsStudent() {
userInput("S\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
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
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
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
void finalTearDown() {
// Reset System Input and Output
System.setIn(systemIn);
System.setOut((PrintStream) systemOut);
courseList = new CourseList();
userList = new UserList();
......
Markdown is supported
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