Fakultas Ilmu Komputer UI

Commit 266023da authored by Mohammad Saddam Mashuri's avatar Mohammad Saddam Mashuri
Browse files

Merge branch 'staging' into 'master'

Add more tests to the CLI subclasses

See merge request !8
parents 7071ef5f a4c25774
Pipeline #82389 passed with stages
in 3 minutes and 29 seconds
......@@ -56,44 +56,44 @@ public class Main {
CourseList courseList;
UserList userList;
// --------Uncomment all of this for the "hardcoded" version.
// Uncomment to add a new fresh userList and courseList
courseList = new CourseList();
userList = new UserList();
// Uncomment here to add a new TA or Admin
// 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);
// Add testStudent
new Student("akuStudent", "Aku Student", Long.valueOf("123456789"), userList);
// Add testTa with a mock calendar and schedule
TeachingAssistant testTa = new TeachingAssistant("akuTA", "Aku Asdos", Long.valueOf("123456789"), userList);
testTa.setSchedule("monday", "Free", "12:00", "17:00");
testTa.setSchedule("tuesday", "Free", "08:00", "11:00");
testTa.setSchedule("tuesday", "Free", "12:00", "15:00");
testTa.setSchedule("saturday", "Busy", "07:00", "21:00");
testTa.setSchedule("sunday", "Free", "12:00", "17:00");
new TeachingAssistant("anotherTA", "Another TA", Long.valueOf("1"), userList);
// Add testAdmin
new Admin("akuAdmin", "Aku Admin", userList);
// Add false course
new Course("False Course", "FALSECOURSE", "False Professor", 0, courseList);
// Add mock courses
new Course("Mata Kuliah", "MATKUL420", "Aku Dosen", 4, courseList);
new Course("Software Engineering", "SOFTENG2021", "Aku Professor", 3, courseList);
new Course("Fisika", "PHYS101", "Aku Dosen Fisika", 4, courseList);
// Save the course
ListIO.saveList(courseList, userList);
// ---------------- END OF "HARDCODED" VERSION
// // --------Uncomment all of this for the "hardcoded" version.
// // Uncomment to add a new fresh userList and courseList
// courseList = new CourseList();
// userList = new UserList();
//
// // Uncomment here to add a new TA or Admin
// // 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);
//
// // Add testStudent
// new Student("akuStudent", "Aku Student", Long.valueOf("123456789"), userList);
//
// // Add testTa with a mock calendar and schedule
// TeachingAssistant testTa = new TeachingAssistant("akuTA", "Aku Asdos", Long.valueOf("123456789"), userList);
// testTa.setSchedule("monday", "Free", "12:00", "17:00");
// testTa.setSchedule("tuesday", "Free", "08:00", "11:00");
// testTa.setSchedule("tuesday", "Free", "12:00", "15:00");
// testTa.setSchedule("saturday", "Busy", "07:00", "21:00");
// testTa.setSchedule("sunday", "Free", "12:00", "17:00");
// new TeachingAssistant("anotherTA", "Another TA", Long.valueOf("1"), userList);
//
// // Add testAdmin
// new Admin("akuAdmin", "Aku Admin", userList);
//
// // Add false course
// new Course("False Course", "FALSECOURSE", "False Professor", 0, courseList);
//
// // Add mock courses
// new Course("Mata Kuliah", "MATKUL420", "Aku Dosen", 4, courseList);
// new Course("Software Engineering", "SOFTENG2021", "Aku Professor", 3, courseList);
// new Course("Fisika", "PHYS101", "Aku Dosen Fisika", 4, courseList);
//
// // Save the course
// ListIO.saveList(courseList, userList);
//
// // ---------------- END OF "HARDCODED" VERSION
while (true) {
......
......@@ -4,6 +4,7 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import softeng.g4.Main;
import softeng.g4.course.Course;
import softeng.g4.course.CourseList;
import softeng.g4.user.Admin;
......@@ -11,6 +12,8 @@ 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 AdminCLITest {
......@@ -21,8 +24,16 @@ class AdminCLITest {
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() {
testOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(testOut));
userList = new UserList();
courseList = new CourseList();
......@@ -36,18 +47,90 @@ class AdminCLITest {
"Test Teacher", Long.parseLong("123456789"), userList);
testStudent = new Student("testStudent",
"Test Student", Long.valueOf("123456789"), userList);
ListIO.saveList(courseList, userList);
}
@Test
void main() {
void mainLogOut() {
userInput("LOG OUT");
AdminCLI.main(new String[] {"testAdmin"});
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], "Input: ");
}
@Test
void mainHelp() {
userInput("HELP\nLOG OUT");
AdminCLI.main(new String[] {"testAdmin"});
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 - 14], "Input: You are logged in as an admin (testAdmin)");
}
@Test
void mainAddCourse() {
userInput("ADD COURSE\ntest\ntestId\nTest Professor\n4\nLOG OUT");
AdminCLI.main(new String[] {"testAdmin"});
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 - 2], "Successfully added a new course!");
}
@Test
void mainAssignTA() {
userInput("ADD COURSE\ntest\ntestId\nTest Professor\n4\n" +
"ASSIGN TA\ntestId\ntestTa\nLOG OUT");
AdminCLI.main(new String[] {"testAdmin"});
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 - 3], "Teaching Assistant testTa has successfully assigned to course testId");
}
@Test
void intro() {
}
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();
......
......@@ -4,6 +4,7 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import softeng.g4.Main;
import softeng.g4.course.Course;
import softeng.g4.course.CourseList;
import softeng.g4.user.Admin;
......@@ -11,6 +12,8 @@ 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 StudentCLITest {
......@@ -21,8 +24,16 @@ class StudentCLITest {
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() {
testOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(testOut));
userList = new UserList();
courseList = new CourseList();
......@@ -36,18 +47,59 @@ class StudentCLITest {
"Test Teacher", Long.parseLong("123456789"), userList);
testStudent = new Student("testStudent",
"Test Student", Long.valueOf("123456789"), userList);
ListIO.saveList(courseList, userList);
}
@Test
void main() {
void mainLogOut() {
userInput("LOG OUT");
StudentCLI.main(new String[] {"testStudent"});
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], "Input: ");
}
@Test
void mainHelp() {
userInput("HELP\nLOG OUT");
StudentCLI.main(new String[] {"testStudent"});
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 - 9], "Input: You are logged in as a student (testStudent)");
}
@Test
void intro() {
}
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();
......
......@@ -11,6 +11,8 @@ 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 TACLITest {
......@@ -21,8 +23,16 @@ class TACLITest {
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() {
testOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(testOut));
userList = new UserList();
courseList = new CourseList();
......@@ -36,16 +46,53 @@ class TACLITest {
"Test Teacher", Long.parseLong("123456789"), userList);
testStudent = new Student("testStudent",
"Test Student", Long.valueOf("123456789"), userList);
ListIO.saveList(courseList, userList);
}
@Test
void mainLogOut() {
userInput("LOG OUT");
TACLI.main(new String[] {"testTa"});
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], "Input: ");
}
@Test
void main() {
void mainHelp() {
userInput("HELP\nLOG OUT");
TACLI.main(new String[] {"testTa"});
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 - 13], "Input: You are logged in as a teaching assistant (testTa)");
}
@Test
void intro() {
}
private void userInput(String data) {
testIn = new ByteArrayInputStream(data.getBytes());
System.setIn(testIn);
}
private String getOutput() {
return testOut.toString();
}
@AfterEach
void finalTearDown() {
courseList = new CourseList();
......
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