Fakultas Ilmu Komputer UI

Commit b3cf240b authored by saddamonpc's avatar saddamonpc
Browse files

Add more tests to the CLI subclasses

parent f8cbd952
Pipeline #82387 passed with stage
in 1 minute and 20 seconds
...@@ -56,44 +56,44 @@ public class Main { ...@@ -56,44 +56,44 @@ public class Main {
CourseList courseList; CourseList courseList;
UserList userList; UserList userList;
// --------Uncomment all of this for the "hardcoded" version. // // --------Uncomment all of this for the "hardcoded" version.
// Uncomment to add a new fresh userList and courseList // // Uncomment to add a new fresh userList and courseList
courseList = new CourseList(); // courseList = new CourseList();
userList = new UserList(); // userList = new UserList();
//
// Uncomment here to add a new TA or Admin // // Uncomment here to add a new TA or Admin
// This will add false users, for checking users in the arraylist. (NECESSARY) // // This will add false users, for checking users in the arraylist. (NECESSARY)
new Student("falseStudent", "False Student", Long.valueOf("123456789"), userList); // new Student("falseStudent", "False Student", Long.valueOf("123456789"), userList);
new TeachingAssistant("falseTA", "False TA", Long.valueOf("123456789"), userList); // new TeachingAssistant("falseTA", "False TA", Long.valueOf("123456789"), userList);
new Admin("falseAdmin", "False Admin", userList); // new Admin("falseAdmin", "False Admin", userList);
//
// Add testStudent // // Add testStudent
new Student("akuStudent", "Aku Student", Long.valueOf("123456789"), userList); // new Student("akuStudent", "Aku Student", Long.valueOf("123456789"), userList);
//
// Add testTa with a mock calendar and schedule // // Add testTa with a mock calendar and schedule
TeachingAssistant testTa = new TeachingAssistant("akuTA", "Aku Asdos", Long.valueOf("123456789"), userList); // TeachingAssistant testTa = new TeachingAssistant("akuTA", "Aku Asdos", Long.valueOf("123456789"), userList);
testTa.setSchedule("monday", "Free", "12:00", "17:00"); // testTa.setSchedule("monday", "Free", "12:00", "17:00");
testTa.setSchedule("tuesday", "Free", "08:00", "11:00"); // testTa.setSchedule("tuesday", "Free", "08:00", "11:00");
testTa.setSchedule("tuesday", "Free", "12:00", "15:00"); // testTa.setSchedule("tuesday", "Free", "12:00", "15:00");
testTa.setSchedule("saturday", "Busy", "07:00", "21:00"); // testTa.setSchedule("saturday", "Busy", "07:00", "21:00");
testTa.setSchedule("sunday", "Free", "12:00", "17:00"); // testTa.setSchedule("sunday", "Free", "12:00", "17:00");
new TeachingAssistant("anotherTA", "Another TA", Long.valueOf("1"), userList); // new TeachingAssistant("anotherTA", "Another TA", Long.valueOf("1"), userList);
//
// Add testAdmin // // Add testAdmin
new Admin("akuAdmin", "Aku Admin", userList); // new Admin("akuAdmin", "Aku Admin", userList);
//
// Add false course // // Add false course
new Course("False Course", "FALSECOURSE", "False Professor", 0, courseList); // new Course("False Course", "FALSECOURSE", "False Professor", 0, courseList);
//
// Add mock courses // // Add mock courses
new Course("Mata Kuliah", "MATKUL420", "Aku Dosen", 4, courseList); // new Course("Mata Kuliah", "MATKUL420", "Aku Dosen", 4, courseList);
new Course("Software Engineering", "SOFTENG2021", "Aku Professor", 3, courseList); // new Course("Software Engineering", "SOFTENG2021", "Aku Professor", 3, courseList);
new Course("Fisika", "PHYS101", "Aku Dosen Fisika", 4, courseList); // new Course("Fisika", "PHYS101", "Aku Dosen Fisika", 4, courseList);
//
// Save the course // // Save the course
ListIO.saveList(courseList, userList); // ListIO.saveList(courseList, userList);
//
// ---------------- END OF "HARDCODED" VERSION // // ---------------- END OF "HARDCODED" VERSION
while (true) { while (true) {
......
...@@ -4,6 +4,7 @@ import org.junit.jupiter.api.AfterAll; ...@@ -4,6 +4,7 @@ import org.junit.jupiter.api.AfterAll;
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.Test; import org.junit.jupiter.api.Test;
import softeng.g4.Main;
import softeng.g4.course.Course; import softeng.g4.course.Course;
import softeng.g4.course.CourseList; import softeng.g4.course.CourseList;
import softeng.g4.user.Admin; import softeng.g4.user.Admin;
...@@ -11,6 +12,8 @@ import softeng.g4.user.Student; ...@@ -11,6 +12,8 @@ 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 AdminCLITest { class AdminCLITest {
...@@ -21,8 +24,16 @@ class AdminCLITest { ...@@ -21,8 +24,16 @@ class AdminCLITest {
private TeachingAssistant testTa; private TeachingAssistant testTa;
private Student testStudent; private Student testStudent;
private final InputStream systemIn = System.in;
private final OutputStream systemOut = System.out;
private ByteArrayInputStream testIn;
private ByteArrayOutputStream testOut;
@BeforeEach @BeforeEach
void setUpTearDown() { void setUpTearDown() {
testOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(testOut));
userList = new UserList(); userList = new UserList();
courseList = new CourseList(); courseList = new CourseList();
...@@ -36,18 +47,90 @@ class AdminCLITest { ...@@ -36,18 +47,90 @@ class AdminCLITest {
"Test Teacher", Long.parseLong("123456789"), userList); "Test Teacher", Long.parseLong("123456789"), userList);
testStudent = new Student("testStudent", testStudent = new Student("testStudent",
"Test Student", Long.valueOf("123456789"), userList); "Test Student", Long.valueOf("123456789"), userList);
ListIO.saveList(courseList, userList);
} }
@Test @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 @Test
void intro() { void intro() {
} }
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();
......
...@@ -4,6 +4,7 @@ import org.junit.jupiter.api.AfterAll; ...@@ -4,6 +4,7 @@ import org.junit.jupiter.api.AfterAll;
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.Test; import org.junit.jupiter.api.Test;
import softeng.g4.Main;
import softeng.g4.course.Course; import softeng.g4.course.Course;
import softeng.g4.course.CourseList; import softeng.g4.course.CourseList;
import softeng.g4.user.Admin; import softeng.g4.user.Admin;
...@@ -11,6 +12,8 @@ import softeng.g4.user.Student; ...@@ -11,6 +12,8 @@ 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 StudentCLITest { class StudentCLITest {
...@@ -21,8 +24,16 @@ class StudentCLITest { ...@@ -21,8 +24,16 @@ class StudentCLITest {
private TeachingAssistant testTa; private TeachingAssistant testTa;
private Student testStudent; private Student testStudent;
private final InputStream systemIn = System.in;
private final OutputStream systemOut = System.out;
private ByteArrayInputStream testIn;
private ByteArrayOutputStream testOut;
@BeforeEach @BeforeEach
void setUpTearDown() { void setUpTearDown() {
testOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(testOut));
userList = new UserList(); userList = new UserList();
courseList = new CourseList(); courseList = new CourseList();
...@@ -36,18 +47,59 @@ class StudentCLITest { ...@@ -36,18 +47,59 @@ class StudentCLITest {
"Test Teacher", Long.parseLong("123456789"), userList); "Test Teacher", Long.parseLong("123456789"), userList);
testStudent = new Student("testStudent", testStudent = new Student("testStudent",
"Test Student", Long.valueOf("123456789"), userList); "Test Student", Long.valueOf("123456789"), userList);
ListIO.saveList(courseList, userList);
} }
@Test @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 @Test
void intro() { void intro() {
} }
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();
......
...@@ -11,6 +11,8 @@ import softeng.g4.user.Student; ...@@ -11,6 +11,8 @@ 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 TACLITest { class TACLITest {
...@@ -21,8 +23,16 @@ class TACLITest { ...@@ -21,8 +23,16 @@ class TACLITest {
private TeachingAssistant testTa; private TeachingAssistant testTa;
private Student testStudent; private Student testStudent;
private final InputStream systemIn = System.in;
private final OutputStream systemOut = System.out;
private ByteArrayInputStream testIn;
private ByteArrayOutputStream testOut;
@BeforeEach @BeforeEach
void setUpTearDown() { void setUpTearDown() {
testOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(testOut));
userList = new UserList(); userList = new UserList();
courseList = new CourseList(); courseList = new CourseList();
...@@ -36,16 +46,53 @@ class TACLITest { ...@@ -36,16 +46,53 @@ class TACLITest {
"Test Teacher", Long.parseLong("123456789"), userList); "Test Teacher", Long.parseLong("123456789"), userList);
testStudent = new Student("testStudent", testStudent = new Student("testStudent",
"Test Student", Long.valueOf("123456789"), userList); "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 @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 @Test
void intro() { void intro() {
} }
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() {
courseList = new CourseList(); courseList = new CourseList();
......
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