Skip to content

Commit

Permalink
Merge pull request #177 from CS2103AUG2016-W11-C3/Storage_Memory_Test
Browse files Browse the repository at this point in the history
Updated Memory Storage test
  • Loading branch information
LuMiN0uSaRc authored Oct 30, 2016
2 parents 93b7eab + 8d3bbe1 commit cb4c51f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/main/java/harmony/mastermind/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public interface Storage extends TaskManagerStorage, UserPrefsStorage {
*/
void handleTaskManagerChangedEvent(TaskManagerChangedEvent tmce);

//@author A0143378Y
//@@author A0143378Y
static void saveToStorage(Memory memory) {
try {
PrintWriter pw = new PrintWriter(SAVE_FILE);
Expand All @@ -82,7 +82,7 @@ static void saveToStorage(Memory memory) {
}
}

//@author A0143378Y
//@@author A0143378Y
static void printEnd(Memory memory, PrintWriter pw, int i) {
if (memory.get(i).getEnd() != null) {
pw.println(calendarToString(memory.get(i).getEnd()));
Expand All @@ -91,7 +91,7 @@ static void printEnd(Memory memory, PrintWriter pw, int i) {
}
}

//@author A0143378Y
//@@author A0143378Y
static void printStart(Memory memory, PrintWriter pw, int i) {
if (memory.get(i).getStart() != null) {
pw.println(calendarToString(memory.get(i).getStart()));
Expand All @@ -109,7 +109,7 @@ static String calendarToString(Calendar a) {
return dateTime;
}

//@author A0143378Y
//@@author A0143378Y
static void printDescription(Memory memory, PrintWriter pw, int i) {
if (memory.get(i).getDescription() != null) {
pw.println(memory.get(i).getDescription());
Expand All @@ -118,7 +118,7 @@ static void printDescription(Memory memory, PrintWriter pw, int i) {
}
}

//@author A0143378Y
//@@author A0143378Y
static void checkForFileExists(Memory memory) {
File file = new File(SAVE_FILE);
if (!file.exists()) {
Expand All @@ -132,7 +132,7 @@ static void checkForFileExists(Memory memory) {
readFromFile(memory);
}

//@author A0143378Y
//@@author A0143378Y
static void readFromFile(Memory memory) {
try {
BufferedReader br = new BufferedReader(new FileReader(SAVE_FILE));
Expand Down Expand Up @@ -164,7 +164,7 @@ static void readFromFile(Memory memory) {
}
}

//@author A0143378Y
//@@author A0143378Y
// Read line for integer for state
static int readState(BufferedReader br) throws IOException {
int state;
Expand All @@ -173,12 +173,12 @@ static int readState(BufferedReader br) throws IOException {
return state;
}

//@author A0143378Y
//@@author A0143378Y
static String reduceToInt(String stateString) {
return stateString.replaceAll("[^0-9]", "");
}

//@author A0143378Y
//@@author A0143378Y
// Read line for calendar for start and end dates
static Calendar readCalendar(BufferedReader br, Calendar startCal) throws IOException {
String start = br.readLine();
Expand All @@ -190,7 +190,7 @@ static Calendar readCalendar(BufferedReader br, Calendar startCal) throws IOExce
return startCal;
}

//@author A0143378Y
//@@author A0143378Y
// Converts string representation of date and time back into Calendar object
static Calendar stringToCalendar(String b){
Calendar setNew = new GregorianCalendar();
Expand All @@ -212,7 +212,7 @@ static Calendar stringToCalendar(String b){
return setNew;
}

//@author A0143378Y
//@@author A0143378Y
static String readDescription(BufferedReader br) throws IOException {
String description;
description = br.readLine();
Expand Down
81 changes: 81 additions & 0 deletions src/test/java/harmony/mastermind/storage/TestStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package harmony.mastermind.storage;

import harmony.mastermind.memory.GenericMemory;
import harmony.mastermind.memory.Memory;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class TestStorage {

@Test
//@@author A0143378Y
public void test() {
testReadFromFile_IO();
}

//@@author A0143378Y
private static void testReadFromFile_IO() {
Memory testMem = new Memory();
Storage.checkForFileExists(testMem);

testMem_getTask(testMem);
testMem_getDeadline(testMem);
testMem_getEvent(testMem);
}

//@@author A0143378Y
private static void testMem_getTask(Memory mem) {
GenericMemory task = new GenericMemory("Task", "I am hungry", "very hungry");
assertEquals("test if they are the same", testTwoTasks(mem.get(0), task), true);
}

//@@author A0143378Y
private static void testMem_getDeadline(Memory mem) {
Calendar end = new GregorianCalendar();
end.set(2014, 1, 27, 23, 59);

GenericMemory deadline = new GenericMemory("Deadline", "still hungry", "more food needed", end);
assertEquals("test if they are the same", testTwoDeadlines(mem.get(1), deadline), true);
}

private static void testMem_getEvent(Memory mem) {
Calendar end = new GregorianCalendar();
Calendar start = new GregorianCalendar();
start.set(2014, 1, 27, 23, 0);
end.set(2014, 1, 28, 2, 0);

GenericMemory event = new GenericMemory("Event", "Lunch?", "Sure!", start, end, 0);
assertEquals("test if they are the same", testTwoEvents(mem.get(2), event), true);
}

//@@author A0143378Y
private static boolean testTwoTasks(GenericMemory a, GenericMemory b) {
return a.getType().equals(b.getType()) &&
a.getDescription().equals(b.getDescription()) &&
a.getName().equals(b.getName());
}

//@@author A0143378Y
private static boolean testTwoDeadlines(GenericMemory a, GenericMemory b) {
return testTwoTasks(a, b) &&
testTwoCalendar(a.getEnd(), b.getEnd());
}

//@@author A0143378Y
private static boolean testTwoEvents(GenericMemory a, GenericMemory b) {
return testTwoDeadlines(a, b) &&
testTwoCalendar(a.getStart(), b.getStart());
}

//@@author A0143378Y
private static boolean testTwoCalendar(Calendar a, Calendar b) {
return a.get(Calendar.YEAR) == b.get(Calendar.YEAR) &&
a.get(Calendar.MONTH) == b.get(Calendar.MONTH) &&
a.get(Calendar.DATE) == b.get(Calendar.DATE);
}
}

0 comments on commit cb4c51f

Please sign in to comment.