Skip to content

Commit 8c97685

Browse files
committed
Add JUnit testcase for Gloqi.java
1 parent 8c12c80 commit 8c97685

File tree

5 files changed

+175
-17
lines changed

5 files changed

+175
-17
lines changed

src/main/java/gloqi/ui/DataManager.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class DataManager {
2424
*
2525
* @param dataPath path to the data file eg."data/gloqi.txt"
2626
*/
27-
public DataManager(String dataPath) {
27+
public DataManager(String dataPath) throws GloqiException {
2828
resolveAppDataPaths(dataPath);
2929
setupDataFile();
3030
}
@@ -40,30 +40,28 @@ private void resolveAppDataPaths(String dataPath) {
4040
this.appDataFile = appDir.resolve(path.getFileName());
4141
}
4242

43-
private void setupDataFile() {
43+
private void setupDataFile() throws GloqiException {
4444
createDirectoryIfMissing();
4545
createFileIfMissing();
4646
}
4747

48-
private void createDirectoryIfMissing() {
48+
private void createDirectoryIfMissing() throws GloqiException {
4949
try {
5050
if (!Files.exists(appDataDir)) {
5151
Files.createDirectories(appDataDir);
52-
System.out.println("Storage directory created: " + appDataDir);
5352
}
5453
} catch (Exception e) {
55-
System.out.println("Error creating directory: " + e.getMessage());
54+
throw new GloqiException("Failed to create directory for data file!\n Error:" + e.getMessage());
5655
}
5756
}
5857

59-
private void createFileIfMissing() {
58+
private void createFileIfMissing() throws GloqiException {
6059
try {
6160
if (!Files.exists(appDataFile)) {
6261
Files.createFile(appDataFile);
63-
System.out.println("Storage file created: " + appDataFile);
6462
}
6563
} catch (Exception e) {
66-
System.out.println("Error creating file: " + e.getMessage());
64+
throw new GloqiException("Failed to create data file!\n Error:" + e.getMessage());
6765
}
6866
}
6967

@@ -89,7 +87,7 @@ public void writeDataFile(ArrayList<Task> bankList) throws GloqiException {
8987
*/
9088
public ArrayList<Task> loadDataFile() throws GloqiException {
9189
if (isFileEmpty()) {
92-
return new ArrayList<>();
90+
throw new GloqiException("No data file found!\nStart up with a fresh file!");
9391
}
9492
Object obj = readSerializedObject();
9593
return validateAndConvert(obj);
@@ -111,7 +109,7 @@ private Object readSerializedObject() throws GloqiException {
111109

112110
private ArrayList<Task> validateAndConvert(Object obj) throws GloqiException {
113111
if (!(obj instanceof ArrayList<?> rawList)) {
114-
throw new GloqiException("File might be corrupted");
112+
throw new GloqiException("Failed to read from file!\nStart up with a fresh file!");
115113
}
116114

117115
ArrayList<Task> tasks = new ArrayList<>();
@@ -123,7 +121,7 @@ private ArrayList<Task> validateAndConvert(Object obj) throws GloqiException {
123121

124122
private Task validateTask(Object o) throws GloqiException {
125123
if (!(o instanceof Task task)) {
126-
throw new GloqiException("File might be corrupted");
124+
throw new GloqiException("Failed to read from file!\nStart up with a fresh file!");
127125
}
128126
return task;
129127
}

src/main/java/gloqi/ui/Gloqi.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,43 @@
1616
public class Gloqi {
1717

1818
private static final String CHATBOT_NAME = "Gloqi";
19+
private static final String DEFAULT_DATA_PATH = "data/data.txt";
1920
private final Ui ui;
2021
private BankList bankList;
2122

2223
/**
2324
* Creates a new Gloqi chatbot with default data file path:data/data.txt for storing tasks.
2425
*/
2526
public Gloqi() {
26-
this.bankList = new BankList(new DataManager("data/data.txt"));
2727
this.ui = new Ui(CHATBOT_NAME);
2828
}
2929

30+
/**
31+
* Initializes the task bank by loading tasks from persistent storage.
32+
* <p>
33+
* This convenience overload uses the default data file path {@value #DEFAULT_DATA_PATH}.
34+
* See {@link #initialize(String)} for behavior when a custom path is supplied.
35+
*
36+
* @return a success message if tasks are loaded, or an error message if loading fails
37+
* @see #initialize(String)
38+
*/
39+
public String initialize() {
40+
return initialize(DEFAULT_DATA_PATH);
41+
}
42+
3043
/**
3144
* Initializes the task bank by loading tasks from persistent storage.
3245
* <p>
3346
* If the data file is corrupted or cannot be read, an error message is returned
3447
* from the thrown {@link GloqiException}. Otherwise, confirms that tasks were
3548
* loaded successfully.
3649
*
50+
* @param filePath path to the data file used for loading tasks
3751
* @return a success message if tasks are loaded, or an error message if loading fails
3852
*/
39-
public String initialize() {
53+
public String initialize(String filePath) {
4054
try {
55+
this.bankList = new BankList(new DataManager(filePath));
4156
bankList = bankList.loadBankList();
4257
} catch (GloqiException e) {
4358
return e.getMessage();
@@ -134,7 +149,7 @@ private String handleShow(CommandParser parser) {
134149
return bankList.printList(parser.getDateArg());
135150
}
136151

137-
private String handleFind(CommandParser parser) throws GloqiException {
152+
private String handleFind(CommandParser parser) {
138153
return bankList.findTask(parser.getStringArg()[0]);
139154
}
140155

src/test/java/gloqi/task/DeadlineTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class DeadlineTest {
1111
@Test
1212
public void deadlineConstructor_validInput_success() throws GloqiException {
13-
assertEquals("[D][ ] test (by: May 06 2019 6 PM)",
13+
assertEquals("[D][ ] test (by: May 06 2019 6 pm)",
1414
new Deadline(new String[]{"test", "2019-05-06 1800"}).toString());
1515

1616
}

src/test/java/gloqi/ui/BankListTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ public void markTask_validInput_success() throws GloqiException {
2525
}
2626

2727
@Test
28-
public void markTask_outOfRangeInput_success() throws GloqiException {
28+
public void markTask_outOfRangeInput_exceptionThrow() throws GloqiException {
2929
BankList bankList = new BankList(new DataManager("data/data.txt"));
3030
bankList.addTask(new Todo("read book"));
3131
GloqiException exception = assertThrows(GloqiException.class, () -> bankList.markTask(9));
3232
assertEquals("Task index 10 is out of range", exception.getMessage());
3333
}
3434

3535
@Test
36-
public void markTask_negativeInput_success() throws GloqiException {
36+
public void markTask_negativeInput_exceptionThrow() throws GloqiException {
3737
BankList bankList = new BankList(new DataManager("data/data.txt"));
3838
bankList.addTask(new Todo("read book"));
3939
GloqiException exception = assertThrows(GloqiException.class, () -> bankList.markTask(-2));
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package gloqi.ui;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.nio.file.Path;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.io.TempDir;
9+
10+
public class GloqiTest {
11+
@Test
12+
public void initialize_invalidStorage_success(@TempDir Path tmp) throws GloqiException {
13+
Gloqi gloqi = new Gloqi();
14+
Path file = tmp.resolve("gloqi.txt");
15+
String response = gloqi.initialize(file.toString());
16+
assertEquals("No data file found!\nStart up with a fresh file!", response);
17+
}
18+
@Test
19+
public void initialize_validStorage_success(@TempDir Path tmp) {
20+
Gloqi gloqi = new Gloqi();
21+
Path file = tmp.resolve("gloqi.txt");
22+
gloqi.initialize(file.toString());
23+
gloqi.run("todo Read book");
24+
String response = gloqi.initialize(file.toString());
25+
assertEquals("Tasks are loaded successfully!", response);
26+
}
27+
28+
@Test
29+
public void getGreeting_success() {
30+
Gloqi gloqi = new Gloqi();
31+
String response = gloqi.getGreeting();
32+
assertEquals("Hello I am Gloqi\nhow can i help you", response);
33+
}
34+
35+
@Test
36+
public void run_validList_success(@TempDir Path tmp) {
37+
Gloqi gloqi = testFileInistalize(tmp);
38+
gloqi.run("todo Read book");
39+
String response = gloqi.run("List");
40+
assertEquals("1. [T][ ] Read book", response);
41+
}
42+
43+
@Test
44+
public void run_validMark_success(@TempDir Path tmp) {
45+
Gloqi gloqi = testFileInistalize(tmp);
46+
gloqi.run("todo Read book");
47+
String response = gloqi.run("mark 1");
48+
assertEquals("Nice! I've marked this task as done:\n[T][x] Read book", response);
49+
}
50+
51+
@Test
52+
public void run_invalidMark_success(@TempDir Path tmp) {
53+
Gloqi gloqi = testFileInistalize(tmp);
54+
String response = gloqi.run("mark 999");
55+
assertEquals("Task index 999 is out of range", response);
56+
}
57+
58+
@Test
59+
public void run_validUnmark_success(@TempDir Path tmp) {
60+
Gloqi gloqi = testFileInistalize(tmp);
61+
gloqi.run("todo Read book");
62+
String response = gloqi.run("unmark 1");
63+
assertEquals("OK, I've marked this task as not done yet:\n[T][ ] Read book", response);
64+
}
65+
66+
@Test
67+
public void run_validDeadline_success(@TempDir Path tmp) {
68+
Gloqi gloqi = testFileInistalize(tmp);
69+
String response = gloqi.run("Deadline Submit assignment /by 2024-09-30 2359");
70+
String expected = """
71+
Got it. I've added this task:
72+
[D][ ] Submit assignment (by: Sep 30 2024 11 pm)
73+
Now you have 1 tasks in the bank.""";
74+
assertEquals(expected, response);
75+
}
76+
77+
@Test
78+
public void run_validEvent_success(@TempDir Path tmp) {
79+
Gloqi gloqi = testFileInistalize(tmp);
80+
String response = gloqi.run("Event Team meeting /from 2024-10-01 1400 /to 2024-10-01 1500");
81+
String expected = """
82+
Got it. I've added this task:
83+
[E][ ] Team meeting (from: Oct 01 2024 2 pm to: Oct 01 2024 3 pm)
84+
Now you have 1 tasks in the bank.""";
85+
assertEquals(expected, response);
86+
}
87+
88+
@Test
89+
public void run_validDelete_success(@TempDir Path tmp) {
90+
Gloqi gloqi = testFileInistalize(tmp);
91+
gloqi.run("todo Read book");
92+
String response = gloqi.run("delete 1");
93+
String expected = """
94+
Tasks have been deleted:
95+
[T][ ] Read book\n
96+
Now you have 0 tasks in the bank.""";
97+
assertEquals(expected, response);
98+
}
99+
100+
@Test
101+
public void run_validShow_success(@TempDir Path tmp) {
102+
Gloqi gloqi = testFileInistalize(tmp);
103+
gloqi.run("Deadline Submit assignment /by 2024-09-30 2359");
104+
String response = gloqi.run("show 2024-09-30");
105+
String expected = """
106+
Tasks found on date: Sep 30 2024
107+
1. [D][ ] Submit assignment (by: Sep 30 2024 11 pm)""";
108+
assertEquals(expected, response);
109+
}
110+
111+
@Test
112+
public void run_validFind_success(@TempDir Path tmp) {
113+
Gloqi gloqi = testFileInistalize(tmp);
114+
gloqi.run("Deadline Submit assignment /by 2024-09-30 2359");
115+
String response = gloqi.run("find assignment");
116+
String expected = """
117+
1. [D][ ] Submit assignment (by: Sep 30 2024 11 pm)""";
118+
assertEquals(expected, response);
119+
}
120+
121+
@Test
122+
public void run_validBye_success() {
123+
Gloqi gloqi = new Gloqi();
124+
String response = gloqi.run("bye");
125+
String expected = "Bye, see you next time!";
126+
assertEquals(expected, response);
127+
}
128+
129+
@Test
130+
public void run_invalidCommand_success() {
131+
Gloqi gloqi = new Gloqi();
132+
String response = gloqi.run("bye1231");
133+
String expected = """
134+
Invalid command, only following commands are supported:
135+
list, mark, unmark, bye, deadline, event, todo, show, delete, find""";
136+
assertEquals(expected, response);
137+
}
138+
139+
private Gloqi testFileInistalize(@TempDir Path tmp) {
140+
Gloqi gloqi = new Gloqi();
141+
Path file = tmp.resolve("gloqi.txt");
142+
gloqi.initialize(file.toString());
143+
return gloqi;
144+
}
145+
}

0 commit comments

Comments
 (0)