Commit c943ce7e authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Added test of saving to and loading from file.

parent 2344624f
......@@ -48,6 +48,10 @@ public class TodoPersistence {
this.saveFilePath = Paths.get(System.getProperty("user.home"), saveFile);
}
public Path getSaveFilePath() {
return this.saveFilePath;
}
/**
* Loads a TodoModel from the saved file (saveFilePath) in the user.home folder.
*
......
......@@ -7,6 +7,8 @@ import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.Iterator;
import org.junit.jupiter.api.Test;
......@@ -20,8 +22,7 @@ public class TodoPersistenceTest {
private TodoPersistence todoPersistence = new TodoPersistence();
@Test
public void testSerializersDeserializers() {
private TodoModel createSampleTodoModel() {
TodoModel model = new TodoModel();
TodoList list = new TodoList("todo");
model.addTodoList(list);
......@@ -34,22 +35,61 @@ public class TodoPersistenceTest {
item2.setDeadline(LocalDateTime.parse("2020-10-01T14:53:11"));
list.addTodoItem(item2);
model.getSettings().setTodoItemsSortOrder(TodoItemsSortOrder.CHECKED_UNCHECKED);
return model;
}
private void checkSampleTodoModel(TodoModel model1, TodoModel model2) {
Iterator<AbstractTodoList> it1 = model1.iterator();
Iterator<AbstractTodoList> it2 = model2.iterator();
assertTrue(it1.hasNext());
assertTrue(it2.hasNext());
AbstractTodoList list1 = model1.iterator().next();
AbstractTodoList list2 = model2.iterator().next();
assertEquals(list1.getName(), list2.getName());
Iterator<TodoItem> itemIt1 = list1.iterator();
Iterator<TodoItem> itemIt2 = list2.iterator();
assertTrue(itemIt1.hasNext());
assertTrue(itemIt2.hasNext());
TodoModuleTest.checkTodoItem(itemIt2.next(), itemIt1.next());
assertTrue(itemIt1.hasNext());
assertTrue(itemIt2.hasNext());
TodoModuleTest.checkTodoItem(itemIt2.next(), itemIt1.next());
assertFalse(itemIt1.hasNext());
assertFalse(itemIt2.hasNext());
}
@Test
public void testSerializersDeserializers_usingStringWriter() {
TodoModel model = createSampleTodoModel();
try {
StringWriter writer = new StringWriter();
todoPersistence.writeTodoModel(model, writer);
String json = writer.toString();
TodoModel model2 = todoPersistence.readTodoModel(new StringReader(json));
assertTrue(model2.iterator().hasNext());
AbstractTodoList list2 = model.iterator().next();
assertEquals("todo", list.getName());
Iterator<TodoItem> it = list2.iterator();
assertTrue(it.hasNext());
TodoModuleTest.checkTodoItem(it.next(), item1);
assertTrue(it.hasNext());
TodoModuleTest.checkTodoItem(it.next(), item2);
assertFalse(it.hasNext());
checkSampleTodoModel(model, model2);
} catch (IOException e) {
fail(e.getMessage());
}
}
@Test
public void testSerializersDeserializers_usingSaveFile() {
TodoModel model = createSampleTodoModel();
// set unique save file path
todoPersistence.setSaveFile("todo-" + System.currentTimeMillis() + ".json");
Path saveFilePath = todoPersistence.getSaveFilePath();
try {
todoPersistence.saveTodoModel(model);
assertTrue(Files.exists(saveFilePath));
TodoModel model2 = todoPersistence.loadTodoModel();
checkSampleTodoModel(model, model2);
} catch (IOException e) {
fail(e.getMessage());
} finally {
try {
Files.deleteIfExists(saveFilePath);
} catch (IOException e) {
}
}
}
}
\ No newline at end of file
}
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